LDAP-160, LDAP-161: Cleaned up DirContextProcessor implementations.

This commit is contained in:
Ulrik Sandberg
2009-01-08 21:29:48 +00:00
parent ca0ac4cb81
commit 4d9feea73c
5 changed files with 41 additions and 147 deletions

View File

@@ -33,6 +33,12 @@ Changes in version 1.3.0 (Jan 2009)
* SortControlDirContextProcessor no longer has hard dependencies to controls in LDAP Booster Pack (LDAP-159).
* Common code in DirContextProcessor implementations has been pulled up to a base class called
AbstractFallbackRequestAndResponseControlDirContextProcessor (LDAP-161).
* PagedResultsRequestControl has been deprecated in favor of PagedResultsDirContextProcessor, which
takes advantage of the new AbstractFallbackRequestAndResponseControlDirContextProcessor (LDAP-160).
* Added spring-tx as required dependency (the DataAccessExceptions require this).
* Added demo projects to simplify refactoring demonstrations. (LDAP-154)

View File

@@ -39,6 +39,7 @@ import java.lang.reflect.Method;
*
* @author Mattias Hellborg Arthursson
* @author Ulrik Sandberg
* @deprecated Use PagedResultsDirContextProcessor instead.
*/
public class PagedResultsRequestControl extends AbstractRequestControlDirContextProcessor {

View File

@@ -16,17 +16,7 @@
package org.springframework.ldap.control;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.ldap.Control;
import javax.naming.ldap.LdapContext;
import org.springframework.ldap.UncategorizedLdapException;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* DirContextProcessor implementation for managing the SortControl. Note that
@@ -35,22 +25,20 @@ import org.springframework.util.ReflectionUtils;
*
* @author Ulrik Sandberg
*/
public class SortControlDirContextProcessor extends AbstractRequestControlDirContextProcessor {
private static final boolean CRITICAL_CONTROL = true;
public class SortControlDirContextProcessor extends AbstractFallbackRequestAndResponseControlDirContextProcessor {
private static final String DEFAULT_REQUEST_CONTROL = "javax.naming.ldap.SortControl";
private static final String LDAPBP_REQUEST_CONTROL = "com.sun.jndi.ldap.ctl.SortControl";
private static final String FALLBACK_REQUEST_CONTROL = "com.sun.jndi.ldap.ctl.SortControl";
private static final String DEFAULT_RESPONSE_CONTROL = "javax.naming.ldap.SortResponseControl";
private static final String LDAPBP_RESPONSE_CONTROL = "com.sun.jndi.ldap.ctl.SortResponseControl";
private static final String FALLBACK_RESPONSE_CONTROL = "com.sun.jndi.ldap.ctl.SortResponseControl";
/**
* What key to sort on.
*/
private String sortKey;
String sortKey;
/**
* Whether the search result actually was sorted.
@@ -62,12 +50,6 @@ public class SortControlDirContextProcessor extends AbstractRequestControlDirCon
*/
private int resultCode;
private Class responseControlClass;
private Class requestControlClass;
private boolean critical = CRITICAL_CONTROL;
/**
* Constructs a new instance using the supplied sort key.
*
@@ -75,45 +57,18 @@ public class SortControlDirContextProcessor extends AbstractRequestControlDirCon
*/
public SortControlDirContextProcessor(String sortKey) {
this.sortKey = sortKey;
setSorted(false);
setResultCode(-1);
this.sorted = false;
this.resultCode = -1;
defaultRequestControl = DEFAULT_REQUEST_CONTROL;
defaultResponseControl = DEFAULT_RESPONSE_CONTROL;
fallbackRequestControl = FALLBACK_REQUEST_CONTROL;
fallbackResponseControl = FALLBACK_RESPONSE_CONTROL;
loadControlClasses();
}
private void loadControlClasses() {
try {
requestControlClass = Class.forName(DEFAULT_REQUEST_CONTROL);
responseControlClass = Class.forName(DEFAULT_RESPONSE_CONTROL);
}
catch (ClassNotFoundException e) {
log.debug("Default control classes not found - falling back to LdapBP classes", e);
try {
requestControlClass = Class.forName(LDAPBP_REQUEST_CONTROL);
responseControlClass = Class.forName(LDAPBP_RESPONSE_CONTROL);
}
catch (ClassNotFoundException e1) {
throw new UncategorizedLdapException(
"Neither default nor fallback classes are available - unable to proceed", e);
}
}
}
/**
* Set the class of the expected ResponseControl for the sorted result
* response.
*
* @param responseControlClass Class of the expected response control.
*/
public void setResponseControlClass(Class responseControlClass) {
this.responseControlClass = responseControlClass;
}
public void setRequestControlClass(Class requestControlClass) {
this.requestControlClass = requestControlClass;
}
/**
* Check whether the returned values were actually sorted by the server.
*
@@ -124,10 +79,6 @@ public class SortControlDirContextProcessor extends AbstractRequestControlDirCon
return sorted;
}
private void setSorted(boolean sorted) {
this.sorted = sorted;
}
/**
* Get the result code returned by the control.
*
@@ -137,10 +88,6 @@ public class SortControlDirContextProcessor extends AbstractRequestControlDirCon
return resultCode;
}
private void setResultCode(int sortResult) {
this.resultCode = sortResult;
}
/**
* Get the sort key.
*
@@ -150,72 +97,25 @@ public class SortControlDirContextProcessor extends AbstractRequestControlDirCon
return sortKey;
}
/**
* Set the sort key, i.e. the attribute on which to sort on.
*
* @param sortKey the sort key.
*/
public void setSortKey(String sortKey) {
this.sortKey = sortKey;
}
/*
* @see
* org.springframework.ldap.control.AbstractRequestControlDirContextProcessor
* #createRequestControl()
*/
public Control createRequestControl() {
Constructor constructor = ClassUtils.getConstructorIfAvailable(requestControlClass, new Class[] {
String[].class, boolean.class });
if (constructor == null) {
throw new IllegalArgumentException("Failed to find an appropriate RequestControl constructor");
}
Control result = null;
try {
result = (Control) constructor.newInstance(new Object[] { new String[] { sortKey },
Boolean.valueOf(critical) });
}
catch (Exception e) {
ReflectionUtils.handleReflectionException(e);
}
return result;
return super.createRequestControl(new Class[] { String[].class, boolean.class }, new Object[] {
new String[] { sortKey }, Boolean.valueOf(critical) });
}
/*
* @see
* org.springframework.ldap.core.DirContextProcessor#postProcess(javax.naming
* .directory.DirContext)
* @see org.springframework.ldap.control.
* AbstractFallbackRequestAndResponseControlDirContextProcessor
* #handleResponse(java.lang.Object)
*/
public void postProcess(DirContext ctx) throws NamingException {
LdapContext ldapContext = (LdapContext) ctx;
Control[] responseControls = ldapContext.getResponseControls();
if (responseControls == null) {
responseControls = new Control[0];
}
// Go through response controls and get info, regardless of class
for (int i = 0; i < responseControls.length; i++) {
Control responseControl = responseControls[i];
// check for match, try fallback otherwise
if (responseControl.getClass().isAssignableFrom(responseControlClass)) {
Object control = responseControl;
Boolean result = (Boolean) invokeMethod("isSorted", responseControlClass, control);
setSorted(result.booleanValue());
Integer code = (Integer) invokeMethod("getResultCode", responseControlClass, control);
setResultCode(code.intValue());
return;
}
}
log.fatal("No matching response control found for paged results - looking for '" + responseControlClass);
}
private Object invokeMethod(String method, Class clazz, Object control) {
Method actualMethod = ReflectionUtils.findMethod(clazz, method);
return ReflectionUtils.invokeMethod(actualMethod, control);
protected void handleResponse(Object control) {
Boolean result = (Boolean) invokeMethod("isSorted", responseControlClass, control);
this.sorted = result.booleanValue();
Integer code = (Integer) invokeMethod("getResultCode", responseControlClass, control);
this.resultCode = code.intValue();
}
}

View File

@@ -28,27 +28,30 @@ import javax.naming.ldap.PagedResultsControl;
import javax.naming.ldap.PagedResultsResponseControl;
import java.io.IOException;
public class PagedResultsRequestControlTest extends TestCase {
public class PagedResultsDirContextProcessorTest extends TestCase {
private MockControl ldapContextControl;
private LdapContext ldapContextMock;
private PagedResultsDirContextProcessor tested;
protected void setUp() throws Exception {
super.setUp();
tested = new PagedResultsDirContextProcessor(20);
// Create ldapContext mock
ldapContextControl = MockControl.createControl(LdapContext.class);
ldapContextMock = (LdapContext) ldapContextControl.getMock();
}
protected void tearDown() throws Exception {
super.tearDown();
tested = null;
ldapContextControl = null;
ldapContextMock = null;
}
protected void replay() {
@@ -60,8 +63,6 @@ public class PagedResultsRequestControlTest extends TestCase {
}
public void testCreateRequestControl() throws Exception {
PagedResultsRequestControl tested = new PagedResultsRequestControl(20);
PagedResultsControl control = (PagedResultsControl) tested
.createRequestControl();
assertNotNull(control);
@@ -69,7 +70,7 @@ public class PagedResultsRequestControlTest extends TestCase {
public void testCreateRequestControl_CookieSet() throws Exception {
PagedResultsCookie cookie = new PagedResultsCookie(new byte[0]);
PagedResultsRequestControl tested = new PagedResultsRequestControl(20,
PagedResultsDirContextProcessor tested = new PagedResultsDirContextProcessor(20,
cookie);
PagedResultsControl control = (PagedResultsControl) tested
@@ -90,8 +91,6 @@ public class PagedResultsRequestControlTest extends TestCase {
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), new Control[] { control });
PagedResultsRequestControl tested = new PagedResultsRequestControl(20);
replay();
tested.postProcess(ldapContextMock);
@@ -119,8 +118,6 @@ public class PagedResultsRequestControlTest extends TestCase {
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), new Control[] { control });
PagedResultsRequestControl tested = new PagedResultsRequestControl(20);
replay();
tested.postProcess(ldapContextMock);
@@ -136,8 +133,6 @@ public class PagedResultsRequestControlTest extends TestCase {
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), null);
PagedResultsRequestControl tested = new PagedResultsRequestControl(20);
replay();
tested.postProcess(ldapContextMock);

View File

@@ -19,7 +19,6 @@ import java.io.IOException;
import javax.naming.ldap.Control;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.PagedResultsControl;
import javax.naming.ldap.SortControl;
import javax.naming.ldap.SortResponseControl;
@@ -34,9 +33,6 @@ import com.sun.jndi.ldap.ctl.DirSyncResponseControl;
/**
* Unit tests for the SortControlDirContextProcessor class.
* {@link javax.naming.ldap.SortControl}
* {@link javax.naming.ldap.SortResponseControl}
* {@link PagedResultsControl}
*
* @author Ulrik Sandberg
*/
@@ -46,9 +42,13 @@ public class SortControlDirContextProcessorTest extends TestCase {
private LdapContext ldapContextMock;
private SortControlDirContextProcessor tested;
protected void setUp() throws Exception {
super.setUp();
tested = new SortControlDirContextProcessor("key");
// Create ldapContext mock
ldapContextControl = MockControl.createControl(LdapContext.class);
ldapContextMock = (LdapContext) ldapContextControl.getMock();
@@ -57,6 +57,7 @@ public class SortControlDirContextProcessorTest extends TestCase {
protected void tearDown() throws Exception {
super.tearDown();
tested = null;
ldapContextControl = null;
ldapContextMock = null;
}
@@ -70,9 +71,6 @@ public class SortControlDirContextProcessorTest extends TestCase {
}
public void testCreateRequestControl() throws Exception {
SortControlDirContextProcessor tested = new SortControlDirContextProcessor(
"key");
SortControl result = (SortControl) tested.createRequestControl();
assertNotNull(result);
assertEquals("1.2.840.113556.1.4.473", result.getID());
@@ -89,8 +87,6 @@ public class SortControlDirContextProcessorTest extends TestCase {
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), new Control[] { control });
SortControlDirContextProcessor tested = new SortControlDirContextProcessor("key");
replay();
tested.postProcess(ldapContextMock);
@@ -111,8 +107,6 @@ public class SortControlDirContextProcessorTest extends TestCase {
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), new Control[] { control });
SortControlDirContextProcessor tested = new SortControlDirContextProcessor("key");
replay();
tested.postProcess(ldapContextMock);
@@ -138,8 +132,6 @@ public class SortControlDirContextProcessorTest extends TestCase {
ldapContextControl.expectAndDefaultReturn(ldapContextMock
.getResponseControls(), new Control[] { control });
SortControlDirContextProcessor tested = new SortControlDirContextProcessor("key");
replay();
tested.postProcess(ldapContextMock);