Ejemplo n.º 1
0
  private Map<String, Object> serviceInvoker(
      String localName, ModelService modelService, Map<String, Object> context)
      throws GenericServiceException {
    if (UtilValidate.isEmpty(modelService.location)) {
      throw new GenericServiceException("Cannot run Groovy service with empty location");
    }
    Map<String, Object> params = FastMap.newInstance();
    params.putAll(context);
    context.put(ScriptUtil.PARAMETERS_KEY, params);

    DispatchContext dctx = dispatcher.getLocalContext(localName);
    context.put("dctx", dctx);
    context.put("dispatcher", dctx.getDispatcher());
    context.put("delegator", dispatcher.getDelegator());
    try {
      ScriptContext scriptContext = ScriptUtil.createScriptContext(context, protectedKeys);
      ScriptHelper scriptHelper =
          (ScriptHelper) scriptContext.getAttribute(ScriptUtil.SCRIPT_HELPER_KEY);
      if (scriptHelper != null) {
        context.put(ScriptUtil.SCRIPT_HELPER_KEY, scriptHelper);
      }
      Script script =
          InvokerHelper.createScript(
              GroovyUtil.getScriptClassFromLocation(
                  this.getLocation(modelService), groovyClassLoader),
              GroovyUtil.getBinding(context));
      Object resultObj = null;
      if (UtilValidate.isEmpty(modelService.invoke)) {
        resultObj = script.run();
      } else {
        resultObj = script.invokeMethod(modelService.invoke, EMPTY_ARGS);
      }
      if (resultObj == null) {
        resultObj = scriptContext.getAttribute(ScriptUtil.RESULT_KEY);
      }
      if (resultObj != null && resultObj instanceof Map<?, ?>) {
        return cast(resultObj);
      }
      Map<String, Object> result = ServiceUtil.returnSuccess();
      result.putAll(
          modelService.makeValid(scriptContext.getBindings(ScriptContext.ENGINE_SCOPE), "OUT"));
      return result;
    } catch (GeneralException ge) {
      throw new GenericServiceException(ge);
    } catch (Exception e) {
      return ServiceUtil.returnError(e.getMessage());
    }
  }
 @Override
 protected Object get(Map<String, ? extends Object> context, TimeZone timeZone, Locale locale) {
   try {
     Object obj =
         InvokerHelper.createScript(this.parsedScript, GroovyUtil.getBinding(context)).run();
     if (obj != null) {
       return obj;
     } else {
       if (Debug.verboseOn()) {
         Debug.logVerbose(
             "Groovy scriptlet evaluated to null ["
                 + this
                 + "], got no return so inserting nothing.",
             module);
       }
     }
   } catch (Exception e) {
     // handle other things, like the groovy.lang.MissingPropertyException
     Debug.logWarning(
         e,
         "Error evaluating Groovy scriptlet [" + this + "], inserting nothing; error was: " + e,
         module);
   }
   return null;
 }
Ejemplo n.º 3
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);
    }
  }
Ejemplo n.º 4
0
  public void runTest() {

    Map<String, Object> map = this.parent.getMap();
    map.put("url", this.urlName);
    try {
      String scriptText = TestUtils.readUrlText(this.urlName);
      Class scriptClass = GroovyUtil.parseClass(scriptText);

      Binding binding = new Binding();
      binding.setVariable("context", map);
      binding.setVariable("seleniumXml", this.parent);
      InvokerHelper.createScript(scriptClass, binding).run();
    } catch (MalformedURLException e) {
      System.out.println("Scriptrunner, runTest, MalformedURLException error: " + e.getMessage());
    } catch (IOException e) {
      System.out.println("Scriptrunner, runTest, IOException error: " + e.getMessage());
    }
  }
  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";
  }
 protected GroovyElem(char[] chars, int offset, int length, int parseStart, int parseLength) {
   super(chars, offset, length);
   this.parsedScript = GroovyUtil.parseClass(new String(chars, parseStart, parseLength));
 }