[
https://issues.apache.org/jira/browse/DIRAPI-189?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13972383#comment-13972383
]
Robert Hou commented on DIRAPI-189:
-----------------------------------
I want to add the two controls into SearchRequest as following:
-------------
SearchRequest sr = new SearchRequestImpl();
AdDirSyncImpl adDirSyncImpl = new AdDirSyncImpl();
AdDirSyncResponseImpl adDirSyncResponseImpl = new AdDirSyncResponseImpl();
//Control[] controls = new Control[]{adDirSyncImpl,adDirSyncResponseImpl};
Control[] controls = new Control[]{adDirSyncResponseImpl,adDirSyncImpl};
sr.addAllControls(controls);
-------------
As I test, I find following issues:
1.SearchRequest only load the last control in the Control array.
2.If the last control in the Control array is not AdDirSyncResponseImpl, i will
get following exception when do the search:
----------------------------------
org.apache.mina.filter.codec.ProtocolEncoderException:
java.lang.ClassCastException:
org.apache.directory.api.ldap.extras.controls.ad.AdDirSyncImpl cannot be cast
to org.apache.directory.api.ldap.extras.controls.ad.AdDirSyncResponse
----------------------------------
3.If the last control in the Control array is AdDirSyncResponseImpl or only set
AdDirSyncResponseImpl in the sr.The functionality(I want to monitor spficied
attributes) already is archieve. So, looks like AdDirSyncImpl is needless.
The test code as following:
@Test
public void testDirSyncControl()
{
LdapConnectionConfig config = new LdapConnectionConfig();
config.setLdapHost("192.168.80.223");
config.setLdapPort(50001);
config.setName("cn=robert,cn=roles,dc=tibco,dc=com");
config.setCredentials("robert");
DefaultConfigurableBinaryAttributeDetector bad = new
DefaultConfigurableBinaryAttributeDetector();
config.setBinaryAttributeDetector(bad);
LdapNetworkConnection connection = new
LdapNetworkConnection(config);
try {
connection.bind("cn=robert,cn=roles,dc=tibco,dc=com","robert");
} catch (LdapException e4) {
e4.printStackTrace();
}
SearchRequest sr = new SearchRequestImpl();
AdDirSyncImpl adDirSyncImpl = new AdDirSyncImpl();
AdDirSyncResponseImpl adDirSyncResponseImpl = new
AdDirSyncResponseImpl();
// Control[] controls = new
Control[]{adDirSyncImpl,adDirSyncResponseImpl};// is ok
// Control[] controls = new
Control[]{adDirSyncResponseImpl,adDirSyncImpl};// will cause
org.apache.mina.filter.codec.ProtocolEncoderException:
java.lang.ClassCastException:
// sr.addAllControls(controls);
sr.addControl(adDirSyncResponseImpl);//is ok
try {
sr.setBase(new Dn("DC=tibco,DC=com"));
} catch (LdapInvalidDnException e1) {
e1.printStackTrace();
}
try {
sr.setFilter("(objectClass=inetorgperson)");
} catch (LdapException e1) {
e1.printStackTrace();
}
sr.setScope(SearchScope.SUBTREE);
SearchCursor cursor = null;
try{
cursor = connection.search(sr);
}catch(LdapException e){
e.printStackTrace();
}
try {
while(cursor.next()){
if(!cursor.isDone()){
continue;
}
}
} catch (LdapException e4) {
e4.printStackTrace();
} catch (CursorException e4) {
e4.printStackTrace();
}
byte[] cookie = null;
if(cursor.isDone()){
SearchResultDone searchResultDone =
cursor.getSearchResultDone();
Control control =
searchResultDone.getControl(AdDirSyncResponseImpl.OID);
if (control instanceof
AdDirSyncResponseDecorator){
AdDirSyncResponseDecorator
adDirSyncResponseDecorator = (AdDirSyncResponseDecorator)control;
cookie =
adDirSyncResponseDecorator.getCookie();
}
}
String[] returnAttributes = {"description"};
sr.addAttributes(returnAttributes);
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while(true){
try {
String input;
while (true) {
System.out.println("Press x to
exit, return to continue");
input = in.readLine();
if (input.startsWith("x") ||
input.startsWith("X")) {
connection.close();
System.exit(0);
}
else {
break;
}
}
}catch (IOException e) {
System.err.println();
}
adDirSyncResponseImpl.setCookie(cookie);
try {
cursor = connection.search(sr);
} catch (LdapException e) {
e.printStackTrace();
}
int i = 0;
try {
while(cursor.next()){
i++;
Entry entry = cursor.getEntry();
Dn dn = entry.getDn();
System.out.println("dn = " +
dn);
Collection<Attribute>
colAttribute = entry.getAttributes();
Iterator<Attribute> itAttribute
= colAttribute.iterator();
while(itAttribute.hasNext()){
Attribute attribute =
itAttribute.next();
String attributeName =
attribute.getUpId();
Iterator<Value<?>>
itValue = attribute.iterator();
while(itValue.hasNext()){
Value<?> value
= itValue.next();
if(value.isHumanReadable()){
String
strValue = value.getString();
System.out.println("\t" + attributeName + "=" + strValue);
}else{
System.out.println("\t" + attributeName + "withBinary=" +
value.getValue().toString());
}
}
}
}
} catch (LdapException e) {
e.printStackTrace();
} catch (CursorException e) {
e.printStackTrace();
}
System.out.println("total is " + i);
if(cursor.isDone()){
SearchResultDone searchResultDone =
cursor.getSearchResultDone();
Control control =
searchResultDone.getControl(AdDirSyncResponseImpl.OID);
if (control instanceof
AdDirSyncResponseDecorator){
AdDirSyncResponseDecorator
adDirSyncResponseDecorator = (AdDirSyncResponseDecorator)control;
cookie =
adDirSyncResponseDecorator.getCookie();
}
}
}
}
> Support DirSync control
> -----------------------
>
> Key: DIRAPI-189
> URL: https://issues.apache.org/jira/browse/DIRAPI-189
> Project: Directory Client API
> Issue Type: Wish
> Affects Versions: 1.0.0-M21
> Reporter: Robert Hou
> Fix For: 1.0.0-M22
>
>
> I originally wanted to impletent DirSync Control using Apache API. After I
> looked into existing Controls under
> org.apache.directory.api.ldap.model.message.controls. I think it's difficult
> to me. I can't understand the decoding and encoding of controls specification
> of Apache. So I want Apache team give implementation for this control.
> Details for this control, please refer to
> http://msdn.microsoft.com/en-us/library/ms677626(v=vs.85).aspx.
--
This message was sent by Atlassian JIRA
(v6.2#6252)