/** * Returns a jndi attribute that represents the values in the supplied ldap attribute. * * @param attr ldap attribute * * @return jndi attribute */ public Attribute fromLdapAttribute(final LdapAttribute attr) { final Attribute attribute = new BasicAttribute(attr.getName()); if (attr.isBinary()) { attr.getBinaryValues().forEach(attribute::add); } else { attr.getStringValues().forEach(attribute::add); } return attribute; }
@Override protected void handleAttributes(final Connection conn, final SearchRequest request, final SearchEntry entry) throws LdapException { boolean newAttribute = false; LdapAttribute mergedAttribute = entry.getAttribute(mergeAttributeName); if (mergedAttribute == null) { mergedAttribute = new LdapAttribute(mergeAttributeName); newAttribute = true; } for (String s : attributeNames) { final LdapAttribute la = entry.getAttribute(s); if (la != null) { if (la.isBinary()) { mergedAttribute.addBinaryValues(la.getBinaryValues()); } else { mergedAttribute.addStringValues(la.getStringValues()); } } } if (mergedAttribute.size() > 0 && newAttribute) { entry.addAttribute(mergedAttribute); } }
protected Map<String, Object> getAttributesFromEntry(final LdapEntry entry) { final Map<String, Object> attributes = new HashMap<>(); for (final LdapAttribute attribute : entry.getAttributes()) { final String name = attribute.getName(); if (attribute.size() > 1) { attributes.put(name, attribute.getStringValues()); } else { attributes.put(name, attribute.getStringValue()); } } return attributes; }
/** * 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 byte[]... values) { this(true); setName(name); addBinaryValue(values); }
/** 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()); }
if (objectSid != null && primaryGroupId != null) { final String sid; if (objectSid.isBinary()) { sid = SecurityIdentifier.toString(objectSid.getBinaryValue()); } else { sid = objectSid.getStringValue(); final String groupSid = sid.substring(0, sid.lastIndexOf('-') + 1) + primaryGroupId.getStringValue(); logger.debug( "created primary group SID {} from object SID {} and primaryGroupID {}", groupSid, sid, primaryGroupId.getStringValue()); LdapAttribute memberOf = entry.getAttribute("memberOf"); if (memberOf == null) { memberOf = new LdapAttribute("memberOf"); entry.addAttribute(memberOf); memberOf.addStringValue(result.getEntry().getDn());
/** Tests default sort behavior. */ @Test(groups = {"bean"}) public void defaultSortBehavior() { final LdapAttribute la = new LdapAttribute("givenName"); AssertJUnit.assertEquals(SortBehavior.getDefaultSortBehavior(), la.getSortBehavior()); AssertJUnit.assertEquals(0, la.size()); AssertJUnit.assertNull(la.getStringValue()); AssertJUnit.assertNull(la.getBinaryValue()); AssertJUnit.assertEquals("givenName", la.getName()); la.setName("sn"); AssertJUnit.assertEquals("sn", la.getName()); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** 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()); }
@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); } } }
/** * Returns the RDN values for the attribute type with the supplied name. * * @param dn to parse * @param name of the attribute type to return values for * * @return DN attribute values */ public static Collection<String> getValues(final String dn, final String name) { final Collection<String> values = new ArrayList<>(); convertDnToAttributes(dn).stream().filter( la -> la.getName().equalsIgnoreCase(name)).forEach(la -> values.addAll(la.getStringValues())); return values; }
@Override protected void writeResponse(final SearchResult result, final HttpServletResponse response) throws IOException { final LdapEntry e = result.getEntry(); if (e != null && e.size() > 0) { final LdapAttribute a = e.getAttribute(); if (a != null && a.size() > 0) { if (a.isBinary()) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s.bin\"", a.getName())); } else { response.setContentType("text/plain"); } final OutputStream out = response.getOutputStream(); out.write(a.getBinaryValue()); out.flush(); } } } }
en = ctx.search( new LdapName(request.getDn()), String.format("(%s={0})", request.getAttribute().getName()), request.getAttribute().isBinary() ? new Object[] {request.getAttribute().getBinaryValue()} : new Object[] { request.getAttribute().getStringValue(), }, getCompareSearchControls());
/** * Returns the name of this attribute. Includes options if they exist. * * @return attribute name */ public String getName() { return getName(true); }
/** * Returns a <dsml:attr/> element for the supplied ldap attribute. * * @param doc to source elements from * @param attr ldap attribute to add * * @return element containing the attribute */ protected Element createAttrElement(final Document doc, final LdapAttribute attr) { final Element attrElement = doc.createElement("dsml:attr"); attrElement.setAttribute("name", attr.getName()); for (String s : attr.getStringValues()) { final Element valueElement = doc.createElement("dsml:value"); attrElement.appendChild(valueElement); setAttrValue(doc, valueElement, s, attr.isBinary()); } return attrElement; }
/** * 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); } }
/** Tests create with two values. */ @Test(groups = {"bean"}) public void createTwo() { final LdapAttribute la = new LdapAttribute("givenName", "Bill", "William"); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertEquals(2, la.size()); AssertJUnit.assertEquals(la, new LdapAttribute("givenName", "William", "Bill")); la.removeStringValue("William"); AssertJUnit.assertEquals(1, la.size()); la.clear(); AssertJUnit.assertEquals(0, la.size()); }
/** * Execute modify operation boolean. * * @param currentDn the current dn * @param connectionFactory the connection factory * @param entry the entry * @return the boolean */ public static boolean executeModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final LdapEntry entry) { final Map<String, Set<String>> attributes = new HashMap<>(entry.getAttribute().size()); for (final LdapAttribute ldapAttribute : entry.getAttributes()) { attributes.put(ldapAttribute.getName(), ImmutableSet.copyOf(ldapAttribute.getStringValues())); } return executeModifyOperation(currentDn, connectionFactory, attributes); }
entry.addAttribute(newAttr); } else { if (newAttr.isBinary()) { for (byte[] value : newAttr.getBinaryValues()) { oldAttr.addBinaryValue(value); for (String value : newAttr.getStringValues()) { oldAttr.addStringValue(value);
@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; } }