You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by jb...@apache.org on 2006/03/07 15:26:47 UTC

svn commit: r383900 - in /cocoon/branches/BRANCH_2_1_X: src/blocks/naming/java/org/apache/cocoon/transformation/LDAPTransformer.java status.xml

Author: jbq
Date: Tue Mar  7 06:26:18 2006
New Revision: 383900

URL: http://svn.apache.org/viewcvs?rev=383900&view=rev
Log:
Fix COCOON-1707: Allow configuration of initial context in LDAPTransformer

Patched by Sébastien Grimault <sg...@jouve.fr>

Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/naming/java/org/apache/cocoon/transformation/LDAPTransformer.java
    cocoon/branches/BRANCH_2_1_X/status.xml

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/naming/java/org/apache/cocoon/transformation/LDAPTransformer.java
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/blocks/naming/java/org/apache/cocoon/transformation/LDAPTransformer.java?rev=383900&r1=383899&r2=383900&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/naming/java/org/apache/cocoon/transformation/LDAPTransformer.java (original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/naming/java/org/apache/cocoon/transformation/LDAPTransformer.java Tue Mar  7 06:26:18 2006
@@ -65,15 +65,18 @@
  * <br>
  *
  * The following DTD is valid:<br>
- * &lt;!ELEMENT execute-query (attribute+ | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error?  doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
- * &lt;!ELEMENT execute-increment (attribute | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
+ * &lt;!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)&gt;<br>
+ * &lt;!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)&gt;<br>
  * increments (+1) an integer attribute on a directory-server (ldap)<br>
- * &lt;!ELEMENT execute-replace (attribute | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
+ * &lt;!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)&gt;<br>
  * replace attribute on a directory-server (ldap)<br>
- * &lt;!ELEMENT execute-add (attribute | show-attribute? | scope? | initializer? | authentication? | error-element? | sax-error? | doc-element? | row-element? | version? | serverurl? | rootdn? | password? | deref-link? | count-limit? | searchbase, filter)&gt;<br>
+ * &lt;!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)&gt;<br>
  * add attribute on a directory-server (ldap)<br>
  * <br>
  * &lt;!ELEMENT initializer (#PCDATA)&gt;+ (default: "com.sun.jndi.ldap.LdapCtxFactory")<br>
+ * &lt;!ELEMENT initial-context (#EMPTY)&gt;<br>
+ * &lt;!ATTLIST initial-context name CDATA #REQUIRED
+                                value CDATA #REQUIRED &gt;<br>
  * &lt;!ELEMENT authentication (#PCDATA)&gt;+ (default: "simple")<br>
  * &lt;!ELEMENT version (#PCDATA)&gt;+ (default: "2")<br>
  * &lt;!ELEMENT serverurl (#PCDATA)&gt;+<br>
@@ -101,9 +104,6 @@
  * + can also be defined as parameter in the sitemap.
  * <br>
  *
- * @author Felix Knecht
- * @author <a href="mailto:unico@hippo.nl">Unico Hommes</a>
- * @author <a href="mailto:yuryx@mobicomk.donpac.ru">Yury Mikhienko</a>
  * @version $Id$
  */
 public class LDAPTransformer extends AbstractTransformer {
@@ -131,6 +131,9 @@
     public static final String MAGIC_EXECUTE_REPLACE = "execute-replace";
     public static final String MAGIC_FILTER_ELEMENT = "filter";
     public static final String MAGIC_INITIALIZER_ELEMENT = "initializer";
+    public static final String MAGIC_INITIAL_CONTEXT_ELEMENT = "initial-context";
+    public static final String MAGIC_INITIAL_CONTEXT_NAME_ATTRIBUTE = "name";
+    public static final String MAGIC_INITIAL_CONTEXT_VALUE_ATTRIBUTE = "value";
     public static final String MAGIC_PASSWORD_ELEMENT = "password";
     public static final String MAGIC_PORT_ELEMENT = "port";
     public static final String MAGIC_ROOT_DN_ELEMENT = "rootdn";
@@ -172,6 +175,7 @@
     public static final int STATE_INSIDE_EXECUTE_REPLACE = 24;
     public static final int STATE_INSIDE_EXECUTE_ADD = 25;
     public static final int STATE_INSIDE_DN_ATTRIBUTE = 26;
+    public static final int STATE_INSIDE_INITIAL_CONTEXT_ELEMENT = 27;
 
     /** Default parameters that might apply to all queries */
     protected HashMap default_properties = new HashMap();
@@ -579,6 +583,51 @@
         }
     }
 
+    protected void startInitialContextElement(Attributes attributes) {
+        switch (current_state) {
+            case STATE_INSIDE_EXECUTE_INCREMENT :
+                current_state = LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT;
+                current_value.setLength(0);
+                break;
+            case STATE_INSIDE_EXECUTE_QUERY :
+            case STATE_INSIDE_EXECUTE_ADD :
+            case STATE_INSIDE_EXECUTE_REPLACE :
+                String name = null, value = null;
+
+                if (attributes != null && attributes.getLength() > 0) {
+                		name = attributes.getValue("name");
+                		value = attributes.getValue("value");
+
+                		if (name != null && value != null)
+                			getCurrentQuery().addInitialContextValue(name, value);
+                }
+                if (name == null) {
+                    this.getLogger().debug("Could not find 'name' attribute");
+                    throwIllegalStateException("Could not find 'name' attribute in initial-context element");
+                }
+                if (value == null) {
+                    this.getLogger().debug("Could not find 'value' attribute");
+                    throwIllegalStateException("Could not find 'value' attribute in initial-context element");
+                }
+
+                current_state = LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT;
+                current_value.setLength(0);
+                break;
+            default :
+                throwIllegalStateException("Not expecting a start initial-context element");
+        }
+    }
+	
+	protected void endInitialContextElement() {
+        switch (current_state) {
+            case LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT :
+                current_state = getCurrentQuery().toDo;
+                break;
+            default :
+                throwIllegalStateException("Not expecting a end initial-context element");
+        }
+    }
+	
     protected void endVersionElement() {
         switch (current_state) {
             case LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT :
@@ -694,6 +743,8 @@
             startExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT, name);
         } else if (name.equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
             startQueryParameterElement(LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT, name);
+        } else if (name.equals(LDAPTransformer.MAGIC_INITIAL_CONTEXT_ELEMENT)) {
+            startInitialContextElement(attributes);
         } else if (name.equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
             startQueryParameterElement(LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT, name);
         } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
@@ -756,6 +807,8 @@
             endExecuteElement(LDAPTransformer.STATE_INSIDE_EXECUTE_INCREMENT, LDAPTransformer.MAGIC_EXECUTE_INCREMENT);
         } else if (name.equals(LDAPTransformer.MAGIC_INITIALIZER_ELEMENT)) {
             endInitializerElement();
+        } else if (name.equals(LDAPTransformer.MAGIC_INITIAL_CONTEXT_ELEMENT)) {
+            endInitialContextElement();
         } else if (name.equals(LDAPTransformer.MAGIC_AUTHENTICATION_ELEMENT)) {
             endAuthenticationElement();
         } else if (name.equals(LDAPTransformer.MAGIC_SCOPE_ELEMENT)) {
@@ -805,6 +858,7 @@
 
     public void characters(char ary[], int start, int length) throws SAXException {
         if (current_state != LDAPTransformer.STATE_INSIDE_INITIALIZER_ELEMENT
+            && current_state != LDAPTransformer.STATE_INSIDE_INITIAL_CONTEXT_ELEMENT
             && current_state != LDAPTransformer.STATE_INSIDE_AUTHENTICATION_ELEMENT
             && current_state != LDAPTransformer.STATE_INSIDE_SCOPE_ELEMENT
             && current_state != LDAPTransformer.STATE_INSIDE_VERSION_ELEMENT
@@ -891,6 +945,7 @@
         protected List attrVale = new LinkedList();
         protected String REPLACE_MODE_DEFAULT = StringUtils.defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_DEFAULT);
         protected String REPLACE_MODE_APPEND = StringUtils.defaultString(LDAPTransformer.MAGIC_ATTRIBUTE_ELEMENT_MODE_ATTRIBUTE_VALUE_A);
+        protected Map initialContextValues = new HashMap();
         protected boolean showAttribute;
         protected String filter;
         protected String doc_element;
@@ -1509,6 +1564,11 @@
             attrVale.add(val);
         }
 
+        protected void addInitialContextValue(String name, String value)
+        {
+            initialContextValues.put(name, value);
+        }
+
         protected void connect() throws NamingException {
             if (root_dn != null && password != null) {
                 env.put(Context.SECURITY_AUTHENTICATION, authentication);
@@ -1520,6 +1580,9 @@
             env.put(Context.INITIAL_CONTEXT_FACTORY, initializer);
             env.put(Context.PROVIDER_URL, serverurl + ":" + port);
 
+            // Override existing properties or add new properties with values from initial-context
+            env.putAll(initialContextValues);
+
             try {
                 ctx = new InitialDirContext(env);
             } catch (NamingException e) {
@@ -1557,6 +1620,7 @@
                 logger.debug("[LDAPTransformer] searchbase: " + searchbase);
                 logger.debug("[LDAPTransformer] showAttribute: " + showAttribute);
                 logger.debug("[LDAPTransformer] attribute: " + attrListe.toString());
+                logger.debug("[LDAPTransformer] initial-context: " + initialContextValues);
                 logger.debug("[LDAPTransformer] filter: " + filter);
                 logger.debug("[LDAPTransformer] doc_element: " + doc_element);
                 logger.debug("[LDAPTransformer] row_element: " + row_element);

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/status.xml?rev=383900&r1=383899&r2=383900&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Tue Mar  7 06:26:18 2006
@@ -180,6 +180,9 @@
   <release version="@version@" date="@date@">
 -->
   <release version="2.1.9" date="TBD">
+    <action dev="JBQ" type="fix" fixes-bug="COCOON-1707" due-to="Sébastien Grimault" due-to-email="sgrimault@jouve.fr">
+      Allow configuration of initial context in LDAPTransformer
+    </action>
     <action dev="DC" type="add" fixes-bug="COCOON-1206" due-to="Miles Elam" due-to-email="miles@geekspeak.org">
        html block: Add CleanupTransformer. Enables indent html, add relevant line-breaks,
        and remove any namespaces that are not wanted.