Exemplo n.º 1
0
  public String invoke(
      Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response)
      throws EventHandlerException {
    try {
      Map<String, Object> groovyContext = FastMap.newInstance();
      groovyContext.put("request", request);
      groovyContext.put("response", response);
      HttpSession session = request.getSession();
      groovyContext.put("session", session);

      groovyContext.put("dispatcher", request.getAttribute("dispatcher"));
      groovyContext.put("delegator", request.getAttribute("delegator"));
      groovyContext.put("security", request.getAttribute("security"));
      groovyContext.put("locale", UtilHttp.getLocale(request));
      groovyContext.put("timeZone", UtilHttp.getTimeZone(request));
      groovyContext.put("userLogin", session.getAttribute("userLogin"));
      groovyContext.put(
          "parameters",
          UtilHttp.getCombinedMap(
              request,
              UtilMisc.toSet(
                  "delegator", "dispatcher", "security", "locale", "timeZone", "userLogin")));

      Object result = GroovyUtil.runScriptAtLocation(event.path + event.invoke, groovyContext);
      // check the result
      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("Groovy Event Error", e);
    }
  }
  public String invoke(
      Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response)
      throws EventHandlerException {

    if (Debug.infoOn()) {
      Debug.logInfo("In GwtRpcGroovyEventHandler", module);
    }

    String requestPayload = null;

    try {

      requestPayload = GwtRpcPayloadUtil.getRequestPayload(request);

    } catch (IOException ioe) {
      throw new EventHandlerException("Exception while getting requestPayload", ioe);
    } catch (ServletException se) {
      throw new EventHandlerException("Exception while getting requestPayload", se);
    }

    HashMap<String, String> gwtParameters = GwtRpcPayloadUtil.getParameters(requestPayload);
    if (Debug.infoOn()) {
      Debug.logInfo("gwtParameters : " + gwtParameters, module);
    }

    if (null != gwtParameters) {

      HttpSession session = request.getSession();

      Map<String, Object> groovyContext = FastMap.newInstance();
      groovyContext.put("request", request);
      groovyContext.put("response", response);
      groovyContext.put("session", session);
      groovyContext.put("dispatcher", request.getAttribute("dispatcher"));
      groovyContext.put("delegator", request.getAttribute("delegator"));
      groovyContext.put("security", request.getAttribute("security"));
      groovyContext.put("locale", UtilHttp.getLocale(request));
      groovyContext.put("timeZone", UtilHttp.getTimeZone(request));
      groovyContext.put("userLogin", session.getAttribute("userLogin"));

      Map<String, Object> parameters =
          UtilHttp.getCombinedMap(
              request,
              UtilMisc.toSet(
                  "delegator", "dispatcher", "security", "locale", "timeZone", "userLogin"));
      if (Debug.infoOn()) {
        Debug.logInfo("parameters : " + parameters, module);
      }

      Set<String> keys = gwtParameters.keySet();
      Iterator<String> iter = keys.iterator();

      while (iter.hasNext()) {
        String key = iter.next();
        parameters.put(key, gwtParameters.get(key));
      }

      groovyContext.put("parameters", parameters);

      Object result = null;

      try {
        result = GroovyUtil.runScriptAtLocation(event.path + event.invoke, groovyContext);
        if (Debug.infoOn()) {
          Debug.logInfo("groovy script result : " + result, module);
        }
      } catch (GeneralException ge) {
        throw new EventHandlerException("Exception while executing groovy script : ", ge);
      }

      Map<String, Object> resultMap = (Map<String, Object>) result;

      // ServletContext sc = (ServletContext)request.getAttribute("servletContext");
      // RequestDispatcher rd = sc.getRequestDispatcher("/gwtrpc");

      request.setAttribute(GwtRpcPayload.OFBIZ_PAYLOAD, resultMap);
      request.setAttribute(GwtRpcPayload.REQUEST_PAYLOAD, requestPayload);

      /*try {
          rd.forward(request, response);
      } catch(IOException ioe) {
          throw new EventHandlerException("IO Exception while forwarding request to GWT RPC servlet  : ",ioe);
      } catch(ServletException se) {
          throw new EventHandlerException("Servlet Exception while forwarding request to GWT RPC servlet  : ",se);
      }*/

      try {

        GwtRpcServletUtil servletUtil = new GwtRpcServletUtil();
        String responsePayload = servletUtil.invokeServlet(request, response, requestPayload);
        servletUtil.writeResponse(servletContext, request, response, responsePayload);

      } catch (Exception e) {
        Debug.logError(e, "gwt remote servlet invocation failed " + e.getMessage(), module);
        throw new EventHandlerException(e);
      }

    } else {
      throw new EventHandlerException("GWT parameters are null  : " + gwtParameters);
    }

    return "success";
  }