1 25 package org.ofbiz.webapp.event; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.FileNotFoundException ; 30 import java.io.IOException ; 31 import java.sql.Timestamp ; 32 import java.util.Date ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Locale ; 36 import java.util.Map ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import org.ofbiz.base.util.Debug; 41 import org.ofbiz.base.util.UtilHttp; 42 import org.ofbiz.base.util.UtilProperties; 43 import org.ofbiz.base.util.UtilValidate; 44 import org.ofbiz.webapp.control.RequestHandler; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericValue; 47 import org.ofbiz.security.Security; 48 import org.ofbiz.service.DispatchContext; 49 import org.ofbiz.service.GenericServiceException; 50 import org.ofbiz.service.LocalDispatcher; 51 import org.ofbiz.service.ModelService; 52 import org.ofbiz.service.ServiceDispatcher; 53 import org.ofbiz.service.WebAppDispatcher; 54 import org.ofbiz.service.calendar.RecurrenceRule; 55 56 63 public class CoreEvents { 64 65 public static final String module = CoreEvents.class.getName(); 66 public static final String err_resource = "WebappUiLabels"; 67 68 74 public static String returnSuccess(HttpServletRequest request, HttpServletResponse response) { 75 return "success"; 76 } 77 78 84 public static String returnError(HttpServletRequest request, HttpServletResponse response) { 85 return "error"; 86 } 87 88 94 public static String returnNull(HttpServletRequest request, HttpServletResponse response) { 95 return null; 96 } 97 98 104 public static String changeDelegator(HttpServletRequest request, HttpServletResponse response) { 105 String delegatorName = request.getParameter("delegator"); 106 Security security = (Security) request.getAttribute("security"); 107 Locale locale = UtilHttp.getLocale(request); 108 109 if (!security.hasPermission("ENTITY_MAINT", request.getSession())) { 110 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.not_authorized_use_fct", locale); 111 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 112 return "error"; 113 } 114 if (delegatorName == null) { 115 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.delegator_not_passed", locale); 116 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 117 return "error"; 118 } 119 120 GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorName); 121 122 if (delegator == null) { 123 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.no_delegator_name_defined", locale); 124 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 125 return "error"; 126 } 127 128 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 130 DispatchContext dctx = dispatcher.getDispatchContext(); 131 String dispatcherName = dispatcher.getName(); 132 133 if (dispatcherName == null) { 134 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.dispatcher_name_null", locale); 135 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 136 return "error"; 137 } 138 if (dctx == null) { 139 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.dispatcher_context_null", locale); 140 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 141 return "error"; 142 } 143 144 ServiceDispatcher sd = ServiceDispatcher.getInstance(dispatcherName, delegator); 145 146 if (sd == null) 147 dispatcher = new WebAppDispatcher(dctx, delegator); 148 else 149 dispatcher = sd.getLocalContext(dispatcherName).getDispatcher(); 150 151 request.getSession().setAttribute("delegator", delegator); 152 request.getSession().setAttribute("dispatcher", dispatcher); 153 154 return "success"; 155 } 156 157 163 public static String changeDispatcher(HttpServletRequest request, HttpServletResponse response) { 164 String dispatcherName = request.getParameter("dispatcher"); 165 Security security = (Security) request.getAttribute("security"); 166 Locale locale = UtilHttp.getLocale(request); 167 168 if (!security.hasPermission("ENTITY_MAINT", request.getSession())) { 169 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.not_authorized_use_fct", locale); 170 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 171 return "error"; 172 } 173 if (dispatcherName == null) { 174 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.dispatcher_not_passed", locale); 175 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 176 return "error"; 177 } 178 179 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 180 ServiceDispatcher sd = ServiceDispatcher.getInstance(dispatcherName, delegator); 181 182 if (sd == null) { 183 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.no_dispachter_name_registered", locale); 184 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 185 return "error"; 186 } 187 LocalDispatcher dispatcher = sd.getLocalContext(dispatcherName).getDispatcher(); 188 189 request.getSession().setAttribute("dispatcher", dispatcher); 190 return "success"; 191 } 192 193 206 public static String scheduleService(HttpServletRequest request, HttpServletResponse response) { 207 GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin"); 208 Security security = (Security) request.getAttribute("security"); 209 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 210 Locale locale = UtilHttp.getLocale(request); 212 213 Map params = UtilHttp.getParameterMap(request); 214 String serviceName = (String ) params.remove("SERVICE_NAME"); 216 String poolName = (String ) params.remove("POOL_NAME"); 217 String serviceTime = (String ) params.remove("SERVICE_TIME"); 218 String serviceEndTime = (String ) params.remove("SERVICE_END_TIME"); 219 String serviceFreq = (String ) params.remove("SERVICE_FREQUENCY"); 220 String serviceIntr = (String ) params.remove("SERVICE_INTERVAL"); 221 String serviceCnt = (String ) params.remove("SERVICE_COUNT"); 222 String retryCnt = (String ) params.remove("SERVICE_MAXRETRY"); 223 224 Map freqMap = new HashMap (); 226 227 freqMap.put("SECONDLY", new Integer (1)); 228 freqMap.put("MINUTELY", new Integer (2)); 229 freqMap.put("HOURLY", new Integer (3)); 230 freqMap.put("DAILY", new Integer (4)); 231 freqMap.put("WEEKLY", new Integer (5)); 232 freqMap.put("MONTHLY", new Integer (6)); 233 freqMap.put("YEARLY", new Integer (7)); 234 235 long startTime = (new Date ()).getTime(); 237 long endTime = 0; 238 int maxRetry = -1; 239 int count = 1; 240 int interval = 1; 241 int frequency = RecurrenceRule.DAILY; 242 243 StringBuffer errorBuf = new StringBuffer (); 244 245 if (serviceName == null) { 247 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.must_specify_service", locale); 248 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 249 return "error"; 250 } 251 252 ModelService modelService = null; 254 try { 255 modelService = dispatcher.getDispatchContext().getModelService(serviceName); 256 } catch (GenericServiceException e) { 257 Debug.logError(e, "Error looking up ModelService for serviceName [" + serviceName + "]", module); 258 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.error_modelservice_for_srv_name", locale); 259 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + " [" + serviceName + "]: " + e.toString()); 260 return "error"; 261 } 262 if (modelService == null) { 263 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_name_not_find", locale); 264 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + " [" + serviceName + "]"); 265 return "error"; 266 } 267 268 Map serviceContext = new HashMap (); 270 Iterator ci = modelService.getInParamNames().iterator(); 271 while (ci.hasNext()) { 272 String name = (String ) ci.next(); 273 274 if ("userLogin".equals(name)) continue; 276 if ("locale".equals(name)) continue; 278 279 Object value = request.getParameter(name); 280 281 if (value == null) { 283 value = request.getAttribute(name); 284 } 285 if (value == null) { 286 value = request.getSession().getAttribute(name); 287 } 288 if (value == null) { 289 continue; 291 } 292 293 if (value instanceof String && ((String ) value).length() == 0) { 294 value = null; 296 } 297 298 serviceContext.put(name, value); 300 } 301 serviceContext = modelService.makeValid(serviceContext, ModelService.IN_PARAM, true, null, locale); 302 303 if (userLogin != null) { 304 serviceContext.put("userLogin", userLogin); 305 } 306 307 if (locale != null) { 308 serviceContext.put("locale", locale); 309 } 310 311 if (!modelService.export && !security.hasPermission("SERVICE_INVOKE_ANY", request.getSession())) { 312 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.not_authorized_to_call", locale); 313 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 314 return "error"; 315 } 316 317 if (serviceTime != null && serviceTime.length() > 0) { 319 try { 320 Timestamp ts1 = Timestamp.valueOf(serviceTime); 321 startTime = ts1.getTime(); 322 } catch (IllegalArgumentException e) { 323 try { 324 startTime = Long.parseLong(serviceTime); 325 } catch (NumberFormatException nfe) { 326 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.invalid_format_time", locale); 327 errorBuf.append("<li>" + errMsg); 328 } 329 } 330 if (startTime < (new Date ()).getTime()) { 331 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_time_already_passed", locale); 332 errorBuf.append("<li>" + errMsg); 333 } 334 } 335 if (serviceEndTime != null && serviceEndTime.length() > 0) { 336 try { 337 Timestamp ts1 = Timestamp.valueOf(serviceEndTime); 338 endTime = ts1.getTime(); 339 } catch (IllegalArgumentException e) { 340 try { 341 endTime = Long.parseLong(serviceTime); 342 } catch (NumberFormatException nfe) { 343 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.invalid_format_time", locale); 344 errorBuf.append("<li>" + errMsg); 345 } 346 } 347 if (endTime < (new Date ()).getTime()) { 348 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_time_already_passed", locale); 349 errorBuf.append("<li>" + errMsg); 350 } 351 } 352 if (serviceIntr != null && serviceIntr.length() > 0) { 353 try { 354 interval = Integer.parseInt(serviceIntr); 355 } catch (NumberFormatException nfe) { 356 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.invalid_format_interval", locale); 357 errorBuf.append("<li>" + errMsg); 358 } 359 } 360 if (serviceCnt != null && serviceCnt.length() > 0) { 361 try { 362 count = Integer.parseInt(serviceCnt); 363 } catch (NumberFormatException nfe) { 364 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.invalid_format_count", locale); 365 errorBuf.append("<li>" + errMsg); 366 } 367 } 368 if (serviceFreq != null && serviceFreq.length() > 0) { 369 int parsedValue = 0; 370 371 try { 372 parsedValue = Integer.parseInt(serviceFreq); 373 if (parsedValue > 0 && parsedValue < 8) 374 frequency = parsedValue; 375 } catch (NumberFormatException nfe) { 376 parsedValue = 0; 377 } 378 if (parsedValue == 0) { 379 if (!freqMap.containsKey(serviceFreq.toUpperCase())) { 380 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.invalid_format_frequency", locale); 381 errorBuf.append("<li>" + errMsg); 382 } else { 383 frequency = ((Integer ) freqMap.get(serviceFreq.toUpperCase())).intValue(); 384 } 385 } 386 } 387 if (retryCnt != null && retryCnt.length() > 0) { 388 int parsedValue = -2; 389 390 try { 391 parsedValue = Integer.parseInt(retryCnt); 392 } catch (NumberFormatException e) { 393 parsedValue = -2; 394 } 395 if (parsedValue > -2) { 396 maxRetry = parsedValue; 397 } else { 398 maxRetry = modelService.maxRetry; 399 } 400 } else { 401 maxRetry = modelService.maxRetry; 402 } 403 404 if (errorBuf.length() > 0) { 406 request.setAttribute("_ERROR_MESSAGE_", errorBuf.toString()); 407 return "error"; 408 } 409 410 try { 412 dispatcher.schedule(poolName, serviceName, serviceContext, startTime, frequency, interval, count, endTime, maxRetry); 413 } catch (GenericServiceException e) { 414 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_dispatcher_exception", locale); 415 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + e.getMessage()); 416 return "error"; 417 } 418 419 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_scheduled", locale); 420 request.setAttribute("_EVENT_MESSAGE_", errMsg); 421 return "success"; 422 } 423 424 public static ServiceEventHandler seh = new ServiceEventHandler(); 425 426 435 public static String runService(HttpServletRequest request, HttpServletResponse response) { 436 String serviceName = request.getParameter("serviceName"); 438 String mode = request.getParameter("mode"); 439 Locale locale = UtilHttp.getLocale(request); 440 441 if (UtilValidate.isEmpty(serviceName)) { 442 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.must_specify_service_name", locale); 443 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg); 444 return "error"; 445 } 446 447 if (UtilValidate.isEmpty(mode)) { 448 mode = "sync"; 449 } 450 451 453 Security security = (Security) request.getAttribute("security"); 454 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 455 456 ModelService modelService = null; 458 try { 459 modelService = dispatcher.getDispatchContext().getModelService(serviceName); 460 } catch (GenericServiceException e) { 461 Debug.logError(e, "Error looking up ModelService for serviceName [" + serviceName + "]", module); 462 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.error_modelservice_for_srv_name", locale); 463 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "[" + serviceName + "]: " + e.toString()); 464 return "error"; 465 } 466 if (modelService == null) { 467 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_name_not_find", locale); 468 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "[" + serviceName + "]"); 469 return "error"; 470 } 471 472 if (!modelService.export && !security.hasPermission("SERVICE_INVOKE_ANY", request.getSession())) { 473 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.not_authorized_to_call", locale); 474 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + "."); 475 return "error"; 476 } 477 478 Debug.logInfo("Running service named [" + serviceName + "] from event with mode [" + mode + "]", module); 479 480 try { 483 return seh.invoke(mode, serviceName, request, response); 484 } catch (EventHandlerException e) { 485 String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_eventhandler_exception", locale); 486 request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + ": " + e.getMessage()); 487 return "error"; 488 } 489 } 490 491 public static String streamFile(HttpServletRequest request, HttpServletResponse response) { 492 String filePath = RequestHandler.getNextPageUri(request.getPathInfo()); 494 496 File file = new File (filePath); 498 if (file.exists()) { 499 Long longLen = new Long (file.length()); 500 int length = longLen.intValue(); 501 try { 502 FileInputStream fis = new FileInputStream (file); 503 UtilHttp.streamContentToBrowser(response, fis, length, null); 504 fis.close(); 505 } catch (FileNotFoundException e) { 506 Debug.logError(e, module); 507 return "error"; 508 } catch (IOException e) { 509 Debug.logError(e, module); 510 return "error"; 511 } 512 } 513 return null; 514 } 515 } 516 | Popular Tags |