0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: package org.apache.cocoon.transformation;
0018:
0019: import java.io.IOException;
0020: import java.io.UnsupportedEncodingException;
0021: import java.util.Enumeration;
0022: import java.util.HashMap;
0023: import java.util.LinkedList;
0024: import java.util.List;
0025: import java.util.Map;
0026: import java.util.Properties;
0027: import java.util.Vector;
0028:
0029: import javax.naming.Context;
0030: import javax.naming.NamingEnumeration;
0031: import javax.naming.NamingException;
0032: import javax.naming.directory.Attribute;
0033: import javax.naming.directory.BasicAttribute;
0034: import javax.naming.directory.DirContext;
0035: import javax.naming.directory.InitialDirContext;
0036: import javax.naming.directory.ModificationItem;
0037: import javax.naming.directory.SearchControls;
0038: import javax.naming.directory.SearchResult;
0039:
0040: import org.apache.avalon.framework.logger.Logger;
0041: import org.apache.avalon.framework.parameters.Parameters;
0042: import org.apache.cocoon.ProcessingException;
0043: import org.apache.cocoon.environment.SourceResolver;
0044: import org.apache.cocoon.xml.XMLConsumer;
0045: import org.apache.commons.collections.MapUtils;
0046: import org.apache.commons.lang.BooleanUtils;
0047: import org.apache.commons.lang.ObjectUtils;
0048: import org.apache.commons.lang.StringUtils;
0049: import org.xml.sax.Attributes;
0050: import org.xml.sax.Locator;
0051: import org.xml.sax.SAXException;
0052: import org.xml.sax.ext.LexicalHandler;
0053: import org.xml.sax.helpers.AttributesImpl;
0054:
0055: /**
0056: * The <code>LDAPTransformer</code> can be plugged into a pipeline to transform
0057: * the SAX events into queries and responses to/from a LDAP interface.
0058: * <br>
0059: * The file will be specified in a parameter tag in the sitemap pipeline to the
0060: * transformer as follows:
0061: * <p>
0062: * <code>
0063: * <map:transform type="ldap"/><br>
0064: * </code>
0065: * </p>
0066: * <br>
0067: *
0068: * The following DTD is valid:<br>
0069: * <!ELEMENT execute-query (attribute+ | show-attribute? | scope? | initializer? | initial-context? | authentication? | error-element? | sax-error? doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)><br>
0070: * <!ELEMENT execute-increment (attribute | show-attribute? | scope? | initializer? | initial-context? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)><br>
0071: * increments (+1) an integer attribute on a directory-server (ldap)<br>
0072: * <!ELEMENT execute-replace (attribute | show-attribute? | scope? | initializer? | initial-context? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)><br>
0073: * replace attribute on a directory-server (ldap)<br>
0074: * <!ELEMENT execute-add (attribute | show-attribute? | scope? | initializer? | initial-context? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)><br>
0075: * add attribute on a directory-server (ldap)<br>
0076: * <br>
0077: * <!ELEMENT initializer (#PCDATA)>+ (default: "com.sun.jndi.ldap.LdapCtxFactory")<br>
0078: * <!ELEMENT initial-context (#EMPTY)><br>
0079: * <!ATTLIST initial-context name CDATA #REQUIRED
0080: value CDATA #REQUIRED ><br>
0081: * <!ELEMENT authentication (#PCDATA)>+ (default: "simple")<br>
0082: * <!ELEMENT version (#PCDATA)>+ (default: "2")<br>
0083: * <!ELEMENT serverurl (#PCDATA)>+<br>
0084: * <!ELEMENT port (#PCDATA)>+ (default: 389)<br>
0085: * <!ELEMENT rootdn (#PCDATA)>+<br>
0086: * <!ELEMENT password (#PCDATA)>+<br>
0087: * <!ELEMENT scope (ONELEVEL_SCOPE | SUBTREE_SCOPE | OBJECT_SCOPE)>+ (default: ONELEVEL_SCOPE)<br>
0088: * <!ELEMENT searchbase (#PCDATA)>+<br>
0089: * <!ELEMENT doc-element (#PCDATA)>+ (default: "doc-element")<br>
0090: * <!ELEMENT row-element (#PCDATA)>+ (default: "row-element")<br>
0091: * <!ELEMENT dn-attribute (#PCDATA)>+ (default: "" meaning no DN attribute)<br>
0092: * <!ELEMENT error-element (#PCDATA)>+ (default: "ldap-error") (in case of error returned error tag)<br>
0093: * <!ELEMENT sax_error (TRUE | FALSE)>+; (default: FALSE) (throws SAX-Exception instead of error tag)<br>
0094: * <!ELEMENT attribute (#PCDATA)><br>
0095: * <!ATTLIST attribute name CDATA #IMPLIED
0096: mode (append|replace) 'replace' #IMPLIED > (in case execute-replace or execute-add elements using) <br>
0097:
0098: * <!ELEMENT show-attribute (TRUE | FALSE)> (default: TRUE)<br>
0099: * <!ELEMENT filter (#PCDATA | execute-query)+><br>
0100: * <!ELEMENT deref-link (TRUE | FALSE)> (default: FALSE)<br>
0101: * <!ELEMENT count-limit (#PCDATA)> (integer default: 0 -> no limit)<br>
0102: * <!ELEMENT time-limit (#PCDATA)> (integer default: 0 -> infinite)<br>
0103: * <!ELEMENT debug (TRUE | FALSE)>+; (default: FALSE)<br>
0104: * <br>
0105: * + can also be defined as parameter in the sitemap.
0106: * <br>
0107: *
0108: * @version $Id: LDAPTransformer.java 433543 2006-08-22 06:22:54Z crossley $
0109: */
0110: public class LDAPTransformer extends AbstractTransformer {
0111:
0112: /** The LDAP namespace ("http://apache.org/cocoon/LDAP/1.0")*/
0113: public static final String my_uri = "http://apache.org/cocoon/LDAP/1.0";
0114: public static final String my_name = "LDAPTransformer";
0115:
0116: /** The LDAP namespace element names */
0117: public static final String MAGIC_ATTRIBUTE_ELEMENT = "attribute";
0118: public static final String MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE = "name";
0119: public static final String MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE = "mode";
0120: public static final String MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT = "replace";
0121: public static final String MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A = "append";
0122: public static final String MAGIC_AUTHENTICATION_ELEMENT = "authentication";
0123: public static final String MAGIC_COUNT_LIMIT_ELEMENT = "count-limit";
0124: public static final String MAGIC_DEBUG_ELEMENT = "debug";
0125: public static final String MAGIC_DEREF_LINK_ELEMENT = "deref-link";
0126: public static final String MAGIC_DOC_ELEMENT = "doc-element";
0127: public static final String MAGIC_ENCODING_ELEMENT = "encoding";
0128: public static final String MAGIC_ERROR_ELEMENT = "error-element";
0129: public static final String MAGIC_EXECUTE_ADD = "execute-add";
0130: public static final String MAGIC_EXECUTE_INCREMENT = "execute-increment";
0131: public static final String MAGIC_EXECUTE_QUERY = "execute-query";
0132: public static final String MAGIC_EXECUTE_REPLACE = "execute-replace";
0133: public static final String MAGIC_FILTER_ELEMENT = "filter";
0134: public static final String MAGIC_INITIALIZER_ELEMENT = "initializer";
0135: public static final String MAGIC_INITIAL_CONTEXT_ELEMENT = "initial-context";
0136: public static final String MAGIC_INITIAL_CONTEXT_NAME_ATTRIBUTE = "name";
0137: public static final String MAGIC_INITIAL_CONTEXT_VALUE_ATTRIBUTE = "value";
0138: public static final String MAGIC_PASSWORD_ELEMENT = "password";
0139: public static final String MAGIC_PORT_ELEMENT = "port";
0140: public static final String MAGIC_ROOT_DN_ELEMENT = "rootdn";
0141: public static final String MAGIC_ROW_ELEMENT = "row-element";
0142: public static final String MAGIC_DN_ATTRIBUTE = "dn-attribute";
0143: public static final String MAGIC_SAX_ERROR = "sax-error";
0144: public static final String MAGIC_SCOPE_ELEMENT = "scope";
0145: public static final String MAGIC_SEARCHBASE_ELEMENT = "searchbase";
0146: public static final String MAGIC_SERVERURL_ELEMENT = "serverurl";
0147: public static final String MAGIC_SHOW_ATTRIBUTE_ELEMENT = "show-attribute";
0148: public static final String MAGIC_TIME_LIMIT_ELEMENT = "time-limit";
0149: public static final String MAGIC_VERSION_ELEMENT = "version";
0150:
0151: /** The states we are allowed to be in */
0152: public static final int STATE_OUTSIDE = 0;
0153: public static final int STATE_INSIDE_EXECUTE_QUERY = 1;
0154: public static final int STATE_INSIDE_EXECUTE_INCREMENT = 2;
0155: public static final int STATE_INSIDE_EXECUTE_ELEMENT = 3;
0156: public static final int STATE_INSIDE_INITIALIZER_ELEMENT = 4;
0157: public static final int STATE_INSIDE_SERVERURL_ELEMENT = 5;
0158: public static final int STATE_INSIDE_PORT_ELEMENT = 6;
0159: public static final int STATE_INSIDE_SCOPE_ELEMENT = 7;
0160: public static final int STATE_INSIDE_VERSION_ELEMENT = 8;
0161: public static final int STATE_INSIDE_AUTHENTICATION_ELEMENT = 9;
0162: public static final int STATE_INSIDE_ROOT_DN_ELEMENT = 10;
0163: public static final int STATE_INSIDE_PASSWORD_ELEMENT = 11;
0164: public static final int STATE_INSIDE_SEARCHBASE_ELEMENT = 12;
0165: public static final int STATE_INSIDE_DOC_ELEMENT = 13;
0166: public static final int STATE_INSIDE_ROW_ELEMENT = 14;
0167: public static final int STATE_INSIDE_ATTRIBUTE_ELEMENT = 15;
0168: public static final int STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT = 16;
0169: public static final int STATE_INSIDE_ERROR_ELEMENT = 17;
0170: public static final int STATE_INSIDE_FILTER_ELEMENT = 18;
0171: public static final int STATE_INSIDE_DEREF_LINK_ELEMENT = 19;
0172: public static final int STATE_INSIDE_COUNT_LIMIT_ELEMENT = 20;
0173: public static final int STATE_INSIDE_TIME_LIMIT_ELEMENT = 21;
0174: public static final int STATE_INSIDE_DEBUG_ELEMENT = 22;
0175: public static final int STATE_INSIDE_SAX_ERROR_ELEMENT = 23;
0176: public static final int STATE_INSIDE_EXECUTE_REPLACE = 24;
0177: public static final int STATE_INSIDE_EXECUTE_ADD = 25;
0178: public static final int STATE_INSIDE_DN_ATTRIBUTE = 26;
0179: public static final int STATE_INSIDE_INITIAL_CONTEXT_ELEMENT = 27;
0180:
0181: /** Default parameters that might apply to all queries */
0182: protected HashMap default_properties = new HashMap();
0183:
0184: /** The name of the value element we're currently receiving */
0185: protected String current_name;
0186:
0187: /** The current state of the event receiving FSM */
0188: protected int current_state = STATE_OUTSIDE;
0189:
0190: /** The value of the value element we're currently receiving */
0191: protected StringBuffer current_value = new StringBuffer();
0192:
0193: /** The list of queries that we're currently working on */
0194: protected Vector queries = new Vector();
0195:
0196: /** The offset of the current query in the queries list */
0197: protected int current_query_index = -1;
0198:
0199: /** SAX producing state information */
0200: protected XMLConsumer xml_consumer;
0201: protected LexicalHandler lexical_handler;
0202:
0203: /** SitemapComponent methods */
0204:
0205: public void setup(SourceResolver resolver, Map objectModel,
0206: String source, Parameters parameters)
0207: throws ProcessingException, SAXException, IOException {
0208: current_state = STATE_OUTSIDE;
0209:
0210: setDefaultProperty(parameters, MAGIC_INITIALIZER_ELEMENT); // Check the initializer
0211: setDefaultProperty(parameters, MAGIC_VERSION_ELEMENT); // Check the version
0212: setDefaultProperty(parameters, MAGIC_AUTHENTICATION_ELEMENT); // Check the authentication
0213: setDefaultProperty(parameters, MAGIC_SCOPE_ELEMENT); // Check the scope
0214: setDefaultProperty(parameters, MAGIC_SERVERURL_ELEMENT); // Check the serverurl
0215: setDefaultProperty(parameters, MAGIC_ROOT_DN_ELEMENT); // Check the ldap-root_dn
0216: setDefaultProperty(parameters, MAGIC_PASSWORD_ELEMENT); // Check the ldap-pwd
0217: setDefaultProperty(parameters, MAGIC_PORT_ELEMENT); // Check the port
0218: setDefaultProperty(parameters, MAGIC_SEARCHBASE_ELEMENT); // Check the searchbase
0219: setDefaultProperty(parameters, MAGIC_DOC_ELEMENT); // Check the doc-element
0220: setDefaultProperty(parameters, MAGIC_ROW_ELEMENT); // Check the row-element
0221: setDefaultProperty(parameters, MAGIC_DN_ATTRIBUTE); // Check the dn-attribute
0222: setDefaultProperty(parameters, MAGIC_ERROR_ELEMENT); // Check the error-element
0223: setDefaultProperty(parameters, MAGIC_SAX_ERROR); // Check the sax-error
0224: setDefaultProperty(parameters, MAGIC_DEREF_LINK_ELEMENT); // Check the deref-link-element
0225: setDefaultProperty(parameters, MAGIC_COUNT_LIMIT_ELEMENT); // Check the count-limit-element
0226: setDefaultProperty(parameters, MAGIC_TIME_LIMIT_ELEMENT); // Check the time-limit-element
0227: setDefaultProperty(parameters, MAGIC_DEBUG_ELEMENT); // Check the debug-element
0228: setDefaultProperty(parameters, MAGIC_ENCODING_ELEMENT); // Check the encoding
0229: setDefaultProperty(parameters, MAGIC_FILTER_ELEMENT); // Check the filter-element
0230: }
0231:
0232: /** My very own methods */
0233:
0234: /* Helper to set default properties */
0235: protected void setDefaultProperty(Parameters parameters,
0236: String propertyName) {
0237: String parameter = parameters.getParameter(propertyName, null);
0238: if (parameter != null) {
0239: default_properties.put(propertyName, parameter);
0240: }
0241: }
0242:
0243: /* Helper class to store a new Query in the stack */
0244: protected void storeQuery(int nextState) {
0245: current_state = nextState;
0246: current_query_index = queries.size();
0247: LDAPQuery query = new LDAPQuery(this );
0248: queries.addElement(query);
0249: getCurrentQuery().toDo = nextState;
0250: getCurrentQuery().query_index = current_query_index;
0251: }
0252:
0253: /**
0254: * This will be the meat of LDAPTransformer, where the query is run.
0255: */
0256: protected void executeQuery(int index) throws SAXException {
0257: this .contentHandler.startPrefixMapping("",
0258: LDAPTransformer.my_uri);
0259: LDAPQuery query = (LDAPQuery) queries.elementAt(index);
0260: try {
0261: query.execute();
0262: } catch (NamingException e) {
0263: getLogger().error(e.toString());
0264: throw new SAXException(e);
0265: } catch (Exception e) {
0266: getLogger().error(e.toString());
0267: throw new SAXException(e);
0268: }
0269: this .contentHandler.endPrefixMapping("");
0270: }
0271:
0272: protected static void throwIllegalStateException(String message) {
0273: throw new IllegalStateException(my_name + ": " + message);
0274: }
0275:
0276: protected void startExecuteElement() {
0277: switch (current_state) {
0278: case LDAPTransformer.STATE_OUTSIDE:
0279: case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT:
0280: storeQuery(LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY);
0281: break;
0282: default:
0283: throwIllegalStateException("Not expecting a start execute-query element");
0284: }
0285: }
0286:
0287: protected void startExecuteElement(int state, String name) {
0288: switch (current_state) {
0289: case LDAPTransformer.STATE_OUTSIDE:
0290: case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY:
0291: storeQuery(state);
0292: break;
0293: default:
0294: throwIllegalStateException("Not expecting a start " + name
0295: + " element");
0296: }
0297: }
0298:
0299: protected void endExecuteElement(int state, String name)
0300: throws SAXException {
0301: if (current_state == state) {
0302: executeQuery(current_query_index);
0303: queries.remove(current_query_index);
0304: --current_query_index;
0305: if (current_query_index > -1) {
0306: current_state = getCurrentQuery().toDo;
0307: } else {
0308: queries.removeAllElements();
0309: current_state = LDAPTransformer.STATE_OUTSIDE;
0310: }
0311: } else {
0312: throwIllegalStateException("Not expecting a end " + name
0313: + " element");
0314: }
0315: }
0316:
0317: protected void startQueryParameterElement(int state, String name) {
0318: switch (current_state) {
0319: case STATE_INSIDE_EXECUTE_QUERY:
0320: case STATE_INSIDE_EXECUTE_INCREMENT:
0321: case STATE_INSIDE_EXECUTE_REPLACE:
0322: case STATE_INSIDE_EXECUTE_ADD:
0323: current_value.setLength(0);
0324: current_state = state;
0325: getCurrentQuery().current_state = state;
0326: break;
0327: default:
0328: throwIllegalStateException("Not expecting a start " + name
0329: + " element");
0330: }
0331: }
0332:
0333: protected void startParameterElement(int state, String name) {
0334: switch (current_state) {
0335: case STATE_INSIDE_EXECUTE_QUERY:
0336: case STATE_INSIDE_EXECUTE_INCREMENT:
0337: case STATE_INSIDE_EXECUTE_REPLACE:
0338: case STATE_INSIDE_EXECUTE_ADD:
0339: current_value.setLength(0);
0340: current_state = state;
0341: break;
0342: default:
0343: throwIllegalStateException("Not expecting a start " + name
0344: + " serverurl element");
0345: }
0346: }
0347:
0348: protected void endInitializerElement() {
0349: switch (current_state) {
0350: case LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT:
0351: getCurrentQuery().initializer = current_value.toString();
0352: current_state = getCurrentQuery().toDo;
0353: break;
0354: default:
0355: throwIllegalStateException("Not expecting a end initializer element");
0356: }
0357: }
0358:
0359: protected void endScopeElement() {
0360: switch (current_state) {
0361: case LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT:
0362: getCurrentQuery().scope = current_value.toString();
0363: current_state = getCurrentQuery().toDo;
0364: break;
0365: default:
0366: throwIllegalStateException("Not expecting a end scope element");
0367: }
0368: }
0369:
0370: protected void endAuthenticationElement() {
0371: switch (current_state) {
0372: case LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT:
0373: getCurrentQuery().authentication = current_value.toString();
0374: current_state = getCurrentQuery().toDo;
0375: break;
0376: default:
0377: throwIllegalStateException("Not expecting a end authentication element");
0378: }
0379: }
0380:
0381: protected void endServerurlElement() {
0382: switch (current_state) {
0383: case LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT:
0384: getCurrentQuery().serverurl = current_value.toString();
0385: current_state = getCurrentQuery().toDo;
0386: break;
0387: default:
0388: throwIllegalStateException("Not expecting a end serverurl element");
0389: }
0390: }
0391:
0392: protected void endPortElement() {
0393: switch (current_state) {
0394: case LDAPTransformer.STATE_INSIDE_PORT_ELEMENT:
0395: getCurrentQuery().port = Integer.parseInt(current_value
0396: .toString());
0397: current_state = getCurrentQuery().toDo;
0398: break;
0399: default:
0400: throwIllegalStateException("Not expecting a end server element");
0401: }
0402: }
0403:
0404: protected void startShowAttributeElement(Attributes attributes) {
0405: switch (current_state) {
0406: case STATE_INSIDE_EXECUTE_QUERY:
0407: case STATE_INSIDE_EXECUTE_INCREMENT:
0408: //case STATE_INSIDE_EXECUTE_REPLACE:
0409: current_value.setLength(0);
0410: current_state = LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT;
0411: break;
0412: default:
0413: throwIllegalStateException("Not expecting a start show-attribute element");
0414: }
0415: }
0416:
0417: protected void endShowAttributeElement() {
0418: switch (current_state) {
0419: case LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT:
0420: if (!BooleanUtils.toBoolean(current_value.toString())) {
0421: getCurrentQuery().showAttribute = false;
0422: }
0423: current_state = getCurrentQuery().toDo;
0424: break;
0425: default:
0426: throwIllegalStateException("Not expecting a end show-attribute element");
0427: }
0428: }
0429:
0430: protected void endSearchbaseElement() {
0431: switch (current_state) {
0432: case LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT:
0433: getCurrentQuery().searchbase = current_value.toString();
0434: current_state = getCurrentQuery().toDo;
0435: break;
0436: default:
0437: throwIllegalStateException("Not expecting a end searchbase element");
0438: }
0439: }
0440:
0441: protected void endDocElement() {
0442: switch (current_state) {
0443: case LDAPTransformer.STATE_INSIDE_DOC_ELEMENT:
0444: getCurrentQuery().doc_element = current_value.toString();
0445: current_state = getCurrentQuery().toDo;
0446: break;
0447: default:
0448: throwIllegalStateException("Not expecting a end doc-element element");
0449: }
0450: }
0451:
0452: protected void endRowElement() {
0453: switch (current_state) {
0454: case LDAPTransformer.STATE_INSIDE_ROW_ELEMENT:
0455: getCurrentQuery().row_element = current_value.toString();
0456: current_state = getCurrentQuery().toDo;
0457: break;
0458: default:
0459: throwIllegalStateException("Not expecting a end row-element element");
0460: }
0461: }
0462:
0463: protected void endDnAttribute() {
0464: switch (current_state) {
0465: case LDAPTransformer.STATE_INSIDE_DN_ATTRIBUTE:
0466: getCurrentQuery().dn_attribute = current_value.toString();
0467: current_state = getCurrentQuery().toDo;
0468: break;
0469: default:
0470: throwIllegalStateException("Not expecting a end dn-attribute element");
0471: }
0472: }
0473:
0474: protected void endErrorElement() {
0475: switch (current_state) {
0476: case LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT:
0477: getCurrentQuery().error_element = current_value.toString();
0478: current_state = getCurrentQuery().toDo;
0479: break;
0480: default:
0481: throwIllegalStateException("Not expecting a end error-element element");
0482: }
0483: }
0484:
0485: protected void endSaxError() {
0486: switch (current_state) {
0487: case LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT:
0488: if (current_value.toString().toUpperCase().equals("TRUE")) {
0489: getCurrentQuery().sax_error = true;
0490: }
0491: current_state = getCurrentQuery().toDo;
0492: break;
0493: default:
0494: throwIllegalStateException("Not expecting a end sax-error element");
0495: }
0496: }
0497:
0498: protected void endRootDnElement() {
0499: switch (current_state) {
0500: case LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT:
0501: getCurrentQuery().root_dn = current_value.toString();
0502: current_state = getCurrentQuery().toDo;
0503: break;
0504: default:
0505: throwIllegalStateException("Not expecting a end root-dn element");
0506: }
0507: }
0508:
0509: protected void endPasswordElement() {
0510: switch (current_state) {
0511: case LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT:
0512: getCurrentQuery().password = current_value.toString();
0513: current_state = getCurrentQuery().toDo;
0514: break;
0515: default:
0516: throwIllegalStateException("Not expecting a end password element");
0517: }
0518: }
0519:
0520: protected void startAttributeElement(Attributes attributes) {
0521: switch (current_state) {
0522: case STATE_INSIDE_EXECUTE_QUERY:
0523: case STATE_INSIDE_EXECUTE_INCREMENT:
0524: current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
0525: current_value.setLength(0);
0526: break;
0527: case STATE_INSIDE_EXECUTE_REPLACE:
0528: boolean is_name_present = false;
0529: String mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT;
0530: if (attributes != null && attributes.getLength() > 0) {
0531: AttributesImpl new_attributes = new AttributesImpl(
0532: attributes);
0533: for (int i = 0; i < new_attributes.getLength(); i++) {
0534: String attr_name = new_attributes.getLocalName(i);
0535: if (attr_name
0536: .equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
0537: String value = new_attributes.getValue(i);
0538: getCurrentQuery().addAttrList(value);
0539: is_name_present = true;
0540: } else if (attr_name
0541: .equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE)) {
0542: if (new_attributes
0543: .getValue(i)
0544: .equals(
0545: MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A))
0546: mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A;
0547: } else {
0548: this .getLogger()
0549: .debug(
0550: "Invalid attribute match: "
0551: + attr_name);
0552: throwIllegalStateException("Invalid attribute match in start attribute element");
0553: }
0554: }
0555: }
0556: if (!is_name_present) {
0557: this .getLogger()
0558: .debug("Do not match 'value' attribute");
0559: throwIllegalStateException("Do not match 'value' attribute in start attribute element");
0560: }
0561: getCurrentQuery().addAttrModeVal(mode);
0562: current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
0563: current_value.setLength(0);
0564: break;
0565: case STATE_INSIDE_EXECUTE_ADD:
0566: if (attributes != null && attributes.getLength() > 0) {
0567: AttributesImpl new_attributes = new AttributesImpl(
0568: attributes);
0569: for (int i = 0; i < new_attributes.getLength(); i++) {
0570: String attr_name = new_attributes.getLocalName(i);
0571: if (attr_name
0572: .equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
0573: String value = new_attributes.getValue(i);
0574: getCurrentQuery().addAttrList(value);
0575: } else {
0576: this .getLogger()
0577: .debug(
0578: "Invalid attribute match: "
0579: + attr_name);
0580: throwIllegalStateException("Invalid attribute match in start attribute element");
0581: }
0582: }
0583: } else {
0584: this .getLogger()
0585: .debug("Do not match 'value' attribute");
0586: throwIllegalStateException("Do not match 'value' attribute in start attribute element");
0587: }
0588: current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
0589: current_value.setLength(0);
0590: break;
0591: default:
0592: throwIllegalStateException("Not expecting a start attribute element");
0593: }
0594: }
0595:
0596: protected void endAttributeElement() {
0597: switch (current_state) {
0598: case LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT:
0599: if ((getCurrentQuery().toDo == STATE_INSIDE_EXECUTE_REPLACE)
0600: || (getCurrentQuery().toDo == STATE_INSIDE_EXECUTE_ADD)) {
0601: getCurrentQuery().addAttrVal(current_value.toString());
0602: current_state = getCurrentQuery().toDo;
0603: break;
0604: }
0605: getCurrentQuery().addAttrList(current_value.toString());
0606: current_state = getCurrentQuery().toDo;
0607: break;
0608: default:
0609: throwIllegalStateException("Not expecting a end attribute element");
0610: }
0611: }
0612:
0613: protected void startInitialContextElement(Attributes attributes) {
0614: switch (current_state) {
0615: case STATE_INSIDE_EXECUTE_INCREMENT:
0616: current_state = LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT;
0617: current_value.setLength(0);
0618: break;
0619: case STATE_INSIDE_EXECUTE_QUERY:
0620: case STATE_INSIDE_EXECUTE_ADD:
0621: case STATE_INSIDE_EXECUTE_REPLACE:
0622: String name = null,
0623: value = null;
0624:
0625: if (attributes != null && attributes.getLength() > 0) {
0626: name = attributes.getValue("name");
0627: value = attributes.getValue("value");
0628:
0629: if (name != null && value != null)
0630: getCurrentQuery().addInitialContextValue(name,
0631: value);
0632: }
0633: if (name == null) {
0634: this .getLogger().debug(
0635: "Could not find 'name' attribute");
0636: throwIllegalStateException("Could not find 'name' attribute in initial-context element");
0637: }
0638: if (value == null) {
0639: this .getLogger().debug(
0640: "Could not find 'value' attribute");
0641: throwIllegalStateException("Could not find 'value' attribute in initial-context element");
0642: }
0643:
0644: current_state = LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT;
0645: current_value.setLength(0);
0646: break;
0647: default:
0648: throwIllegalStateException("Not expecting a start initial-context element");
0649: }
0650: }
0651:
0652: protected void endInitialContextElement() {
0653: switch (current_state) {
0654: case LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT:
0655: current_state = getCurrentQuery().toDo;
0656: break;
0657: default:
0658: throwIllegalStateException("Not expecting a end initial-context element");
0659: }
0660: }
0661:
0662: protected void endVersionElement() {
0663: switch (current_state) {
0664: case LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT:
0665: getCurrentQuery().version = current_value.toString();
0666: current_state = getCurrentQuery().toDo;
0667: break;
0668: default:
0669: throwIllegalStateException("Not expecting a end version element");
0670: }
0671: }
0672:
0673: protected void startFilterElement(Attributes attributes) {
0674: switch (current_state) {
0675: case STATE_INSIDE_EXECUTE_QUERY:
0676: case STATE_INSIDE_EXECUTE_INCREMENT:
0677: case STATE_INSIDE_EXECUTE_REPLACE:
0678: case STATE_INSIDE_EXECUTE_ADD:
0679: current_state = LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT;
0680: getCurrentQuery().current_state = LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT;
0681: current_value.setLength(0);
0682: break;
0683: default:
0684: throwIllegalStateException("Not expecting a start filter element");
0685: }
0686: }
0687:
0688: protected void endFilterElement() {
0689: switch (current_state) {
0690: case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT:
0691: getCurrentQuery().filter = current_value.toString();
0692: current_state = getCurrentQuery().toDo;
0693: break;
0694: default:
0695: throwIllegalStateException("Not expecting a end filter element");
0696: }
0697: }
0698:
0699: protected void endDerefLinkElement() {
0700: switch (current_state) {
0701: case LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT:
0702: getCurrentQuery().deref_link = BooleanUtils
0703: .toBoolean(current_value.toString());
0704: current_state = getCurrentQuery().toDo;
0705: break;
0706: default:
0707: throwIllegalStateException("Not expecting a end deref-link element");
0708: }
0709: }
0710:
0711: protected void endCountLimitElement() {
0712: switch (current_state) {
0713: case LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT:
0714: getCurrentQuery().count_limit = Integer
0715: .parseInt(current_value.toString());
0716: current_state = getCurrentQuery().toDo;
0717: break;
0718: default:
0719: throwIllegalStateException("Not expecting a end count-limit element");
0720: }
0721: }
0722:
0723: protected void endTimeLimitElement() {
0724: switch (current_state) {
0725: case LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT:
0726: getCurrentQuery().time_limit = Integer
0727: .parseInt(current_value.toString());
0728: current_state = getCurrentQuery().toDo;
0729: break;
0730: default:
0731: throwIllegalStateException("Not expecting a end time-limit element");
0732: }
0733: }
0734:
0735: protected void endDebugElement() {
0736: switch (current_state) {
0737: case LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT:
0738: getCurrentQuery().debug = BooleanUtils
0739: .toBoolean(current_value.toString());
0740: current_state = getCurrentQuery().toDo;
0741: break;
0742: default:
0743: throwIllegalStateException("Not expecting a end debug element");
0744: }
0745: }
0746:
0747: protected LDAPQuery getCurrentQuery() {
0748: return (LDAPQuery) queries.elementAt(current_query_index);
0749: }
0750:
0751: protected LDAPQuery getQuery(int i) {
0752: return (LDAPQuery) queries.elementAt(i);
0753: }
0754:
0755: /** END my very own methods */
0756:
0757: /** BEGIN SAX ContentHandler handlers */
0758:
0759: public void setDocumentLocator(Locator locator) {
0760: if (getLogger().isDebugEnabled()) {
0761: getLogger().debug("PUBLIC ID: " + locator.getPublicId());
0762: getLogger().debug("SYSTEM ID: " + locator.getSystemId());
0763: }
0764: if (super .contentHandler != null)
0765: super .contentHandler.setDocumentLocator(locator);
0766: }
0767:
0768: public void startElement(String uri, String name, String raw,
0769: Attributes attributes) throws SAXException {
0770: if (uri == null || !uri.equals(my_uri)) {
0771: super .startElement(uri, name, raw, attributes);
0772: return;
0773: }
0774: getLogger().debug(
0775: "RECEIVED START ELEMENT " + name + "(" + uri + ")");
0776:
0777: if (name.equals(LDAPTransformer.MAGIC_EXECUTE_QUERY)) {
0778: startExecuteElement();
0779: } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_INCREMENT)) {
0780: startExecuteElement(
0781: LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT,
0782: name);
0783: } else if (name
0784: .equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
0785: startQueryParameterElement(
0786: LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT,
0787: name);
0788: } else if (name
0789: .equals(LDAPTransformer.MAGIC_INITIAL_CONTEXT_ELEMENT)) {
0790: startInitialContextElement(attributes);
0791: } else if (name
0792: .equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
0793: startQueryParameterElement(
0794: LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT,
0795: name);
0796: } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
0797: startQueryParameterElement(
0798: LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT, name);
0799: } else if (name.equals(LDAPTransformer.MAGIC_VERSION_ELEMENT)) {
0800: startParameterElement(
0801: LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT, name);
0802: } else if (name.equals(LDAPTransformer.MAGIC_SERVERURL_ELEMENT)) {
0803: startParameterElement(
0804: LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT,
0805: name);
0806: } else if (name.equals(LDAPTransformer.MAGIC_PORT_ELEMENT)) {
0807: startParameterElement(
0808: LDAPTransformer.STATE_INSIDE_PORT_ELEMENT, name);
0809: } else if (name.equals(LDAPTransformer.MAGIC_DOC_ELEMENT)) {
0810: startParameterElement(
0811: LDAPTransformer.STATE_INSIDE_DOC_ELEMENT, name);
0812: } else if (name.equals(LDAPTransformer.MAGIC_ROW_ELEMENT)) {
0813: startParameterElement(
0814: LDAPTransformer.STATE_INSIDE_ROW_ELEMENT, name);
0815: } else if (name.equals(LDAPTransformer.MAGIC_DN_ATTRIBUTE)) {
0816: startParameterElement(
0817: LDAPTransformer.STATE_INSIDE_DN_ATTRIBUTE, name);
0818: } else if (name.equals(LDAPTransformer.MAGIC_ERROR_ELEMENT)) {
0819: startParameterElement(
0820: LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT, name);
0821: } else if (name.equals(LDAPTransformer.MAGIC_SAX_ERROR)) {
0822: startParameterElement(
0823: LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT,
0824: name);
0825: } else if (name.equals(LDAPTransformer.MAGIC_ROOT_DN_ELEMENT)) {
0826: startParameterElement(
0827: LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT, name);
0828: } else if (name.equals(LDAPTransformer.MAGIC_PASSWORD_ELEMENT)) {
0829: startParameterElement(
0830: LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT, name);
0831: } else if (name.equals(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT)) {
0832: startAttributeElement(attributes);
0833: } else if (name
0834: .equals(LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT)) {
0835: startShowAttributeElement(attributes);
0836: } else if (name
0837: .equals(LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT)) {
0838: startParameterElement(
0839: LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT,
0840: name);
0841: } else if (name.equals(LDAPTransformer.MAGIC_FILTER_ELEMENT)) {
0842: startFilterElement(attributes);
0843: } else if (name
0844: .equals(LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT)) {
0845: startParameterElement(
0846: LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT,
0847: name);
0848: } else if (name
0849: .equals(LDAPTransformer.MAGIC_COUNT_LIMIT_ELEMENT)) {
0850: startParameterElement(
0851: LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT,
0852: name);
0853: } else if (name
0854: .equals(LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT)) {
0855: startParameterElement(
0856: LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT,
0857: name);
0858: } else if (name.equals(LDAPTransformer.MAGIC_DEBUG_ELEMENT)) {
0859: startParameterElement(
0860: LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT, name);
0861: } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_REPLACE)) {
0862: startExecuteElement(
0863: LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE,
0864: LDAPTransformer.MAGIC_EXECUTE_REPLACE);
0865: } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_ADD)) {
0866: startExecuteElement(
0867: LDAPTransformer.STATE_INSIDE_EXECUTE_ADD,
0868: LDAPTransformer.MAGIC_EXECUTE_ADD);
0869: }
0870: }
0871:
0872: public void endElement(String uri, String name, String raw)
0873: throws SAXException {
0874: if (!uri.equals(my_uri)) {
0875: super .endElement(uri, name, raw);
0876: return;
0877: }
0878: if (getLogger().isDebugEnabled()) {
0879: getLogger().debug(
0880: "RECEIVED END ELEMENT " + name + "(" + uri + ")");
0881: }
0882:
0883: if (name.equals(LDAPTransformer.MAGIC_EXECUTE_QUERY)) {
0884: endExecuteElement(
0885: LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY,
0886: LDAPTransformer.MAGIC_EXECUTE_QUERY);
0887: } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_INCREMENT)) {
0888: endExecuteElement(
0889: LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT,
0890: LDAPTransformer.MAGIC_EXECUTE_INCREMENT);
0891: } else if (name
0892: .equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
0893: endInitializerElement();
0894: } else if (name
0895: .equals(LDAPTransformer.MAGIC_INITIAL_CONTEXT_ELEMENT)) {
0896: endInitialContextElement();
0897: } else if (name
0898: .equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
0899: endAuthenticationElement();
0900: } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
0901: endScopeElement();
0902: } else if (name.equals(LDAPTransformer.MAGIC_VERSION_ELEMENT)) {
0903: endVersionElement();
0904: } else if (name.equals(LDAPTransformer.MAGIC_SERVERURL_ELEMENT)) {
0905: endServerurlElement();
0906: } else if (name.equals(LDAPTransformer.MAGIC_PORT_ELEMENT)) {
0907: endPortElement();
0908: } else if (name.equals(LDAPTransformer.MAGIC_DOC_ELEMENT)) {
0909: endDocElement();
0910: } else if (name.equals(LDAPTransformer.MAGIC_ROW_ELEMENT)) {
0911: endRowElement();
0912: } else if (name.equals(LDAPTransformer.MAGIC_DN_ATTRIBUTE)) {
0913: endDnAttribute();
0914: } else if (name.equals(LDAPTransformer.MAGIC_ERROR_ELEMENT)) {
0915: endErrorElement();
0916: } else if (name.equals(LDAPTransformer.MAGIC_SAX_ERROR)) {
0917: endSaxError();
0918: } else if (name.equals(LDAPTransformer.MAGIC_ROOT_DN_ELEMENT)) {
0919: endRootDnElement();
0920: } else if (name.equals(LDAPTransformer.MAGIC_PASSWORD_ELEMENT)) {
0921: endPasswordElement();
0922: } else if (name.equals(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT)) {
0923: endAttributeElement();
0924: } else if (name
0925: .equals(LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT)) {
0926: endShowAttributeElement();
0927: } else if (name
0928: .equals(LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT)) {
0929: endSearchbaseElement();
0930: } else if (name.equals(LDAPTransformer.MAGIC_FILTER_ELEMENT)) {
0931: endFilterElement();
0932: } else if (name
0933: .equals(LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT)) {
0934: endDerefLinkElement();
0935: } else if (name
0936: .equals(LDAPTransformer.MAGIC_COUNT_LIMIT_ELEMENT)) {
0937: endCountLimitElement();
0938: } else if (name
0939: .equals(LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT)) {
0940: endTimeLimitElement();
0941: } else if (name.equals(LDAPTransformer.MAGIC_DEBUG_ELEMENT)) {
0942: endDebugElement();
0943: } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_REPLACE)) {
0944: endExecuteElement(
0945: LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE,
0946: LDAPTransformer.MAGIC_EXECUTE_REPLACE);
0947: } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_ADD)) {
0948: endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_ADD,
0949: LDAPTransformer.MAGIC_EXECUTE_ADD);
0950: }
0951: }
0952:
0953: public void characters(char ary[], int start, int length)
0954: throws SAXException {
0955: if (current_state != LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT
0956: && current_state != LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT
0957: && current_state != LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT
0958: && current_state != LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT
0959: && current_state != LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT
0960: && current_state != LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT
0961: && current_state != LDAPTransformer.STATE_INSIDE_PORT_ELEMENT
0962: && current_state != LDAPTransformer.STATE_INSIDE_DOC_ELEMENT
0963: && current_state != LDAPTransformer.STATE_INSIDE_ROW_ELEMENT
0964: && current_state != LDAPTransformer.STATE_INSIDE_DN_ATTRIBUTE
0965: && current_state != LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT
0966: && current_state != LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT
0967: && current_state != LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT
0968: && current_state != LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT
0969: && current_state != LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT
0970: && current_state != LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT
0971: && current_state != LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT
0972: && current_state != LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT
0973: && current_state != LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT
0974: && current_state != LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT
0975: && current_state != LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT
0976: && current_state != LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT) {
0977: super .characters(ary, start, length);
0978: }
0979: if (getLogger().isDebugEnabled()) {
0980: getLogger().debug(
0981: "RECEIVED CHARACTERS: "
0982: + new String(ary, start, length));
0983: }
0984: current_value.append(ary, start, length);
0985: }
0986:
0987: protected void start(String name, AttributesImpl attr)
0988: throws SAXException {
0989: super .contentHandler.startElement("", name, name, attr);
0990: attr.clear();
0991: }
0992:
0993: protected void end(String name) throws SAXException {
0994: super .contentHandler.endElement("", name, name);
0995: }
0996:
0997: protected void data(String data) throws SAXException {
0998: if (data != null) {
0999: super .contentHandler.characters(data.toCharArray(), 0, data
1000: .length());
1001: }
1002: }
1003:
1004: protected static String getStringValue(Object object) {
1005: return ObjectUtils.toString(object);
1006: }
1007:
1008: public final Logger getTheLogger() {
1009: return getLogger();
1010: }
1011:
1012: static class LDAPQuery {
1013:
1014: /** What index are you in daddy's queries list */
1015: protected int query_index;
1016:
1017: /** The current state of the event receiving FSM */
1018: protected int current_state;
1019:
1020: /** Who's your daddy? */
1021: protected LDAPTransformer transformer;
1022:
1023: /** LDAP configuration information */
1024: protected String initializer;
1025: protected String serverurl;
1026: protected int port;
1027: protected String root_dn;
1028: protected String password;
1029: protected String version;
1030: protected String scope;
1031: protected String authentication;
1032: private final static String LDAP_ENCODING = "ISO-8859-1";
1033: protected String encoding;
1034:
1035: /** LDAP environment information */
1036: protected Properties env = new Properties();
1037: protected DirContext ctx;
1038:
1039: /** LDAP Query */
1040: protected int toDo;
1041: protected String searchbase;
1042: protected List attrModeVal = new LinkedList();
1043: protected List attrListe = new LinkedList();
1044: protected List attrVale = new LinkedList();
1045: protected String REPLACE_MODE_DEFAULT = StringUtils
1046: .defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT);
1047: protected String REPLACE_MODE_APPEND = StringUtils
1048: .defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A);
1049: protected Map initialContextValues = new HashMap();
1050: protected boolean showAttribute;
1051: protected String filter;
1052: protected String doc_element;
1053: protected String exec_element = "exec-element";
1054: protected String row_element;
1055: protected String dn_attribute;
1056: protected String error_element;
1057: protected boolean sax_error;
1058: protected boolean deref_link; // Dereference: true -> dereference the link during search
1059: protected long count_limit; // Maximum number of entries to return: 0 -> no limit
1060: protected int time_limit; // Number of milliseconds to wait before return: 0 -> infinite
1061: protected boolean debug;
1062:
1063: protected LDAPQuery(LDAPTransformer transformer) {
1064: this .transformer = transformer;
1065: initializer = MapUtils.getString(
1066: transformer.default_properties,
1067: LDAPTransformer.MAGIC_INITIALIZER_ELEMENT,
1068: "com.sun.jndi.ldap.LdapCtxFactory");
1069: serverurl = MapUtils.getString(
1070: transformer.default_properties,
1071: LDAPTransformer.MAGIC_SERVERURL_ELEMENT,
1072: "localhost");
1073: port = MapUtils.getIntValue(transformer.default_properties,
1074: LDAPTransformer.MAGIC_PORT_ELEMENT, 389);
1075: root_dn = MapUtils.getString(
1076: transformer.default_properties,
1077: LDAPTransformer.MAGIC_ROOT_DN_ELEMENT, "");
1078: password = MapUtils.getString(
1079: transformer.default_properties,
1080: LDAPTransformer.MAGIC_PASSWORD_ELEMENT, "");
1081: version = MapUtils.getString(
1082: transformer.default_properties,
1083: LDAPTransformer.MAGIC_VERSION_ELEMENT, "2");
1084: scope = MapUtils.getString(transformer.default_properties,
1085: LDAPTransformer.MAGIC_SCOPE_ELEMENT,
1086: "ONELEVEL_SCOPE");
1087: authentication = MapUtils.getString(
1088: transformer.default_properties,
1089: LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT,
1090: "simple");
1091: searchbase = MapUtils.getString(
1092: transformer.default_properties,
1093: LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT, "");
1094: showAttribute = MapUtils.getBooleanValue(
1095: transformer.default_properties,
1096: LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT, true);
1097: doc_element = MapUtils.getString(
1098: transformer.default_properties,
1099: LDAPTransformer.MAGIC_DOC_ELEMENT, "doc-element");
1100: row_element = MapUtils.getString(
1101: transformer.default_properties,
1102: LDAPTransformer.MAGIC_ROW_ELEMENT, "row-element");
1103: dn_attribute = MapUtils.getString(
1104: transformer.default_properties,
1105: LDAPTransformer.MAGIC_DN_ATTRIBUTE, "");
1106: error_element = MapUtils.getString(
1107: transformer.default_properties,
1108: LDAPTransformer.MAGIC_ERROR_ELEMENT, "ldap-error");
1109: sax_error = MapUtils.getBooleanValue(
1110: transformer.default_properties,
1111: LDAPTransformer.MAGIC_SAX_ERROR);
1112: deref_link = MapUtils.getBooleanValue(
1113: transformer.default_properties,
1114: LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT);
1115: count_limit = MapUtils.getLongValue(
1116: transformer.default_properties,
1117: LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT);
1118: time_limit = MapUtils.getIntValue(
1119: transformer.default_properties,
1120: LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT);
1121: debug = MapUtils.getBooleanValue(
1122: transformer.default_properties,
1123: LDAPTransformer.MAGIC_DEBUG_ELEMENT);
1124: encoding = MapUtils.getString(
1125: transformer.default_properties,
1126: LDAPTransformer.MAGIC_ENCODING_ELEMENT,
1127: LDAP_ENCODING);
1128: filter = MapUtils.getString(transformer.default_properties,
1129: LDAPTransformer.MAGIC_FILTER_ELEMENT, "");
1130: }
1131:
1132: /** shared constraints initialization */
1133: protected void initConstraints(SearchControls constraints) {
1134: if (scope.equals("OBJECT_SCOPE")) {
1135: constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
1136: } else if (scope.equals("SUBTREE_SCOPE")) {
1137: constraints
1138: .setSearchScope(SearchControls.SUBTREE_SCOPE);
1139: } else {
1140: constraints
1141: .setSearchScope(SearchControls.ONELEVEL_SCOPE);
1142: }
1143: constraints.setTimeLimit(time_limit);
1144: constraints.setDerefLinkFlag(deref_link);
1145: constraints.setCountLimit(count_limit);
1146: }
1147:
1148: protected void execute() throws Exception, NamingException {
1149: String[] attrList = new String[attrListe.size()];
1150:
1151: AttributesImpl attr = new AttributesImpl();
1152: if (debug) {
1153: debugPrint();
1154: }
1155: SearchControls constraints = new SearchControls();
1156: attrListe.toArray(attrList);
1157: attrListe.clear();
1158: try {
1159: connect();
1160: switch (toDo) {
1161: case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY:
1162: try {
1163: initConstraints(constraints);
1164: if (attrList.length > 0) {
1165: constraints
1166: .setReturningAttributes(attrList);
1167: }
1168:
1169: if (!filter.equals("")) {
1170: //filter is present
1171: if (!doc_element.equals("")) {
1172: transformer.start(doc_element, attr);
1173: }
1174: NamingEnumeration ldapresults = ctx.search(
1175: searchbase, filter, constraints);
1176:
1177: while (ldapresults != null
1178: && ldapresults.hasMore()) {
1179: SearchResult si = (SearchResult) ldapresults
1180: .next();
1181: if (!row_element.equals("")) {
1182: if (!"".equals(dn_attribute)) {
1183: String dn;
1184: if (!"".equals(searchbase))
1185: dn = si.getName() + ','
1186: + searchbase;
1187: else
1188: dn = si.getName();
1189: attr.addAttribute(null,
1190: dn_attribute,
1191: dn_attribute, "CDATA",
1192: dn);
1193: }
1194: transformer
1195: .start(row_element, attr);
1196: }
1197: javax.naming.directory.Attributes attrs = si
1198: .getAttributes();
1199: if (attrs != null) {
1200: NamingEnumeration ae = attrs
1201: .getAll();
1202: while (ae.hasMoreElements()) {
1203: Attribute at = (Attribute) ae
1204: .next();
1205: Enumeration vals = at.getAll();
1206: String attrID = at.getID();
1207: while (vals.hasMoreElements()) {
1208: if (showAttribute) {
1209: transformer.start(
1210: attrID, attr);
1211: }
1212: String attrVal = recodeFromLDAPEncoding((String) vals
1213: .nextElement());
1214: if (query_index > 0) {
1215: switch (transformer
1216: .getQuery(query_index - 1).current_state) {
1217: case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT:
1218: StringBuffer temp = new StringBuffer(
1219: transformer
1220: .getQuery(query_index - 1).filter);
1221: if (temp.length() > 0) {
1222: temp
1223: .append(", ");
1224: }
1225: temp
1226: .append(
1227: attrID)
1228: .append("=")
1229: .append(
1230: attrVal);
1231: transformer
1232: .getQuery(query_index - 1).filter = temp
1233: .toString();
1234: break;
1235: default:
1236: transformer.start(
1237: attrID,
1238: attr);
1239: }
1240: } else {
1241: transformer
1242: .data(String
1243: .valueOf(attrVal));
1244: }
1245: if (showAttribute) {
1246: transformer.end(attrID);
1247: }
1248: }
1249: }
1250: }
1251: if (!row_element.equals("")) {
1252: transformer.end(row_element);
1253: }
1254: }
1255: if (!doc_element.equals("")) {
1256: transformer.end(doc_element);
1257: }
1258: } else {
1259: //filter not present, get the values from absolete path
1260: javax.naming.directory.Attributes attrs = ctx
1261: .getAttributes(searchbase, attrList);
1262: if (!doc_element.equals("")) {
1263: transformer.start(doc_element, attr);
1264: }
1265: if (!row_element.equals("")) {
1266: if (!"".equals(dn_attribute)) {
1267: attr.addAttribute(null,
1268: dn_attribute, dn_attribute,
1269: "CDATA", searchbase);
1270: }
1271: transformer.start(row_element, attr);
1272: }
1273: if (attrs != null) {
1274: NamingEnumeration ae = attrs.getAll();
1275: while (ae.hasMoreElements()) {
1276: Attribute at = (Attribute) ae
1277: .next();
1278: Enumeration vals = at.getAll();
1279: String attrID = at.getID();
1280: while (vals.hasMoreElements()) {
1281: if (showAttribute) {
1282: transformer.start(attrID,
1283: attr);
1284: }
1285: String attrVal = recodeFromLDAPEncoding((String) vals
1286: .nextElement());
1287:
1288: if (query_index > 0) {
1289: switch (transformer
1290: .getQuery(query_index - 1).current_state) {
1291: case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT:
1292: StringBuffer temp = new StringBuffer(
1293: transformer
1294: .getQuery(query_index - 1).filter);
1295: if (temp.length() > 0) {
1296: temp.append(", ");
1297: }
1298: temp
1299: .append(attrID)
1300: .append("=")
1301: .append(attrVal);
1302: transformer
1303: .getQuery(query_index - 1).filter = temp
1304: .toString();
1305: break;
1306: default:
1307: transformer.start(
1308: attrID, attr);
1309: }
1310: } else {
1311: transformer.data(String
1312: .valueOf(attrVal));
1313: }
1314: if (showAttribute) {
1315: transformer.end(attrID);
1316: }
1317: }
1318: }
1319: }
1320: if (!row_element.equals("")) {
1321: transformer.end(row_element);
1322: }
1323: if (!doc_element.equals("")) {
1324: transformer.end(doc_element);
1325: }
1326: }
1327: } catch (Exception e) {
1328: if (sax_error) {
1329: throw new Exception(
1330: "[LDAPTransformer] Error in LDAP-Query: "
1331: + e.toString());
1332: } else {
1333: transformer.start(error_element, attr);
1334: transformer
1335: .data("[LDAPTransformer] Error in LDAP-Query: "
1336: + e);
1337: transformer.end(error_element);
1338: transformer.getTheLogger().error(
1339: "[LDAPTransformer] Exception: "
1340: + e.toString());
1341: }
1342: }
1343: break;
1344: case LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT:
1345: try {
1346: initConstraints(constraints);
1347: if (attrList.length != 1) {
1348: transformer.start(error_element, attr);
1349: transformer
1350: .data("Increment must reference exactly 1 attribute.");
1351: transformer.end(error_element);
1352: } else {
1353: constraints
1354: .setReturningAttributes(attrList);
1355: NamingEnumeration ldapresults = ctx.search(
1356: searchbase, filter, constraints);
1357: int attrVal = 0;
1358: String attrID = "";
1359: SearchResult si = null;
1360: while (ldapresults != null
1361: && ldapresults.hasMore()) {
1362: si = (SearchResult) ldapresults.next();
1363: javax.naming.directory.Attributes attrs = si
1364: .getAttributes();
1365: if (attrs != null) {
1366: NamingEnumeration ae = attrs
1367: .getAll();
1368: while (ae.hasMoreElements()) {
1369: Attribute at = (Attribute) ae
1370: .next();
1371: Enumeration vals = at.getAll();
1372: attrID = at.getID();
1373: attrVal = Integer
1374: .parseInt((String) vals
1375: .nextElement());
1376: }
1377: }
1378: }
1379: ++attrVal;
1380: // Specify the changes to make
1381: ModificationItem[] mods = new ModificationItem[1];
1382: // Replace the "mail" attribute with a new value
1383: mods[0] = new ModificationItem(
1384: DirContext.REPLACE_ATTRIBUTE,
1385: new BasicAttribute(attrID, Integer
1386: .toString(attrVal)));
1387: // Perform the requested modifications on the named object
1388: ctx
1389: .modifyAttributes(
1390: new StringBuffer(
1391: si
1392: .toString()
1393: .substring(
1394: 0,
1395: si
1396: .toString()
1397: .indexOf(
1398: ":")))
1399: .append(",")
1400: .append(searchbase)
1401: .toString(), mods);
1402: }
1403: } catch (Exception e) {
1404: if (sax_error) {
1405: throw new Exception(
1406: "[LDAPTransformer] Error incrementing an attribute: "
1407: + e.toString());
1408: } else {
1409: transformer.start(error_element, attr);
1410: transformer
1411: .data("[LDAPTransformer] Error incrementing an attribute: "
1412: + e.toString());
1413: transformer.end(error_element);
1414: transformer.getTheLogger().error(
1415: "[LDAPTransformer] Error incrementing an attribute: "
1416: + e.toString());
1417: }
1418: }
1419: break;
1420: /* execute modes */
1421: case LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE:
1422: try {
1423: String[] attrVal = new String[attrVale.size()];
1424: String[] attrMode = new String[attrModeVal
1425: .size()];
1426: String replaceMode = REPLACE_MODE_DEFAULT;
1427: attrVale.toArray(attrVal);
1428: attrVale.clear();
1429: attrModeVal.toArray(attrMode);
1430: attrModeVal.clear();
1431:
1432: if (attrVal.length != attrList.length) {
1433: transformer.start(error_element, attr);
1434: transformer
1435: .data("Attribute values must have the some number as a names");
1436: transformer.end(error_element);
1437: break;
1438: }
1439: HashMap attrMap = new HashMap(attrVal.length);
1440: HashMap attrModeMap = new HashMap(
1441: attrMode.length);
1442:
1443: for (int i = 0; i < attrVal.length; i++) {
1444: attrMap.put(attrList[i], attrVal[i]);
1445: attrModeMap.put(attrList[i], attrMode[i]);
1446: }
1447:
1448: initConstraints(constraints);
1449: if (attrList.length < 1) {
1450: transformer.start(error_element, attr);
1451: transformer
1452: .data("Modify must reference 1 or more attribute.");
1453: transformer.end(error_element);
1454: } else {
1455: if (!filter.equals("")) {
1456: constraints
1457: .setReturningAttributes(attrList);
1458: NamingEnumeration ldapresults = ctx
1459: .search(searchbase, filter,
1460: constraints);
1461: SearchResult si = null;
1462: /* start indicate element of executing query */
1463: if (!exec_element.equals("")) {
1464: transformer.start(exec_element,
1465: attr);
1466: }
1467: while (ldapresults != null
1468: && ldapresults.hasMore()) {
1469: if (!row_element.equals("")) {
1470: transformer.start(row_element,
1471: attr);
1472: }
1473:
1474: si = (SearchResult) ldapresults
1475: .next();
1476: javax.naming.directory.Attributes attrs = si
1477: .getAttributes();
1478: if (attrs != null) {
1479: NamingEnumeration ae = attrs
1480: .getAll();
1481: while (ae.hasMoreElements()) {
1482: Attribute at = (Attribute) ae
1483: .next();
1484: Enumeration vals = at
1485: .getAll();
1486: String attrID = at.getID();
1487: ModificationItem[] mods = new ModificationItem[1];
1488: replaceMode = (String) attrModeMap
1489: .get(attrID);
1490:
1491: String attrValue = recodeFromLDAPEncoding((String) vals
1492: .nextElement());
1493: String newAttrValue = "";
1494: /* Check the replacing method */
1495: if (replaceMode
1496: .equals(REPLACE_MODE_DEFAULT)) {
1497: newAttrValue = (String) attrMap
1498: .get(attrID);
1499: } else if (replaceMode
1500: .equals(REPLACE_MODE_APPEND)) {
1501: newAttrValue = attrValue
1502: + (String) attrMap
1503: .get(attrID);
1504: }
1505: newAttrValue = recodeToLDAPEncoding(newAttrValue);
1506:
1507: mods[0] = new ModificationItem(
1508: DirContext.REPLACE_ATTRIBUTE,
1509: new BasicAttribute(
1510: attrID,
1511: newAttrValue));
1512:
1513: // Perform the requested modifications on the named object
1514: ctx
1515: .modifyAttributes(
1516: si
1517: .toString()
1518: .substring(
1519: 0,
1520: si
1521: .toString()
1522: .indexOf(
1523: ":"))
1524: + ","
1525: + searchbase,
1526: mods);
1527:
1528: /* confirm of success */
1529: transformer.start(attrID,
1530: attr);
1531: transformer
1532: .data("replaced");
1533: transformer.end(attrID);
1534: }
1535: }
1536:
1537: if (!row_element.equals("")) {
1538: transformer.end(row_element);
1539: }
1540:
1541: }
1542: if (!exec_element.equals("")) {
1543: transformer.end(exec_element);
1544: }
1545: } else {
1546: //filter is not present
1547: javax.naming.directory.Attributes attrs = ctx
1548: .getAttributes(searchbase,
1549: attrList);
1550: /* start indicate element of executing query */
1551: if (!exec_element.equals("")) {
1552: transformer.start(exec_element,
1553: attr);
1554: }
1555: if (!row_element.equals("")) {
1556: transformer
1557: .start(row_element, attr);
1558: }
1559: if (attrs != null) {
1560: NamingEnumeration ae = attrs
1561: .getAll();
1562: while (ae.hasMoreElements()) {
1563: Attribute at = (Attribute) ae
1564: .next();
1565: Enumeration vals = at.getAll();
1566: String attrID = at.getID();
1567: ModificationItem[] mods = new ModificationItem[1];
1568: replaceMode = (String) attrModeMap
1569: .get(attrID);
1570:
1571: String attrValue = recodeFromLDAPEncoding((String) vals
1572: .nextElement());
1573:
1574: String newAttrValue = "";
1575: /* Check the replacing method */
1576: if (replaceMode
1577: .equals(REPLACE_MODE_DEFAULT)) {
1578: newAttrValue = (String) attrMap
1579: .get(attrID);
1580: } else if (replaceMode
1581: .equals(REPLACE_MODE_APPEND)) {
1582: newAttrValue = attrValue
1583: + (String) attrMap
1584: .get(attrID);
1585: }
1586: newAttrValue = recodeToLDAPEncoding(newAttrValue);
1587:
1588: mods[0] = new ModificationItem(
1589: DirContext.REPLACE_ATTRIBUTE,
1590: new BasicAttribute(
1591: attrID,
1592: newAttrValue));
1593:
1594: // Perform the requested modifications on the named object
1595: ctx.modifyAttributes(
1596: searchbase, mods);
1597:
1598: /* confirm of success */
1599: transformer.start(attrID, attr);
1600: transformer.data("replaced");
1601: transformer.end(attrID);
1602: }
1603: }
1604:
1605: if (!row_element.equals("")) {
1606: transformer.end(row_element);
1607: }
1608:
1609: /* end indicate element of executing query */
1610: if (!exec_element.equals("")) {
1611: transformer.end(exec_element);
1612: }
1613: }
1614: }
1615:
1616: } catch (Exception e) {
1617: if (sax_error) {
1618: throw new Exception(
1619: "[LDAPTransformer] Error replacing an attribute: "
1620: + e.toString());
1621: } else {
1622: transformer.start(error_element, attr);
1623: transformer
1624: .data("[LDAPTransformer] Error replacing an attribute: "
1625: + e.toString());
1626: transformer.end(error_element);
1627: transformer.getTheLogger().error(
1628: "[LDAPTransformer] Error replacing an attribute: "
1629: + e.toString());
1630: if (!row_element.equals("")) {
1631: transformer.end(row_element);
1632: }
1633: if (!exec_element.equals("")) {
1634: transformer.end(exec_element);
1635: }
1636: }
1637: }
1638: break;
1639: case LDAPTransformer.STATE_INSIDE_EXECUTE_ADD:
1640: try {
1641: String[] attrVal = new String[attrVale.size()];
1642: attrVale.toArray(attrVal);
1643: attrVale.clear();
1644: if (attrVal.length != attrList.length) {
1645: transformer.start(error_element, attr);
1646: transformer
1647: .data("Attribute values must have the some number as a names");
1648: transformer.end(error_element);
1649: break;
1650: }
1651: HashMap attrMap = new HashMap(attrVal.length);
1652:
1653: for (int i = 0; i < attrVal.length; i++)
1654: attrMap.put(attrList[i], attrVal[i]);
1655:
1656: initConstraints(constraints);
1657: if (attrList.length < 1) {
1658: transformer.start(error_element, attr);
1659: transformer
1660: .data("Modify must reference 1 or more attribute.");
1661: transformer.end(error_element);
1662: } else {
1663: if (!filter.equals("")) {
1664: constraints
1665: .setReturningAttributes(attrList);
1666: NamingEnumeration ldapresults = ctx
1667: .search(searchbase, filter,
1668: constraints);
1669: SearchResult si = null;
1670: /* start indicate element of executing query */
1671: if (!exec_element.equals("")) {
1672: transformer.start(exec_element,
1673: attr);
1674: }
1675: while (ldapresults != null
1676: && ldapresults.hasMore()) {
1677: if (!row_element.equals("")) {
1678: transformer.start(row_element,
1679: attr);
1680: }
1681:
1682: si = (SearchResult) ldapresults
1683: .next();
1684: javax.naming.directory.Attributes attrs = si
1685: .getAttributes();
1686: if (attrs != null) {
1687: /* Replace the attribute if attribute already exist */
1688: NamingEnumeration ae = attrs
1689: .getAll();
1690: while (ae.hasMoreElements()) {
1691: Attribute at = (Attribute) ae
1692: .next();
1693: String attrID = at.getID();
1694: // Specify the changes to make
1695: ModificationItem[] mods = new ModificationItem[1];
1696:
1697: String attrValue = recodeToLDAPEncoding((String) attrMap
1698: .get(attrID));
1699: mods[0] = new ModificationItem(
1700: DirContext.REPLACE_ATTRIBUTE,
1701: new BasicAttribute(
1702: attrID,
1703: attrValue));
1704: // Perform the requested modifications on the named object
1705: ctx
1706: .modifyAttributes(
1707: new StringBuffer(
1708: si
1709: .toString()
1710: .substring(
1711: 0,
1712: si
1713: .toString()
1714: .indexOf(
1715: ":")))
1716: .append(
1717: ",")
1718: .append(
1719: searchbase)
1720: .toString(),
1721: mods);
1722:
1723: /* confirm of success */
1724: transformer.start(attrID,
1725: attr);
1726: transformer
1727: .data("replaced");
1728: transformer.end(attrID);
1729: /* Remove the attribute from map after replacing */
1730: attrMap.remove(attrID);
1731: }
1732: }
1733: /* Add the attributes */
1734: if (!attrMap.isEmpty()) {
1735: ModificationItem[] mods = new ModificationItem[1];
1736: for (int i = 0; i < attrList.length; i++) {
1737: if (attrMap
1738: .containsKey(attrList[i])) {
1739: String attrValue = recodeToLDAPEncoding((String) attrMap
1740: .get(attrList[i]));
1741: mods[0] = new ModificationItem(
1742: DirContext.ADD_ATTRIBUTE,
1743: new BasicAttribute(
1744: attrList[i],
1745: attrValue));
1746: // Perform the requested modifications on the named object
1747: ctx
1748: .modifyAttributes(
1749: new StringBuffer(
1750: si
1751: .toString()
1752: .substring(
1753: 0,
1754: si
1755: .toString()
1756: .indexOf(
1757: ":")))
1758: .append(
1759: ",")
1760: .append(
1761: searchbase)
1762: .toString(),
1763: mods);
1764:
1765: /* confirm of success */
1766: transformer.start(
1767: attrList[i],
1768: attr);
1769: transformer.data("add");
1770: transformer
1771: .end(attrList[i]);
1772: }
1773: }
1774: }
1775: if (!row_element.equals("")) {
1776: transformer.end(row_element);
1777: }
1778: }
1779: if (!exec_element.equals("")) {
1780: transformer.end(exec_element);
1781: }
1782: } else {
1783: //filter is not present
1784: javax.naming.directory.Attributes attrs = ctx
1785: .getAttributes(searchbase,
1786: attrList);
1787: /* start indicate element of executing query */
1788: if (!exec_element.equals("")) {
1789: transformer.start(exec_element,
1790: attr);
1791: }
1792: if (!row_element.equals("")) {
1793: transformer
1794: .start(row_element, attr);
1795: }
1796:
1797: if (attrs != null) {
1798: NamingEnumeration ae = attrs
1799: .getAll();
1800: while (ae.hasMoreElements()) {
1801: Attribute at = (Attribute) ae
1802: .next();
1803: String attrID = at.getID();
1804: // Specify the changes to make
1805: ModificationItem[] mods = new ModificationItem[1];
1806:
1807: String attrValue = recodeToLDAPEncoding((String) attrMap
1808: .get(attrID));
1809: mods[0] = new ModificationItem(
1810: DirContext.REPLACE_ATTRIBUTE,
1811: new BasicAttribute(
1812: attrID,
1813: attrValue));
1814: // Perform the requested modifications on the named object
1815: ctx.modifyAttributes(
1816: searchbase, mods);
1817:
1818: /* confirm of success */
1819: transformer.start(attrID, attr);
1820: transformer.data("replaced");
1821: transformer.end(attrID);
1822: /* Remove the attribute from map after replacing */
1823: attrMap.remove(attrID);
1824: }
1825: }
1826: /* Add the attributes */
1827: if (!attrMap.isEmpty()) {
1828: ModificationItem[] mods = new ModificationItem[1];
1829: for (int i = 0; i < attrList.length; i++) {
1830: if (attrMap
1831: .containsKey(attrList[i])) {
1832: String attrValue = recodeToLDAPEncoding((String) attrMap
1833: .get(attrList[i]));
1834: mods[0] = new ModificationItem(
1835: DirContext.ADD_ATTRIBUTE,
1836: new BasicAttribute(
1837: attrList[i],
1838: attrValue));
1839: // Perform the requested modifications on the named object
1840: ctx.modifyAttributes(
1841: searchbase, mods);
1842: /* confirm of success */
1843: transformer.start(
1844: attrList[i], attr);
1845: transformer.data("add");
1846: transformer
1847: .end(attrList[i]);
1848: }
1849: }
1850: }
1851: if (!row_element.equals("")) {
1852: transformer.end(row_element);
1853: }
1854: /* end indicate element of executing query */
1855: if (!exec_element.equals("")) {
1856: transformer.end(exec_element);
1857: }
1858: }
1859: }
1860: } catch (Exception e) {
1861: if (sax_error) {
1862: throw new Exception(
1863: "[LDAPTransformer] Error replacing an attribute: "
1864: + e.toString());
1865: } else {
1866: transformer.start(error_element, attr);
1867: transformer
1868: .data("[LDAPTransformer] Error replacing an attribute: "
1869: + e.toString());
1870: transformer.end(error_element);
1871: transformer.getTheLogger().error(
1872: "[LDAPTransformer] Error replacing an attribute: "
1873: + e.toString());
1874: if (!row_element.equals("")) {
1875: transformer.end(row_element);
1876: }
1877: if (!exec_element.equals("")) {
1878: transformer.end(exec_element);
1879: }
1880: }
1881: }
1882: break;
1883: default:
1884: } //end switch
1885: } catch (NamingException e) {
1886: if (sax_error) {
1887: throw new NamingException(
1888: "[LDAPTransformer] Failed ldap-connection to directory service: "
1889: + e.toString());
1890: } else {
1891: transformer.start(error_element, attr);
1892: transformer
1893: .data("[LDAPTransformer] Failed ldap-connection to directory service.");
1894: transformer.end(error_element);
1895: transformer.getTheLogger().error(
1896: "[LDAPTransformer] Failed to connect to "
1897: + serverurl + e.toString());
1898: }
1899: }
1900: try {
1901: disconnect();
1902: } catch (NamingException e) {
1903: if (sax_error) {
1904: throw new NamingException(
1905: "[LDAPTransformer] Failed ldap-disconnection from directory service: "
1906: + e.toString());
1907: } else {
1908: transformer.start(error_element, attr);
1909: transformer
1910: .data("[LDAPTransformer] Failed ldap-disconnection to directory service.");
1911: transformer.end(error_element);
1912: transformer.getTheLogger().error(
1913: "[LDAPTransformer] Failed to disconnect from "
1914: + serverurl + e.toString());
1915: }
1916: }
1917: }
1918:
1919: protected void addAttrList(String attr) {
1920: attrListe.add(attr);
1921: }
1922:
1923: protected void addAttrModeVal(String mode) {
1924: attrModeVal.add(mode);
1925: }
1926:
1927: protected void addAttrVal(String val) {
1928: attrVale.add(val);
1929: }
1930:
1931: protected void addInitialContextValue(String name, String value) {
1932: initialContextValues.put(name, value);
1933: }
1934:
1935: protected void connect() throws NamingException {
1936: if (root_dn != null && password != null) {
1937: env
1938: .put(Context.SECURITY_AUTHENTICATION,
1939: authentication);
1940: env.put(Context.SECURITY_PRINCIPAL, root_dn);
1941: env.put(Context.SECURITY_CREDENTIALS, password);
1942: }
1943:
1944: env.put("java.naming.ldap.version", version);
1945: env.put(Context.INITIAL_CONTEXT_FACTORY, initializer);
1946: env.put(Context.PROVIDER_URL, serverurl + ":" + port);
1947:
1948: // Override existing properties or add new properties with values from initial-context
1949: env.putAll(initialContextValues);
1950:
1951: try {
1952: ctx = new InitialDirContext(env);
1953: } catch (NamingException e) {
1954: env.clear();
1955: throw new NamingException(e.toString());
1956: }
1957: }
1958:
1959: protected void disconnect() throws NamingException {
1960: try {
1961: if (ctx != null)
1962: ctx.close();
1963: } catch (NamingException e) {
1964: ctx = null;
1965: env.clear();
1966: throw new NamingException(e.toString());
1967: }
1968: ctx = null;
1969: env.clear();
1970: }
1971:
1972: protected void debugPrint() {
1973: Logger logger = transformer.getTheLogger();
1974: if (logger.isDebugEnabled()) {
1975: logger.debug("[LDAPTransformer] query_index: "
1976: + query_index);
1977: logger.debug("[LDAPTransformer] current_state: "
1978: + current_state);
1979: logger.debug("[LDAPTransformer] serverurl: "
1980: + serverurl);
1981: logger.debug("[LDAPTransformer] port: " + port);
1982: logger.debug("[LDAPTransformer] root_dn: " + root_dn);
1983: logger.debug("[LDAPTransformer] password: " + password);
1984: logger.debug("[LDAPTransformer] version: " + version);
1985: logger.debug("[LDAPTransformer] scope: " + scope);
1986: logger.debug("[LDAPTransformer] authentication: "
1987: + authentication);
1988: logger.debug("[LDAPTransformer] toDo: " + toDo);
1989: logger.debug("[LDAPTransformer] searchbase: "
1990: + searchbase);
1991: logger.debug("[LDAPTransformer] showAttribute: "
1992: + showAttribute);
1993: logger.debug("[LDAPTransformer] attribute: "
1994: + attrListe.toString());
1995: logger.debug("[LDAPTransformer] initial-context: "
1996: + initialContextValues);
1997: logger.debug("[LDAPTransformer] filter: " + filter);
1998: logger.debug("[LDAPTransformer] doc_element: "
1999: + doc_element);
2000: logger.debug("[LDAPTransformer] row_element: "
2001: + row_element);
2002: logger.debug("[LDAPTransformer] error_element: "
2003: + error_element);
2004: logger.debug("[LDAPTransformer] sax-error: "
2005: + sax_error);
2006: logger.debug("[LDAPTransformer] deref_link: "
2007: + deref_link);
2008: logger.debug("[LDAPTransformer] count_limit: "
2009: + count_limit);
2010: logger.debug("[LDAPTransformer] time_limit: "
2011: + time_limit);
2012: }
2013: }
2014:
2015: /**
2016: * Recodes a String value from {@link #LDAP_ENCODING} to specified {@link #encoding}.
2017: * @param value the String to recode
2018: * @return the recoded String
2019: * @throws UnsupportedEncodingException if either the used encoding
2020: */
2021: private String recodeFromLDAPEncoding(String value)
2022: throws UnsupportedEncodingException {
2023: if (!LDAP_ENCODING.equals(encoding)) {
2024: value = new String(value.getBytes(LDAP_ENCODING),
2025: encoding);
2026: }
2027: return value;
2028: }
2029:
2030: /**
2031: * Recodes a String value from specified {@link #encoding} to {@link #LDAP_ENCODING}.
2032: * @param value the String to recode
2033: * @return the recoded String
2034: * @throws UnsupportedEncodingException if either the used encoding
2035: */
2036: private String recodeToLDAPEncoding(String value)
2037: throws UnsupportedEncodingException {
2038: if (!LDAP_ENCODING.equals(encoding)) {
2039: value = new String(value.getBytes(encoding),
2040: LDAP_ENCODING);
2041: }
2042: return value;
2043: }
2044: }
2045: }
|