Search in sources :

Example 6 with RequestMap

use of org.apache.ofbiz.webapp.control.ConfigXMLReader.RequestMap in project ofbiz-framework by apache.

the class ScriptEventHandler method invoke.

@Override
public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
    try {
        Map<String, Object> context = new HashMap<String, Object>();
        context.put("request", request);
        context.put("response", response);
        HttpSession session = request.getSession();
        context.put("session", session);
        context.put("dispatcher", request.getAttribute("dispatcher"));
        context.put("delegator", request.getAttribute("delegator"));
        context.put("security", request.getAttribute("security"));
        context.put("locale", UtilHttp.getLocale(request));
        context.put("timeZone", UtilHttp.getTimeZone(request));
        context.put("userLogin", session.getAttribute("userLogin"));
        context.put(ScriptUtil.PARAMETERS_KEY, UtilHttp.getCombinedMap(request, UtilMisc.toSet("delegator", "dispatcher", "security", "locale", "timeZone", "userLogin")));
        Object result = null;
        try {
            ScriptContext scriptContext = ScriptUtil.createScriptContext(context, protectedKeys);
            result = ScriptUtil.executeScript(event.path, event.invoke, scriptContext, null);
            if (result == null) {
                result = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
            }
        } catch (Exception e) {
            Debug.logWarning(e, "Error running event " + event.path + ": ", module);
            request.setAttribute("_ERROR_MESSAGE_", e.getMessage());
            return "error";
        }
        if (result instanceof Map) {
            Map resultMap = (Map) result;
            String successMessage = (String) resultMap.get("_event_message_");
            if (successMessage != null) {
                request.setAttribute("_EVENT_MESSAGE_", successMessage);
            }
            String errorMessage = (String) resultMap.get("_error_message_");
            if (errorMessage != null) {
                request.setAttribute("_ERROR_MESSAGE_", errorMessage);
            }
            return (String) resultMap.get("_response_code_");
        }
        if (result != null && !(result instanceof String)) {
            throw new EventHandlerException("Event did not return a String result, it returned a " + result.getClass().getName());
        }
        return (String) result;
    } catch (Exception e) {
        throw new EventHandlerException("Error running event " + event.path, e);
    }
}
Also used : HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) ScriptContext(javax.script.ScriptContext) HashMap(java.util.HashMap) Map(java.util.Map) RequestMap(org.apache.ofbiz.webapp.control.ConfigXMLReader.RequestMap)

Aggregations

RequestMap (org.apache.ofbiz.webapp.control.ConfigXMLReader.RequestMap)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 HttpSession (javax.servlet.http.HttpSession)4 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Locale (java.util.Locale)2 TimeZone (java.util.TimeZone)2 ScriptContext (javax.script.ScriptContext)2 GenericValue (org.apache.ofbiz.entity.GenericValue)2 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)2 DispatchContext (org.apache.ofbiz.service.DispatchContext)2 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)2 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)2 ModelParam (org.apache.ofbiz.service.ModelParam)2 ModelService (org.apache.ofbiz.service.ModelService)2 ServiceAuthException (org.apache.ofbiz.service.ServiceAuthException)2 ServiceValidationException (org.apache.ofbiz.service.ServiceValidationException)2 ConfigXMLReader (org.apache.ofbiz.webapp.control.ConfigXMLReader)2 WebAppConfigurationException (org.apache.ofbiz.webapp.control.WebAppConfigurationException)2