protected void execute()

in blocks/cocoon-naming/cocoon-naming-impl/src/main/java/org/apache/cocoon/transformation/LDAPTransformer.java [1000:1552]


        protected void execute() throws Exception {
            String[] attrList = new String[attrListe.size()];

            AttributesImpl attr = new AttributesImpl();
            if (debug) {
                debugPrint();
            }
            SearchControls constraints = new SearchControls();
            attrListe.toArray(attrList);
            attrListe.clear();
            try {
                connect();
                switch (toDo) {
                    case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY :
                        try {
                            initConstraints(constraints);
                            if (attrList.length > 0) {
                                constraints.setReturningAttributes(attrList);
                            }

                            if (!filter.equals("")) {
                                //filter is present
                                if (!doc_element.equals("")) {
                                    transformer.start(doc_element, attr);
                                }
                                NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);

                                while (ldapresults != null && ldapresults.hasMore()) {
                                    SearchResult si = (SearchResult) ldapresults.next();
                                    if (!row_element.equals("")) {
                                        if(!"".equals(dn_attribute)) {
                                            String dn;
                                            if(!"".equals(searchbase))
                                                dn = si.getName() + ',' + searchbase;
                                            else
                                                dn = si.getName();
                                            attr.addAttribute(null, dn_attribute, dn_attribute, "CDATA", dn);
                                        }
                                        transformer.start(row_element, attr);
                                    }
                                    javax.naming.directory.Attributes attrs = si.getAttributes();
                                    if (attrs != null) {
                                        NamingEnumeration ae = attrs.getAll();
                                        while (ae.hasMoreElements()) {
                                            Attribute at = (Attribute) ae.next();
                                            Enumeration vals = at.getAll();
                                            String attrID = at.getID();
                                            while (vals.hasMoreElements()) {
                                                if (showAttribute) {
                                                    transformer.start(attrID, attr);
                                                }
                                                String attrVal = recodeFromLDAPEncoding((String) vals.nextElement());
                                                if (query_index > 0) {
                                                    switch (transformer.getQuery(query_index - 1).current_state) {
                                                        case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
                                                            StringBuffer temp =  new StringBuffer(transformer.getQuery(query_index - 1).filter);
                                                            if (temp.length() > 0) {
                                                                temp.append(", ");
                                                            }
                                                            temp.append(attrID).append("=").append(attrVal);
                                                            transformer.getQuery(query_index - 1).filter = temp.toString();
                                                            break;
                                                        default :
                                                            transformer.start(attrID, attr);
                                                    }
                                                } else {
                                                    transformer.data(String.valueOf(attrVal));
                                                }
                                                if (showAttribute) {
                                                    transformer.end(attrID);
                                                }
                                            }
                                        }
                                    }
                                    if (!row_element.equals("")) {
                                        transformer.end(row_element);
                                    }
                                }
                                if (!doc_element.equals("")) {
                                    transformer.end(doc_element);
                                }
                            } else {
                                //filter not present, get the values from absolete path
                                javax.naming.directory.Attributes attrs = ctx.getAttributes(searchbase, attrList);
                                if (!doc_element.equals("")) {
                                    transformer.start(doc_element, attr);
                                }
                                if (!row_element.equals("")) {
                                    if(!"".equals(dn_attribute)) {
                                        attr.addAttribute(null, dn_attribute, dn_attribute, "CDATA", searchbase);
                                    }
                                    transformer.start(row_element, attr);
                                }
                                if (attrs != null) {
                                    NamingEnumeration ae = attrs.getAll();
                                    while (ae.hasMoreElements()) {
                                        Attribute at = (Attribute) ae.next();
                                        Enumeration vals = at.getAll();
                                        String attrID = at.getID();
                                        while (vals.hasMoreElements()) {
                                            if (showAttribute) {
                                                transformer.start(attrID, attr);
                                            }
                                            String attrVal = recodeFromLDAPEncoding((String)vals.nextElement());

                                            if (query_index > 0) {
                                                switch (transformer.getQuery(query_index - 1).current_state) {
                                                    case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
                                                        StringBuffer temp = new StringBuffer(transformer.getQuery(query_index - 1).filter);
                                                        if (temp.length() > 0) {
                                                            temp.append(", ");
                                                        }
                                                        temp.append(attrID).append("=").append(attrVal);
                                                        transformer.getQuery(query_index - 1).filter = temp.toString();
                                                        break;
                                                    default :
                                                        transformer.start(attrID, attr);
                                                }
                                            } else {
                                                transformer.data(String.valueOf(attrVal));
                                            }
                                            if (showAttribute) {
                                                transformer.end(attrID);
                                            }
                                        }
                                    }
                                }
                                if (!row_element.equals("")) {
                                    transformer.end(row_element);
                                }
                                if (!doc_element.equals("")) {
                                    transformer.end(doc_element);
                                }
                            }
                        } catch (Exception e) {
                            if (sax_error) {
                                throw new Exception("[LDAPTransformer] Error in LDAP-Query: " + e.toString());
                            } else {
                                transformer.start(error_element, attr);
                                transformer.data("[LDAPTransformer] Error in LDAP-Query: " + e);
                                transformer.end(error_element);
                                transformer.getLogger().error("[LDAPTransformer] Exception: " + e.toString());
                            }
                        }
                        break;
                    case LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT :
                        try {
                            initConstraints(constraints);
                            if (attrList.length != 1) {
                                transformer.start(error_element, attr);
                                transformer.data("Increment must reference exactly 1 attribute.");
                                transformer.end(error_element);
                            } else {
                                constraints.setReturningAttributes(attrList);
                                NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
                                int attrVal = 0;
                                String attrID = "";
                                SearchResult si = null;
                                while (ldapresults != null && ldapresults.hasMore()) {
                                    si = (SearchResult) ldapresults.next();
                                    javax.naming.directory.Attributes attrs = si.getAttributes();
                                    if (attrs != null) {
                                        NamingEnumeration ae = attrs.getAll();
                                        while (ae.hasMoreElements()) {
                                            Attribute at = (Attribute) ae.next();
                                            Enumeration vals = at.getAll();
                                            attrID = at.getID();
                                            attrVal = Integer.parseInt((String) vals.nextElement());
                                        }
                                    }
                                }
                                ++attrVal;
                                // Specify the changes to make
                                ModificationItem[] mods = new ModificationItem[1];
                                // Replace the "mail" attribute with a new value
                                mods[0] =
                                    new ModificationItem(
                                        DirContext.REPLACE_ATTRIBUTE,
                                        new BasicAttribute(attrID, Integer.toString(attrVal)));
                                // Perform the requested modifications on the named object
                                ctx.modifyAttributes(
                                    new StringBuffer(si.toString().substring(0, si.toString().indexOf(":")))
                                        .append(",")
                                        .append(searchbase)
                                        .toString(),
                                    mods);
                            }
                        } catch (Exception e) {
                            if (sax_error) {
                                throw new Exception("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
                            } else {
                                transformer.start(error_element, attr);
                                transformer.data("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
                                transformer.end(error_element);
                                transformer.getLogger().error("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
                            }
                        }
                        break;
                    /* execute modes */
                    case LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE :
                        try {
                            String[] attrVal = new String[attrVale.size()];
                            String[] attrMode = new String[attrModeVal.size()];
                            String replaceMode;
                            attrVale.toArray(attrVal);
                            attrVale.clear();
                            attrModeVal.toArray(attrMode);
                            attrModeVal.clear();

                            if (attrVal.length != attrList.length) {
                                transformer.start(error_element, attr);
                                transformer.data("Attribute values must have the some number as a names");
                                transformer.end(error_element);
                                break;
                            }
                            HashMap attrMap = new HashMap(attrVal.length);
                            HashMap attrModeMap = new HashMap(attrMode.length);

                            for (int i = 0; i < attrVal.length; i++) {
                                attrMap.put(attrList[i], attrVal[i]);
                                attrModeMap.put(attrList[i], attrMode[i]);
                            }

                            initConstraints(constraints);
                            if (attrList.length < 1) {
                                transformer.start(error_element, attr);
                                transformer.data("Modify must reference 1 or more attribute.");
                                transformer.end(error_element);
                            } else {
                                if (!filter.equals("")) {
                                    constraints.setReturningAttributes(attrList);
                                    NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
                                    SearchResult si;
                                    /* start indicate element of executing query */
                                    if (!exec_element.equals("")) {
                                        transformer.start(exec_element, attr);
                                    }
                                    while (ldapresults != null && ldapresults.hasMore()) {
                                        if (!row_element.equals("")) {
                                            transformer.start(row_element, attr);
                                        }

                                        si = (SearchResult) ldapresults.next();
                                        javax.naming.directory.Attributes attrs = si.getAttributes();
                                        if (attrs != null) {
                                            NamingEnumeration ae = attrs.getAll();
                                            while (ae.hasMoreElements()) {
                                                Attribute at = (Attribute) ae.next();
                                                Enumeration vals = at.getAll();
                                                String attrID = at.getID();
                                                ModificationItem[] mods = new ModificationItem[1];
                                                replaceMode = (String) attrModeMap.get(attrID);

                                                String attrValue = recodeFromLDAPEncoding((String) vals.nextElement());
                                                String newAttrValue = "";
                                                /* Check the replacing method */
                                                if (replaceMode.equals(REPLACE_MODE_DEFAULT)) {
                                                    newAttrValue = (String)attrMap.get(attrID);
                                                } else if (replaceMode.equals(REPLACE_MODE_APPEND)) {
                                                    newAttrValue = attrValue + attrMap.get(attrID);
                                                }
                                                newAttrValue = recodeToLDAPEncoding(newAttrValue);

                                                mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                                                                               new BasicAttribute(attrID,newAttrValue));

                                                // Perform the requested modifications on the named object
                                                ctx.modifyAttributes(
                                                        si.toString().substring(0, si.toString().indexOf(":")) + "," + searchbase,
                                                        mods);

                                                /* confirm of success */
                                                transformer.start(attrID, attr);
                                                transformer.data("replaced");
                                                transformer.end(attrID);
                                            }
                                        }

                                        if (!row_element.equals("")) {
                                            transformer.end(row_element);
                                        }

                                    }
                                    if (!exec_element.equals("")) {
                                        transformer.end(exec_element);
                                    }
                                } else {
                                    //filter is not present
                                    javax.naming.directory.Attributes attrs = ctx.getAttributes(searchbase, attrList);
                                    /* start indicate element of executing query */
                                    if (!exec_element.equals("")) {
                                        transformer.start(exec_element, attr);
                                    }
                                    if (!row_element.equals("")) {
                                        transformer.start(row_element, attr);
                                    }
                                    if (attrs != null) {
                                        NamingEnumeration ae = attrs.getAll();
                                        while (ae.hasMoreElements()) {
                                            Attribute at = (Attribute) ae.next();
                                            Enumeration vals = at.getAll();
                                            String attrID = at.getID();
                                            ModificationItem[] mods = new ModificationItem[1];
                                            replaceMode = (String) attrModeMap.get(attrID);

                                            String attrValue = recodeFromLDAPEncoding((String) vals.nextElement());

                                            String newAttrValue = "";
                                            /* Check the replacing method */
                                            if (replaceMode.equals(REPLACE_MODE_DEFAULT)) {
                                                newAttrValue = (String) attrMap.get(attrID);
                                            } else if (replaceMode.equals(REPLACE_MODE_APPEND)) {
                                                newAttrValue = attrValue + attrMap.get(attrID);
                                            }
                                            newAttrValue = recodeToLDAPEncoding(newAttrValue);

                                            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                                                                           new BasicAttribute(attrID, newAttrValue));

                                            // Perform the requested modifications on the named object
                                            ctx.modifyAttributes(searchbase, mods);

                                            /* confirm of success */
                                            transformer.start(attrID, attr);
                                            transformer.data("replaced");
                                            transformer.end(attrID);
                                        }
                                    }

                                    if (!row_element.equals("")) {
                                        transformer.end(row_element);
                                    }

                                    /* end indicate element of executing query */
                                    if (!exec_element.equals("")) {
                                        transformer.end(exec_element);
                                    }
                                }
                            }

                        } catch (Exception e) {
                            if (sax_error) {
                                throw new Exception("[LDAPTransformer] Error replacing an attribute: " + e.toString());
                            } else {
                                transformer.start(error_element, attr);
                                transformer.data("[LDAPTransformer] Error replacing an attribute: " + e.toString());
                                transformer.end(error_element);
                                transformer.getLogger().error("[LDAPTransformer] Error replacing an attribute: " + e.toString());
                                if (!row_element.equals("")) {
                                    transformer.end(row_element);
                                }
                                if (!exec_element.equals("")) {
                                    transformer.end(exec_element);
                                }
                            }
                        }
                        break;
                    case LDAPTransformer.STATE_INSIDE_EXECUTE_ADD :
                        try {
                            String[] attrVal = new String[attrVale.size()];
                            attrVale.toArray(attrVal);
                            attrVale.clear();
                            if (attrVal.length != attrList.length) {
                                transformer.start(error_element, attr);
                                transformer.data("Attribute values must have the some number as a names");
                                transformer.end(error_element);
                                break;
                            }
                            HashMap attrMap = new HashMap(attrVal.length);

                            for (int i = 0; i < attrVal.length; i++)
                                attrMap.put(attrList[i], attrVal[i]);

                            initConstraints(constraints);
                            if (attrList.length < 1) {
                                transformer.start(error_element, attr);
                                transformer.data("Modify must reference 1 or more attribute.");
                                transformer.end(error_element);
                            } else {
                                if (!filter.equals("")) {
                                    constraints.setReturningAttributes(attrList);
                                    NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
                                    SearchResult si;
                                    /* start indicate element of executing query */
                                    if (!exec_element.equals("")) {
                                        transformer.start(exec_element, attr);
                                    }
                                    while (ldapresults != null && ldapresults.hasMore()) {
                                        if (!row_element.equals("")) {
                                            transformer.start(row_element, attr);
                                        }

                                        si = (SearchResult) ldapresults.next();
                                        javax.naming.directory.Attributes attrs = si.getAttributes();
                                        if (attrs != null) {
                                            /* Replace the attribute if attribute already exist */
                                            NamingEnumeration ae = attrs.getAll();
                                            while (ae.hasMoreElements()) {
                                                Attribute at = (Attribute) ae.next();
                                                String attrID = at.getID();
                                                // Specify the changes to make
                                                ModificationItem[] mods = new ModificationItem[1];

                                                String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrID));
                                                mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                                                                               new BasicAttribute(attrID, attrValue));
                                                // Perform the requested modifications on the named object
                                                ctx.modifyAttributes(
                                                    new StringBuffer(si.toString().substring(0, si.toString().indexOf(":")))
                                                        .append(",")
                                                        .append(searchbase)
                                                        .toString(),
                                                    mods);

                                                /* confirm of success */
                                                transformer.start(attrID, attr);
                                                transformer.data("replaced");
                                                transformer.end(attrID);
                                                /* Remove the attribute from map after replacing */
                                                attrMap.remove(attrID);
                                            }
                                        }
                                        /* Add the attributes */
                                        if (!attrMap.isEmpty()) {
                                            ModificationItem[] mods = new ModificationItem[1];
                                            for (int i = 0; i < attrList.length; i++) {
                                                if (attrMap.containsKey(attrList[i])) {
                                                    String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrList[i]));
                                                    mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                                                                                   new BasicAttribute(attrList[i], attrValue));
                                                    // Perform the requested modifications on the named object
                                                    ctx.modifyAttributes(
                                                        new StringBuffer(si.toString().substring(0, si.toString().indexOf(":")))
                                                            .append(",")
                                                            .append(searchbase)
                                                            .toString(),
                                                        mods);

                                                    /* confirm of success */
                                                    transformer.start(attrList[i], attr);
                                                    transformer.data("add");
                                                    transformer.end(attrList[i]);
                                                }
                                            }
                                        }
                                        if (!row_element.equals("")) {
                                            transformer.end(row_element);
                                        }
                                    }
                                    if (!exec_element.equals("")) {
                                        transformer.end(exec_element);
                                    }
                                } else {
                                    //filter is not present
                                    javax.naming.directory.Attributes attrs = ctx.getAttributes(searchbase, attrList);
                                    /* start indicate element of executing query */
                                    if (!exec_element.equals("")) {
                                        transformer.start(exec_element, attr);
                                    }
                                    if (!row_element.equals("")) {
                                        transformer.start(row_element, attr);
                                    }

                                    if (attrs != null) {
                                        NamingEnumeration ae = attrs.getAll();
                                        while (ae.hasMoreElements()) {
                                            Attribute at = (Attribute) ae.next();
                                            String attrID = at.getID();
                                            // Specify the changes to make
                                            ModificationItem[] mods = new ModificationItem[1];

                                            String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrID));
                                            mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                                                                           new BasicAttribute(attrID,  attrValue));
                                            // Perform the requested modifications on the named object
                                            ctx.modifyAttributes(searchbase, mods);

                                            /* confirm of success */
                                            transformer.start(attrID, attr);
                                            transformer.data("replaced");
                                            transformer.end(attrID);
                                            /* Remove the attribute from map after replacing */
                                            attrMap.remove(attrID);
                                        }
                                    }
                                    /* Add the attributes */
                                    if (!attrMap.isEmpty()) {
                                        ModificationItem[] mods = new ModificationItem[1];
                                        for (int i = 0; i < attrList.length; i++) {
                                            if (attrMap.containsKey(attrList[i])) {
                                                String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrList[i]));
                                                mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                                                                               new BasicAttribute(attrList[i], attrValue));
                                                // Perform the requested modifications on the named object
                                                ctx.modifyAttributes(searchbase, mods);
                                                /* confirm of success */
                                                transformer.start(attrList[i], attr);
                                                transformer.data("add");
                                                transformer.end(attrList[i]);
                                            }
                                        }
                                    }
                                    if (!row_element.equals("")) {
                                        transformer.end(row_element);
                                    }
                                    /* end indicate element of executing query */
                                    if (!exec_element.equals("")) {
                                        transformer.end(exec_element);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            if (sax_error) {
                                throw new Exception("[LDAPTransformer] Error replacing an attribute: " + e.toString());
                            } else {
                                transformer.start(error_element, attr);
                                transformer.data("[LDAPTransformer] Error replacing an attribute: " + e.toString());
                                transformer.end(error_element);
                                transformer.getLogger().error("[LDAPTransformer] Error replacing an attribute: " + e.toString());
                                if (!row_element.equals("")) {
                                    transformer.end(row_element);
                                }
                                if (!exec_element.equals("")) {
                                    transformer.end(exec_element);
                                }
                            }
                        }
                        break;
                    default :
                } //end switch
            } catch (NamingException e) {
                if (sax_error) {
                    throw new NamingException("[LDAPTransformer] Failed ldap-connection to directory service: " + e.toString());
                } else {
                    transformer.start(error_element, attr);
                    transformer.data("[LDAPTransformer] Failed ldap-connection to directory service.");
                    transformer.end(error_element);
                    transformer.getLogger().error("[LDAPTransformer] Failed to connect to " + serverurl + e.toString());
                }
            }
            try {
                disconnect();
            } catch (NamingException e) {
                if (sax_error) {
                    throw new NamingException("[LDAPTransformer] Failed ldap-disconnection from directory service: " + e.toString());
                } else {
                    transformer.start(error_element, attr);
                    transformer.data("[LDAPTransformer] Failed ldap-disconnection to directory service.");
                    transformer.end(error_element);
                    transformer.getLogger().error("[LDAPTransformer] Failed to disconnect from " + serverurl + e.toString());
                }
            }
        }