Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented logging for LDAP searches performed by LdapTemplate #764

Merged
merged 4 commits into from
Feb 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 69 additions & 101 deletions core/src/main/java/org/springframework/ldap/core/LdapTemplate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2005-2022 the original author or authors.
* Copyright 2005-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,6 +74,7 @@
*
* @author Mattias Hellborg Arthursson
* @author Ulrik Sandberg
* @author Roman Zabaluev
* @see org.springframework.ldap.core.ContextSource
*/
public class LdapTemplate implements LdapOperations, InitializingBean {
Expand Down Expand Up @@ -251,13 +252,11 @@ public void search(String base, String filter, int searchScope, boolean returnin
@Override
public void search(final Name base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler) {

// Create a SearchExecutor to perform the search.
SearchExecutor se = new SearchExecutor() {
public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
return ctx.search(base, filter, controls);
}
SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
Expand All @@ -270,13 +269,11 @@ public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.Namin
@Override
public void search(final String base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler) {

// Create a SearchExecutor to perform the search.
SearchExecutor se = new SearchExecutor() {
public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
return ctx.search(base, filter, controls);
}
SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
Expand All @@ -289,13 +286,11 @@ public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.Namin
@Override
public void search(final Name base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler, DirContextProcessor processor) {

// Create a SearchExecutor to perform the search.
SearchExecutor se = new SearchExecutor() {
public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
return ctx.search(base, filter, controls);
}
SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
Expand All @@ -308,13 +303,11 @@ public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.Namin
@Override
public void search(final String base, final String filter, final SearchControls controls,
NameClassPairCallbackHandler handler, DirContextProcessor processor) {

// Create a SearchExecutor to perform the search.
SearchExecutor se = new SearchExecutor() {
public NamingEnumeration executeSearch(DirContext ctx) throws javax.naming.NamingException {
return ctx.search(base, filter, controls);
}
SearchExecutor se = (ctx) -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, filter, controls);
};
// Create a SearchExecutor to perform the search.
if (handler instanceof ContextMapperCallbackHandler) {
assureReturnObjFlagSet(controls);
}
Expand Down Expand Up @@ -847,11 +840,9 @@ public Object executeWithContext(DirContext ctx) throws javax.naming.NamingExcep
*/
@Override
public <T> T lookup(final Name dn, final AttributesMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Attributes attributes = ctx.getAttributes(dn);
return mapper.mapFromAttributes(attributes);
}
return executeReadOnly((ctx) -> {
Attributes attributes = ctx.getAttributes(dn);
return mapper.mapFromAttributes(attributes);
});
}

Expand All @@ -861,11 +852,9 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
@Override
public <T> T lookup(final String dn, final AttributesMapper<T> mapper) {

return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Attributes attributes = ctx.getAttributes(dn);
return mapper.mapFromAttributes(attributes);
}
return executeReadOnly((ctx) -> {
Attributes attributes = ctx.getAttributes(dn);
return mapper.mapFromAttributes(attributes);
});
}

Expand All @@ -874,11 +863,9 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
*/
@Override
public <T> T lookup(final Name dn, final ContextMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Object object = ctx.lookup(dn);
return mapper.mapFromContext(object);
}
return executeReadOnly((ctx) -> {
Object object = ctx.lookup(dn);
return mapper.mapFromContext(object);
});
}

Expand All @@ -887,11 +874,9 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
*/
@Override
public <T> T lookup(final String dn, final ContextMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Object object = ctx.lookup(dn);
return mapper.mapFromContext(object);
}
return executeReadOnly((ctx) -> {
Object object = ctx.lookup(dn);
return mapper.mapFromContext(object);
});
}

Expand All @@ -900,11 +885,9 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
*/
@Override
public <T> T lookup(final Name dn, final String[] attributes, final AttributesMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
return mapper.mapFromAttributes(filteredAttributes);
}
return executeReadOnly((ctx) -> {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
return mapper.mapFromAttributes(filteredAttributes);
});
}

Expand All @@ -913,11 +896,9 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
*/
@Override
public <T> T lookup(final String dn, final String[] attributes, final AttributesMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
return mapper.mapFromAttributes(filteredAttributes);
}
return executeReadOnly((ctx) -> {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
return mapper.mapFromAttributes(filteredAttributes);
});
}

Expand All @@ -926,12 +907,10 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
*/
@Override
public <T> T lookup(final Name dn, final String[] attributes, final ContextMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
DirContextAdapter contextAdapter = new DirContextAdapter(filteredAttributes, dn);
return mapper.mapFromContext(contextAdapter);
}
return executeReadOnly((ctx) -> {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
DirContextAdapter contextAdapter = new DirContextAdapter(filteredAttributes, dn);
return mapper.mapFromContext(contextAdapter);
});
}

Expand All @@ -940,13 +919,11 @@ public T executeWithContext(DirContext ctx) throws javax.naming.NamingException
*/
@Override
public <T> T lookup(final String dn, final String[] attributes, final ContextMapper<T> mapper) {
return executeReadOnly(new ContextExecutor<T>() {
public T executeWithContext(DirContext ctx) throws javax.naming.NamingException {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
LdapName name = LdapUtils.newLdapName(dn);
DirContextAdapter contextAdapter = new DirContextAdapter(filteredAttributes, name);
return mapper.mapFromContext(contextAdapter);
}
return executeReadOnly((ctx) -> {
Attributes filteredAttributes = ctx.getAttributes(dn, attributes);
LdapName name = LdapUtils.newLdapName(dn);
DirContextAdapter contextAdapter = new DirContextAdapter(filteredAttributes, name);
return mapper.mapFromContext(contextAdapter);
});
}

Expand Down Expand Up @@ -981,11 +958,9 @@ public Object executeWithContext(DirContext ctx) throws javax.naming.NamingExcep
*/
@Override
public void bind(final Name dn, final Object obj, final Attributes attributes) {
executeReadWrite(new ContextExecutor<Object>() {
public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
ctx.bind(dn, obj, attributes);
return null;
}
executeReadWrite((ctx) -> {
ctx.bind(dn, obj, attributes);
return null;
});
}

Expand All @@ -994,11 +969,9 @@ public Object executeWithContext(DirContext ctx) throws javax.naming.NamingExcep
*/
@Override
public void bind(final String dn, final Object obj, final Attributes attributes) {
executeReadWrite(new ContextExecutor<Object>() {
public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
ctx.bind(dn, obj, attributes);
return null;
}
executeReadWrite((ctx) -> {
ctx.bind(dn, obj, attributes);
return null;
});
}

Expand Down Expand Up @@ -1045,38 +1018,30 @@ public void unbind(final String dn, boolean recursive) {
}

private void doUnbind(final Name dn) {
executeReadWrite(new ContextExecutor<Object>() {
public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
ctx.unbind(dn);
return null;
}
executeReadWrite((ctx) -> {
ctx.unbind(dn);
return null;
});
}

private void doUnbind(final String dn) {
executeReadWrite(new ContextExecutor<Object>() {
public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
ctx.unbind(dn);
return null;
}
executeReadWrite((ctx) -> {
ctx.unbind(dn);
return null;
});
}

private void doUnbindRecursively(final Name dn) {
executeReadWrite(new ContextExecutor<Object>() {
public Object executeWithContext(DirContext ctx) {
deleteRecursively(ctx, LdapUtils.newLdapName(dn));
return null;
}
executeReadWrite((ctx) -> {
deleteRecursively(ctx, LdapUtils.newLdapName(dn));
return null;
});
}

private void doUnbindRecursively(final String dn) {
executeReadWrite(new ContextExecutor<Object>() {
public Object executeWithContext(DirContext ctx) throws javax.naming.NamingException {
deleteRecursively(ctx, LdapUtils.newLdapName(dn));
return null;
}
executeReadWrite((ctx) -> {
deleteRecursively(ctx, LdapUtils.newLdapName(dn));
return null;
});
}

Expand Down Expand Up @@ -1599,7 +1564,10 @@ <T> Stream<T> searchForStream(LdapQuery query, Function<SearchResult, T> mapper)

assureReturnObjFlagSet(searchControls);

NamingEnumeration<SearchResult> results = unchecked(() -> ctx.search(base, encodedFilter, searchControls));
NamingEnumeration<SearchResult> results = unchecked(() -> {
LOG.debug("Executing search with base [{}] and filter [{}]", base, filter);
return ctx.search(base, encodedFilter, searchControls);
});
if (results == null) {
return Stream.empty();
}
Expand Down Expand Up @@ -1884,15 +1852,15 @@ private enum AuthenticationStatus {
*/
UNDEFINED_FAILURE(false);

private boolean success;
private final boolean success;

AuthenticationStatus(boolean success) {
this.success = success;
}

/**
* Return true if the authentication attempt was successful
* @return
* @return true if the authentication attempt was successful
*/
public boolean isSuccess() {
return this.success;
Expand Down