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