mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Mark Craig
21.45.2012 298f4fe16d8077e148d8c505590fe6aeac827e49
Some updates to the example based on Matt's suggestions

Also a little script to smoke test the different requests and entries being rewritten
1 files added
1 files modified
190 ■■■■ changed files
opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/RewriterProxy.java 96 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk-examples/src/test/bin/checkRewriterProxy.sh 94 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/RewriterProxy.java
@@ -30,7 +30,9 @@
import static org.forgerock.opendj.ldap.ErrorResultException.newErrorResult;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.forgerock.opendj.ldap.Attribute;
import org.forgerock.opendj.ldap.AttributeDescription;
@@ -73,6 +75,7 @@
import org.forgerock.opendj.ldap.responses.Result;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;
import org.forgerock.opendj.ldap.responses.SearchResultReference;
import org.forgerock.opendj.ldap.schema.AttributeType;
/**
 * This example is based on the {@link Proxy}. This example does no load
@@ -206,13 +209,31 @@
            private SearchResultEntry rewrite(SearchResultEntry entry) {
                // Replace server attributes with client attributes.
                // TODO: Handle attributes with options
                Attribute serverAttribute = entry.getAttribute(
                        serverAttributeDescription);
                Attribute clientAttribute = new LinkedAttribute(
                        clientAttributeDescription, serverAttribute.toArray());
                entry.addAttribute(clientAttribute);
                entry.removeAttribute(serverAttributeDescription);
                Set<Attribute> attrsToAdd = new HashSet<Attribute>();
                Set<AttributeDescription> attrsToRemove = new HashSet<AttributeDescription>();
                for (Attribute a : entry.getAllAttributes(serverAttributeDescription)) {
                    AttributeDescription ad = a.getAttributeDescription();
                    AttributeType at = ad.getAttributeType();
                    if (at.equals(serverAttributeDescription.getAttributeType())) {
                        AttributeDescription clientAttrDesc =
                                AttributeDescription.valueOf(ad.toString()
                                        .replaceFirst(
                                                serverAttributeTypeName,
                                                clientAttributeTypeName));
                        attrsToAdd.add(new LinkedAttribute(clientAttrDesc, a.toArray()));
                        attrsToRemove.add(ad);
                    }
                }
                if (!attrsToAdd.isEmpty() && !attrsToRemove.isEmpty()) {
                    for (Attribute a : attrsToAdd) {
                        entry.addAttribute(a);
                    }
                    for (AttributeDescription ad : attrsToRemove) {
                        entry.removeAttribute(ad);
                    }
                }
                // Transform the server DN suffix into a client DN suffix.
                return entry.setName(entry.getName().toString()
@@ -260,20 +281,19 @@
                            // Transform the client attribute names into server
                            // attribute names, fullname;lang-fr ==> cn;lang-fr.
                            for (Attribute clientAttribute
                            for (Attribute a
                                    : request.getAllAttributes(clientAttributeDescription)) {
                                if (clientAttribute != null) {
                                    String attrDesc = clientAttribute
                                if (a != null) {
                                    String ad = a
                                            .getAttributeDescriptionAsString()
                                            .replaceFirst(clientAttributeTypeName,
                                                          serverAttributeTypeName);
                                    Attribute serverAttribute =
                                            new LinkedAttribute(
                                                    AttributeDescription.valueOf(attrDesc),
                                                    clientAttribute.toArray());
                                    rewrittenRequest.addAttribute(serverAttribute);
                                    Attribute serverAttr = new LinkedAttribute(
                                            AttributeDescription.valueOf(ad),
                                            a.toArray());
                                    rewrittenRequest.addAttribute(serverAttr);
                                    rewrittenRequest.removeAttribute(
                                            clientAttribute.getAttributeDescription());
                                            a.getAttributeDescription());
                                }
                            }
@@ -368,15 +388,15 @@
                            // Transform the client attribute name into a server
                            // attribute name, fullname;lang-fr ==> cn;lang-fr.
                            String attrName = request.getAttributeDescription().toString();
                            if (attrName.toLowerCase().startsWith(
                            String ad = request.getAttributeDescription().toString();
                            if (ad.toLowerCase().startsWith(
                                    clientAttributeTypeName.toLowerCase())) {
                                String rewrittenAttrName = attrName
                                String serverAttrDesc = ad
                                        .replaceFirst(clientAttributeTypeName,
                                                      serverAttributeTypeName);
                                request.setAttributeDescription(
                                        AttributeDescription.valueOf(
                                                rewrittenAttrName));
                                                serverAttrDesc));
                            }
                            // Transform the client DN into a server DN.
@@ -489,20 +509,20 @@
                            // attribute names, fullname;lang-fr ==> cn;lang-fr.
                            List<Modification> mods = request.getModifications();
                            for (Modification mod : mods) {
                                AttributeDescription attrDesc =
                                        mod.getAttribute().getAttributeDescription();
                                Attribute a = mod.getAttribute();
                                AttributeDescription ad = a.getAttributeDescription();
                                AttributeType at = ad.getAttributeType();
                                if (attrDesc.equals(clientAttributeDescription)) {
                                    String rewrittenAttrName =
                                            attrDesc.toString()
                                                .replaceFirst(clientAttributeTypeName,
                                                              serverAttributeTypeName);
                                    Attribute serverAttribute = new LinkedAttribute(
                                            AttributeDescription.valueOf(rewrittenAttrName),
                                            mod.getAttribute().toArray());
                                if (at.equals(clientAttributeDescription.getAttributeType())) {
                                    AttributeDescription serverAttrDesc =
                                            AttributeDescription.valueOf(ad.toString()
                                                    .replaceFirst(
                                                            clientAttributeTypeName,
                                                            serverAttributeTypeName));
                                    rewrittenRequest.addModification(new Modification(
                                            mod.getModificationType(),
                                            serverAttribute));
                                            new LinkedAttribute(
                                                    serverAttrDesc, a.toArray())));
                                } else {
                                    rewrittenRequest.addModification(mod);
                                }
@@ -581,14 +601,16 @@
                        private SearchRequest rewrite(final SearchRequest request) {
                            // Transform the client attribute names to a server
                            // attribute names, fullname;lang-fr ==> cn;lang-fr.
                            String[] attrNames =
                                    new String[request.getAttributes().size()];
                            String[] a = new String[request.getAttributes().size()];
                            int count = 0;
                            for (String attrName : request.getAttributes()) {
                                if (attrName.equalsIgnoreCase(clientAttributeTypeName)) {
                                    attrNames[count] = serverAttributeTypeName;
                                if (attrName.toLowerCase().startsWith(
                                        clientAttributeTypeName.toLowerCase())) {
                                    a[count] = attrName.replaceFirst(
                                            clientAttributeTypeName,
                                            serverAttributeTypeName);
                                } else {
                                    attrNames[count] = attrName;
                                    a[count] = attrName;
                                }
                                ++count;
                            }
@@ -605,7 +627,7 @@
                                    Filter.valueOf(request.getFilter().toString()
                                            .replace(clientAttributeTypeName,
                                                     serverAttributeTypeName)),
                                    attrNames);
                                    a);
                        }
                    };
opendj3/opendj-ldap-sdk-examples/src/test/bin/checkRewriterProxy.sh
New file
@@ -0,0 +1,94 @@
#!/bin/bash
# Smoke test RewriterProxy.java using OpenDJ tools.
# Depends on http://opendj.forgerock.org/Example.ldif being in OpenDJ.
OPENDJ_TOOLS_DIR="/path/to/OpenDJ/bin"  # ldapcompare, ldapdelete, ldapmodify, ldapsearch
HOST=localhost                          # Host where proxy listens
PORT=8389                               # Port where proxy listens
BINDDN="uid=kvaughan,ou=People,dc=example,dc=com"
BINDPWD=bribery
CURRDIR=`pwd`
if [ -e $OPENDJ_TOOLS_DIR ]
then
    cd $OPENDJ_TOOLS_DIR
else
    exit 1
fi
#set -x
echo Deleting uid=fdupont,ou=People,o=example...
./ldapdelete -h $HOST -p $PORT -D $BINDDN -w $BINDPWD uid=fdupont,ou=People,o=example
echo
echo Adding uid=fdupont,ou=People,o=example...
./ldapmodify -h $HOST -p $PORT -D $BINDDN -w $BINDPWD -a <<EOF
dn: uid=fdupont,ou=People,o=example
uid: fdupont
fullname: Frederique Dupont
fullname;lang-fr: Fredérique Dupont
givenName: Fredérique
sn: Dupont
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: posixAccount
objectClass: top
ou: People
ou: Product Development
telephoneNumber: +33 1 23 45 67 89
facsimileTelephoneNumber: +33 1 23 45 67 88
mail: fdupont@example.fr
roomNumber: 0042
l: Paris
gidNumber: 1000
uidNumber: 1110
homeDirectory: /home/fdupont
userPassword: password
EOF
echo
echo Looking for fullname=Frederique Dupont...
./ldapsearch -h $HOST -p $PORT -D $BINDDN -w $BINDPWD -b o=example "(fullname=Frederique Dupont)" fullname
echo
echo Comparing fullname:Frederique Dupont...
./ldapcompare -h $HOST -p $PORT -D $BINDDN -w $BINDPWD "fullname:Frederique Dupont" uid=fdupont,ou=People,o=example
echo
echo Changing fullname...
./ldapmodify -h $HOST -p $PORT -D $BINDDN -w $BINDPWD <<EOM
dn: uid=fdupont,ou=People,o=example
changetype: modify
replace: fullname
fullname: Fred Dupont
EOM
echo
echo Changing uid=fdupont to uid=qdupont...
./ldapmodify -h $HOST -p $PORT -D $BINDDN -w $BINDPWD <<EOR
dn: uid=fdupont,ou=People,o=example
changetype: modrdn
newrdn: uid=qdupont
deleteoldrdn: 1
EOR
echo
echo Deleting uid=qdupont,ou=People,o=example
./ldapdelete -h $HOST -p $PORT -D $BINDDN -w $BINDPWD uid=qdupont,ou=People,o=example
echo
cd $CURRDIR
echo Done.
exit 0