1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.cocoon.transformation;
18
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.Vector;
28
29 import javax.naming.Context;
30 import javax.naming.NamingEnumeration;
31 import javax.naming.NamingException;
32 import javax.naming.directory.Attribute;
33 import javax.naming.directory.BasicAttribute;
34 import javax.naming.directory.DirContext;
35 import javax.naming.directory.InitialDirContext;
36 import javax.naming.directory.ModificationItem;
37 import javax.naming.directory.SearchControls;
38 import javax.naming.directory.SearchResult;
39
40 import org.apache.avalon.framework.logger.Logger;
41 import org.apache.avalon.framework.parameters.Parameters;
42 import org.apache.cocoon.ProcessingException;
43 import org.apache.cocoon.environment.SourceResolver;
44 import org.apache.cocoon.xml.XMLConsumer;
45 import org.apache.commons.collections.MapUtils;
46 import org.apache.commons.lang.BooleanUtils;
47 import org.apache.commons.lang.ObjectUtils;
48 import org.apache.commons.lang.StringUtils;
49 import org.xml.sax.Attributes;
50 import org.xml.sax.Locator;
51 import org.xml.sax.SAXException;
52 import org.xml.sax.ext.LexicalHandler;
53 import org.xml.sax.helpers.AttributesImpl;
54
55 /**
56 * The <code>LDAPTransformer</code> can be plugged into a pipeline to transform
57 * the SAX events into queries and responses to/from a LDAP interface.
58 * <br>
59 * The file will be specified in a parameter tag in the sitemap pipeline to the
60 * transformer as follows:
61 * <p>
62 * <code>
63 * <map:transform type="ldap"/><br>
64 * </code>
65 * </p>
66 * <br>
67 *
68 * The following DTD is valid:<br>
69 * <!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>
70 * <!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>
71 * increments (+1) an integer attribute on a directory-server (ldap)<br>
72 * <!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>
73 * replace attribute on a directory-server (ldap)<br>
74 * <!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>
75 * add attribute on a directory-server (ldap)<br>
76 * <br>
77 * <!ELEMENT initializer (#PCDATA)>+ (default: "com.sun.jndi.ldap.LdapCtxFactory")<br>
78 * <!ELEMENT initial-context (#EMPTY)><br>
79 * <!ATTLIST initial-context name CDATA #REQUIRED
80 value CDATA #REQUIRED ><br>
81 * <!ELEMENT authentication (#PCDATA)>+ (default: "simple")<br>
82 * <!ELEMENT version (#PCDATA)>+ (default: "2")<br>
83 * <!ELEMENT serverurl (#PCDATA)>+<br>
84 * <!ELEMENT port (#PCDATA)>+ (default: 389)<br>
85 * <!ELEMENT rootdn (#PCDATA)>+<br>
86 * <!ELEMENT password (#PCDATA)>+<br>
87 * <!ELEMENT scope (ONELEVEL_SCOPE | SUBTREE_SCOPE | OBJECT_SCOPE)>+ (default: ONELEVEL_SCOPE)<br>
88 * <!ELEMENT searchbase (#PCDATA)>+<br>
89 * <!ELEMENT doc-element (#PCDATA)>+ (default: "doc-element")<br>
90 * <!ELEMENT row-element (#PCDATA)>+ (default: "row-element")<br>
91 * <!ELEMENT dn-attribute (#PCDATA)>+ (default: "" meaning no DN attribute)<br>
92 * <!ELEMENT error-element (#PCDATA)>+ (default: "ldap-error") (in case of error returned error tag)<br>
93 * <!ELEMENT sax_error (TRUE | FALSE)>+; (default: FALSE) (throws SAX-Exception instead of error tag)<br>
94 * <!ELEMENT attribute (#PCDATA)><br>
95 * <!ATTLIST attribute name CDATA #IMPLIED
96 mode (append|replace) 'replace' #IMPLIED > (in case execute-replace or execute-add elements using) <br>
97
98 * <!ELEMENT show-attribute (TRUE | FALSE)> (default: TRUE)<br>
99 * <!ELEMENT filter (#PCDATA | execute-query)+><br>
100 * <!ELEMENT deref-link (TRUE | FALSE)> (default: FALSE)<br>
101 * <!ELEMENT count-limit (#PCDATA)> (integer default: 0 -> no limit)<br>
102 * <!ELEMENT time-limit (#PCDATA)> (integer default: 0 -> infinite)<br>
103 * <!ELEMENT debug (TRUE | FALSE)>+; (default: FALSE)<br>
104 * <br>
105 * + can also be defined as parameter in the sitemap.
106 * <br>
107 *
108 * @version $Id: LDAPTransformer.java 433543 2006-08-22 06:22:54Z crossley $
109 */
110 public class LDAPTransformer extends AbstractTransformer {
111
112 /** The LDAP namespace ("http://apache.org/cocoon/LDAP/1.0")*/
113 public static final String my_uri = "http://apache.org/cocoon/LDAP/1.0";
114 public static final String my_name = "LDAPTransformer";
115
116 /** The LDAP namespace element names */
117 public static final String MAGIC_ATTRIBUTE_ELEMENT = "attribute";
118 public static final String MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE = "name";
119 public static final String MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE = "mode";
120 public static final String MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT = "replace";
121 public static final String MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A = "append";
122 public static final String MAGIC_AUTHENTICATION_ELEMENT = "authentication";
123 public static final String MAGIC_COUNT_LIMIT_ELEMENT = "count-limit";
124 public static final String MAGIC_DEBUG_ELEMENT = "debug";
125 public static final String MAGIC_DEREF_LINK_ELEMENT = "deref-link";
126 public static final String MAGIC_DOC_ELEMENT = "doc-element";
127 public static final String MAGIC_ENCODING_ELEMENT = "encoding";
128 public static final String MAGIC_ERROR_ELEMENT = "error-element";
129 public static final String MAGIC_EXECUTE_ADD = "execute-add";
130 public static final String MAGIC_EXECUTE_INCREMENT = "execute-increment";
131 public static final String MAGIC_EXECUTE_QUERY = "execute-query";
132 public static final String MAGIC_EXECUTE_REPLACE = "execute-replace";
133 public static final String MAGIC_FILTER_ELEMENT = "filter";
134 public static final String MAGIC_INITIALIZER_ELEMENT = "initializer";
135 public static final String MAGIC_INITIAL_CONTEXT_ELEMENT = "initial-context";
136 public static final String MAGIC_INITIAL_CONTEXT_NAME_ATTRIBUTE = "name";
137 public static final String MAGIC_INITIAL_CONTEXT_VALUE_ATTRIBUTE = "value";
138 public static final String MAGIC_PASSWORD_ELEMENT = "password";
139 public static final String MAGIC_PORT_ELEMENT = "port";
140 public static final String MAGIC_ROOT_DN_ELEMENT = "rootdn";
141 public static final String MAGIC_ROW_ELEMENT = "row-element";
142 public static final String MAGIC_DN_ATTRIBUTE = "dn-attribute";
143 public static final String MAGIC_SAX_ERROR = "sax-error";
144 public static final String MAGIC_SCOPE_ELEMENT = "scope";
145 public static final String MAGIC_SEARCHBASE_ELEMENT = "searchbase";
146 public static final String MAGIC_SERVERURL_ELEMENT = "serverurl";
147 public static final String MAGIC_SHOW_ATTRIBUTE_ELEMENT = "show-attribute";
148 public static final String MAGIC_TIME_LIMIT_ELEMENT = "time-limit";
149 public static final String MAGIC_VERSION_ELEMENT = "version";
150
151 /** The states we are allowed to be in */
152 public static final int STATE_OUTSIDE = 0;
153 public static final int STATE_INSIDE_EXECUTE_QUERY = 1;
154 public static final int STATE_INSIDE_EXECUTE_INCREMENT = 2;
155 public static final int STATE_INSIDE_EXECUTE_ELEMENT = 3;
156 public static final int STATE_INSIDE_INITIALIZER_ELEMENT = 4;
157 public static final int STATE_INSIDE_SERVERURL_ELEMENT = 5;
158 public static final int STATE_INSIDE_PORT_ELEMENT = 6;
159 public static final int STATE_INSIDE_SCOPE_ELEMENT = 7;
160 public static final int STATE_INSIDE_VERSION_ELEMENT = 8;
161 public static final int STATE_INSIDE_AUTHENTICATION_ELEMENT = 9;
162 public static final int STATE_INSIDE_ROOT_DN_ELEMENT = 10;
163 public static final int STATE_INSIDE_PASSWORD_ELEMENT = 11;
164 public static final int STATE_INSIDE_SEARCHBASE_ELEMENT = 12;
165 public static final int STATE_INSIDE_DOC_ELEMENT = 13;
166 public static final int STATE_INSIDE_ROW_ELEMENT = 14;
167 public static final int STATE_INSIDE_ATTRIBUTE_ELEMENT = 15;
168 public static final int STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT = 16;
169 public static final int STATE_INSIDE_ERROR_ELEMENT = 17;
170 public static final int STATE_INSIDE_FILTER_ELEMENT = 18;
171 public static final int STATE_INSIDE_DEREF_LINK_ELEMENT = 19;
172 public static final int STATE_INSIDE_COUNT_LIMIT_ELEMENT = 20;
173 public static final int STATE_INSIDE_TIME_LIMIT_ELEMENT = 21;
174 public static final int STATE_INSIDE_DEBUG_ELEMENT = 22;
175 public static final int STATE_INSIDE_SAX_ERROR_ELEMENT = 23;
176 public static final int STATE_INSIDE_EXECUTE_REPLACE = 24;
177 public static final int STATE_INSIDE_EXECUTE_ADD = 25;
178 public static final int STATE_INSIDE_DN_ATTRIBUTE = 26;
179 public static final int STATE_INSIDE_INITIAL_CONTEXT_ELEMENT = 27;
180
181 /** Default parameters that might apply to all queries */
182 protected HashMap default_properties = new HashMap();
183
184 /** The name of the value element we're currently receiving */
185 protected String current_name;
186
187 /** The current state of the event receiving FSM */
188 protected int current_state = STATE_OUTSIDE;
189
190 /** The value of the value element we're currently receiving */
191 protected StringBuffer current_value = new StringBuffer();
192
193 /** The list of queries that we're currently working on */
194 protected Vector queries = new Vector();
195
196 /** The offset of the current query in the queries list */
197 protected int current_query_index = -1;
198
199 /** SAX producing state information */
200 protected XMLConsumer xml_consumer;
201 protected LexicalHandler lexical_handler;
202
203 /** SitemapComponent methods */
204
205 public void setup(SourceResolver resolver, Map objectModel, String source, Parameters parameters)
206 throws ProcessingException, SAXException, IOException {
207 current_state = STATE_OUTSIDE;
208
209 setDefaultProperty(parameters, MAGIC_INITIALIZER_ELEMENT); // Check the initializer
210 setDefaultProperty(parameters, MAGIC_VERSION_ELEMENT); // Check the version
211 setDefaultProperty(parameters, MAGIC_AUTHENTICATION_ELEMENT); // Check the authentication
212 setDefaultProperty(parameters, MAGIC_SCOPE_ELEMENT); // Check the scope
213 setDefaultProperty(parameters, MAGIC_SERVERURL_ELEMENT); // Check the serverurl
214 setDefaultProperty(parameters, MAGIC_ROOT_DN_ELEMENT); // Check the ldap-root_dn
215 setDefaultProperty(parameters, MAGIC_PASSWORD_ELEMENT); // Check the ldap-pwd
216 setDefaultProperty(parameters, MAGIC_PORT_ELEMENT); // Check the port
217 setDefaultProperty(parameters, MAGIC_SEARCHBASE_ELEMENT); // Check the searchbase
218 setDefaultProperty(parameters, MAGIC_DOC_ELEMENT); // Check the doc-element
219 setDefaultProperty(parameters, MAGIC_ROW_ELEMENT); // Check the row-element
220 setDefaultProperty(parameters, MAGIC_DN_ATTRIBUTE); // Check the dn-attribute
221 setDefaultProperty(parameters, MAGIC_ERROR_ELEMENT); // Check the error-element
222 setDefaultProperty(parameters, MAGIC_SAX_ERROR); // Check the sax-error
223 setDefaultProperty(parameters, MAGIC_DEREF_LINK_ELEMENT); // Check the deref-link-element
224 setDefaultProperty(parameters, MAGIC_COUNT_LIMIT_ELEMENT); // Check the count-limit-element
225 setDefaultProperty(parameters, MAGIC_TIME_LIMIT_ELEMENT); // Check the time-limit-element
226 setDefaultProperty(parameters, MAGIC_DEBUG_ELEMENT); // Check the debug-element
227 setDefaultProperty(parameters, MAGIC_ENCODING_ELEMENT); // Check the encoding
228 setDefaultProperty(parameters, MAGIC_FILTER_ELEMENT); // Check the filter-element
229 }
230
231 /** My very own methods */
232
233 /* Helper to set default properties */
234 protected void setDefaultProperty(Parameters parameters, String propertyName) {
235 String parameter = parameters.getParameter(propertyName, null);
236 if (parameter != null) {
237 default_properties.put(propertyName, parameter);
238 }
239 }
240
241 /* Helper class to store a new Query in the stack */
242 protected void storeQuery(int nextState) {
243 current_state = nextState;
244 current_query_index = queries.size();
245 LDAPQuery query = new LDAPQuery(this);
246 queries.addElement(query);
247 getCurrentQuery().toDo = nextState;
248 getCurrentQuery().query_index = current_query_index;
249 }
250
251 /**
252 * This will be the meat of LDAPTransformer, where the query is run.
253 */
254 protected void executeQuery(int index) throws SAXException {
255 this.contentHandler.startPrefixMapping("", LDAPTransformer.my_uri);
256 LDAPQuery query = (LDAPQuery)queries.elementAt(index);
257 try {
258 query.execute();
259 } catch (NamingException e) {
260 getLogger().error(e.toString());
261 throw new SAXException(e);
262 } catch (Exception e) {
263 getLogger().error(e.toString());
264 throw new SAXException(e);
265 }
266 this.contentHandler.endPrefixMapping("");
267 }
268
269 protected static void throwIllegalStateException(String message) {
270 throw new IllegalStateException(my_name + ": " + message);
271 }
272
273 protected void startExecuteElement() {
274 switch (current_state) {
275 case LDAPTransformer.STATE_OUTSIDE :
276 case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
277 storeQuery(LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY);
278 break;
279 default :
280 throwIllegalStateException("Not expecting a start execute-query element");
281 }
282 }
283
284 protected void startExecuteElement(int state, String name) {
285 switch (current_state) {
286 case LDAPTransformer.STATE_OUTSIDE :
287 case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY :
288 storeQuery(state);
289 break;
290 default :
291 throwIllegalStateException("Not expecting a start " + name + " element");
292 }
293 }
294
295 protected void endExecuteElement(int state, String name) throws SAXException {
296 if (current_state == state) {
297 executeQuery(current_query_index);
298 queries.remove(current_query_index);
299 --current_query_index;
300 if (current_query_index > -1) {
301 current_state = getCurrentQuery().toDo;
302 } else {
303 queries.removeAllElements();
304 current_state = LDAPTransformer.STATE_OUTSIDE;
305 }
306 } else {
307 throwIllegalStateException("Not expecting a end " + name + " element");
308 }
309 }
310
311 protected void startQueryParameterElement(int state, String name) {
312 switch (current_state) {
313 case STATE_INSIDE_EXECUTE_QUERY :
314 case STATE_INSIDE_EXECUTE_INCREMENT :
315 case STATE_INSIDE_EXECUTE_REPLACE :
316 case STATE_INSIDE_EXECUTE_ADD :
317 current_value.setLength(0);
318 current_state = state;
319 getCurrentQuery().current_state = state;
320 break;
321 default :
322 throwIllegalStateException("Not expecting a start " + name + " element");
323 }
324 }
325
326 protected void startParameterElement(int state, String name) {
327 switch (current_state) {
328 case STATE_INSIDE_EXECUTE_QUERY :
329 case STATE_INSIDE_EXECUTE_INCREMENT :
330 case STATE_INSIDE_EXECUTE_REPLACE :
331 case STATE_INSIDE_EXECUTE_ADD :
332 current_value.setLength(0);
333 current_state = state;
334 break;
335 default :
336 throwIllegalStateException("Not expecting a start " + name + " serverurl element");
337 }
338 }
339
340 protected void endInitializerElement() {
341 switch (current_state) {
342 case LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT :
343 getCurrentQuery().initializer = current_value.toString();
344 current_state = getCurrentQuery().toDo;
345 break;
346 default :
347 throwIllegalStateException("Not expecting a end initializer element");
348 }
349 }
350
351 protected void endScopeElement() {
352 switch (current_state) {
353 case LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT :
354 getCurrentQuery().scope = current_value.toString();
355 current_state = getCurrentQuery().toDo;
356 break;
357 default :
358 throwIllegalStateException("Not expecting a end scope element");
359 }
360 }
361
362 protected void endAuthenticationElement() {
363 switch (current_state) {
364 case LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT :
365 getCurrentQuery().authentication = current_value.toString();
366 current_state = getCurrentQuery().toDo;
367 break;
368 default :
369 throwIllegalStateException("Not expecting a end authentication element");
370 }
371 }
372
373 protected void endServerurlElement() {
374 switch (current_state) {
375 case LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT :
376 getCurrentQuery().serverurl = current_value.toString();
377 current_state = getCurrentQuery().toDo;
378 break;
379 default :
380 throwIllegalStateException("Not expecting a end serverurl element");
381 }
382 }
383
384 protected void endPortElement() {
385 switch (current_state) {
386 case LDAPTransformer.STATE_INSIDE_PORT_ELEMENT :
387 getCurrentQuery().port = Integer.parseInt(current_value.toString());
388 current_state = getCurrentQuery().toDo;
389 break;
390 default :
391 throwIllegalStateException("Not expecting a end server element");
392 }
393 }
394
395 protected void startShowAttributeElement(Attributes attributes) {
396 switch (current_state) {
397 case STATE_INSIDE_EXECUTE_QUERY :
398 case STATE_INSIDE_EXECUTE_INCREMENT :
399 //case STATE_INSIDE_EXECUTE_REPLACE:
400 current_value.setLength(0);
401 current_state = LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT;
402 break;
403 default :
404 throwIllegalStateException("Not expecting a start show-attribute element");
405 }
406 }
407
408 protected void endShowAttributeElement() {
409 switch (current_state) {
410 case LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT :
411 if (!BooleanUtils.toBoolean(current_value.toString())) {
412 getCurrentQuery().showAttribute = false;
413 }
414 current_state = getCurrentQuery().toDo;
415 break;
416 default :
417 throwIllegalStateException("Not expecting a end show-attribute element");
418 }
419 }
420
421 protected void endSearchbaseElement() {
422 switch (current_state) {
423 case LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT :
424 getCurrentQuery().searchbase = current_value.toString();
425 current_state = getCurrentQuery().toDo;
426 break;
427 default :
428 throwIllegalStateException("Not expecting a end searchbase element");
429 }
430 }
431
432 protected void endDocElement() {
433 switch (current_state) {
434 case LDAPTransformer.STATE_INSIDE_DOC_ELEMENT :
435 getCurrentQuery().doc_element = current_value.toString();
436 current_state = getCurrentQuery().toDo;
437 break;
438 default :
439 throwIllegalStateException("Not expecting a end doc-element element");
440 }
441 }
442
443 protected void endRowElement() {
444 switch (current_state) {
445 case LDAPTransformer.STATE_INSIDE_ROW_ELEMENT :
446 getCurrentQuery().row_element = current_value.toString();
447 current_state = getCurrentQuery().toDo;
448 break;
449 default :
450 throwIllegalStateException("Not expecting a end row-element element");
451 }
452 }
453
454 protected void endDnAttribute() {
455 switch (current_state) {
456 case LDAPTransformer.STATE_INSIDE_DN_ATTRIBUTE :
457 getCurrentQuery().dn_attribute = current_value.toString();
458 current_state = getCurrentQuery().toDo;
459 break;
460 default :
461 throwIllegalStateException("Not expecting a end dn-attribute element");
462 }
463 }
464
465 protected void endErrorElement() {
466 switch (current_state) {
467 case LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT :
468 getCurrentQuery().error_element = current_value.toString();
469 current_state = getCurrentQuery().toDo;
470 break;
471 default :
472 throwIllegalStateException("Not expecting a end error-element element");
473 }
474 }
475
476 protected void endSaxError() {
477 switch (current_state) {
478 case LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT :
479 if (current_value.toString().toUpperCase().equals("TRUE")) {
480 getCurrentQuery().sax_error = true;
481 }
482 current_state = getCurrentQuery().toDo;
483 break;
484 default :
485 throwIllegalStateException("Not expecting a end sax-error element");
486 }
487 }
488
489 protected void endRootDnElement() {
490 switch (current_state) {
491 case LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT :
492 getCurrentQuery().root_dn = current_value.toString();
493 current_state = getCurrentQuery().toDo;
494 break;
495 default :
496 throwIllegalStateException("Not expecting a end root-dn element");
497 }
498 }
499
500 protected void endPasswordElement() {
501 switch (current_state) {
502 case LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT :
503 getCurrentQuery().password = current_value.toString();
504 current_state = getCurrentQuery().toDo;
505 break;
506 default :
507 throwIllegalStateException("Not expecting a end password element");
508 }
509 }
510
511 protected void startAttributeElement(Attributes attributes) {
512 switch (current_state) {
513 case STATE_INSIDE_EXECUTE_QUERY :
514 case STATE_INSIDE_EXECUTE_INCREMENT :
515 current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
516 current_value.setLength(0);
517 break;
518 case STATE_INSIDE_EXECUTE_REPLACE :
519 boolean is_name_present = false;
520 String mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT;
521 if (attributes != null && attributes.getLength() > 0) {
522 AttributesImpl new_attributes = new AttributesImpl(attributes);
523 for (int i = 0; i < new_attributes.getLength(); i++) {
524 String attr_name = new_attributes.getLocalName(i);
525 if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
526 String value = new_attributes.getValue(i);
527 getCurrentQuery().addAttrList(value);
528 is_name_present = true;
529 } else if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE)) {
530 if (new_attributes.getValue(i).equals(MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A))
531 mode = MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A;
532 } else {
533 this.getLogger().debug("Invalid attribute match: " + attr_name);
534 throwIllegalStateException("Invalid attribute match in start attribute element");
535 }
536 }
537 }
538 if (!is_name_present) {
539 this.getLogger().debug("Do not match 'value' attribute");
540 throwIllegalStateException("Do not match 'value' attribute in start attribute element");
541 }
542 getCurrentQuery().addAttrModeVal(mode);
543 current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
544 current_value.setLength(0);
545 break;
546 case STATE_INSIDE_EXECUTE_ADD :
547 if (attributes != null && attributes.getLength() > 0) {
548 AttributesImpl new_attributes = new AttributesImpl(attributes);
549 for (int i = 0; i < new_attributes.getLength(); i++) {
550 String attr_name = new_attributes.getLocalName(i);
551 if (attr_name.equals(MAGIC_ATTRIBUTE_ELEMENT_ATTRIBUTE)) {
552 String value = new_attributes.getValue(i);
553 getCurrentQuery().addAttrList(value);
554 } else {
555 this.getLogger().debug("Invalid attribute match: " + attr_name);
556 throwIllegalStateException("Invalid attribute match in start attribute element");
557 }
558 }
559 } else {
560 this.getLogger().debug("Do not match 'value' attribute");
561 throwIllegalStateException("Do not match 'value' attribute in start attribute element");
562 }
563 current_state = LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT;
564 current_value.setLength(0);
565 break;
566 default :
567 throwIllegalStateException("Not expecting a start attribute element");
568 }
569 }
570
571 protected void endAttributeElement() {
572 switch (current_state) {
573 case LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT :
574 if ((getCurrentQuery().toDo == STATE_INSIDE_EXECUTE_REPLACE) || (getCurrentQuery().toDo == STATE_INSIDE_EXECUTE_ADD)) {
575 getCurrentQuery().addAttrVal(current_value.toString());
576 current_state = getCurrentQuery().toDo;
577 break;
578 }
579 getCurrentQuery().addAttrList(current_value.toString());
580 current_state = getCurrentQuery().toDo;
581 break;
582 default :
583 throwIllegalStateException("Not expecting a end attribute element");
584 }
585 }
586
587 protected void startInitialContextElement(Attributes attributes) {
588 switch (current_state) {
589 case STATE_INSIDE_EXECUTE_INCREMENT :
590 current_state = LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT;
591 current_value.setLength(0);
592 break;
593 case STATE_INSIDE_EXECUTE_QUERY :
594 case STATE_INSIDE_EXECUTE_ADD :
595 case STATE_INSIDE_EXECUTE_REPLACE :
596 String name = null, value = null;
597
598 if (attributes != null && attributes.getLength() > 0) {
599 name = attributes.getValue("name");
600 value = attributes.getValue("value");
601
602 if (name != null && value != null)
603 getCurrentQuery().addInitialContextValue(name, value);
604 }
605 if (name == null) {
606 this.getLogger().debug("Could not find 'name' attribute");
607 throwIllegalStateException("Could not find 'name' attribute in initial-context element");
608 }
609 if (value == null) {
610 this.getLogger().debug("Could not find 'value' attribute");
611 throwIllegalStateException("Could not find 'value' attribute in initial-context element");
612 }
613
614 current_state = LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT;
615 current_value.setLength(0);
616 break;
617 default :
618 throwIllegalStateException("Not expecting a start initial-context element");
619 }
620 }
621
622 protected void endInitialContextElement() {
623 switch (current_state) {
624 case LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT :
625 current_state = getCurrentQuery().toDo;
626 break;
627 default :
628 throwIllegalStateException("Not expecting a end initial-context element");
629 }
630 }
631
632 protected void endVersionElement() {
633 switch (current_state) {
634 case LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT :
635 getCurrentQuery().version = current_value.toString();
636 current_state = getCurrentQuery().toDo;
637 break;
638 default :
639 throwIllegalStateException("Not expecting a end version element");
640 }
641 }
642
643 protected void startFilterElement(Attributes attributes) {
644 switch (current_state) {
645 case STATE_INSIDE_EXECUTE_QUERY :
646 case STATE_INSIDE_EXECUTE_INCREMENT :
647 case STATE_INSIDE_EXECUTE_REPLACE :
648 case STATE_INSIDE_EXECUTE_ADD :
649 current_state = LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT;
650 getCurrentQuery().current_state = LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT;
651 current_value.setLength(0);
652 break;
653 default :
654 throwIllegalStateException("Not expecting a start filter element");
655 }
656 }
657
658 protected void endFilterElement() {
659 switch (current_state) {
660 case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
661 getCurrentQuery().filter = current_value.toString();
662 current_state = getCurrentQuery().toDo;
663 break;
664 default :
665 throwIllegalStateException("Not expecting a end filter element");
666 }
667 }
668
669 protected void endDerefLinkElement() {
670 switch (current_state) {
671 case LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT :
672 getCurrentQuery().deref_link = BooleanUtils.toBoolean(current_value.toString());
673 current_state = getCurrentQuery().toDo;
674 break;
675 default :
676 throwIllegalStateException("Not expecting a end deref-link element");
677 }
678 }
679
680 protected void endCountLimitElement() {
681 switch (current_state) {
682 case LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT :
683 getCurrentQuery().count_limit = Integer.parseInt(current_value.toString());
684 current_state = getCurrentQuery().toDo;
685 break;
686 default :
687 throwIllegalStateException("Not expecting a end count-limit element");
688 }
689 }
690
691 protected void endTimeLimitElement() {
692 switch (current_state) {
693 case LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT :
694 getCurrentQuery().time_limit = Integer.parseInt(current_value.toString());
695 current_state = getCurrentQuery().toDo;
696 break;
697 default :
698 throwIllegalStateException("Not expecting a end time-limit element");
699 }
700 }
701
702 protected void endDebugElement() {
703 switch (current_state) {
704 case LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT :
705 getCurrentQuery().debug = BooleanUtils.toBoolean(current_value.toString());
706 current_state = getCurrentQuery().toDo;
707 break;
708 default :
709 throwIllegalStateException("Not expecting a end debug element");
710 }
711 }
712
713 protected LDAPQuery getCurrentQuery() {
714 return (LDAPQuery)queries.elementAt(current_query_index);
715 }
716
717 protected LDAPQuery getQuery(int i) {
718 return (LDAPQuery)queries.elementAt(i);
719 }
720
721 /** END my very own methods */
722
723 /** BEGIN SAX ContentHandler handlers */
724
725 public void setDocumentLocator(Locator locator) {
726 if (getLogger().isDebugEnabled()) {
727 getLogger().debug("PUBLIC ID: " + locator.getPublicId());
728 getLogger().debug("SYSTEM ID: " + locator.getSystemId());
729 }
730 if (super.contentHandler != null)
731 super.contentHandler.setDocumentLocator(locator);
732 }
733
734 public void startElement(String uri, String name, String raw, Attributes attributes) throws SAXException {
735 if (uri == null || !uri.equals(my_uri)) {
736 super.startElement(uri, name, raw, attributes);
737 return;
738 }
739 getLogger().debug("RECEIVED START ELEMENT " + name + "(" + uri + ")");
740
741 if (name.equals(LDAPTransformer.MAGIC_EXECUTE_QUERY)) {
742 startExecuteElement();
743 } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_INCREMENT)) {
744 startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT, name);
745 } else if (name.equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
746 startQueryParameterElement(LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT, name);
747 } else if (name.equals(LDAPTransformer.MAGIC_INITIAL_CONTEXT_ELEMENT)) {
748 startInitialContextElement(attributes);
749 } else if (name.equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
750 startQueryParameterElement(LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT, name);
751 } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
752 startQueryParameterElement(LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT, name);
753 } else if (name.equals(LDAPTransformer.MAGIC_VERSION_ELEMENT)) {
754 startParameterElement(LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT, name);
755 } else if (name.equals(LDAPTransformer.MAGIC_SERVERURL_ELEMENT)) {
756 startParameterElement(LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT, name);
757 } else if (name.equals(LDAPTransformer.MAGIC_PORT_ELEMENT)) {
758 startParameterElement(LDAPTransformer.STATE_INSIDE_PORT_ELEMENT, name);
759 } else if (name.equals(LDAPTransformer.MAGIC_DOC_ELEMENT)) {
760 startParameterElement(LDAPTransformer.STATE_INSIDE_DOC_ELEMENT, name);
761 } else if (name.equals(LDAPTransformer.MAGIC_ROW_ELEMENT)) {
762 startParameterElement(LDAPTransformer.STATE_INSIDE_ROW_ELEMENT, name);
763 } else if (name.equals(LDAPTransformer.MAGIC_DN_ATTRIBUTE)) {
764 startParameterElement(LDAPTransformer.STATE_INSIDE_DN_ATTRIBUTE, name);
765 } else if (name.equals(LDAPTransformer.MAGIC_ERROR_ELEMENT)) {
766 startParameterElement(LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT, name);
767 } else if (name.equals(LDAPTransformer.MAGIC_SAX_ERROR)) {
768 startParameterElement(LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT, name);
769 } else if (name.equals(LDAPTransformer.MAGIC_ROOT_DN_ELEMENT)) {
770 startParameterElement(LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT, name);
771 } else if (name.equals(LDAPTransformer.MAGIC_PASSWORD_ELEMENT)) {
772 startParameterElement(LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT, name);
773 } else if (name.equals(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT)) {
774 startAttributeElement(attributes);
775 } else if (name.equals(LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT)) {
776 startShowAttributeElement(attributes);
777 } else if (name.equals(LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT)) {
778 startParameterElement(LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT, name);
779 } else if (name.equals(LDAPTransformer.MAGIC_FILTER_ELEMENT)) {
780 startFilterElement(attributes);
781 } else if (name.equals(LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT)) {
782 startParameterElement(LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT, name);
783 } else if (name.equals(LDAPTransformer.MAGIC_COUNT_LIMIT_ELEMENT)) {
784 startParameterElement(LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT, name);
785 } else if (name.equals(LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT)) {
786 startParameterElement(LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT, name);
787 } else if (name.equals(LDAPTransformer.MAGIC_DEBUG_ELEMENT)) {
788 startParameterElement(LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT, name);
789 } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_REPLACE)) {
790 startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE, LDAPTransformer.MAGIC_EXECUTE_REPLACE);
791 } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_ADD)) {
792 startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_ADD, LDAPTransformer.MAGIC_EXECUTE_ADD);
793 }
794 }
795
796 public void endElement(String uri, String name, String raw) throws SAXException {
797 if (!uri.equals(my_uri)) {
798 super.endElement(uri, name, raw);
799 return;
800 }
801 if (getLogger().isDebugEnabled()) {
802 getLogger().debug("RECEIVED END ELEMENT " + name + "(" + uri + ")");
803 }
804
805 if (name.equals(LDAPTransformer.MAGIC_EXECUTE_QUERY)) {
806 endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY, LDAPTransformer.MAGIC_EXECUTE_QUERY);
807 } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_INCREMENT)) {
808 endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT, LDAPTransformer.MAGIC_EXECUTE_INCREMENT);
809 } else if (name.equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
810 endInitializerElement();
811 } else if (name.equals(LDAPTransformer.MAGIC_INITIAL_CONTEXT_ELEMENT)) {
812 endInitialContextElement();
813 } else if (name.equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
814 endAuthenticationElement();
815 } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
816 endScopeElement();
817 } else if (name.equals(LDAPTransformer.MAGIC_VERSION_ELEMENT)) {
818 endVersionElement();
819 } else if (name.equals(LDAPTransformer.MAGIC_SERVERURL_ELEMENT)) {
820 endServerurlElement();
821 } else if (name.equals(LDAPTransformer.MAGIC_PORT_ELEMENT)) {
822 endPortElement();
823 } else if (name.equals(LDAPTransformer.MAGIC_DOC_ELEMENT)) {
824 endDocElement();
825 } else if (name.equals(LDAPTransformer.MAGIC_ROW_ELEMENT)) {
826 endRowElement();
827 } else if (name.equals(LDAPTransformer.MAGIC_DN_ATTRIBUTE)) {
828 endDnAttribute();
829 } else if (name.equals(LDAPTransformer.MAGIC_ERROR_ELEMENT)) {
830 endErrorElement();
831 } else if (name.equals(LDAPTransformer.MAGIC_SAX_ERROR)) {
832 endSaxError();
833 } else if (name.equals(LDAPTransformer.MAGIC_ROOT_DN_ELEMENT)) {
834 endRootDnElement();
835 } else if (name.equals(LDAPTransformer.MAGIC_PASSWORD_ELEMENT)) {
836 endPasswordElement();
837 } else if (name.equals(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT)) {
838 endAttributeElement();
839 } else if (name.equals(LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT)) {
840 endShowAttributeElement();
841 } else if (name.equals(LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT)) {
842 endSearchbaseElement();
843 } else if (name.equals(LDAPTransformer.MAGIC_FILTER_ELEMENT)) {
844 endFilterElement();
845 } else if (name.equals(LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT)) {
846 endDerefLinkElement();
847 } else if (name.equals(LDAPTransformer.MAGIC_COUNT_LIMIT_ELEMENT)) {
848 endCountLimitElement();
849 } else if (name.equals(LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT)) {
850 endTimeLimitElement();
851 } else if (name.equals(LDAPTransformer.MAGIC_DEBUG_ELEMENT)) {
852 endDebugElement();
853 } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_REPLACE)) {
854 endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE, LDAPTransformer.MAGIC_EXECUTE_REPLACE);
855 } else if (name.equals(LDAPTransformer.MAGIC_EXECUTE_ADD)) {
856 endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_ADD, LDAPTransformer.MAGIC_EXECUTE_ADD);
857 }
858 }
859
860 public void characters(char ary[], int start, int length) throws SAXException {
861 if (current_state != LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT
862 && current_state != LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT
863 && current_state != LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT
864 && current_state != LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT
865 && current_state != LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT
866 && current_state != LDAPTransformer.STATE_INSIDE_SERVERURL_ELEMENT
867 && current_state != LDAPTransformer.STATE_INSIDE_PORT_ELEMENT
868 && current_state != LDAPTransformer.STATE_INSIDE_DOC_ELEMENT
869 && current_state != LDAPTransformer.STATE_INSIDE_ROW_ELEMENT
870 && current_state != LDAPTransformer.STATE_INSIDE_DN_ATTRIBUTE
871 && current_state != LDAPTransformer.STATE_INSIDE_ERROR_ELEMENT
872 && current_state != LDAPTransformer.STATE_INSIDE_SAX_ERROR_ELEMENT
873 && current_state != LDAPTransformer.STATE_INSIDE_ROOT_DN_ELEMENT
874 && current_state != LDAPTransformer.STATE_INSIDE_PASSWORD_ELEMENT
875 && current_state != LDAPTransformer.STATE_INSIDE_ATTRIBUTE_ELEMENT
876 && current_state != LDAPTransformer.STATE_INSIDE_SHOW_ATTRIBUTE_ELEMENT
877 && current_state != LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT
878 && current_state != LDAPTransformer.STATE_INSIDE_DEREF_LINK_ELEMENT
879 && current_state != LDAPTransformer.STATE_INSIDE_COUNT_LIMIT_ELEMENT
880 && current_state != LDAPTransformer.STATE_INSIDE_TIME_LIMIT_ELEMENT
881 && current_state != LDAPTransformer.STATE_INSIDE_DEBUG_ELEMENT
882 && current_state != LDAPTransformer.STATE_INSIDE_SEARCHBASE_ELEMENT) {
883 super.characters(ary, start, length);
884 }
885 if (getLogger().isDebugEnabled()) {
886 getLogger().debug("RECEIVED CHARACTERS: " + new String(ary, start, length));
887 }
888 current_value.append(ary, start, length);
889 }
890
891 protected void start(String name, AttributesImpl attr) throws SAXException {
892 super.contentHandler.startElement("", name, name, attr);
893 attr.clear();
894 }
895
896 protected void end(String name) throws SAXException {
897 super.contentHandler.endElement("", name, name);
898 }
899
900 protected void data(String data) throws SAXException {
901 if (data != null) {
902 super.contentHandler.characters(data.toCharArray(), 0, data.length());
903 }
904 }
905
906 protected static String getStringValue(Object object) {
907 return ObjectUtils.toString(object);
908 }
909
910 public final Logger getTheLogger() {
911 return getLogger();
912 }
913
914 static class LDAPQuery {
915
916 /** What index are you in daddy's queries list */
917 protected int query_index;
918
919 /** The current state of the event receiving FSM */
920 protected int current_state;
921
922 /** Who's your daddy? */
923 protected LDAPTransformer transformer;
924
925 /** LDAP configuration information */
926 protected String initializer;
927 protected String serverurl;
928 protected int port;
929 protected String root_dn;
930 protected String password;
931 protected String version;
932 protected String scope;
933 protected String authentication;
934 private final static String LDAP_ENCODING = "ISO-8859-1";
935 protected String encoding;
936
937 /** LDAP environment information */
938 protected Properties env = new Properties();
939 protected DirContext ctx;
940
941 /** LDAP Query */
942 protected int toDo;
943 protected String searchbase;
944 protected List attrModeVal = new LinkedList();
945 protected List attrListe = new LinkedList();
946 protected List attrVale = new LinkedList();
947 protected String REPLACE_MODE_DEFAULT = StringUtils.defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT);
948 protected String REPLACE_MODE_APPEND = StringUtils.defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A);
949 protected Map initialContextValues = new HashMap();
950 protected boolean showAttribute;
951 protected String filter;
952 protected String doc_element;
953 protected String exec_element = "exec-element";
954 protected String row_element;
955 protected String dn_attribute;
956 protected String error_element;
957 protected boolean sax_error;
958 protected boolean deref_link; // Dereference: true -> dereference the link during search
959 protected long count_limit; // Maximum number of entries to return: 0 -> no limit
960 protected int time_limit; // Number of milliseconds to wait before return: 0 -> infinite
961 protected boolean debug;
962
963 protected LDAPQuery(LDAPTransformer transformer) {
964 this.transformer = transformer;
965 initializer = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_INITIALIZER_ELEMENT, "com.sun.jndi.ldap.LdapCtxFactory");
966 serverurl = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_SERVERURL_ELEMENT, "localhost");
967 port = MapUtils.getIntValue(transformer.default_properties, LDAPTransformer.MAGIC_PORT_ELEMENT, 389);
968 root_dn = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ROOT_DN_ELEMENT, "");
969 password = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_PASSWORD_ELEMENT, "");
970 version = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_VERSION_ELEMENT, "2");
971 scope = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_SCOPE_ELEMENT, "ONELEVEL_SCOPE");
972 authentication = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT, "simple");
973 searchbase = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_SEARCHBASE_ELEMENT, "");
974 showAttribute = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_SHOW_ATTRIBUTE_ELEMENT, true);
975 doc_element = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_DOC_ELEMENT, "doc-element");
976 row_element = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ROW_ELEMENT, "row-element");
977 dn_attribute = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_DN_ATTRIBUTE, "");
978 error_element = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ERROR_ELEMENT, "ldap-error");
979 sax_error = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_SAX_ERROR);
980 deref_link = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT);
981 count_limit = MapUtils.getLongValue(transformer.default_properties, LDAPTransformer.MAGIC_DEREF_LINK_ELEMENT);
982 time_limit = MapUtils.getIntValue(transformer.default_properties, LDAPTransformer.MAGIC_TIME_LIMIT_ELEMENT);
983 debug = MapUtils.getBooleanValue(transformer.default_properties, LDAPTransformer.MAGIC_DEBUG_ELEMENT);
984 encoding = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_ENCODING_ELEMENT, LDAP_ENCODING);
985 filter = MapUtils.getString(transformer.default_properties, LDAPTransformer.MAGIC_FILTER_ELEMENT, "");
986 }
987
988 /** shared constraints initialization */
989 protected void initConstraints(SearchControls constraints) {
990 if (scope.equals("OBJECT_SCOPE")) {
991 constraints.setSearchScope(SearchControls.OBJECT_SCOPE);
992 } else if (scope.equals("SUBTREE_SCOPE")) {
993 constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
994 } else {
995 constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
996 }
997 constraints.setTimeLimit(time_limit);
998 constraints.setDerefLinkFlag(deref_link);
999 constraints.setCountLimit(count_limit);
1000 }
1001
1002 protected void execute() throws Exception, NamingException {
1003 String[] attrList = new String[attrListe.size()];
1004
1005 AttributesImpl attr = new AttributesImpl();
1006 if (debug) {
1007 debugPrint();
1008 }
1009 SearchControls constraints = new SearchControls();
1010 attrListe.toArray(attrList);
1011 attrListe.clear();
1012 try {
1013 connect();
1014 switch (toDo) {
1015 case LDAPTransformer.STATE_INSIDE_EXECUTE_QUERY :
1016 try {
1017 initConstraints(constraints);
1018 if (attrList.length > 0) {
1019 constraints.setReturningAttributes(attrList);
1020 }
1021
1022 if (!filter.equals("")) {
1023 //filter is present
1024 if (!doc_element.equals("")) {
1025 transformer.start(doc_element, attr);
1026 }
1027 NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
1028
1029 while (ldapresults != null && ldapresults.hasMore()) {
1030 SearchResult si = (SearchResult) ldapresults.next();
1031 if (!row_element.equals("")) {
1032 if(!"".equals(dn_attribute)) {
1033 String dn;
1034 if(!"".equals(searchbase))
1035 dn = si.getName() + ',' + searchbase;
1036 else
1037 dn = si.getName();
1038 attr.addAttribute(null, dn_attribute, dn_attribute, "CDATA", dn);
1039 }
1040 transformer.start(row_element, attr);
1041 }
1042 javax.naming.directory.Attributes attrs = si.getAttributes();
1043 if (attrs != null) {
1044 NamingEnumeration ae = attrs.getAll();
1045 while (ae.hasMoreElements()) {
1046 Attribute at = (Attribute) ae.next();
1047 Enumeration vals = at.getAll();
1048 String attrID = at.getID();
1049 while (vals.hasMoreElements()) {
1050 if (showAttribute) {
1051 transformer.start(attrID, attr);
1052 }
1053 String attrVal = recodeFromLDAPEncoding((String) vals.nextElement());
1054 if (query_index > 0) {
1055 switch (transformer.getQuery(query_index - 1).current_state) {
1056 case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
1057 StringBuffer temp = new StringBuffer(transformer.getQuery(query_index - 1).filter);
1058 if (temp.length() > 0) {
1059 temp.append(", ");
1060 }
1061 temp.append(attrID).append("=").append(attrVal);
1062 transformer.getQuery(query_index - 1).filter = temp.toString();
1063 break;
1064 default :
1065 transformer.start(attrID, attr);
1066 }
1067 } else {
1068 transformer.data(String.valueOf(attrVal));
1069 }
1070 if (showAttribute) {
1071 transformer.end(attrID);
1072 }
1073 }
1074 }
1075 }
1076 if (!row_element.equals("")) {
1077 transformer.end(row_element);
1078 }
1079 }
1080 if (!doc_element.equals("")) {
1081 transformer.end(doc_element);
1082 }
1083 } else {
1084 //filter not present, get the values from absolete path
1085 javax.naming.directory.Attributes attrs = ctx.getAttributes(searchbase, attrList);
1086 if (!doc_element.equals("")) {
1087 transformer.start(doc_element, attr);
1088 }
1089 if (!row_element.equals("")) {
1090 if(!"".equals(dn_attribute)) {
1091 attr.addAttribute(null, dn_attribute, dn_attribute, "CDATA", searchbase);
1092 }
1093 transformer.start(row_element, attr);
1094 }
1095 if (attrs != null) {
1096 NamingEnumeration ae = attrs.getAll();
1097 while (ae.hasMoreElements()) {
1098 Attribute at = (Attribute) ae.next();
1099 Enumeration vals = at.getAll();
1100 String attrID = at.getID();
1101 while (vals.hasMoreElements()) {
1102 if (showAttribute) {
1103 transformer.start(attrID, attr);
1104 }
1105 String attrVal = recodeFromLDAPEncoding((String)vals.nextElement());
1106
1107 if (query_index > 0) {
1108 switch (transformer.getQuery(query_index - 1).current_state) {
1109 case LDAPTransformer.STATE_INSIDE_FILTER_ELEMENT :
1110 StringBuffer temp = new StringBuffer(transformer.getQuery(query_index - 1).filter);
1111 if (temp.length() > 0) {
1112 temp.append(", ");
1113 }
1114 temp.append(attrID).append("=").append(attrVal);
1115 transformer.getQuery(query_index - 1).filter = temp.toString();
1116 break;
1117 default :
1118 transformer.start(attrID, attr);
1119 }
1120 } else {
1121 transformer.data(String.valueOf(attrVal));
1122 }
1123 if (showAttribute) {
1124 transformer.end(attrID);
1125 }
1126 }
1127 }
1128 }
1129 if (!row_element.equals("")) {
1130 transformer.end(row_element);
1131 }
1132 if (!doc_element.equals("")) {
1133 transformer.end(doc_element);
1134 }
1135 }
1136 } catch (Exception e) {
1137 if (sax_error) {
1138 throw new Exception("[LDAPTransformer] Error in LDAP-Query: " + e.toString());
1139 } else {
1140 transformer.start(error_element, attr);
1141 transformer.data("[LDAPTransformer] Error in LDAP-Query: " + e);
1142 transformer.end(error_element);
1143 transformer.getTheLogger().error("[LDAPTransformer] Exception: " + e.toString());
1144 }
1145 }
1146 break;
1147 case LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT :
1148 try {
1149 initConstraints(constraints);
1150 if (attrList.length != 1) {
1151 transformer.start(error_element, attr);
1152 transformer.data("Increment must reference exactly 1 attribute.");
1153 transformer.end(error_element);
1154 } else {
1155 constraints.setReturningAttributes(attrList);
1156 NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
1157 int attrVal = 0;
1158 String attrID = "";
1159 SearchResult si = null;
1160 while (ldapresults != null && ldapresults.hasMore()) {
1161 si = (SearchResult) ldapresults.next();
1162 javax.naming.directory.Attributes attrs = si.getAttributes();
1163 if (attrs != null) {
1164 NamingEnumeration ae = attrs.getAll();
1165 while (ae.hasMoreElements()) {
1166 Attribute at = (Attribute) ae.next();
1167 Enumeration vals = at.getAll();
1168 attrID = at.getID();
1169 attrVal = Integer.parseInt((String) vals.nextElement());
1170 }
1171 }
1172 }
1173 ++attrVal;
1174 // Specify the changes to make
1175 ModificationItem[] mods = new ModificationItem[1];
1176 // Replace the "mail" attribute with a new value
1177 mods[0] =
1178 new ModificationItem(
1179 DirContext.REPLACE_ATTRIBUTE,
1180 new BasicAttribute(attrID, Integer.toString(attrVal)));
1181 // Perform the requested modifications on the named object
1182 ctx.modifyAttributes(
1183 new StringBuffer(si.toString().substring(0, si.toString().indexOf(":")))
1184 .append(",")
1185 .append(searchbase)
1186 .toString(),
1187 mods);
1188 }
1189 } catch (Exception e) {
1190 if (sax_error) {
1191 throw new Exception("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
1192 } else {
1193 transformer.start(error_element, attr);
1194 transformer.data("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
1195 transformer.end(error_element);
1196 transformer.getTheLogger().error("[LDAPTransformer] Error incrementing an attribute: " + e.toString());
1197 }
1198 }
1199 break;
1200 /* execute modes */
1201 case LDAPTransformer.STATE_INSIDE_EXECUTE_REPLACE :
1202 try {
1203 String[] attrVal = new String[attrVale.size()];
1204 String[] attrMode = new String[attrModeVal.size()];
1205 String replaceMode = REPLACE_MODE_DEFAULT;
1206 attrVale.toArray(attrVal);
1207 attrVale.clear();
1208 attrModeVal.toArray(attrMode);
1209 attrModeVal.clear();
1210
1211 if (attrVal.length != attrList.length) {
1212 transformer.start(error_element, attr);
1213 transformer.data("Attribute values must have the some number as a names");
1214 transformer.end(error_element);
1215 break;
1216 }
1217 HashMap attrMap = new HashMap(attrVal.length);
1218 HashMap attrModeMap = new HashMap(attrMode.length);
1219
1220 for (int i = 0; i < attrVal.length; i++) {
1221 attrMap.put(attrList[i], attrVal[i]);
1222 attrModeMap.put(attrList[i], attrMode[i]);
1223 }
1224
1225 initConstraints(constraints);
1226 if (attrList.length < 1) {
1227 transformer.start(error_element, attr);
1228 transformer.data("Modify must reference 1 or more attribute.");
1229 transformer.end(error_element);
1230 } else {
1231 if (!filter.equals("")) {
1232 constraints.setReturningAttributes(attrList);
1233 NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
1234 SearchResult si = null;
1235 /* start indicate element of executing query */
1236 if (!exec_element.equals("")) {
1237 transformer.start(exec_element, attr);
1238 }
1239 while (ldapresults != null && ldapresults.hasMore()) {
1240 if (!row_element.equals("")) {
1241 transformer.start(row_element, attr);
1242 }
1243
1244 si = (SearchResult) ldapresults.next();
1245 javax.naming.directory.Attributes attrs = si.getAttributes();
1246 if (attrs != null) {
1247 NamingEnumeration ae = attrs.getAll();
1248 while (ae.hasMoreElements()) {
1249 Attribute at = (Attribute) ae.next();
1250 Enumeration vals = at.getAll();
1251 String attrID = at.getID();
1252 ModificationItem[] mods = new ModificationItem[1];
1253 replaceMode = (String) attrModeMap.get(attrID);
1254
1255 String attrValue = recodeFromLDAPEncoding((String) vals.nextElement());
1256 String newAttrValue = "";
1257 /* Check the replacing method */
1258 if (replaceMode.equals(REPLACE_MODE_DEFAULT)) {
1259 newAttrValue = (String)attrMap.get(attrID);
1260 } else if (replaceMode.equals(REPLACE_MODE_APPEND)) {
1261 newAttrValue = attrValue + (String) attrMap.get(attrID);
1262 }
1263 newAttrValue = recodeToLDAPEncoding(newAttrValue);
1264
1265 mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
1266 new BasicAttribute(attrID,newAttrValue));
1267
1268 // Perform the requested modifications on the named object
1269 ctx.modifyAttributes(
1270 si.toString().substring(0, si.toString().indexOf(":")) + "," + searchbase,
1271 mods);
1272
1273 /* confirm of success */
1274 transformer.start(attrID, attr);
1275 transformer.data("replaced");
1276 transformer.end(attrID);
1277 }
1278 }
1279
1280 if (!row_element.equals("")) {
1281 transformer.end(row_element);
1282 }
1283
1284 }
1285 if (!exec_element.equals("")) {
1286 transformer.end(exec_element);
1287 }
1288 } else {
1289 //filter is not present
1290 javax.naming.directory.Attributes attrs = ctx.getAttributes(searchbase, attrList);
1291 /* start indicate element of executing query */
1292 if (!exec_element.equals("")) {
1293 transformer.start(exec_element, attr);
1294 }
1295 if (!row_element.equals("")) {
1296 transformer.start(row_element, attr);
1297 }
1298 if (attrs != null) {
1299 NamingEnumeration ae = attrs.getAll();
1300 while (ae.hasMoreElements()) {
1301 Attribute at = (Attribute) ae.next();
1302 Enumeration vals = at.getAll();
1303 String attrID = at.getID();
1304 ModificationItem[] mods = new ModificationItem[1];
1305 replaceMode = (String) attrModeMap.get(attrID);
1306
1307 String attrValue = recodeFromLDAPEncoding((String) vals.nextElement());
1308
1309 String newAttrValue = "";
1310 /* Check the replacing method */
1311 if (replaceMode.equals(REPLACE_MODE_DEFAULT)) {
1312 newAttrValue = (String) attrMap.get(attrID);
1313 } else if (replaceMode.equals(REPLACE_MODE_APPEND)) {
1314 newAttrValue = attrValue + (String) attrMap.get(attrID);
1315 }
1316 newAttrValue = recodeToLDAPEncoding(newAttrValue);
1317
1318 mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
1319 new BasicAttribute(attrID, newAttrValue));
1320
1321 // Perform the requested modifications on the named object
1322 ctx.modifyAttributes(searchbase, mods);
1323
1324 /* confirm of success */
1325 transformer.start(attrID, attr);
1326 transformer.data("replaced");
1327 transformer.end(attrID);
1328 }
1329 }
1330
1331 if (!row_element.equals("")) {
1332 transformer.end(row_element);
1333 }
1334
1335 /* end indicate element of executing query */
1336 if (!exec_element.equals("")) {
1337 transformer.end(exec_element);
1338 }
1339 }
1340 }
1341
1342 } catch (Exception e) {
1343 if (sax_error) {
1344 throw new Exception("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1345 } else {
1346 transformer.start(error_element, attr);
1347 transformer.data("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1348 transformer.end(error_element);
1349 transformer.getTheLogger().error("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1350 if (!row_element.equals("")) {
1351 transformer.end(row_element);
1352 }
1353 if (!exec_element.equals("")) {
1354 transformer.end(exec_element);
1355 }
1356 }
1357 }
1358 break;
1359 case LDAPTransformer.STATE_INSIDE_EXECUTE_ADD :
1360 try {
1361 String[] attrVal = new String[attrVale.size()];
1362 attrVale.toArray(attrVal);
1363 attrVale.clear();
1364 if (attrVal.length != attrList.length) {
1365 transformer.start(error_element, attr);
1366 transformer.data("Attribute values must have the some number as a names");
1367 transformer.end(error_element);
1368 break;
1369 }
1370 HashMap attrMap = new HashMap(attrVal.length);
1371
1372 for (int i = 0; i < attrVal.length; i++)
1373 attrMap.put(attrList[i], attrVal[i]);
1374
1375 initConstraints(constraints);
1376 if (attrList.length < 1) {
1377 transformer.start(error_element, attr);
1378 transformer.data("Modify must reference 1 or more attribute.");
1379 transformer.end(error_element);
1380 } else {
1381 if (!filter.equals("")) {
1382 constraints.setReturningAttributes(attrList);
1383 NamingEnumeration ldapresults = ctx.search(searchbase, filter, constraints);
1384 SearchResult si = null;
1385 /* start indicate element of executing query */
1386 if (!exec_element.equals("")) {
1387 transformer.start(exec_element, attr);
1388 }
1389 while (ldapresults != null && ldapresults.hasMore()) {
1390 if (!row_element.equals("")) {
1391 transformer.start(row_element, attr);
1392 }
1393
1394 si = (SearchResult) ldapresults.next();
1395 javax.naming.directory.Attributes attrs = si.getAttributes();
1396 if (attrs != null) {
1397 /* Replace the attribute if attribute already exist */
1398 NamingEnumeration ae = attrs.getAll();
1399 while (ae.hasMoreElements()) {
1400 Attribute at = (Attribute) ae.next();
1401 String attrID = at.getID();
1402 // Specify the changes to make
1403 ModificationItem[] mods = new ModificationItem[1];
1404
1405 String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrID));
1406 mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
1407 new BasicAttribute(attrID, attrValue));
1408 // Perform the requested modifications on the named object
1409 ctx.modifyAttributes(
1410 new StringBuffer(si.toString().substring(0, si.toString().indexOf(":")))
1411 .append(",")
1412 .append(searchbase)
1413 .toString(),
1414 mods);
1415
1416 /* confirm of success */
1417 transformer.start(attrID, attr);
1418 transformer.data("replaced");
1419 transformer.end(attrID);
1420 /* Remove the attribute from map after replacing */
1421 attrMap.remove(attrID);
1422 }
1423 }
1424 /* Add the attributes */
1425 if (!attrMap.isEmpty()) {
1426 ModificationItem[] mods = new ModificationItem[1];
1427 for (int i = 0; i < attrList.length; i++) {
1428 if (attrMap.containsKey(attrList[i])) {
1429 String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrList[i]));
1430 mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
1431 new BasicAttribute(attrList[i], attrValue));
1432 // Perform the requested modifications on the named object
1433 ctx.modifyAttributes(
1434 new StringBuffer(si.toString().substring(0, si.toString().indexOf(":")))
1435 .append(",")
1436 .append(searchbase)
1437 .toString(),
1438 mods);
1439
1440 /* confirm of success */
1441 transformer.start(attrList[i], attr);
1442 transformer.data("add");
1443 transformer.end(attrList[i]);
1444 }
1445 }
1446 }
1447 if (!row_element.equals("")) {
1448 transformer.end(row_element);
1449 }
1450 }
1451 if (!exec_element.equals("")) {
1452 transformer.end(exec_element);
1453 }
1454 } else {
1455 //filter is not present
1456 javax.naming.directory.Attributes attrs = ctx.getAttributes(searchbase, attrList);
1457 /* start indicate element of executing query */
1458 if (!exec_element.equals("")) {
1459 transformer.start(exec_element, attr);
1460 }
1461 if (!row_element.equals("")) {
1462 transformer.start(row_element, attr);
1463 }
1464
1465 if (attrs != null) {
1466 NamingEnumeration ae = attrs.getAll();
1467 while (ae.hasMoreElements()) {
1468 Attribute at = (Attribute) ae.next();
1469 String attrID = at.getID();
1470 // Specify the changes to make
1471 ModificationItem[] mods = new ModificationItem[1];
1472
1473 String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrID));
1474 mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
1475 new BasicAttribute(attrID, attrValue));
1476 // Perform the requested modifications on the named object
1477 ctx.modifyAttributes(searchbase, mods);
1478
1479 /* confirm of success */
1480 transformer.start(attrID, attr);
1481 transformer.data("replaced");
1482 transformer.end(attrID);
1483 /* Remove the attribute from map after replacing */
1484 attrMap.remove(attrID);
1485 }
1486 }
1487 /* Add the attributes */
1488 if (!attrMap.isEmpty()) {
1489 ModificationItem[] mods = new ModificationItem[1];
1490 for (int i = 0; i < attrList.length; i++) {
1491 if (attrMap.containsKey(attrList[i])) {
1492 String attrValue = recodeToLDAPEncoding((String)attrMap.get(attrList[i]));
1493 mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
1494 new BasicAttribute(attrList[i], attrValue));
1495 // Perform the requested modifications on the named object
1496 ctx.modifyAttributes(searchbase, mods);
1497 /* confirm of success */
1498 transformer.start(attrList[i], attr);
1499 transformer.data("add");
1500 transformer.end(attrList[i]);
1501 }
1502 }
1503 }
1504 if (!row_element.equals("")) {
1505 transformer.end(row_element);
1506 }
1507 /* end indicate element of executing query */
1508 if (!exec_element.equals("")) {
1509 transformer.end(exec_element);
1510 }
1511 }
1512 }
1513 } catch (Exception e) {
1514 if (sax_error) {
1515 throw new Exception("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1516 } else {
1517 transformer.start(error_element, attr);
1518 transformer.data("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1519 transformer.end(error_element);
1520 transformer.getTheLogger().error("[LDAPTransformer] Error replacing an attribute: " + e.toString());
1521 if (!row_element.equals("")) {
1522 transformer.end(row_element);
1523 }
1524 if (!exec_element.equals("")) {
1525 transformer.end(exec_element);
1526 }
1527 }
1528 }
1529 break;
1530 default :
1531 } //end switch
1532 } catch (NamingException e) {
1533 if (sax_error) {
1534 throw new NamingException("[LDAPTransformer] Failed ldap-connection to directory service: " + e.toString());
1535 } else {
1536 transformer.start(error_element, attr);
1537 transformer.data("[LDAPTransformer] Failed ldap-connection to directory service.");
1538 transformer.end(error_element);
1539 transformer.getTheLogger().error("[LDAPTransformer] Failed to connect to " + serverurl + e.toString());
1540 }
1541 }
1542 try {
1543 disconnect();
1544 } catch (NamingException e) {
1545 if (sax_error) {
1546 throw new NamingException("[LDAPTransformer] Failed ldap-disconnection from directory service: " + e.toString());
1547 } else {
1548 transformer.start(error_element, attr);
1549 transformer.data("[LDAPTransformer] Failed ldap-disconnection to directory service.");
1550 transformer.end(error_element);
1551 transformer.getTheLogger().error("[LDAPTransformer] Failed to disconnect from " + serverurl + e.toString());
1552 }
1553 }
1554 }
1555
1556 protected void addAttrList(String attr) {
1557 attrListe.add(attr);
1558 }
1559
1560 protected void addAttrModeVal(String mode) {
1561 attrModeVal.add(mode);
1562 }
1563
1564 protected void addAttrVal(String val) {
1565 attrVale.add(val);
1566 }
1567
1568 protected void addInitialContextValue(String name, String value)
1569 {
1570 initialContextValues.put(name, value);
1571 }
1572
1573 protected void connect() throws NamingException {
1574 if (root_dn != null && password != null) {
1575 env.put(Context.SECURITY_AUTHENTICATION, authentication);
1576 env.put(Context.SECURITY_PRINCIPAL, root_dn);
1577 env.put(Context.SECURITY_CREDENTIALS, password);
1578 }
1579
1580 env.put("java.naming.ldap.version", version);
1581 env.put(Context.INITIAL_CONTEXT_FACTORY, initializer);
1582 env.put(Context.PROVIDER_URL, serverurl + ":" + port);
1583
1584 // Override existing properties or add new properties with values from initial-context
1585 env.putAll(initialContextValues);
1586
1587 try {
1588 ctx = new InitialDirContext(env);
1589 } catch (NamingException e) {
1590 env.clear();
1591 throw new NamingException(e.toString());
1592 }
1593 }
1594
1595 protected void disconnect() throws NamingException {
1596 try {
1597 if (ctx != null)
1598 ctx.close();
1599 } catch (NamingException e) {
1600 ctx = null;
1601 env.clear();
1602 throw new NamingException(e.toString());
1603 }
1604 ctx = null;
1605 env.clear();
1606 }
1607
1608 protected void debugPrint() {
1609 Logger logger = transformer.getTheLogger();
1610 if (logger.isDebugEnabled()) {
1611 logger.debug("[LDAPTransformer] query_index: " + query_index);
1612 logger.debug("[LDAPTransformer] current_state: " + current_state);
1613 logger.debug("[LDAPTransformer] serverurl: " + serverurl);
1614 logger.debug("[LDAPTransformer] port: " + port);
1615 logger.debug("[LDAPTransformer] root_dn: " + root_dn);
1616 logger.debug("[LDAPTransformer] password: " + password);
1617 logger.debug("[LDAPTransformer] version: " + version);
1618 logger.debug("[LDAPTransformer] scope: " + scope);
1619 logger.debug("[LDAPTransformer] authentication: " + authentication);
1620 logger.debug("[LDAPTransformer] toDo: " + toDo);
1621 logger.debug("[LDAPTransformer] searchbase: " + searchbase);
1622 logger.debug("[LDAPTransformer] showAttribute: " + showAttribute);
1623 logger.debug("[LDAPTransformer] attribute: " + attrListe.toString());
1624 logger.debug("[LDAPTransformer] initial-context: " + initialContextValues);
1625 logger.debug("[LDAPTransformer] filter: " + filter);
1626 logger.debug("[LDAPTransformer] doc_element: " + doc_element);
1627 logger.debug("[LDAPTransformer] row_element: " + row_element);
1628 logger.debug("[LDAPTransformer] error_element: " + error_element);
1629 logger.debug("[LDAPTransformer] sax-error: " + sax_error);
1630 logger.debug("[LDAPTransformer] deref_link: " + deref_link);
1631 logger.debug("[LDAPTransformer] count_limit: " + count_limit);
1632 logger.debug("[LDAPTransformer] time_limit: " + time_limit);
1633 }
1634 }
1635
1636 /**
1637 * Recodes a String value from {@link #LDAP_ENCODING} to specified {@link #encoding}.
1638 * @param value the String to recode
1639 * @return the recoded String
1640 * @throws UnsupportedEncodingException if either the used encoding
1641 */
1642 private String recodeFromLDAPEncoding(String value) throws UnsupportedEncodingException {
1643 if (!LDAP_ENCODING.equals(encoding)) {
1644 value = new String(value.getBytes(LDAP_ENCODING), encoding);
1645 }
1646 return value;
1647 }
1648
1649 /**
1650 * Recodes a String value from specified {@link #encoding} to {@link #LDAP_ENCODING}.
1651 * @param value the String to recode
1652 * @return the recoded String
1653 * @throws UnsupportedEncodingException if either the used encoding
1654 */
1655 private String recodeToLDAPEncoding(String value) throws UnsupportedEncodingException {
1656 if (!LDAP_ENCODING.equals(encoding)) {
1657 value = new String(value.getBytes(encoding), LDAP_ENCODING);
1658 }
1659 return value;
1660 }
1661 }
1662 }