/** * Gets x509 cRL from attribute. Retrieves the binary attribute value, * decodes it to base64, and fetches it as a byte-array resource. * * @param aval the attribute, which may be null if it's not found * @return the x 509 cRL from attribute * @throws Exception if attribute not found or could could not be decoded. */ protected X509CRL fetchX509CRLFromAttribute(final LdapAttribute aval) throws Exception { if (aval != null) { final byte[] val = aval.getBinaryValue(); if (val == null || val.length == 0) { throw new CertificateException("Empty attribute. Can not download CRL from ldap"); } final byte[] decoded64 = CompressionUtils.decodeBase64ToByteArray(val); if (decoded64 == null) { throw new CertificateException("Could not decode the attribute value to base64"); } logger.debug("Retrieved CRL from ldap as byte array decoded in base64. Fetching..."); return super.fetch(new ByteArrayResource(decoded64)); } throw new CertificateException("Attribute not found. Can not retrieve CRL"); }
/** 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()); }
/** * Reads a String value from the LdapEntry. * * @param entry the ldap entry * @param attribute the attribute name * @param nullValue the value which should be returning in case of a null value * @return the string */ public static String getString(final LdapEntry entry, final String attribute, final String nullValue) { final LdapAttribute attr = entry.getAttribute(attribute); if (attr == null) { return nullValue; } final String v; if (attr.isBinary()) { final byte[] b = attr.getBinaryValue(); v = new String(b, Charset.forName("UTF-8")); } else { v = attr.getStringValue(); } if (StringUtils.isNotBlank(v)) { return v; } return nullValue; }
AssertJUnit.assertNotNull(la.getStringValues()); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertNotNull(la.getBinaryValue()); AssertJUnit.assertNotNull(la.getBinaryValues()); AssertJUnit.assertEquals(2, la.getBinaryValues().size()); AssertJUnit.assertArrayEquals(stringJpegPhotos.toArray(new String[2]), la.getStringValues().toArray(new String[2])); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertEquals("image1".getBytes(), la.getBinaryValue()); AssertJUnit.assertArrayEquals(jpegPhotos.toArray(new byte[2][0]), la.getBinaryValues().toArray(new byte[2][0])); AssertJUnit.assertEquals(2, la.getBinaryValues().size()); AssertJUnit.assertArrayEquals(stringJpegPhotos.toArray(new String[2]), la.getStringValues().toArray(new String[2])); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertEquals("image1".getBytes(), la.getBinaryValue()); AssertJUnit.assertArrayEquals(jpegPhotos.toArray(new byte[2][0]), la.getBinaryValues().toArray(new byte[2][0])); AssertJUnit.assertEquals(2, la.getBinaryValues().size());
@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(); } } } }
AssertJUnit.assertNotNull(la.getStringValues()); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertNotNull(la.getBinaryValue()); AssertJUnit.assertNotNull(la.getBinaryValues()); AssertJUnit.assertEquals(2, la.getBinaryValues().size()); AssertJUnit.assertArrayEquals(commonNames.toArray(new String[2]), la.getStringValues().toArray(new String[2])); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertEquals("Bill Wallace".getBytes(StandardCharsets.UTF_8), la.getBinaryValue()); AssertJUnit.assertArrayEquals( binaryCommonNames.toArray(new byte[2][0]), AssertJUnit.assertArrayEquals(commonNames.toArray(new String[2]), la.getStringValues().toArray(new String[2])); AssertJUnit.assertEquals(2, la.getStringValues().size()); AssertJUnit.assertEquals("Bill Wallace".getBytes(StandardCharsets.UTF_8), la.getBinaryValue()); AssertJUnit.assertArrayEquals( binaryCommonNames.toArray(new byte[2][0]),
@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(); } } } }
/** 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()); }
@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(); } } } }
/** * Gets x509 cRL from attribute. Retrieves the binary attribute value, * decodes it to base64, and fetches it as a byte-array resource. * * @param aval the attribute, which may be null if it's not found * @return the x 509 cRL from attribute * @throws IOException the exception thrown if resources cant be fetched * @throws CRLException the exception thrown if resources cant be fetched * @throws CertificateException if connection to ldap fails, or attribute to get the revocation list is unavailable */ protected X509CRL fetchX509CRLFromAttribute(final LdapAttribute aval) throws CertificateException, IOException, CRLException { if (aval != null && aval.isBinary()) { val val = aval.getBinaryValue(); if (val == null || val.length == 0) { throw new CertificateException("Empty attribute. Can not download CRL from ldap"); } val decoded64 = EncodingUtils.decodeBase64(val); if (decoded64 == null) { throw new CertificateException("Could not decode the attribute value to base64"); } LOGGER.debug("Retrieved CRL from ldap as byte array decoded in base64. Fetching..."); return super.fetch(new ByteArrayResource(decoded64)); } throw new CertificateException("Attribute not found. Can not retrieve CRL"); }
String sid; if (objectSid.isBinary()) { sid = SecurityIdentifier.toString(objectSid.getBinaryValue()); } else { sid = objectSid.getStringValue();
final String sid; if (objectSid.isBinary()) { sid = SecurityIdentifier.toString(objectSid.getBinaryValue()); } else { sid = objectSid.getStringValue();
final String sid; if (objectSid.isBinary()) { sid = SecurityIdentifier.toString(objectSid.getBinaryValue()); } else { sid = objectSid.getStringValue();
/** 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()); }
new LdapName(request.getDn()), String.format("(%s={0})", request.getAttribute().getName()), request.getAttribute().isBinary() ? new Object[] {request.getAttribute().getBinaryValue()} : new Object[] { request.getAttribute().getStringValue(), },
new LdapName(request.getDn()), String.format("(%s={0})", request.getAttribute().getName()), request.getAttribute().isBinary() ? new Object[] {request.getAttribute().getBinaryValue()} : new Object[] { request.getAttribute().getStringValue(), },
new LdapName(request.getDn()), String.format("(%s={0})", request.getAttribute().getName()), request.getAttribute().isBinary() ? new Object[] {request.getAttribute().getBinaryValue()} : new Object[] { request.getAttribute().getStringValue(), },
/** 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()); }
@Override public Response<Boolean> compare(final CompareRequest request) throws LdapException { Response<Boolean> response = null; try { final CompareRequestImpl cri = new CompareRequestImpl(); if (request.getControls() != null) { cri.addAllControls(config.getControlProcessor().processRequestControls(request.getControls())); } cri.setName(new Dn(request.getDn())); cri.setAttributeId(request.getAttribute().getName()); if (request.getAttribute().isBinary()) { cri.setAssertionValue(request.getAttribute().getBinaryValue()); } else { cri.setAssertionValue(request.getAttribute().getStringValue()); } final CompareResponse cr = connection.compare(cri); throwOperationException(request, cr); response = createResponse(request, cr.isTrue(), cr); } catch (LdapOperationException e) { processLdapOperationException(e); } catch (org.apache.directory.api.ldap.model.exception.LdapException e) { processLdapException(e); } return response; }