From 3c24b4da66300967e62fc75cbfca1f2e03ca7754 Mon Sep 17 00:00:00 2001
From: treydrake <treydrake@localhost>
Date: Fri, 16 Feb 2007 06:18:16 +0000
Subject: [PATCH] Issue #1233 - LDAP server configuration is now defined in the standard fashion (via web.xml) vs the ldap.properties file 

---
 opends/resource/dsml/webapp/web.xml                       |   36 ++++--
 opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java |  244 ++++++++++++++++++------------------------------
 2 files changed, 113 insertions(+), 167 deletions(-)

diff --git a/opends/resource/dsml/webapp/web.xml b/opends/resource/dsml/webapp/web.xml
index 42ba0f4..0ab7ce7 100644
--- a/opends/resource/dsml/webapp/web.xml
+++ b/opends/resource/dsml/webapp/web.xml
@@ -1,20 +1,30 @@
 <?xml version="1.0" encoding="ISO-8859-1"?>
 
-<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
+<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
+         version="2.4">
 
-    <servlet>
-        <servlet-name>DSMLServlet</servlet-name>
-        <servlet-class>org.opends.dsml.protocol.DSMLServlet</servlet-class>
-        <init-param>
-          <param-name>config-file</param-name>
-          <param-value>/server.properties</param-value>
-        </init-param>
-    </servlet>
+  <context-param>
+    <description>The hostname or IP address of the OpenDS server; e.g., localhost</description>
+    <param-name>ldap.host</param-name>
+    <param-value>localhost</param-value>
+  </context-param>
+  
+  <context-param>
+    <description>The port number of the OpenDS server; e.g., 389</description>
+    <param-name>ldap.port</param-name>
+    <param-value>389</param-value>
+  </context-param>
 
-    <servlet-mapping>
-        <servlet-name>DSMLServlet</servlet-name>
-        <url-pattern>/DSMLServlet</url-pattern>
-    </servlet-mapping>
+  <servlet>
+    <servlet-name>DSMLServlet</servlet-name>
+    <servlet-class>org.opends.dsml.protocol.DSMLServlet</servlet-class>
+  </servlet>
+
+  <servlet-mapping>
+    <servlet-name>DSMLServlet</servlet-name>
+    <url-pattern>/DSMLServlet</url-pattern>
+  </servlet-mapping>
 
 </web-app>
 
diff --git a/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java b/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
index dd9b54a..58f1f02 100644
--- a/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
+++ b/opends/src/dsml/org/opends/dsml/protocol/DSMLServlet.java
@@ -22,21 +22,17 @@
  * CDDL HEADER END
  *
  *
- *      Portions Copyright 2006 Sun Microsystems, Inc.
+ *      Portions Copyright 2006-2007 Sun Microsystems, Inc.
  */
 package org.opends.dsml.protocol;
 
 
+import org.opends.server.core.DirectoryServer;
+import org.opends.server.tools.LDAPConnection;
+import org.opends.server.tools.LDAPConnectionOptions;
+import org.opends.server.util.Base64;
+import org.w3c.dom.Document;
 
-import java.io.InputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.URL;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Properties;
-import java.util.StringTokenizer;
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
@@ -48,26 +44,13 @@
 import javax.xml.bind.Unmarshaller;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.soap.MessageFactory;
-import javax.xml.soap.MimeHeaders;
-import javax.xml.soap.Name;
-import javax.xml.soap.SOAPBody;
-import javax.xml.soap.SOAPConstants;
-import javax.xml.soap.SOAPElement;
-import javax.xml.soap.SOAPException;
-import javax.xml.soap.SOAPFactory;
-import javax.xml.soap.SOAPFault;
-import javax.xml.soap.SOAPHeader;
-import javax.xml.soap.SOAPMessage;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-
-import org.opends.server.core.DirectoryServer;
-import org.opends.server.tools.LDAPConnection;
-import org.opends.server.tools.LDAPConnectionOptions;
-import org.opends.server.util.Base64;
-
+import javax.xml.soap.*;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+import java.util.StringTokenizer;
 
 
 /**
@@ -76,12 +59,10 @@
  * which performs the LDAP operation, and returns the response
  * as a DSML response.
  */
-public class DSMLServlet extends HttpServlet
-{
+public class DSMLServlet extends HttpServlet {
   private static final String PKG_NAME = "org.opends.dsml.protocol";
-  private static final String SERVER_PROP_RESOURCE = "/server.properties";
-  private static final String PORT = "port";
-  private static final String HOST = "host";
+  private static final String PORT = "ldap.port";
+  private static final String HOST = "ldap.host";
   private static final long serialVersionUID = -3748022009593442973L;
 
   private Unmarshaller unmarshaller;
@@ -91,43 +72,30 @@
   private DocumentBuilder db;
 
   private String hostName;
-  private int port = 389;
+  private Integer port;
 
   /**
    * This method will be called by the Servlet Container when
    * this servlet is being placed into service.
-   * @param config - the <CODE>ServletConfig</CODE> object that
-   * contains configutation information for this servlet.
    *
-   * @throws  ServletException  If an error occurs during processing.
+   * @param config - the <CODE>ServletConfig</CODE> object that
+   *               contains configutation information for this servlet.
+   * @throws ServletException If an error occurs during processing.
    */
-  public void init(ServletConfig config) throws ServletException
-  {
-    System.out.println("DSMLServlet: init()");
+  public void init(ServletConfig config) throws ServletException {
 
-    try
-    {
-      URL myURL=config.getServletContext().getResource(SERVER_PROP_RESOURCE);
-      InputStream in = myURL.openStream();
-      Properties p = new Properties();
-      p.load( in );
-      // System.out.println( p.getProperty(HOST) );
-      // System.out.println( p.getProperty(PORT) );
-      hostName = p.getProperty(HOST);
-      if(hostName == null)
-      {
-        hostName = "localhost";
-      }
+    try {
 
-      String portStr = p.getProperty(PORT);
-      port = Integer.parseInt(portStr);
+      hostName = config.getServletContext().getInitParameter(HOST);
+
+      port = new Integer(config.getServletContext().getInitParameter(PORT));
 
       JAXBContext jaxbContext = JAXBContext.newInstance(PKG_NAME);
       unmarshaller = jaxbContext.createUnmarshaller();
 
       marshaller = jaxbContext.createMarshaller();
       marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
-                             new NamespacePrefixMapperImpl());
+        new NamespacePrefixMapperImpl());
 
       objFactory = new ObjectFactory();
       messageFactory = MessageFactory.newInstance();
@@ -137,8 +105,7 @@
 
       DirectoryServer.bootstrapClient();
 
-    } catch(Exception je)
-    {
+    } catch (Exception je) {
       je.printStackTrace();
       throw new ServletException(je.getMessage());
     }
@@ -148,16 +115,13 @@
   /**
    * The HTTP GET operation.
    *
-   * @param  req  Information about the request received from the client.
-   * @param  res  Information about the response to send to the client.
-   *
-   * @throws  ServletException  If an error occurs during servlet processing.
-   *
-   * @throws  IOException  If an error occurs while interacting with the client.
+   * @param req Information about the request received from the client.
+   * @param res Information about the response to send to the client.
+   * @throws ServletException If an error occurs during servlet processing.
+   * @throws IOException      If an error occurs while interacting with the client.
    */
   public void doGet(HttpServletRequest req, HttpServletResponse res)
-  throws ServletException, IOException
-  {
+    throws ServletException, IOException {
     super.doGet(req, res);
   }
 
@@ -166,48 +130,40 @@
    * The HTTP POST operation. This servlet expects a SOAP message
    * with a DSML request payload.
    *
-   * @param  req  Information about the request received from the client.
-   * @param  res  Information about the response to send to the client.
-   *
-   * @throws  ServletException  If an error occurs during servlet processing.
-   *
-   * @throws  IOException  If an error occurs while interacting with the client.
+   * @param req Information about the request received from the client.
+   * @param res Information about the response to send to the client.
+   * @throws ServletException If an error occurs during servlet processing.
+   * @throws IOException      If an error occurs while interacting with the client.
    */
   public void doPost(HttpServletRequest req, HttpServletResponse res)
-      throws ServletException, IOException
-  {
-    SOAPMessage reply = null;
+    throws ServletException, IOException {
+    SOAPMessage reply;
     LDAPConnectionOptions connOptions = new LDAPConnectionOptions();
     LDAPConnection connection = null;
 
-    try
-    {
+    try {
       MimeHeaders mimeHeaders = new MimeHeaders();
       Enumeration en = req.getHeaderNames();
       String bindDN = "";
       String bindPassword = "";
-      while (en.hasMoreElements())
-      {
-        String headerName = (String)en.nextElement();
+      while (en.hasMoreElements()) {
+        String headerName = (String) en.nextElement();
         String headerVal = req.getHeader(headerName);
-        if(headerName.equalsIgnoreCase("authorization"))
-        {
-          if(headerVal.startsWith("Basic "))
-          {
+        if (headerName.equalsIgnoreCase("authorization")) {
+          if (headerVal.startsWith("Basic ")) {
             String authorization = headerVal.substring(6).trim();
             // Decode and parse the authorization credentials
-                  String unencoded =
-                    new String(Base64.decode(authorization));
-                  int colon = unencoded.indexOf(':');
-                  if (colon < 0)
-                    continue;
-                  bindDN = unencoded.substring(0, colon).trim();
-                  bindPassword = unencoded.substring(colon + 1);
+            String unencoded =
+              new String(Base64.decode(authorization));
+            int colon = unencoded.indexOf(':');
+            if (colon < 0)
+              continue;
+            bindDN = unencoded.substring(0, colon).trim();
+            bindPassword = unencoded.substring(colon + 1);
           }
         }
         StringTokenizer tk = new StringTokenizer(headerVal, ",");
-        while (tk.hasMoreTokens())
-        {
+        while (tk.hasMoreTokens()) {
           mimeHeaders.addHeader(headerName, tk.nextToken().trim());
         }
       }
@@ -220,11 +176,9 @@
       SOAPBody body = message.getSOAPBody();
 
       Iterator it = body.getChildElements();
-      while(it.hasNext())
-      {
+      while (it.hasNext()) {
         Object obj = it.next();
-        if(!(obj instanceof SOAPElement))
-        {
+        if (!(obj instanceof SOAPElement)) {
           continue;
         }
         SOAPElement se = (SOAPElement) obj;
@@ -236,10 +190,8 @@
         List<JAXBElement<?>> batchResponses = batchResponse.getBatchResponses();
         List<DsmlMessage> list = batchRequest.getBatchRequests();
 
-        for(DsmlMessage nextElement : list)
-        {
-          if(nextElement instanceof SearchRequest)
-          {
+        for (DsmlMessage nextElement : list) {
+          if (nextElement instanceof SearchRequest) {
             // Process the search request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
@@ -248,54 +200,49 @@
             DSMLSearchOperation ds = new DSMLSearchOperation(connection);
             SearchResponse searchResponse = ds.doSearch(objFactory, sr);
 
-                  JAXBElement<SearchResponse> searchResponseEl =
+            JAXBElement<SearchResponse> searchResponseEl =
               objFactory.createBatchResponseSearchResponse(searchResponse);
             batchResponses.add(searchResponseEl);
 
-          } else if(nextElement instanceof AddRequest)
-          {
+          } else if (nextElement instanceof AddRequest) {
             // Process the add request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
             AddRequest ar = (AddRequest) nextElement;
             DSMLAddOperation addOp = new DSMLAddOperation(connection);
             LDAPResult addResponse = addOp.doOperation(objFactory, ar);
-                  JAXBElement<LDAPResult> addResponseEl =
+            JAXBElement<LDAPResult> addResponseEl =
               objFactory.createBatchResponseAddResponse(addResponse);
             batchResponses.add(addResponseEl);
-          } else if(nextElement instanceof AbandonRequest)
-          {
+          } else if (nextElement instanceof AbandonRequest) {
             // Process the abandon request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
             AbandonRequest ar = (AbandonRequest) nextElement;
             DSMLAbandonOperation ao = new DSMLAbandonOperation(connection);
             LDAPResult abandonResponse = ao.doOperation(objFactory, ar);
-          } else if(nextElement instanceof ExtendedRequest)
-          {
+          } else if (nextElement instanceof ExtendedRequest) {
             // Process the extended request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
             ExtendedRequest er = (ExtendedRequest) nextElement;
             DSMLExtendedOperation eo = new DSMLExtendedOperation(connection);
             ExtendedResponse extendedResponse = eo.doOperation(objFactory, er);
-                  JAXBElement<ExtendedResponse> extendedResponseEl =
+            JAXBElement<ExtendedResponse> extendedResponseEl =
               objFactory.createBatchResponseExtendedResponse(extendedResponse);
             batchResponses.add(extendedResponseEl);
 
-          } else if (nextElement instanceof DelRequest)
-          {
+          } else if (nextElement instanceof DelRequest) {
             // Process the delete request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
             DelRequest dr = (DelRequest) nextElement;
             DSMLDeleteOperation delOp = new DSMLDeleteOperation(connection);
             LDAPResult delResponse = delOp.doOperation(objFactory, dr);
-                  JAXBElement<LDAPResult> delResponseEl =
+            JAXBElement<LDAPResult> delResponseEl =
               objFactory.createBatchResponseDelResponse(delResponse);
             batchResponses.add(delResponseEl);
-          } else if (nextElement instanceof CompareRequest)
-          {
+          } else if (nextElement instanceof CompareRequest) {
             // Process the compare request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
@@ -303,11 +250,10 @@
             DSMLCompareOperation compareOp =
               new DSMLCompareOperation(connection);
             LDAPResult compareResponse = compareOp.doOperation(objFactory, cr);
-                  JAXBElement<LDAPResult> compareResponseEl =
+            JAXBElement<LDAPResult> compareResponseEl =
               objFactory.createBatchResponseCompareResponse(compareResponse);
             batchResponses.add(compareResponseEl);
-          } else if (nextElement instanceof ModifyDNRequest)
-          {
+          } else if (nextElement instanceof ModifyDNRequest) {
             // Process the Modify DN request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
@@ -315,29 +261,27 @@
             DSMLModifyDNOperation moddnOp =
               new DSMLModifyDNOperation(connection);
             LDAPResult moddnResponse = moddnOp.doOperation(objFactory, mr);
-                  JAXBElement<LDAPResult> moddnResponseEl =
+            JAXBElement<LDAPResult> moddnResponseEl =
               objFactory.createBatchResponseModDNResponse(moddnResponse);
             batchResponses.add(moddnResponseEl);
-          } else if( nextElement instanceof ModifyRequest)
-          {
+          } else if (nextElement instanceof ModifyRequest) {
             // Process the Modify request.
             connection = new LDAPConnection(hostName, port, connOptions);
             connection.connectToHost(bindDN, bindPassword);
             ModifyRequest modr = (ModifyRequest) nextElement;
             DSMLModifyOperation modOp = new DSMLModifyOperation(connection);
-            LDAPResult modResponse = modOp.doOperation(objFactory,  modr);
-                  JAXBElement<LDAPResult> modResponseEl =
+            LDAPResult modResponse = modOp.doOperation(objFactory, modr);
+            JAXBElement<LDAPResult> modResponseEl =
               objFactory.createBatchResponseModifyResponse(modResponse);
             batchResponses.add(modResponseEl);
-          } else
-          {
+          } else {
             String msg = "Invalid DSML request:" + nextElement;
             throw new IOException(msg);
           }
         }
 
         JAXBElement<BatchResponse> batchResponseElement =
-         objFactory.createBatchResponse(batchResponse);
+          objFactory.createBatchResponse(batchResponse);
 
         marshaller.marshal(batchResponseElement, System.out);
 
@@ -348,19 +292,16 @@
       reply = messageFactory.createMessage();
       sendResponse(doc, false, reply, res, null);
 
-    } catch(Exception se)
-    {
+    } catch (Exception se) {
       se.printStackTrace();
       // send SOAP fault
-      try
-      {
+      try {
         reply = messageFactory.createMessage();
         sendResponse(null, true, reply, res, se);
-      } catch(Exception e) { }
-    } finally
-    {
-      if(connection != null)
-      {
+      } catch (Exception e) {
+      }
+    } finally {
+      if (connection != null) {
         connection.close();
       }
     }
@@ -370,37 +311,32 @@
    * Send a response back to the client. This could be either a SOAP fault
    * or a correct DSML response.
    *
-   * @param  doc    The document to include in the response.
-   * @param  error  Indicates whether an error occurred.
-   * @param  reply  The reply to send to the client.
-   * @param  res    Information about the HTTP response to the client.
-   * @param  e      Information about any exception that was thrown.
-   *
-   * @throws  IOException  If an error occurs while interacting with the client.
-   *
-   * @throws  SOAPException  If an encoding or decoding error occurs.
+   * @param doc   The document to include in the response.
+   * @param error Indicates whether an error occurred.
+   * @param reply The reply to send to the client.
+   * @param res   Information about the HTTP response to the client.
+   * @param e     Information about any exception that was thrown.
+   * @throws IOException   If an error occurs while interacting with the client.
+   * @throws SOAPException If an encoding or decoding error occurs.
    */
   private void sendResponse(Document doc, boolean error, SOAPMessage reply,
-    HttpServletResponse res, Exception e)
-    throws IOException, SOAPException
-  {
+                            HttpServletResponse res, Exception e)
+    throws IOException, SOAPException {
     SOAPHeader header = reply.getSOAPHeader();
     header.detachNode();
     SOAPBody replyBody = reply.getSOAPBody();
 
     res.setHeader("Content-Type", "text/xml");
 
-    if(error)
-    {
+    if (error) {
       SOAPFault fault = replyBody.addFault();
       Name faultName = SOAPFactory.newInstance().createName("Server",
-          "", SOAPConstants.URI_NS_SOAP_ENVELOPE);
+        "", SOAPConstants.URI_NS_SOAP_ENVELOPE);
       fault.setFaultCode(faultName);
       fault.setFaultString("Server Error: " + e.getMessage());
       // FIXME - Set correct fault actor
       fault.setFaultActor("http://localhost:8080");
-    } else
-    {
+    } else {
       SOAPElement bodyElement = replyBody.addDocument(doc);
     }
 

--
Gitblit v1.10.0