001: /*
002: * $Id: HttpEngine.java,v 1.3 2003/12/08 20:13:42 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 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: package org.ofbiz.service.engine;
025:
026: import java.io.IOException;
027: import java.io.PrintWriter;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.HttpClient;
036: import org.ofbiz.base.util.HttpClientException;
037: import org.ofbiz.entity.GenericDelegator;
038: import org.ofbiz.entity.serialize.XmlSerializer;
039: import org.ofbiz.service.DispatchContext;
040: import org.ofbiz.service.GenericServiceException;
041: import org.ofbiz.service.LocalDispatcher;
042: import org.ofbiz.service.ModelService;
043: import org.ofbiz.service.ServiceDispatcher;
044:
045: /**
046: * HttpEngine.java
047: *
048: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
049: * @version $Revision: 1.3 $
050: * @since 2.0
051: */
052: public class HttpEngine extends GenericAsyncEngine {
053:
054: public static final String module = HttpEngine.class.getName();
055: private static final boolean exportAll = false;
056:
057: public HttpEngine(ServiceDispatcher dispatcher) {
058: super (dispatcher);
059: }
060:
061: /**
062: * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
063: */
064: public Map runSync(String localName, ModelService modelService,
065: Map context) throws GenericServiceException {
066: DispatchContext dctx = dispatcher.getLocalContext(localName);
067: String xmlContext = null;
068:
069: try {
070: if (Debug.verboseOn())
071: Debug.logVerbose("Serializing Context --> " + context,
072: module);
073: xmlContext = XmlSerializer.serialize(context);
074: } catch (Exception e) {
075: throw new GenericServiceException(
076: "Cannot serialize context.", e);
077: }
078:
079: Map parameters = new HashMap();
080: parameters.put("serviceName", modelService.invoke);
081: if (xmlContext != null)
082: parameters.put("serviceContext", xmlContext);
083:
084: HttpClient http = new HttpClient(modelService.location,
085: parameters);
086: String postResult = null;
087: try {
088: postResult = http.post();
089: } catch (HttpClientException e) {
090: throw new GenericServiceException(
091: "Problems invoking HTTP request", e);
092: }
093:
094: Map result = null;
095: try {
096: Object res = XmlSerializer.deserialize(postResult, dctx
097: .getDelegator());
098: if (res instanceof Map)
099: result = (Map) res;
100: else
101: throw new GenericServiceException(
102: "Result not an instance of Map.");
103: } catch (Exception e) {
104: throw new GenericServiceException(
105: "Problems deserializing result.", e);
106: }
107:
108: return result;
109: }
110:
111: /**
112: * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
113: */
114: public void runSyncIgnore(String localName,
115: ModelService modelService, Map context)
116: throws GenericServiceException {
117: Map result = runSync(localName, modelService, context);
118: }
119:
120: /**
121: * Event for handling HTTP services
122: * @param request HttpServletRequest object
123: * @param response HttpServletResponse object
124: * @return null
125: */
126: public static String httpEngine(HttpServletRequest request,
127: HttpServletResponse response) {
128: LocalDispatcher dispatcher = (LocalDispatcher) request
129: .getAttribute("dispatcher");
130: GenericDelegator delegator = (GenericDelegator) request
131: .getAttribute("delegator");
132:
133: String serviceName = request.getParameter("serviceName");
134: String serviceMode = request.getParameter("serviceMode");
135: String xmlContext = request.getParameter("serviceContext");
136:
137: Map result = new HashMap();
138: Map context = null;
139:
140: if (serviceName == null)
141: result.put(ModelService.ERROR_MESSAGE,
142: "Cannot have null service name");
143:
144: if (serviceMode == null)
145: serviceMode = "SYNC";
146:
147: // deserialize the context
148: if (!result.containsKey(ModelService.ERROR_MESSAGE)) {
149: if (xmlContext != null) {
150: try {
151: Object o = XmlSerializer.deserialize(xmlContext,
152: delegator);
153: if (o instanceof Map)
154: context = (Map) o;
155: else {
156: Debug.logError(
157: "Context not an instance of Map error",
158: module);
159: result.put(ModelService.ERROR_MESSAGE,
160: "Context not an instance of Map");
161: }
162: } catch (Exception e) {
163: Debug.logError(e, "Deserialization error", module);
164: result.put(ModelService.ERROR_MESSAGE,
165: "Error occurred deserializing context: "
166: + e.toString());
167: }
168: }
169: }
170:
171: // invoke the service
172: if (!result.containsKey(ModelService.ERROR_MESSAGE)) {
173: try {
174: ModelService model = dispatcher.getDispatchContext()
175: .getModelService(serviceName);
176: if (model.export || exportAll) {
177: if (serviceMode.equals("ASYNC")) {
178: dispatcher.runAsync(serviceName, context);
179: } else {
180: result = dispatcher.runSync(serviceName,
181: context);
182: }
183: } else {
184: Debug.logWarning(
185: "Attempt to invoke a non-exported service: "
186: + serviceName, module);
187: throw new GenericServiceException(
188: "Cannot find requested service");
189: }
190: } catch (GenericServiceException e) {
191: Debug.logError(e, "Service invocation error", module);
192: result.put(ModelService.ERROR_MESSAGE,
193: "Service invocation error: " + e.toString());
194: }
195: }
196:
197: // backup error message
198: StringBuffer errorMessage = new StringBuffer();
199:
200: // process the result
201: String resultString = null;
202: try {
203: resultString = XmlSerializer.serialize(result);
204: } catch (Exception e) {
205: Debug.logError(e, "Cannot serialize result", module);
206: if (result.containsKey(ModelService.ERROR_MESSAGE))
207: errorMessage.append(result
208: .get(ModelService.ERROR_MESSAGE));
209: errorMessage.append("::");
210: errorMessage.append(e);
211: }
212:
213: // handle the response
214: try {
215: PrintWriter out = response.getWriter();
216: response.setContentType("plain/text");
217:
218: if (errorMessage.length() > 0) {
219: response.setContentLength(errorMessage.length());
220: out.write(errorMessage.toString());
221: } else {
222: response.setContentLength(resultString.length());
223: out.write(resultString);
224: }
225:
226: out.flush();
227: response.flushBuffer();
228: } catch (IOException e) {
229: Debug.logError(e,
230: "Problems w/ getting the servlet writer.", module);
231: return "error";
232: }
233:
234: return null;
235: }
236:
237: }
|