@Override public Collection<byte[]> getBinaryValues(final Object object) { return la.getBinaryValues(); }
/** * Returns a single byte array value of this attribute. See {@link #getBinaryValues()}. * * @return single byte array attribute value */ public byte[] getBinaryValue() { final Collection<byte[]> values = getBinaryValues(); if (values.isEmpty()) { return null; } return values.iterator().next(); }
/** * Returns a single byte array value of this attribute. See {@link #getBinaryValues()}. * * @return single byte array attribute value */ public byte[] getBinaryValue() { final Collection<byte[]> values = getBinaryValues(); if (values.isEmpty()) { return null; } return values.iterator().next(); }
/** * Returns a single byte array value of this attribute. See {@link #getBinaryValues()}. * * @return single byte array attribute value */ public byte[] getBinaryValue() { final Collection<byte[]> values = getBinaryValues(); if (values.isEmpty()) { return null; } return values.iterator().next(); }
/** * Returns the values of this attribute decoded by the supplied transcoder. * * @param <T> type of decoded attributes * @param transcoder to decode attribute values with * * @return collection of decoded attribute values */ public <T> Collection<T> getValues(final ValueTranscoder<T> transcoder) { final Collection<T> values = createSortBehaviorCollection(transcoder.getType()); if (isBinary()) { values.addAll(getBinaryValues().stream().map(transcoder::decodeBinaryValue).collect(Collectors.toList())); } else { values.addAll(getStringValues().stream().map(transcoder::decodeStringValue).collect(Collectors.toList())); } return values; }
/** * Returns the values of this attribute decoded by the supplied transcoder. * * @param <T> type of decoded attributes * @param transcoder to decode attribute values with * * @return collection of decoded attribute values */ public <T> Collection<T> getValues(final ValueTranscoder<T> transcoder) { final Collection<T> values = createSortBehaviorCollection(transcoder.getType()); if (isBinary()) { values.addAll(getBinaryValues().stream().map(transcoder::decodeBinaryValue).collect(Collectors.toList())); } else { values.addAll(getStringValues().stream().map(transcoder::decodeStringValue).collect(Collectors.toList())); } return values; }
/** * 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; }
/** * 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; }
/** * Returns the values of this attribute decoded by the supplied transcoder. * * @param <T> type of decoded attributes * @param transcoder to decode attribute values with * * @return collection of decoded attribute values */ public <T> Collection<T> getValues(final ValueTranscoder<T> transcoder) { final Collection<T> values = createSortBehaviorCollection(transcoder.getType()); if (isBinary()) { for (byte[] b : getBinaryValues()) { values.add(transcoder.decodeBinaryValue(b)); } } else { for (String s : getStringValues()) { values.add(transcoder.decodeStringValue(s)); } } return values; }
@Override public void map(final LdapEntry source, final T dest) { logger.debug("map {} to {}", source, dest); final ClassDescriptor descriptor = getClassDescriptor(dest); final DnValueMutator dnMutator = descriptor.getDnValueMutator(); if (dnMutator != null) { dnMutator.setValue(dest, source.getDn()); } source.getAttributes().stream().filter(attr -> attr.size() > 0).forEach(attr -> { final AttributeValueMutator mutator = descriptor.getAttributeValueMutator(attr.getName()); logger.debug("using mutator {} for attribute {}", mutator, attr); if (mutator != null) { if (attr.isBinary()) { mutator.setBinaryValues(dest, attr.getBinaryValues()); } else { mutator.setStringValues(dest, attr.getStringValues()); } } }); } }
/** * 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()) { for (byte[] value : attr.getBinaryValues()) { attribute.add(value); } } else { for (String value : attr.getStringValues()) { attribute.add(value); } } return attribute; }
@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); } } }
/** * Returns an apache ldap attribute that represents the values in the supplied ldap attribute. * * @param la ldap attribute * * @return apache ldap attribute */ public Attribute fromLdapAttribute(final LdapAttribute la) { final DefaultAttribute attribute = new DefaultAttribute(la.getName()); if (la.isBinary()) { for (byte[] value : la.getBinaryValues()) { attribute.add(createValue(value)); } } else { for (String value : la.getStringValues()) { attribute.add(createValue(value)); } } 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); } }
@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); } }
@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); } }
/** 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 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()); }