📄 coreevents.java
字号:
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
Locale locale = UtilHttp.getLocale(request);
// lookup the service definition to see if this service is externally available, if not require the SERVICE_INVOKE_ANY permission
ModelService modelService = null;
try {
modelService = dispatcher.getDispatchContext().getModelService(serviceName);
} catch (GenericServiceException e) {
Debug.logError(e, "Error looking up ModelService for serviceName [" + serviceName + "]", module);
request.setAttribute("_ERROR_MESSAGE_", "<li>Error looking up ModelService for serviceName [" + serviceName + "]: " + e.toString());
return "error";
}
if (modelService == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Could not find a service with the serviceName [" + serviceName + "]");
return "error";
}
// make the context valid; using the makeValid method from ModelService
Map serviceContext = new HashMap();
Iterator ci = modelService.getInParamNames().iterator();
while (ci.hasNext()) {
String name = (String) ci.next();
// don't include userLogin, that's taken care of below
if ("userLogin".equals(name)) continue;
// don't include locale, that is also taken care of below
if ("locale".equals(name)) continue;
Object value = request.getParameter(name);
// if the parameter wasn't passed and no other value found, don't pass on the null
if (value == null) {
value = request.getAttribute(name);
}
if (value == null) {
value = request.getSession().getAttribute(name);
}
if (value == null) {
// still null, give up for this one
continue;
}
if (value instanceof String && ((String) value).length() == 0) {
// interpreting empty fields as null values for each in back end handling...
value = null;
}
// set even if null so that values will get nulled in the db later on
serviceContext.put(name, value);
}
serviceContext = modelService.makeValid(serviceContext, ModelService.IN_PARAM);
if (userLogin != null) {
serviceContext.put("userLogin", userLogin);
}
if (locale != null) {
serviceContext.put("locale", locale);
}
if (!modelService.export && !security.hasPermission("SERVICE_INVOKE_ANY", request.getSession())) {
request.setAttribute("_ERROR_MESSAGE_", "<li>You are not authorized to call this non-exported service, you must be logged in and have the SERVICE_INVOKE_ANY permission.");
return "error";
}
// some conversions
if (serviceTime != null && serviceTime.length() > 0) {
try {
Timestamp ts1 = Timestamp.valueOf(serviceTime);
startTime = ts1.getTime();
} catch (IllegalArgumentException e) {
try {
startTime = Long.parseLong(serviceTime);
} catch (NumberFormatException nfe) {
errorBuf.append("<li>Invalid format for SERVICE_TIME");
}
}
if (startTime < (new Date()).getTime()) {
errorBuf.append("<li>SERVICE_TIME has already passed");
}
}
if (serviceEndTime != null && serviceEndTime.length() > 0) {
try {
Timestamp ts1 = Timestamp.valueOf(serviceEndTime);
endTime = ts1.getTime();
} catch (IllegalArgumentException e) {
try {
endTime = Long.parseLong(serviceTime);
} catch (NumberFormatException nfe) {
errorBuf.append("<li>Invalid format for SERVICE_TIME");
}
}
if (endTime < (new Date()).getTime()) {
errorBuf.append("<li>SERVICE_TIME has already passed");
}
}
if (serviceIntr != null && serviceIntr.length() > 0) {
try {
interval = Integer.parseInt(serviceIntr);
} catch (NumberFormatException nfe) {
errorBuf.append("<li>Invalid format for SERVICE_INTERVAL");
}
}
if (serviceCnt != null && serviceCnt.length() > 0) {
try {
count = Integer.parseInt(serviceCnt);
} catch (NumberFormatException nfe) {
errorBuf.append("<li>Invalid format for SERVICE_COUNT");
}
}
if (serviceFreq != null && serviceFreq.length() > 0) {
int parsedValue = 0;
try {
parsedValue = Integer.parseInt(serviceFreq);
if (parsedValue > 0 && parsedValue < 8)
frequency = parsedValue;
} catch (NumberFormatException nfe) {
parsedValue = 0;
}
if (parsedValue == 0) {
if (!freqMap.containsKey(serviceFreq.toUpperCase())) {
errorBuf.append("<li>Invalid format for SERIVCE_FREQUENCY");
} else {
frequency = ((Integer) freqMap.get(serviceFreq.toUpperCase())).intValue();
}
}
}
// return the errors
if (errorBuf.length() > 0) {
request.setAttribute("_ERROR_MESSAGE_", errorBuf.toString());
return "error";
}
// schedule service
try {
dispatcher.schedule(poolName, serviceName, serviceContext, startTime, frequency, interval, count, endTime);
} catch (GenericServiceException e) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Service dispatcher threw an exception: " + e.getMessage());
return "error";
}
request.setAttribute("_EVENT_MESSAGE_", "<li>Service has been scheduled");
return "success";
}
public static ServiceEventHandler seh = new ServiceEventHandler();
/**
* Run a service.
* Request Parameters which are used for this event:
* SERVICE_NAME - Name of the service to invoke
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String runService(HttpServletRequest request, HttpServletResponse response) {
// get the mode and service name
String serviceName = request.getParameter("serviceName");
String mode = request.getParameter("mode");
if (UtilValidate.isEmpty(serviceName)) {
request.setAttribute("_ERROR_MESSAGE_", "<li>You must specify a 'serviceName', and optionally a 'mode' (sync or async, defaults to sync).");
return "error";
}
if (UtilValidate.isEmpty(mode)) {
mode = "sync";
}
// now do a security check
Security security = (Security) request.getAttribute("security");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
//lookup the service definition to see if this service is externally available, if not require the SERVICE_INVOKE_ANY permission
ModelService modelService = null;
try {
modelService = dispatcher.getDispatchContext().getModelService(serviceName);
} catch (GenericServiceException e) {
Debug.logError(e, "Error looking up ModelService for serviceName [" + serviceName + "]", module);
request.setAttribute("_ERROR_MESSAGE_", "<li>Error looking up ModelService for serviceName [" + serviceName + "]: " + e.toString());
return "error";
}
if (modelService == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Could not find a service with the serviceName [" + serviceName + "]");
return "error";
}
if (!modelService.export && !security.hasPermission("SERVICE_INVOKE_ANY", request.getSession())) {
request.setAttribute("_ERROR_MESSAGE_", "<li>You are not authorized to call this non-exported service, you must be logged in and have the SERVICE_INVOKE_ANY permission.");
return "error";
}
Debug.logInfo("Running service named [" + serviceName + "] from event with mode [" + mode + "]", module);
// call the service via the ServiceEventHandler which
// adapts an event to a service.
try {
return seh.invoke(mode, serviceName, request, response);
} catch (EventHandlerException e) {
request.setAttribute("_ERROR_MESSAGE_", "<li>ServiceEventHandler threw an exception: " + e.getMessage());
return "error";
}
}
public static String streamFile(HttpServletRequest request, HttpServletResponse response) {
RequestHandler rh = (RequestHandler) request.getAttribute("_REQUEST_HANDLER_");
String filePath = RequestHandler.getNextPageUri(request.getPathInfo());
String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
// load the file
File file = new File(filePath);
if (file.exists()) {
Long longLen = new Long(file.length());
int length = longLen.intValue();
try {
FileInputStream fis = new FileInputStream(file);
UtilHttp.streamContentToBrowser(response, fis, length, null);
fis.close();
} catch (FileNotFoundException e) {
Debug.logError(e, module);
return "error";
} catch (IOException e) {
Debug.logError(e, module);
return "error";
}
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -