@SuppressWarnings("unchecked")
    private void configureUrlMapping(UrlMapping urlMapping) {
      if (binding != null) {
        Map<String, Object> vars = binding.getVariables();
        for (String key : vars.keySet()) {
          if (isNotCoreMappingKey(key)) {
            parameterValues.put(key, vars.get(key));
          }
        }

        binding.getVariables().clear();
      }

      // Add the controller and action to the params map if
      // they are set. This ensures consistency of behaviour
      // for the application, i.e. "controller" and "action"
      // parameters will always be available to it.
      if (urlMapping.getControllerName() != null) {
        parameterValues.put("controller", urlMapping.getControllerName());
      }
      if (urlMapping.getActionName() != null) {
        parameterValues.put("action", urlMapping.getActionName());
      }

      urlMapping.setParameterValues(parameterValues);
      urlMappings.add(urlMapping);
    }
Esempio n. 2
0
 @SuppressWarnings("unchecked")
 public static void bindingsToVelocityInternalContext(
     Binding binding, InternalContextAdapter context) {
   if (context == null || binding == null) return;
   for (Object e : binding.getVariables().entrySet()) {
     Entry<String, Object> var = (Entry<String, Object>) e;
     context.put(var.getKey(), var.getValue());
   }
 }
 /** Return a script object with the given vars from the compiled script object */
 private Script createScript(Object compiledScript, Map<String, Object> vars)
     throws InstantiationException, IllegalAccessException {
   Class scriptClass = (Class) compiledScript;
   Script scriptObject = (Script) scriptClass.newInstance();
   Binding binding = new Binding();
   binding.getVariables().putAll(vars);
   scriptObject.setBinding(binding);
   return scriptObject;
 }
Esempio n. 4
0
      @SuppressWarnings("unchecked")
      private void inheritBinding(final Binding binding) {
        if (binding == null) {
          return;
        }

        for (Object field : binding.getVariables().entrySet()) {
          this.binding.setVariable(
              ((Map.Entry<String, Object>) field).getKey(),
              ((Map.Entry<String, Object>) field).getValue());
        }
      }
  public void loadEventsScript(File eventScript) {
    if (eventScript == null) {
      return;
    }

    GrailsConsole console = GrailsConsole.getInstance();
    try {
      Class<?> scriptClass = classLoader.parseClass(eventScript);
      if (scriptClass == null) {
        console.error("Could not load event script (script may be empty): " + eventScript);
        return;
      }

      Script script = (Script) scriptClass.newInstance();
      script.setBinding(
          new Binding(binding.getVariables()) {
            @SuppressWarnings("rawtypes")
            @Override
            public void setVariable(String var, Object o) {
              final Matcher matcher = EVENT_NAME_PATTERN.matcher(var);
              if (matcher.matches() && (o instanceof Closure)) {
                String eventName = matcher.group(1);
                List<Closure> hooks = globalEventHooks.get(eventName);
                if (hooks == null) {
                  hooks = new ArrayList<Closure>();
                  globalEventHooks.put(eventName, hooks);
                }
                hooks.add((Closure<?>) o);
              }
              super.setVariable(var, o);
            }
          });
      script.run();
    } catch (Throwable e) {
      StackTraceUtils.deepSanitize(e);
      console.error(
          "Error loading event script from file [" + eventScript + "] " + e.getMessage(), e);
    }
  }
    private UrlMapping getURLMappingForNamedArgs(
        Map namedArguments, UrlMappingData urlData, String mapping, boolean isResponseCode) {
      @SuppressWarnings("hiding")
      Object controllerName;
      @SuppressWarnings("hiding")
      Object actionName;
      final Map bindingVariables = binding != null ? binding.getVariables() : null;
      boolean restRequest = false;
      if (namedArguments.containsKey(RESOURCE)) {
        controllerName = namedArguments.get(RESOURCE);
        actionName = DEFAULT_REST_MAPPING;
        restRequest = true;
      } else {
        controllerName = getControllerName(namedArguments, bindingVariables);
        actionName = getActionName(namedArguments, bindingVariables);
      }

      @SuppressWarnings("hiding")
      Object viewName = getViewName(namedArguments, bindingVariables);
      if (actionName != null && viewName != null) {
        viewName = null;
        LOG.warn(
            "Both [action] and [view] specified in URL mapping ["
                + mapping
                + "]. The action takes precendence!");
      }

      @SuppressWarnings("hiding")
      Object uri = getURI(namedArguments, bindingVariables);
      ConstrainedProperty[] constraints =
          previousConstraints.toArray(new ConstrainedProperty[previousConstraints.size()]);

      UrlMapping urlMapping;
      if (uri != null) {
        try {
          urlMapping = new RegexUrlMapping(urlData, new URI(uri.toString()), constraints, sc);
        } catch (URISyntaxException e) {
          throw new UrlMappingException("Cannot map to invalid URI: " + e.getMessage(), e);
        }
      } else {
        urlMapping =
            createURLMapping(
                urlData, isResponseCode, controllerName, actionName, viewName, constraints);
      }

      Object exceptionArg = getException(namedArguments, bindingVariables);

      if (isResponseCode && exceptionArg != null) {
        if (exceptionArg instanceof Class) {
          Class exClass = (Class) exceptionArg;
          if (Throwable.class.isAssignableFrom(exClass)) {
            ((ResponseCodeUrlMapping) urlMapping).setExceptionType(exClass);
          } else {
            LOG.error(
                "URL mapping argument [exception] with value ["
                    + exceptionArg
                    + "] must be a subclass of java.lang.Throwable");
          }
        } else {
          LOG.error(
              "URL mapping argument [exception] with value ["
                  + exceptionArg
                  + "] must be a valid class");
        }
      }

      if (restRequest) {
        urlMapping.setParseRequest(true);
        urlMapping.setRestfulMapping(true);
      } else {
        @SuppressWarnings("hiding")
        Object parseRequest = getParseRequest(namedArguments, bindingVariables);
        if (parseRequest instanceof Boolean) {
          urlMapping.setParseRequest((Boolean) parseRequest);
        }
      }

      if (actionName instanceof Map) {
        urlMapping.setRestfulMapping(true);
      }

      return urlMapping;
    }
    private Object _invoke(String methodName, Object arg, Object delegate) {
      Object[] args = (Object[]) arg;
      final boolean isResponseCode = isResponseCode(methodName);
      if (methodName.startsWith(SLASH) || isResponseCode) {
        // Create a new parameter map for this mapping.
        parameterValues = new HashMap<String, Object>();
        try {
          urlDefiningMode = false;
          args = args != null && args.length > 0 ? args : new Object[] {Collections.EMPTY_MAP};
          if (args[0] instanceof Closure) {
            UrlMappingData urlData = createUrlMappingData(methodName, isResponseCode);

            Closure callable = (Closure) args[0];
            if (delegate != null) callable.setDelegate(delegate);
            callable.call();

            @SuppressWarnings("hiding")
            Object controllerName;
            @SuppressWarnings("hiding")
            Object actionName;
            @SuppressWarnings("hiding")
            Object viewName;
            @SuppressWarnings("hiding")
            Object uri;

            if (binding != null) {
              controllerName = binding.getVariables().get(GrailsControllerClass.CONTROLLER);
              actionName = binding.getVariables().get(GrailsControllerClass.ACTION);
              viewName = binding.getVariables().get(GrailsControllerClass.VIEW);
              uri = binding.getVariables().get("uri");
            } else {
              controllerName = this.controllerName;
              actionName = this.actionName;
              viewName = this.viewName;
              uri = this.uri;
            }

            ConstrainedProperty[] constraints =
                previousConstraints.toArray(new ConstrainedProperty[previousConstraints.size()]);
            UrlMapping urlMapping;
            if (uri != null) {
              try {
                urlMapping = new RegexUrlMapping(urlData, new URI(uri.toString()), constraints, sc);
              } catch (URISyntaxException e) {
                throw new UrlMappingException("Cannot map to invalid URI: " + e.getMessage(), e);
              }
            } else {
              urlMapping =
                  createURLMapping(
                      urlData, isResponseCode, controllerName, actionName, viewName, constraints);
            }

            if (binding != null) {
              Map bindingVariables = binding.getVariables();
              Object parseRequest = getParseRequest(Collections.EMPTY_MAP, bindingVariables);
              if (parseRequest instanceof Boolean) {
                urlMapping.setParseRequest((Boolean) parseRequest);
              }
            }
            configureUrlMapping(urlMapping);
            return urlMapping;
          }

          if (args[0] instanceof Map) {
            Map namedArguments = (Map) args[0];
            UrlMappingData urlData = createUrlMappingData(methodName, isResponseCode);
            if (args.length > 1 && args[1] instanceof Closure) {
              Closure callable = (Closure) args[1];
              callable.call();
            }

            UrlMapping urlMapping =
                getURLMappingForNamedArgs(namedArguments, urlData, methodName, isResponseCode);
            configureUrlMapping(urlMapping);
            return urlMapping;
          }
          return null;
        } finally {
          if (binding != null) {
            binding.getVariables().clear();
          } else {
            controllerName = null;
            actionName = null;
            viewName = null;
          }
          previousConstraints.clear();
          urlDefiningMode = true;
        }
      } else if (!urlDefiningMode && CONSTRAINTS.equals(methodName)) {
        ConstrainedPropertyBuilder builder = new ConstrainedPropertyBuilder(this);
        if (args.length > 0 && (args[0] instanceof Closure)) {

          Closure callable = (Closure) args[0];
          callable.setDelegate(builder);
          for (ConstrainedProperty constrainedProperty : previousConstraints) {
            builder
                .getConstrainedProperties()
                .put(constrainedProperty.getPropertyName(), constrainedProperty);
          }
          callable.call();
        }
        return builder.getConstrainedProperties();
      } else {
        return super.invokeMethod(methodName, arg);
      }
    }