/** * Checks to see if response has a result. * * @param response the response * @return true, if successful */ public static boolean containsResultEntry(final Response<SearchResult> response) { final SearchResult result = response.getResult(); if (result != null && result.getEntry() != null) { return true; } return false; }
/** * Tests create with one entry. * * @param entry1 ldap entry * @param entry2 ldap entry */ @Test(groups = {"bean"}, dataProvider = "entries") public void createOne(final LdapEntry entry1, final LdapEntry entry2) { final SearchResult sr = new SearchResult(entry1); AssertJUnit.assertEquals(entry1, sr.getEntry()); AssertJUnit.assertEquals(entry1, sr.getEntry("uid=1")); AssertJUnit.assertEquals(entry1, sr.getEntry("UID=1")); AssertJUnit.assertEquals("uid=1", sr.getEntryDns()[0]); AssertJUnit.assertEquals(1, sr.size()); AssertJUnit.assertEquals(sr, new SearchResult(entry1)); sr.clear(); AssertJUnit.assertEquals(0, sr.size()); }
private Map<String, String> entryToAttributes(final String[] attributes, final SearchResult result) { final LdapEntry entry = result.getEntry(); return Stream.of(attributes == null || attributes.length == 0 ? entry.getAttributeNames() : attributes) .collect(toMap(identity(), a -> readAttribute(entry, a))); }
/** * Tests create with a collection. * * @param entry1 ldap entry * @param entry2 ldap entry */ @Test(groups = {"bean"}, dataProvider = "entries") public void createCollection(final LdapEntry entry1, final LdapEntry entry2) { final Set<LdapEntry> s = new HashSet<>(); s.add(entry1); final SearchResult sr = new SearchResult(s); sr.addEntry(entry2); AssertJUnit.assertEquals(entry1, sr.getEntry("UID=1")); AssertJUnit.assertEquals(entry2, sr.getEntry("uid=2")); AssertJUnit.assertEquals(2, sr.getEntryDns().length); AssertJUnit.assertEquals(2, sr.size()); AssertJUnit.assertEquals(sr, new SearchResult(entry1, entry2)); sr.removeEntry("UID=1"); AssertJUnit.assertEquals(1, sr.size()); sr.clear(); AssertJUnit.assertEquals(0, sr.size()); }
/** * Creates a new schema. The input stream should contain the LDIF for the subschema entry. * * @param is containing the schema ldif * * @return schema created from the ldif * * @throws IOException if an error occurs reading the input stream */ public static Schema createSchema(final InputStream is) throws IOException { final LdifReader reader = new LdifReader(new InputStreamReader(is)); return createSchema(reader.read().getEntry()); }
/** * Tests create with two entries. * * @param entry1 ldap entry * @param entry2 ldap entry */ @Test(groups = {"bean"}, dataProvider = "entries") public void createTwo(final LdapEntry entry1, final LdapEntry entry2) { final SearchResult sr = new SearchResult(entry2, entry1); AssertJUnit.assertEquals(entry1, sr.getEntry("uid=1")); AssertJUnit.assertEquals(entry2, sr.getEntry("UID=2")); AssertJUnit.assertEquals(2, sr.getEntryDns().length); AssertJUnit.assertEquals(2, sr.size()); AssertJUnit.assertEquals(sr, new SearchResult(entry1, entry2)); sr.removeEntry(entry2); AssertJUnit.assertEquals(1, sr.size()); sr.clear(); AssertJUnit.assertEquals(0, sr.size()); }
/** * Creates a new schema. The input stream should contain the LDIF for the subschema entry. * * @param is containing the schema ldif * * @return schema created from the ldif * * @throws IOException if an error occurs reading the input stream */ public static Schema createSchema(final InputStream is) throws IOException { final LdifReader reader = new LdifReader(new InputStreamReader(is)); return createSchema(reader.read().getEntry()); }
/** * Creates a new schema. The input stream should contain the LDIF for the subschema entry. * * @param is containing the schema ldif * * @return schema created from the ldif * * @throws IOException if an error occurs reading the input stream */ public static Schema createSchema(final InputStream is) throws IOException { final LdifReader reader = new LdifReader(new InputStreamReader(is)); return createSchema(reader.read().getEntry()); }
@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(); } } } }
@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(); } } } }
@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(); } } } }
/** * Verify spnego attribute value. * * @param searchResult the search result * @return true if attribute value exists and has a value */ protected boolean processSpnegoAttribute(final Response<SearchResult> searchResult) { final SearchResult result = searchResult.getResult(); if (result == null || result.getEntries().isEmpty()) { logger.debug("Spnego attribute is not found in the search results"); return false; } final LdapEntry entry = result.getEntry(); final LdapAttribute attribute = entry.getAttribute(this.spnegoAttributeName); return verifySpnegyAttributeValue(attribute); }
/** * Downloads a CRL from given LDAP url. * * @param r the resource that is the ldap url. * @return the x 509 cRL * @throws Exception if connection to ldap fails, or attribute to get the revocation list is unavailable */ protected X509CRL fetchCRLFromLdap(final Object r) throws Exception { try { final String ldapURL = r.toString(); logger.debug("Fetching CRL from ldap {}", ldapURL); final Response<SearchResult> result = performLdapSearch(ldapURL); if (result.getResultCode() == ResultCode.SUCCESS) { final LdapEntry entry = result.getResult().getEntry(); final LdapAttribute attribute = entry.getAttribute(); logger.debug("Located entry [{}]. Retrieving first attribute [{}]", entry, attribute); return fetchX509CRLFromAttribute(attribute); } else { logger.debug("Failed to execute the search [{}]", result); } throw new CertificateException("Failed to establish a connection ldap and search."); } catch (final LdapException e) { logger.error(e.getMessage(), e); throw new CertificateException(e); } }
/** * Downloads a CRL from given LDAP url. * * @param r the resource that is the ldap url. * @return the x 509 cRL * @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 fetchCRLFromLdap(final Object r) throws CertificateException, IOException, CRLException { try { val ldapURL = r.toString(); LOGGER.debug("Fetching CRL from ldap [{}]", ldapURL); val result = performLdapSearch(ldapURL); if (result.getResultCode() == ResultCode.SUCCESS) { val entry = result.getResult().getEntry(); val attribute = entry.getAttribute(this.certificateAttribute); if (attribute.isBinary()) { LOGGER.debug("Located entry [{}]. Retrieving first attribute [{}]", entry, attribute); return fetchX509CRLFromAttribute(attribute); } LOGGER.warn("Found certificate attribute [{}] but it is not marked as a binary attribute", this.certificateAttribute); } LOGGER.debug("Failed to execute the search [{}]", result); throw new CertificateException("Failed to establish a connection ldap and search."); } catch (final LdapException e) { LOGGER.error(e.getMessage(), e); throw new CertificateException(e.getMessage()); } }
/** {@inheritDoc} */ @Override @Nullable public StorageRecord read(@Nonnull @NotEmpty final String context, @Nonnull @NotEmpty final String key) throws IOException { SearchResult result = null; try { result = search(context, key).getResult(); } catch (LdapException e) { if (e.getResultCode() != ResultCode.NO_SUCH_OBJECT) { log.error("LDAP search operation failed", e); throw new IOException(e); } } StorageRecord record = null; if (result != null && result.size() > 0) { final LdapEntry entry = result.getEntry(); if (entry != null) { final LdapAttribute attr = entry.getAttribute(key); if (attr != null) { record = new StorageRecord(attr.getStringValue(), null); } } } return record; }
/** * Searches for the supplied dn and returns its ldap entry. * * @param factory to obtain an LDAP connection from * @param dn to search for * @param filter search filter * @param retAttrs attributes to return * * @return ldap entry * * @throws LdapException if the search fails */ protected static LdapEntry getLdapEntry( final ConnectionFactory factory, final String dn, final String filter, final String[] retAttrs) throws LdapException { final SearchExecutor executor = new SearchExecutor(); executor.setBaseDn(dn); executor.setSearchScope(SearchScope.OBJECT); executor.setReturnAttributes(retAttrs); final SearchResult result = executor.search(factory, filter).getResult(); return result.getEntry(); } }
/** * Searches for the supplied dn and returns its ldap entry. * * @param factory to obtain an LDAP connection from * @param dn to search for * @param filter search filter * @param retAttrs attributes to return * * @return ldap entry * * @throws LdapException if the search fails */ protected static LdapEntry getLdapEntry( final ConnectionFactory factory, final String dn, final String filter, final String[] retAttrs) throws LdapException { final SearchExecutor executor = new SearchExecutor(); executor.setBaseDn(dn); executor.setSearchScope(SearchScope.OBJECT); executor.setReturnAttributes(retAttrs); final SearchResult result = executor.search(factory, filter).getResult(); return result.getEntry(); } }
/** * Searches for the supplied dn and returns its ldap entry. * * @param factory to obtain an LDAP connection from * @param dn to search for * @param filter search filter * @param retAttrs attributes to return * * @return ldap entry * * @throws LdapException if the search fails */ protected static LdapEntry getLdapEntry( final ConnectionFactory factory, final String dn, final String filter, final String[] retAttrs) throws LdapException { final SearchExecutor executor = new SearchExecutor(); executor.setBaseDn(dn); executor.setSearchScope(SearchScope.OBJECT); executor.setReturnAttributes(retAttrs); final SearchResult result = executor.search(factory, filter).getResult(); return result.getEntry(); } }
@Override public T find(final T object) throws LdapException { final String dn = getLdapEntryMapper().mapDn(object); String[] attrs = ReturnAttributes.ALL.value(); if (returnAttributes != null) { attrs = LdapUtils.concatArrays(attrs, returnAttributes); } final SearchRequest request = SearchRequest.newObjectScopeSearchRequest(dn, attrs); try (Connection conn = getConnectionFactory().getConnection()) { conn.open(); final SearchOperation search = new SearchOperation(conn); final Response<SearchResult> response = search.execute(request); if (response.getResult().size() == 0) { throw new IllegalArgumentException( String.format("Unable to find ldap entry %s, no entries returned: %s", dn, response)); } if (response.getResult().size() > 1) { throw new IllegalArgumentException( String.format("Unable to find ldap entry %s, multiple entries returned: %s", dn, response)); } getLdapEntryMapper().map(response.getResult().getEntry(), object); } return object; }
/** Tests default sort behavior. */ @Test(groups = {"bean"}) public void defaultSortBehavior() { final SearchResult sr = new SearchResult(); AssertJUnit.assertEquals(SortBehavior.getDefaultSortBehavior(), sr.getSortBehavior()); AssertJUnit.assertEquals(0, sr.size()); AssertJUnit.assertNull(sr.getEntry()); sr.clear(); AssertJUnit.assertEquals(0, sr.size()); }