mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

treydrake
16.18.2007 3c24b4da66300967e62fc75cbfca1f2e03ca7754
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      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 javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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;
 
 
/**
 * This class provides the entry point for the DSML request.
 * It parses the SOAP request, calls the appropriate class
 * which performs the LDAP operation, and returns the response
 * as a DSML response.
 */
public class DSMLServlet extends HttpServlet {
  private static final String PKG_NAME = "org.opends.dsml.protocol";
  private static final String PORT = "ldap.port";
  private static final String HOST = "ldap.host";
  private static final long serialVersionUID = -3748022009593442973L;
 
  private Unmarshaller unmarshaller;
  private Marshaller marshaller;
  private ObjectFactory objFactory;
  private MessageFactory messageFactory;
  private DocumentBuilder db;
 
  private String hostName;
  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.
   */
  public void init(ServletConfig config) throws ServletException {
 
    try {
 
      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());
 
      objFactory = new ObjectFactory();
      messageFactory = MessageFactory.newInstance();
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      db = dbf.newDocumentBuilder();
 
      DirectoryServer.bootstrapClient();
 
    } catch (Exception je) {
      je.printStackTrace();
      throw new ServletException(je.getMessage());
    }
  }
 
 
  /**
   * 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.
   */
  public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    super.doGet(req, res);
  }
 
 
  /**
   * 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.
   */
  public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    SOAPMessage reply;
    LDAPConnectionOptions connOptions = new LDAPConnectionOptions();
    LDAPConnection connection = null;
 
    try {
      MimeHeaders mimeHeaders = new MimeHeaders();
      Enumeration en = req.getHeaderNames();
      String bindDN = "";
      String bindPassword = "";
      while (en.hasMoreElements()) {
        String headerName = (String) en.nextElement();
        String headerVal = req.getHeader(headerName);
        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);
          }
        }
        StringTokenizer tk = new StringTokenizer(headerVal, ",");
        while (tk.hasMoreTokens()) {
          mimeHeaders.addHeader(headerName, tk.nextToken().trim());
        }
      }
 
      SOAPMessage message =
        messageFactory.createMessage(mimeHeaders, req.getInputStream());
      message.writeTo(System.out);
 
      Document doc = db.newDocument();
      SOAPBody body = message.getSOAPBody();
 
      Iterator it = body.getChildElements();
      while (it.hasNext()) {
        Object obj = it.next();
        if (!(obj instanceof SOAPElement)) {
          continue;
        }
        SOAPElement se = (SOAPElement) obj;
        JAXBElement<BatchRequest> batchRequestElement =
          unmarshaller.unmarshal(se, BatchRequest.class);
        BatchRequest batchRequest = batchRequestElement.getValue();
        BatchResponse batchResponse = objFactory.createBatchResponse();
 
        List<JAXBElement<?>> batchResponses = batchResponse.getBatchResponses();
        List<DsmlMessage> list = batchRequest.getBatchRequests();
 
        for (DsmlMessage nextElement : list) {
          if (nextElement instanceof SearchRequest) {
            // Process the search request.
            connection = new LDAPConnection(hostName, port, connOptions);
            connection.connectToHost(bindDN, bindPassword);
 
            SearchRequest sr = (SearchRequest) nextElement;
            DSMLSearchOperation ds = new DSMLSearchOperation(connection);
            SearchResponse searchResponse = ds.doSearch(objFactory, sr);
 
            JAXBElement<SearchResponse> searchResponseEl =
              objFactory.createBatchResponseSearchResponse(searchResponse);
            batchResponses.add(searchResponseEl);
 
          } 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 =
              objFactory.createBatchResponseAddResponse(addResponse);
            batchResponses.add(addResponseEl);
          } 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) {
            // 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 =
              objFactory.createBatchResponseExtendedResponse(extendedResponse);
            batchResponses.add(extendedResponseEl);
 
          } 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 =
              objFactory.createBatchResponseDelResponse(delResponse);
            batchResponses.add(delResponseEl);
          } else if (nextElement instanceof CompareRequest) {
            // Process the compare request.
            connection = new LDAPConnection(hostName, port, connOptions);
            connection.connectToHost(bindDN, bindPassword);
            CompareRequest cr = (CompareRequest) nextElement;
            DSMLCompareOperation compareOp =
              new DSMLCompareOperation(connection);
            LDAPResult compareResponse = compareOp.doOperation(objFactory, cr);
            JAXBElement<LDAPResult> compareResponseEl =
              objFactory.createBatchResponseCompareResponse(compareResponse);
            batchResponses.add(compareResponseEl);
          } else if (nextElement instanceof ModifyDNRequest) {
            // Process the Modify DN request.
            connection = new LDAPConnection(hostName, port, connOptions);
            connection.connectToHost(bindDN, bindPassword);
            ModifyDNRequest mr = (ModifyDNRequest) nextElement;
            DSMLModifyDNOperation moddnOp =
              new DSMLModifyDNOperation(connection);
            LDAPResult moddnResponse = moddnOp.doOperation(objFactory, mr);
            JAXBElement<LDAPResult> moddnResponseEl =
              objFactory.createBatchResponseModDNResponse(moddnResponse);
            batchResponses.add(moddnResponseEl);
          } 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 =
              objFactory.createBatchResponseModifyResponse(modResponse);
            batchResponses.add(modResponseEl);
          } else {
            String msg = "Invalid DSML request:" + nextElement;
            throw new IOException(msg);
          }
        }
 
        JAXBElement<BatchResponse> batchResponseElement =
          objFactory.createBatchResponse(batchResponse);
 
        marshaller.marshal(batchResponseElement, System.out);
 
        marshaller.marshal(batchResponseElement, doc);
      }
 
      // Send the DSML response back to the client.
      reply = messageFactory.createMessage();
      sendResponse(doc, false, reply, res, null);
 
    } catch (Exception se) {
      se.printStackTrace();
      // send SOAP fault
      try {
        reply = messageFactory.createMessage();
        sendResponse(null, true, reply, res, se);
      } catch (Exception e) {
      }
    } finally {
      if (connection != null) {
        connection.close();
      }
    }
  }
 
  /**
   * 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.
   */
  private void sendResponse(Document doc, boolean error, SOAPMessage reply,
                            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) {
      SOAPFault fault = replyBody.addFault();
      Name faultName = SOAPFactory.newInstance().createName("Server",
        "", 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 {
      SOAPElement bodyElement = replyBody.addDocument(doc);
    }
 
    reply.saveChanges();
 
    OutputStream os = res.getOutputStream();
    reply.writeTo(os);
    os.flush();
  }
}