1 24 package org.ofbiz.service.engine; 25 26 import java.io.IOException ; 27 import java.io.PrintWriter ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 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 52 public class HttpEngine extends GenericAsyncEngine { 53 54 public static final String module = HttpEngine.class.getName(); 55 private static final boolean exportAll = false; 56 57 public HttpEngine(ServiceDispatcher dispatcher) { 58 super(dispatcher); 59 } 60 61 64 public Map runSync(String localName, ModelService modelService, Map context) throws GenericServiceException { 65 DispatchContext dctx = dispatcher.getLocalContext(localName); 66 String xmlContext = null; 67 68 try { 69 if (Debug.verboseOn()) Debug.logVerbose("Serializing Context --> " + context, module); 70 xmlContext = XmlSerializer.serialize(context); 71 } catch (Exception e) { 72 throw new GenericServiceException("Cannot serialize context.", e); 73 } 74 75 Map parameters = new HashMap (); 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 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 result = null; 89 try { 90 Object res = XmlSerializer.deserialize(postResult, dctx.getDelegator()); 91 if (res instanceof Map ) 92 result = (Map ) res; 93 else 94 throw new GenericServiceException("Result not an instance of Map."); 95 } catch (Exception e) { 96 throw new GenericServiceException("Problems deserializing result.", e); 97 } 98 99 return result; 100 } 101 102 105 public void runSyncIgnore(String localName, ModelService modelService, Map context) throws GenericServiceException { 106 Map result = runSync(localName, modelService, context); 107 } 108 109 115 public static String httpEngine(HttpServletRequest request, HttpServletResponse response) { 116 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 117 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 118 119 String serviceName = request.getParameter("serviceName"); 120 String serviceMode = request.getParameter("serviceMode"); 121 String xmlContext = request.getParameter("serviceContext"); 122 123 Map result = new HashMap (); 124 Map 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 if (!result.containsKey(ModelService.ERROR_MESSAGE)) { 134 if (xmlContext != null) { 135 try { 136 Object o = XmlSerializer.deserialize(xmlContext, delegator); 137 if (o instanceof Map ) 138 context = (Map ) 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 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 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 StringBuffer errorMessage = new StringBuffer (); 172 173 String resultString = null; 175 try { 176 resultString = XmlSerializer.serialize(result); 177 } catch (Exception 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 try { 187 PrintWriter 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 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 |