/** * Adds all the strings in the supplied collection as values for this attribute. See {@link * #addStringValue(String...)}. * * @param values to add */ public void addStringValues(final Collection<String> values) { for (String value : values) { addStringValue(value); } }
/** * Creates a new ldap attribute. * * @param name of this attribute * @param values of this attribute */ public LdapAttribute(final String name, final String... values) { this(false); setName(name); addStringValue(values); }
/** * Creates a new ldap attribute. * * @param name of this attribute * @param values of this attribute */ public LdapAttribute(final String name, final String... values) { this(false); setName(name); addStringValue(values); }
/** * Creates a new ldap attribute. * * @param name of this attribute * @param values of this attribute */ public LdapAttribute(final String name, final String... values) { this(false); setName(name); addStringValue(values); }
@Override public SearchResult deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) throws JsonParseException { final SearchResult result = new SearchResult(sortBehavior); final JsonArray jsonResult = json.getAsJsonArray(); for (JsonElement jsonEntry : jsonResult) { final LdapEntry entry = new LdapEntry(sortBehavior); for (Map.Entry<String, JsonElement> jsonAttr : jsonEntry.getAsJsonObject().entrySet()) { if ("dn".equals(jsonAttr.getKey())) { entry.setDn(jsonAttr.getValue().getAsString()); } else { final LdapAttribute attr = new LdapAttribute(sortBehavior); attr.setName(jsonAttr.getKey()); jsonAttr.getValue().getAsJsonArray().forEach(i -> attr.addStringValue(i.getAsString())); entry.addAttribute(attr); } } result.addEntry(entry); } return result; } }
@Override public SearchResult deserialize(final JsonElement json, final Type type, final JsonDeserializationContext context) throws JsonParseException { final SearchResult result = new SearchResult(sortBehavior); final JsonArray jsonResult = json.getAsJsonArray(); for (JsonElement jsonEntry : jsonResult) { final LdapEntry entry = new LdapEntry(sortBehavior); for (Map.Entry<String, JsonElement> jsonAttr : jsonEntry.getAsJsonObject().entrySet()) { if ("dn".equals(jsonAttr.getKey())) { entry.setDn(jsonAttr.getValue().getAsString()); } else { final LdapAttribute attr = new LdapAttribute(sortBehavior); attr.setName(jsonAttr.getKey()); jsonAttr.getValue().getAsJsonArray().forEach(i -> attr.addStringValue(i.getAsString())); entry.addAttribute(attr); } } result.addEntry(entry); } return result; } }
final LdapAttribute attr = new LdapAttribute(e.getKey()); for (final JsonValue v : (JsonArray) e.getValue()) { attr.addStringValue(((JsonString) v).getString());
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { if (entry.getAttribute(dnAttributeName) == null) { entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn())); } else if (addIfExists) { entry.getAttribute(dnAttributeName).addStringValue(entry.getDn()); } }
/** * Creates a new simple attribute value mutator. * * @param name of the attribute * @param values of the attribute * @param binary whether values contains base64 encoded data * @param behavior sort behavior of the attribute */ public SimpleAttributeValueMutator( final String name, final String[] values, final boolean binary, final SortBehavior behavior) { la = new LdapAttribute(behavior, binary); la.setName(name); if (binary) { for (String value : values) { la.addBinaryValue(LdapUtils.base64Decode(value)); } } else { la.addStringValue(values); } }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { if (entry.getAttribute(dnAttributeName) == null) { entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn())); } else if (addIfExists) { entry.getAttribute(dnAttributeName).addStringValue(entry.getDn()); } }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { if (entry.getAttribute(dnAttributeName) == null) { entry.addAttribute(new LdapAttribute(dnAttributeName, entry.getDn())); } else if (addIfExists) { entry.getAttribute(dnAttributeName).addStringValue(entry.getDn()); } }
/** * Returns an ldap attribute using the supplied apache ldap attribute. * * @param a apache ldap attribute * * @return ldap attribute */ public LdapAttribute toLdapAttribute(final Attribute a) { boolean isBinary = false; if (a.getId().contains(";binary")) { isBinary = true; } else if (binaryAttrs != null && binaryAttrs.contains(a.getUpId())) { isBinary = true; } final LdapAttribute la = new LdapAttribute(sortBehavior, isBinary); la.setName(a.getUpId()); for (Value<?> v : a) { if (isBinary) { la.addBinaryValue(v.getBytes()); } else { la.addStringValue(v.getString()); } } return la; }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { for (LdapAttribute la : entry.getAttributes()) { if (attributeName.equalsIgnoreCase(la.getName())) { if (la.isBinary()) { final LdapAttribute newAttr = new LdapAttribute(la.getSortBehavior()); newAttr.setName(la.getName()); for (byte[] b : la.getBinaryValues()) { newAttr.addStringValue(convertValue(b)); } entry.addAttribute(newAttr); logger.debug("Processed attribute {}", newAttr); handleAttribute(conn, request, newAttr); } else { logger.warn("Attribute {} must be set as a binary attribute", attributeName); handleAttribute(conn, request, la); } } else { handleAttribute(conn, request, la); } } }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { for (LdapAttribute la : entry.getAttributes()) { if (attributeName.equalsIgnoreCase(la.getName())) { if (la.isBinary()) { final LdapAttribute newAttr = new LdapAttribute(la.getSortBehavior()); newAttr.setName(la.getName()); for (byte[] b : la.getBinaryValues()) { newAttr.addStringValue(convertValue(b)); } entry.addAttribute(newAttr); logger.debug("Processed attribute {}", newAttr); handleAttribute(conn, request, newAttr); } else { logger.warn("Attribute {} must be set as a binary attribute", attributeName); handleAttribute(conn, request, la); } } else { handleAttribute(conn, request, la); } } }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { for (LdapAttribute la : entry.getAttributes()) { if (attributeName.equalsIgnoreCase(la.getName())) { if (la.isBinary()) { final LdapAttribute newAttr = new LdapAttribute(la.getSortBehavior()); newAttr.setName(la.getName()); for (byte[] b : la.getBinaryValues()) { newAttr.addStringValue(convertValue(b)); } entry.addAttribute(newAttr); logger.debug("Processed attribute {}", newAttr); handleAttribute(conn, request, newAttr); } else { logger.warn("Attribute {} must be set as a binary attribute", attributeName); handleAttribute(conn, request, la); } } else { handleAttribute(conn, request, la); } } }
/** Tests ordered sort behavior. */ @Test(groups = {"bean"}) public void orderedSortBehavior() { final LdapAttribute la = new LdapAttribute(SortBehavior.ORDERED); AssertJUnit.assertEquals(SortBehavior.ORDERED, la.getSortBehavior()); la.addStringValue("William", "Bill"); final String[] values = la.getStringValues().toArray(new String[2]); AssertJUnit.assertEquals("William", values[0]); AssertJUnit.assertEquals("Bill", values[1]); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** Tests sorted sort behavior. */ @Test(groups = {"bean"}) public void sortedSortBehavior() { final LdapAttribute la = new LdapAttribute(SortBehavior.SORTED); AssertJUnit.assertEquals(SortBehavior.SORTED, la.getSortBehavior()); la.addStringValue("William", "Bill"); final String[] values = la.getStringValues().toArray(new String[2]); AssertJUnit.assertEquals("Bill", values[0]); AssertJUnit.assertEquals("William", values[1]); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** Tests various binary input. */ @Test(groups = {"bean"}) public void binaryValue() { final LdapAttribute la = new LdapAttribute("jpegPhoto", "image".getBytes()); AssertJUnit.assertTrue(Arrays.equals("image".getBytes(), la.getBinaryValue())); AssertJUnit.assertEquals(1, la.getBinaryValues().size()); AssertJUnit.assertEquals("aW1hZ2U=", la.getStringValue()); AssertJUnit.assertEquals(1, la.getStringValues().size()); AssertJUnit.assertEquals(la, new LdapAttribute("jpegPhoto", "image".getBytes())); try { la.addBinaryValue((byte[][]) null); AssertJUnit.fail("Should have thrown NullPointerException"); } catch (Exception e) { AssertJUnit.assertEquals(NullPointerException.class, e.getClass()); } try { la.addStringValue("Bill"); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** Tests create with one value. */ @Test(groups = {"bean"}) public void createOne() { final LdapAttribute la = new LdapAttribute("givenName", "William"); AssertJUnit.assertEquals("William", la.getStringValue()); AssertJUnit.assertEquals(1, la.getStringValues().size()); AssertJUnit.assertEquals("William", la.getStringValues().iterator().next()); AssertJUnit.assertTrue(Arrays.equals("William".getBytes(), la.getBinaryValue())); AssertJUnit.assertEquals(1, la.size()); AssertJUnit.assertEquals(la, new LdapAttribute("givenName", "William")); try { la.addStringValue((String) null); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } try { la.addBinaryValue("Bill".getBytes()); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** Tests various string input. */ @Test(groups = {"bean"}) public void stringValue() { final LdapAttribute la = new LdapAttribute("cn", "William Wallace"); AssertJUnit.assertEquals("William Wallace", la.getStringValue()); AssertJUnit.assertEquals("William Wallace".getBytes(StandardCharsets.UTF_8), la.getBinaryValue()); AssertJUnit.assertEquals(1, la.getStringValues().size()); AssertJUnit.assertEquals(1, la.getBinaryValues().size()); AssertJUnit.assertEquals(la, new LdapAttribute("cn", "William Wallace")); try { la.addStringValue((String[]) null); AssertJUnit.fail("Should have thrown NullPointerException"); } catch (Exception e) { AssertJUnit.assertEquals(NullPointerException.class, e.getClass()); } try { la.addBinaryValue("Bill".getBytes()); AssertJUnit.fail("Should have thrown IllegalArgumentException"); } catch (Exception e) { AssertJUnit.assertEquals(IllegalArgumentException.class, e.getClass()); } la.clear(); AssertJUnit.assertEquals(0, la.size()); }