KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > engine > HttpEngine


1 /*
2  * $Id: HttpEngine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.service.engine;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.ofbiz.base.util.Debug;
35 import org.ofbiz.base.util.HttpClient;
36 import org.ofbiz.base.util.HttpClientException;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.serialize.XmlSerializer;
39 import org.ofbiz.service.DispatchContext;
40 import org.ofbiz.service.GenericServiceException;
41 import org.ofbiz.service.LocalDispatcher;
42 import org.ofbiz.service.ModelService;
43 import org.ofbiz.service.ServiceDispatcher;
44
45 /**
46  * HttpEngine.java
47  *
48  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
49  * @version $Rev: 5462 $
50  * @since 2.0
51  */

52 public class HttpEngine extends GenericAsyncEngine {
53     
54     public static final String JavaDoc module = HttpEngine.class.getName();
55     private static final boolean exportAll = false;
56
57     public HttpEngine(ServiceDispatcher dispatcher) {
58         super(dispatcher);
59     }
60     
61     /**
62      * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
63      */

64     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
65         DispatchContext dctx = dispatcher.getLocalContext(localName);
66         String JavaDoc xmlContext = null;
67         
68         try {
69             if (Debug.verboseOn()) Debug.logVerbose("Serializing Context --> " + context, module);
70             xmlContext = XmlSerializer.serialize(context);
71         } catch (Exception JavaDoc e) {
72             throw new GenericServiceException("Cannot serialize context.", e);
73         }
74         
75         Map JavaDoc parameters = new HashMap JavaDoc();
76         parameters.put("serviceName", modelService.invoke);
77         if (xmlContext != null)
78             parameters.put("serviceContext", xmlContext);
79         
80         HttpClient http = new HttpClient(this.getLocation(modelService), parameters);
81         String JavaDoc postResult = null;
82         try {
83             postResult = http.post();
84         } catch (HttpClientException e) {
85             throw new GenericServiceException("Problems invoking HTTP request", e);
86         }
87         
88         Map JavaDoc result = null;
89         try {
90             Object JavaDoc res = XmlSerializer.deserialize(postResult, dctx.getDelegator());
91             if (res instanceof Map JavaDoc)
92                 result = (Map JavaDoc) res;
93             else
94                 throw new GenericServiceException("Result not an instance of Map.");
95         } catch (Exception JavaDoc e) {
96             throw new GenericServiceException("Problems deserializing result.", e);
97         }
98         
99         return result;
100     }
101
102     /**
103      * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
104      */

105     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
106         Map JavaDoc result = runSync(localName, modelService, context);
107     }
108     
109     /**
110      * Event for handling HTTP services
111      * @param request HttpServletRequest object
112      * @param response HttpServletResponse object
113      * @return null
114      */

115     public static String JavaDoc httpEngine(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
116         LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
117         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
118         
119         String JavaDoc serviceName = request.getParameter("serviceName");
120         String JavaDoc serviceMode = request.getParameter("serviceMode");
121         String JavaDoc xmlContext = request.getParameter("serviceContext");
122         
123         Map JavaDoc result = new HashMap JavaDoc();
124         Map JavaDoc context = null;
125         
126         if (serviceName == null)
127             result.put(ModelService.ERROR_MESSAGE, "Cannot have null service name");
128             
129         if (serviceMode == null)
130             serviceMode = "SYNC";
131                 
132         // deserialize the context
133
if (!result.containsKey(ModelService.ERROR_MESSAGE)) {
134             if (xmlContext != null) {
135                 try {
136                     Object JavaDoc o = XmlSerializer.deserialize(xmlContext, delegator);
137                     if (o instanceof Map JavaDoc)
138                         context = (Map JavaDoc) o;
139                     else {
140                         Debug.logError("Context not an instance of Map error", module);
141                         result.put(ModelService.ERROR_MESSAGE, "Context not an instance of Map");
142                     }
143                 } catch (Exception JavaDoc e) {
144                     Debug.logError(e, "Deserialization error", module);
145                     result.put(ModelService.ERROR_MESSAGE, "Error occurred deserializing context: " + e.toString());
146                 }
147             }
148         }
149         
150         // invoke the service
151
if (!result.containsKey(ModelService.ERROR_MESSAGE)) {
152             try {
153                 ModelService model = dispatcher.getDispatchContext().getModelService(serviceName);
154                 if (model.export || exportAll) {
155                     if (serviceMode.equals("ASYNC")) {
156                         dispatcher.runAsync(serviceName, context);
157                     } else {
158                         result = dispatcher.runSync(serviceName, context);
159                     }
160                 } else {
161                     Debug.logWarning("Attempt to invoke a non-exported service: " + serviceName, module);
162                     throw new GenericServiceException("Cannot find requested service");
163                 }
164             } catch (GenericServiceException e) {
165                 Debug.logError(e, "Service invocation error", module);
166                 result.put(ModelService.ERROR_MESSAGE, "Service invocation error: " + e.toString());
167             }
168         }
169         
170         // backup error message
171
StringBuffer JavaDoc errorMessage = new StringBuffer JavaDoc();
172         
173         // process the result
174
String JavaDoc resultString = null;
175         try {
176             resultString = XmlSerializer.serialize(result);
177         } catch (Exception JavaDoc e) {
178             Debug.logError(e, "Cannot serialize result", module);
179             if (result.containsKey(ModelService.ERROR_MESSAGE))
180                 errorMessage.append(result.get(ModelService.ERROR_MESSAGE));
181             errorMessage.append("::");
182             errorMessage.append(e);
183         }
184         
185         // handle the response
186
try {
187             PrintWriter JavaDoc out = response.getWriter();
188             response.setContentType("plain/text");
189             
190             if (errorMessage.length() > 0) {
191                 response.setContentLength(errorMessage.length());
192                 out.write(errorMessage.toString());
193             } else {
194                 response.setContentLength(resultString.length());
195                 out.write(resultString);
196             }
197             
198             out.flush();
199             response.flushBuffer();
200         } catch (IOException JavaDoc e) {
201             Debug.logError(e, "Problems w/ getting the servlet writer.", module);
202             return "error";
203         }
204                                                                 
205         return null;
206     }
207
208     
209 }
210
Popular Tags