001: /*
002: * $Id: SOAPEventHandler.java,v 1.2 2003/09/14 05:36:47 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.event;
026:
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Map;
032: import java.util.Set;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import javax.servlet.http.HttpSession;
037: import javax.xml.soap.SOAPException;
038:
039: import org.apache.axis.AxisFault;
040: import org.apache.axis.Constants;
041: import org.apache.axis.Message;
042: import org.apache.axis.MessageContext;
043: import org.apache.axis.message.RPCElement;
044: import org.apache.axis.message.RPCParam;
045: import org.apache.axis.message.SOAPEnvelope;
046: import org.apache.axis.server.AxisServer;
047: import org.apache.log4j.Category;
048: import org.ofbiz.base.util.Debug;
049: import org.ofbiz.base.util.UtilMisc;
050: import org.ofbiz.service.GenericServiceException;
051: import org.ofbiz.service.LocalDispatcher;
052: import org.ofbiz.service.ModelService;
053:
054: /**
055: * SOAPEventHandler - SOAP Event Handler implementation
056: *
057: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
058: * @author <a href="mailto:">Andy Chen</a>
059: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
060: * @version $Revision: 1.2 $
061: * @since 2.0
062: */
063: public class SOAPEventHandler implements EventHandler {
064:
065: public static final String module = SOAPEventHandler.class
066: .getName();
067: public static Category category = Category
068: .getInstance(SOAPEventHandler.class.getName());
069:
070: /** Invoke the web event
071: *@param eventPath The path or location of this event
072: *@param eventMethod The method to invoke
073: *@param request The servlet request object
074: *@param response The servlet response object
075: *@return String Result code
076: *@throws EventHandlerException
077: */
078: public String invoke(String eventPath, String eventMethod,
079: HttpServletRequest request, HttpServletResponse response)
080: throws EventHandlerException {
081: HttpSession session = request.getSession();
082: LocalDispatcher dispatcher = (LocalDispatcher) request
083: .getAttribute("dispatcher");
084: AxisServer axisServer;
085:
086: try {
087: axisServer = AxisServer.getServer(UtilMisc.toMap("name",
088: "OFBiz/Axis Server", "provider", null));
089: } catch (AxisFault e) {
090: sendError(response, e);
091: throw new EventHandlerException(
092: "Problems with the AXIS server", e);
093: }
094: MessageContext mctx = new MessageContext(axisServer);
095:
096: // get the SOAP message
097: Message msg = null;
098:
099: try {
100: msg = new Message(request.getInputStream(), false, request
101: .getHeader("Content-Type"), request
102: .getHeader("Content-Location"));
103: } catch (IOException ioe) {
104: throw new EventHandlerException(
105: "Cannot read the input stream", ioe);
106: }
107:
108: if (msg == null) {
109: sendError(response, "No message");
110: throw new EventHandlerException("SOAP Message is null");
111: }
112:
113: mctx.setRequestMessage(msg);
114:
115: // new envelopes
116: SOAPEnvelope resEnv = new SOAPEnvelope();
117: SOAPEnvelope reqEnv = null;
118:
119: // get the service name and parameters
120: try {
121: reqEnv = (SOAPEnvelope) msg.getSOAPPart().getEnvelope();
122: } catch (SOAPException e) {
123: throw new EventHandlerException("Cannot get the envelope",
124: e);
125: }
126:
127: List bodies = null;
128:
129: try {
130: bodies = reqEnv.getBodyElements();
131: } catch (AxisFault e) {
132: sendError(response, e);
133: throw new EventHandlerException(e.getMessage(), e);
134: }
135:
136: Debug.logVerbose("[Processing]: SOAP Event", module);
137:
138: // each is a different service call
139: Iterator i = bodies.iterator();
140:
141: while (i.hasNext()) {
142: Object o = i.next();
143:
144: if (o instanceof RPCElement) {
145: RPCElement body = (RPCElement) o;
146: String serviceName = body.getMethodName();
147: List params = null;
148:
149: try {
150: params = body.getParams();
151: } catch (Exception e) {
152: sendError(response, e);
153: throw new EventHandlerException(e.getMessage(), e);
154: }
155: Map serviceContext = new HashMap();
156: Iterator p = params.iterator();
157:
158: while (p.hasNext()) {
159: RPCParam param = (RPCParam) p.next();
160:
161: if (Debug.verboseOn())
162: Debug.logVerbose("[Reading Param]: "
163: + param.getName(), module);
164: serviceContext.put(param.getName(), param
165: .getValue());
166: }
167: try {
168: // verify the service is exported for remote execution and invoke it
169: ModelService model = dispatcher
170: .getDispatchContext().getModelService(
171: serviceName);
172:
173: if (model != null && model.export) {
174: Map result = dispatcher.runSync(serviceName,
175: serviceContext);
176:
177: Debug.logVerbose(
178: "[EventHandler] : Service invoked",
179: module);
180: RPCElement resBody = new RPCElement(serviceName
181: + "Response");
182:
183: resBody.setPrefix(body.getPrefix());
184: resBody.setNamespaceURI(body.getNamespaceURI());
185: Set keySet = result.keySet();
186: Iterator ri = keySet.iterator();
187:
188: while (ri.hasNext()) {
189: Object key = ri.next();
190: RPCParam par = new RPCParam(((String) key),
191: result.get(key));
192:
193: resBody.addParam(par);
194: }
195: resEnv.addBodyElement(resBody);
196: resEnv
197: .setEncodingStyle(Constants.URI_DEFAULT_SOAP_ENC);
198: } else {
199: sendError(response,
200: "Request service not available");
201: throw new EventHandlerException(
202: "Service is not exported");
203: }
204: } catch (GenericServiceException e) {
205: sendError(response, "Problem process the service");
206: throw new EventHandlerException(e.getMessage(), e);
207: } catch (javax.xml.soap.SOAPException e) {
208: sendError(response, "Problem process the service");
209: throw new EventHandlerException(e.getMessage(), e);
210: }
211: }
212: }
213:
214: // setup the response
215: Debug.logVerbose(
216: "[EventHandler] : Setting up response message", module);
217: msg = new Message(resEnv);
218: mctx.setResponseMessage(msg);
219: if (msg == null) {
220: sendError(response, "No response message available");
221: throw new EventHandlerException(
222: "No response message available");
223: }
224:
225: try {
226: response.setContentType(msg
227: .getContentType(Constants.DEFAULT_SOAP_VERSION));
228: response.setContentLength(Integer.parseInt(Long
229: .toString(msg.getContentLength())));
230: } catch (AxisFault e) {
231: sendError(response, e);
232: throw new EventHandlerException(e.getMessage(), e);
233: }
234:
235: try {
236: msg.writeTo(response.getOutputStream());
237: response.flushBuffer();
238: } catch (IOException e) {
239: throw new EventHandlerException(
240: "Cannot write to the output stream");
241: } catch (SOAPException e) {
242: throw new EventHandlerException(
243: "Cannot write message to the output stream");
244: }
245:
246: Debug.logVerbose("[EventHandler] : Message sent to requester",
247: module);
248:
249: return "success";
250: }
251:
252: private void sendError(HttpServletResponse res, Object obj)
253: throws EventHandlerException {
254: Message msg = new Message(obj);
255:
256: try {
257: res.setContentType(msg
258: .getContentType(Constants.DEFAULT_SOAP_VERSION));
259: res.setContentLength(Integer.parseInt(Long.toString(msg
260: .getContentLength())));
261: msg.writeTo(res.getOutputStream());
262: res.flushBuffer();
263: } catch (Exception e) {
264: throw new EventHandlerException(e.getMessage(), e);
265: }
266: }
267: }
|