| | |
| | | * 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; |
| | |
| | | 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; |
| | | |
| | | |
| | | /** |
| | |
| | | * 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; |
| | |
| | | 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. |
| | | */ |
| | | 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(); |
| | |
| | | |
| | | DirectoryServer.bootstrapClient(); |
| | | |
| | | } catch(Exception je) |
| | | { |
| | | } catch (Exception je) { |
| | | je.printStackTrace(); |
| | | throw new ServletException(je.getMessage()); |
| | | } |
| | |
| | | * |
| | | * @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); |
| | | } |
| | | |
| | |
| | | * |
| | | * @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()) |
| | | { |
| | | 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 = |
| | |
| | | } |
| | | } |
| | | StringTokenizer tk = new StringTokenizer(headerVal, ","); |
| | | while (tk.hasMoreTokens()) |
| | | { |
| | | while (tk.hasMoreTokens()) { |
| | | mimeHeaders.addHeader(headerName, tk.nextToken().trim()); |
| | | } |
| | | } |
| | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | 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); |
| | |
| | | JAXBElement<LDAPResult> modResponseEl = |
| | | objFactory.createBatchResponseModifyResponse(modResponse); |
| | | batchResponses.add(modResponseEl); |
| | | } else |
| | | { |
| | | } else { |
| | | String msg = "Invalid DSML request:" + nextElement; |
| | | throw new IOException(msg); |
| | | } |
| | |
| | | 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(); |
| | | } |
| | | } |
| | |
| | | * @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 |
| | | { |
| | | 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); |
| | |
| | | fault.setFaultString("Server Error: " + e.getMessage()); |
| | | // FIXME - Set correct fault actor |
| | | fault.setFaultActor("http://localhost:8080"); |
| | | } else |
| | | { |
| | | } else { |
| | | SOAPElement bodyElement = replyBody.addDocument(doc); |
| | | } |
| | | |