gatewayHandler.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * --- END COPYRIGHT BLOCK --- */
  37. package com.netscape.dsml.gateway;
  38. import java.io.*;
  39. import java.util.Iterator;
  40. import javax.xml.namespace.QName;
  41. import javax.xml.parsers.*;
  42. import javax.xml.rpc.handler.Handler;
  43. import javax.xml.rpc.handler.HandlerInfo;
  44. import javax.xml.rpc.handler.soap.SOAPMessageContext;
  45. import javax.xml.soap.Name;
  46. import javax.xml.soap.SOAPEnvelope;
  47. import javax.xml.soap.SOAPHeader;
  48. import javax.xml.soap.SOAPHeaderElement;
  49. import javax.xml.soap.SOAPMessage;
  50. import javax.xml.soap.SOAPPart;
  51. import javax.xml.soap.SOAPConstants;
  52. import javax.xml.transform.*;
  53. import javax.xml.transform.dom.DOMSource;
  54. import javax.xml.transform.stream.StreamResult;
  55. import org.w3c.dom.Document;
  56. import org.xml.sax.*;
  57. import org.w3c.dom.*;
  58. import javax.xml.soap.*;
  59. import org.apache.axis.AxisFault;
  60. import org.apache.axis.Message;
  61. import org.apache.axis.MessageContext;
  62. import org.apache.axis.handlers.BasicHandler;
  63. import org.apache.commons.codec.binary.Base64;
  64. public class gatewayHandler extends BasicHandler {
  65. private HandlerInfo handlerInfo;
  66. static private javax.xml.soap.MessageFactory messageFactory ;
  67. static private javax.xml.soap.SOAPFactory sef ;
  68. private static boolean ready = false;
  69. public gatewayHandler() {
  70. super();
  71. try {
  72. messageFactory = javax.xml.soap.MessageFactory.newInstance();
  73. sef = javax.xml.soap.SOAPFactory.newInstance();
  74. } catch (Exception e) { }
  75. }
  76. public void invoke(MessageContext context) throws AxisFault {
  77. if (context.getPastPivot() == false) {
  78. handleRequest(context);
  79. }
  80. }
  81. public void cleanup() {
  82. super.cleanup();
  83. }
  84. protected void finalize() {
  85. new ProxyConnMgrFactory().getInstance().shutdown();
  86. }
  87. public boolean handleRequest(MessageContext context) {
  88. /*
  89. * this section will set user, pwd, if it came via a http authentication header
  90. *
  91. */
  92. Configuration config = Configuration.getInstance();
  93. String tmp = (String)context.getProperty("Authorization");
  94. String user=null ;
  95. String pwd =null;
  96. if ( tmp != null )
  97. tmp = tmp.trim();
  98. if ( tmp != null && tmp.startsWith("Basic ") ) {
  99. int i ;
  100. Base64 bd = new Base64();
  101. try {
  102. tmp = new String( (bd.decode(tmp.substring(6).getBytes() )));
  103. }
  104. catch (Exception e) {
  105. // couldn't decode auth info
  106. }
  107. i = tmp.indexOf( ':' );
  108. if ( i == -1 )
  109. user = new String(tmp) ;
  110. else
  111. user = new String(tmp.substring( 0, i));
  112. if ( i != -1 ) {
  113. pwd= new String(tmp.substring(i+1));
  114. if ( pwd != null && pwd.equals("") ) pwd = null ;
  115. }
  116. }
  117. org.apache.axis.Message out_m = null;
  118. SOAPEnvelope out_env = null;
  119. javax.xml.soap.SOAPBody out_body = null;
  120. javax.xml.soap.SOAPElement out_fResponse = null;
  121. try {
  122. javax.xml.soap.SOAPPart sp = ((SOAPMessageContext) context).getMessage().getSOAPPart();
  123. javax.xml.soap.SOAPEnvelope se = sp.getEnvelope();
  124. javax.xml.soap.SOAPBody sb = se.getBody();
  125. out_m= new Message(context.getRequestMessage().getSOAPEnvelope());
  126. out_env = out_m.getSOAPPart().getEnvelope();
  127. out_body = out_env.getBody();
  128. out_fResponse = out_body.addBodyElement(out_env.createName("batchResponse"));
  129. out_fResponse.addAttribute(out_env.createName("xmlns"), "urn:oasis:names:tc:DSML:2:0:core");
  130. int authorizationCode = new com.netscape.dsml.gateway.LDAPAuthenticator(user, pwd).authenticate();
  131. if (config.getUseAuth() && authorizationCode >0 ) {
  132. out_fResponse.addChildElement("errorResponse").addAttribute(messageFactory.createMessage().getSOAPPart().getEnvelope().createName("code"), String.valueOf(authorizationCode) );
  133. }
  134. else {
  135. BatchProcessor batchProcessor = new BatchProcessor(sb);
  136. batchProcessor.init();
  137. if (config.getUseAuth())
  138. batchProcessor.setProxy(user);
  139. else
  140. batchProcessor.setProxy("");
  141. boolean preprocess_successful = batchProcessor.preprocess();
  142. Message request= context.getRequestMessage();
  143. if (request != null)
  144. {
  145. java.util.Iterator request_elements = request.getSOAPBody().getChildElements();
  146. if (request_elements != null)
  147. {
  148. if (request_elements.hasNext()){
  149. ((org.apache.axis.message.RPCElement) request_elements.next()).detachNode();
  150. }
  151. }
  152. }
  153. if (preprocess_successful) {
  154. int i = -1;
  155. int NumberRequests = batchProcessor.getRequestCount();
  156. while (++i < NumberRequests && ! batchProcessor.Error()) {
  157. batchProcessor.process(i);
  158. SOAPElement RequestedItem = batchProcessor.getRequestItem(i);
  159. if ( RequestedItem != null )
  160. out_fResponse.addChildElement( RequestedItem );
  161. }
  162. }
  163. if ( out_fResponse.getChildElements().hasNext() == false) {
  164. try {
  165. /* This is slightly inaccurate. This simply checks to see if the batch is empty, and if it is, return
  166. * something. the error isn't always 91, but hopefully it's slightly more descriptive to the end user */
  167. out_fResponse.addChildElement("errorResponse").addAttribute(messageFactory.createMessage().getSOAPPart().getEnvelope().createName("code"), "91" );
  168. }
  169. catch (javax.xml.soap.SOAPException soapException) { } /* Not important to catch this */
  170. }
  171. }
  172. }
  173. catch (gatewayException gwe) {
  174. try {
  175. out_fResponse.addChildElement("errorResponse").addAttribute(messageFactory.createMessage().getSOAPPart().getEnvelope().createName("code"), "81" );
  176. }
  177. catch (javax.xml.soap.SOAPException soapException) {
  178. /* We did our best to try and exit gracefully. Give up. */
  179. }
  180. }
  181. catch (javax.xml.soap.SOAPException soapException) {
  182. }
  183. catch (Exception e) {
  184. e.printStackTrace();
  185. }
  186. /* To return false means do not try and continue onto the
  187. * deployed service. Since we context.setProperty our
  188. * SOAPResponse message, it will be sent as soon as the request
  189. * turns the other way into a reponse.
  190. */
  191. context.setResponseMessage( (Message)out_m );
  192. return false;
  193. }
  194. public boolean handleFault(MessageContext context) {
  195. return false;
  196. }
  197. public void init(HandlerInfo config) {
  198. handlerInfo = config;
  199. }
  200. public QName[] getHeaders() {
  201. return handlerInfo.getHeaders();
  202. }
  203. public void destroy() {
  204. /* Bad things happen if the pool isn't shutdown before the servlet goes away */
  205. IConnectionManager ldap_pool = null;
  206. ProxyConnMgrFactory pmc = new ProxyConnMgrFactory();
  207. try {
  208. ldap_pool = pmc.getInstance();
  209. ldap_pool.shutdown();
  210. }
  211. catch (Exception e) { }
  212. }
  213. }