public String configFile() { // Check template URL exists String templateUrl = driver.getEntity().getConfig(NginxController.SERVER_CONF_TEMPLATE_URL); ResourceUtils.create(this).checkUrlExists(templateUrl); // Check SSL configuration ProxySslConfig ssl = driver.getEntity().getConfig(NginxController.SSL_CONFIG); if (ssl != null && Strings.isEmpty(ssl.getCertificateDestination()) && Strings.isEmpty(ssl.getCertificateSourceUrl())) { throw new IllegalStateException( "ProxySslConfig can't have a null certificateDestination and null certificateSourceUrl. One or both need to be set"); } // For mapping by URL Iterable<UrlMapping> mappings = ((NginxController) driver.getEntity()).getUrlMappings(); Multimap<String, UrlMapping> mappingsByDomain = LinkedHashMultimap.create(); for (UrlMapping mapping : mappings) { Collection<String> addrs = mapping.getAttribute(UrlMapping.TARGET_ADDRESSES); if (addrs != null && addrs.size() > 0) { mappingsByDomain.put(mapping.getDomain(), mapping); } } Map<String, Object> substitutions = MutableMap.<String, Object>builder() .putIfNotNull("ssl", ssl) .put("urlMappings", mappings) .put("domainMappings", mappingsByDomain) .build(); // Get template contents and process String contents = ResourceUtils.create(driver.getEntity()).getResourceAsString(templateUrl); return TemplateProcessor.processTemplateContents(contents, driver, substitutions); }
@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); }
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); } }