1 25 package org.ofbiz.webapp.event; 26 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 import java.util.Locale ; 34 import java.util.Map ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 40 import org.apache.commons.fileupload.DiskFileUpload; 41 import org.apache.commons.fileupload.FileItem; 42 import org.apache.commons.fileupload.FileUpload; 43 import org.apache.commons.fileupload.FileUploadException; 44 45 import org.ofbiz.base.util.Debug; 46 import org.ofbiz.base.util.UtilHttp; 47 import org.ofbiz.base.util.UtilProperties; 48 import org.ofbiz.base.util.UtilValidate; 49 import org.ofbiz.entity.GenericValue; 50 import org.ofbiz.entity.util.ByteWrapper; 51 import org.ofbiz.service.DispatchContext; 52 import org.ofbiz.service.GenericServiceException; 53 import org.ofbiz.service.LocalDispatcher; 54 import org.ofbiz.service.ModelParam; 55 import org.ofbiz.service.ModelService; 56 import org.ofbiz.service.ServiceAuthException; 57 import org.ofbiz.service.ServiceValidationException; 58 59 67 public class ServiceEventHandler implements EventHandler { 68 69 public static final String module = ServiceEventHandler.class.getName(); 70 71 public static final String SYNC = "sync"; 72 public static final String ASYNC = "async"; 73 74 77 public void init(ServletContext context) throws EventHandlerException { 78 } 79 80 83 public String invoke(String eventPath, String eventMethod, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException { 84 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 86 if (dispatcher == null) { 87 throw new EventHandlerException("The local service dispatcher is null"); 88 } 89 DispatchContext dctx = dispatcher.getDispatchContext(); 90 if (dctx == null) { 91 throw new EventHandlerException("Dispatch context cannot be found"); 92 } 93 94 String mode = SYNC; 96 String serviceName = null; 97 98 if (eventPath == null || eventPath.length() == 0) { 99 mode = SYNC; 100 } else { 101 mode = eventPath; 102 } 103 104 serviceName = eventMethod; 106 if (serviceName == null) { 107 throw new EventHandlerException("Service name (eventMethod) cannot be null"); 108 } 109 if (Debug.verboseOn()) Debug.logVerbose("[Set mode/service]: " + mode + "/" + serviceName, module); 110 111 Locale locale = UtilHttp.getLocale(request); 113 HttpSession session = request.getSession(); 114 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 115 116 ModelService model = null; 118 119 try { 120 model = dctx.getModelService(serviceName); 121 } catch (GenericServiceException e) { 122 throw new EventHandlerException("Problems getting the service model", e); 123 } 124 125 if (model == null) { 126 throw new EventHandlerException("Problems getting the service model"); 127 } 128 129 if (Debug.verboseOn()) Debug.logVerbose("[Processing]: SERVICE Event", module); 130 if (Debug.verboseOn()) Debug.logVerbose("[Using delegator]: " + dispatcher.getDelegator().getDelegatorName(), module); 131 132 String maxSizeStr = UtilProperties.getPropertyValue("general.properties", "http.upload.max.size", "-1"); 134 long maxUploadSize = -1; 135 try { 136 maxUploadSize = Long.parseLong(maxSizeStr); 137 } catch (NumberFormatException e) { 138 Debug.logError(e, "Unable to obtain the max upload size from general.properties; using default -1", module); 139 maxUploadSize = -1; 140 } 141 142 boolean isMultiPart = FileUpload.isMultipartContent(request); 144 Map multiPartMap = new HashMap (); 145 if (isMultiPart) { 146 DiskFileUpload upload = new DiskFileUpload(); 147 upload.setSizeMax(maxUploadSize); 148 149 List uploadedItems = null; 150 try { 151 uploadedItems = upload.parseRequest(request); 152 } catch (FileUploadException e) { 153 throw new EventHandlerException("Problems reading uploaded data", e); 154 } 155 if (uploadedItems != null) { 156 Iterator i = uploadedItems.iterator(); 157 while (i.hasNext()) { 158 FileItem item = (FileItem) i.next(); 159 String fieldName = item.getFieldName(); 160 162 if (item.isFormField() || item.getSize() == 0) { 164 if (multiPartMap.containsKey(fieldName)) { 165 Object mapValue = multiPartMap.get(fieldName); 166 if (mapValue instanceof List ) { 167 ((List ) mapValue).add(item.getString()); 168 } else if (mapValue instanceof String ) { 169 List newList = new ArrayList (); 170 newList.add((String ) mapValue); 171 newList.add(item.getString()); 172 multiPartMap.put(fieldName, newList); 173 } else { 174 Debug.logWarning("Form field found [" + fieldName + "] which was not handled!", module); 175 } 176 } else { 177 multiPartMap.put(fieldName, item.getString()); 178 } 179 } else { 180 String fileName = item.getName(); 181 if (fileName.indexOf('\\') > -1 || fileName.indexOf('/') > -1) { 182 int lastIndex = fileName.lastIndexOf('\\'); 184 if (lastIndex == -1) { 185 lastIndex = fileName.lastIndexOf('/'); 186 } 187 if (lastIndex > -1) { 188 fileName = fileName.substring(lastIndex + 1); 189 } 190 } 191 multiPartMap.put(fieldName, new ByteWrapper(item.get())); 192 multiPartMap.put("_" + fieldName + "_size", new Long (item.getSize())); 193 multiPartMap.put("_" + fieldName + "_fileName", fileName); 194 multiPartMap.put("_" + fieldName + "_contentType", item.getContentType()); 195 } 196 } 197 } 198 } 199 200 request.setAttribute("multiPartMap", multiPartMap); 202 203 Map serviceContext = new HashMap (); 205 Iterator modelParmInIter = model.getInModelParamList().iterator(); 206 while (modelParmInIter.hasNext()) { 207 ModelParam modelParam = (ModelParam) modelParmInIter.next(); 208 String name = modelParam.name; 209 210 if ("userLogin".equals(name)) continue; 212 if ("locale".equals(name)) continue; 214 215 Object value = null; 216 if (modelParam.stringMapPrefix != null && modelParam.stringMapPrefix.length() > 0) { 217 Map paramMap = UtilHttp.makeParamMapWithPrefix(request, multiPartMap, modelParam.stringMapPrefix, null); 218 value = paramMap; 219 if (Debug.verboseOn()) Debug.log("Set [" + modelParam.name + "]: " + paramMap, module); 220 } else if (modelParam.stringListSuffix != null && modelParam.stringListSuffix.length() > 0) { 221 List paramList = UtilHttp.makeParamListWithSuffix(request, multiPartMap, modelParam.stringListSuffix, null); 222 value = paramList; 223 } else { 224 value = multiPartMap.get(name); 226 227 if (UtilValidate.isEmpty(value)) { 229 String [] paramArr = request.getParameterValues(name); 231 if (paramArr != null) { 232 if (paramArr.length > 1) { 233 value = Arrays.asList(paramArr); 234 } else { 235 value = paramArr[0]; 236 } 237 } 238 if (value == null) { 240 value = UtilHttp.makeParamValueFromComposite(request, name, locale); 241 } 242 } 243 244 if (UtilValidate.isEmpty(value)) { 246 Object tempVal = request.getAttribute(name); 247 if (tempVal != null) { 248 value = tempVal; 249 } 250 } 251 252 if (UtilValidate.isEmpty(value)) { 254 Object tempVal = request.getSession().getAttribute(name); 255 if (tempVal != null) { 256 value = tempVal; 257 } 258 } 259 260 if (value == null) { 262 continue; 264 } 265 266 if (value instanceof String && ((String ) value).length() == 0) { 267 value = null; 269 } 270 } 271 serviceContext.put(name, value); 273 } 274 275 List errorMessages = new LinkedList (); 278 serviceContext = model.makeValid(serviceContext, ModelService.IN_PARAM, true, errorMessages, locale); 279 if (errorMessages.size() > 0) { 280 request.setAttribute("_ERROR_MESSAGE_LIST_", errorMessages); 282 return "error"; 283 } 284 285 if (userLogin != null) { 287 serviceContext.put("userLogin", userLogin); 288 } 289 290 if (locale != null) { 292 serviceContext.put("locale", locale); 293 } 294 295 Map result = null; 297 try { 298 if (ASYNC.equalsIgnoreCase(mode)) { 299 dispatcher.runAsync(serviceName, serviceContext); 300 } else { 301 result = dispatcher.runSync(serviceName, serviceContext); 302 } 303 } catch (ServiceAuthException e) { 304 request.setAttribute("_ERROR_MESSAGE_", e.getNonNestedMessage()); 306 return "error"; 307 } catch (ServiceValidationException e) { 308 request.setAttribute("serviceValidationException", e); 310 if (e.getMessageList() != null) { 311 request.setAttribute("_ERROR_MESSAGE_LIST_", e.getMessageList()); 312 } else { 313 request.setAttribute("_ERROR_MESSAGE_", e.getNonNestedMessage()); 314 } 315 return "error"; 316 } catch (GenericServiceException e) { 317 Debug.logError(e, "Service invocation error", module); 318 throw new EventHandlerException("Service invocation error", e.getNested()); 319 } 320 321 String responseString = null; 322 323 if (result == null) { 324 responseString = ModelService.RESPOND_SUCCESS; 325 } else { 326 327 if (!result.containsKey(ModelService.RESPONSE_MESSAGE)) { 328 responseString = ModelService.RESPOND_SUCCESS; 329 } else { 330 responseString = (String ) result.get(ModelService.RESPONSE_MESSAGE); 331 } 332 333 request.setAttribute("_ERROR_MESSAGE_LIST_", result.get(ModelService.ERROR_MESSAGE_LIST)); 335 request.setAttribute("_ERROR_MESSAGE_MAP_", result.get(ModelService.ERROR_MESSAGE_MAP)); 336 request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE)); 337 338 request.setAttribute("_EVENT_MESSAGE_LIST_", result.get(ModelService.SUCCESS_MESSAGE_LIST)); 339 request.setAttribute("_EVENT_MESSAGE_", result.get(ModelService.SUCCESS_MESSAGE)); 340 341 Iterator rmei = result.entrySet().iterator(); 343 while (rmei.hasNext()) { 344 Map.Entry rme = (Map.Entry ) rmei.next(); 345 String resultKey = (String ) rme.getKey(); 346 Object resultValue = rme.getValue(); 347 348 if (resultKey != null && !ModelService.RESPONSE_MESSAGE.equals(resultKey) && !ModelService.ERROR_MESSAGE.equals(resultKey) && 349 !ModelService.ERROR_MESSAGE_LIST.equals(resultKey) && !ModelService.ERROR_MESSAGE_MAP.equals(resultKey) && 350 !ModelService.SUCCESS_MESSAGE.equals(resultKey) && !ModelService.SUCCESS_MESSAGE_LIST.equals(resultKey)) { 351 request.setAttribute(resultKey, resultValue); 352 } 353 } 354 } 355 356 if (Debug.verboseOn()) Debug.logVerbose("[Event Return]: " + responseString, module); 357 return responseString; 358 } 359 } 360 | Popular Tags |