Search in sources :

Example 1 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class InterceptorBuilder method constructParameterizedInterceptorReferences.

/**
 * Builds a list of interceptors referenced by the refName in the supplied PackageConfig overriding the properties
 * of the referenced interceptor with refParams.
 *
 * @param interceptorLocator interceptor locator
 * @param stackConfig interceptor stack configuration
 * @param refParams The overridden interceptor properties
 * @return list of interceptors referenced by the refName in the supplied PackageConfig overridden with refParams.
 */
private static List<InterceptorMapping> constructParameterizedInterceptorReferences(InterceptorLocator interceptorLocator, InterceptorStackConfig stackConfig, Map<String, String> refParams, ObjectFactory objectFactory) {
    List<InterceptorMapping> result;
    Map<String, Map<String, String>> params = new LinkedHashMap<>();
    /*
         * We strip
         *
         * <interceptor-ref name="someStack">
         *    <param name="interceptor1.param1">someValue</param>
         *    <param name="interceptor1.param2">anotherValue</param>
         * </interceptor-ref>
         *
         * down to map
         *  interceptor1 -> [param1 -> someValue, param2 -> anotherValue]
         *
         * or
         * <interceptor-ref name="someStack">
         *    <param name="interceptorStack1.interceptor1.param1">someValue</param>
         *    <param name="interceptorStack1.interceptor1.param2">anotherValue</param>
         * </interceptor-ref>
         *
         * down to map
         *  interceptorStack1 -> [interceptor1.param1 -> someValue, interceptor1.param2 -> anotherValue]
         *
         */
    for (Map.Entry<String, String> entry : refParams.entrySet()) {
        String key = entry.getKey();
        try {
            String name = key.substring(0, key.indexOf('.'));
            key = key.substring(key.indexOf('.') + 1);
            Map<String, String> map;
            if (params.containsKey(name)) {
                map = params.get(name);
            } else {
                map = new LinkedHashMap<>();
            }
            map.put(key, entry.getValue());
            params.put(name, map);
        } catch (Exception e) {
            LOG.warn("No interceptor found for name = {}", key);
        }
    }
    result = new ArrayList<>(stackConfig.getInterceptors());
    for (Map.Entry<String, Map<String, String>> entry : params.entrySet()) {
        String key = entry.getKey();
        Map<String, String> map = entry.getValue();
        Object interceptorCfgObj = interceptorLocator.getInterceptorConfig(key);
        /*
             * Now we attempt to separate out param that refers to Interceptor
             * and Interceptor stack, eg.
             *
             * <interceptor-ref name="someStack">
             *    <param name="interceptor1.param1">someValue</param>
             *    ...
             * </interceptor-ref>
             *
             *  vs
             *
             *  <interceptor-ref name="someStack">
             *    <param name="interceptorStack1.interceptor1.param1">someValue</param>
             *    ...
             *  </interceptor-ref>
             */
        if (interceptorCfgObj instanceof InterceptorConfig) {
            // interceptor-ref param refer to an interceptor
            InterceptorConfig cfg = (InterceptorConfig) interceptorCfgObj;
            Interceptor interceptor = objectFactory.buildInterceptor(cfg, map);
            InterceptorMapping mapping = new InterceptorMapping(key, interceptor);
            if (result.contains(mapping)) {
                for (int index = 0; index < result.size(); index++) {
                    InterceptorMapping interceptorMapping = result.get(index);
                    if (interceptorMapping.getName().equals(key)) {
                        LOG.debug("Overriding interceptor config [{}] with new mapping {} using new params {}", key, interceptorMapping, map);
                        result.set(index, mapping);
                    }
                }
            } else {
                result.add(mapping);
            }
        } else if (interceptorCfgObj instanceof InterceptorStackConfig) {
            // interceptor-ref param refer to an interceptor stack
            // If its an interceptor-stack, we call this method recursively until,
            // all the params (eg. interceptorStack1.interceptor1.param etc.)
            // are resolved down to a specific interceptor.
            InterceptorStackConfig stackCfg = (InterceptorStackConfig) interceptorCfgObj;
            List<InterceptorMapping> tmpResult = constructParameterizedInterceptorReferences(interceptorLocator, stackCfg, map, objectFactory);
            for (InterceptorMapping tmpInterceptorMapping : tmpResult) {
                if (result.contains(tmpInterceptorMapping)) {
                    int index = result.indexOf(tmpInterceptorMapping);
                    result.set(index, tmpInterceptorMapping);
                } else {
                    result.add(tmpInterceptorMapping);
                }
            }
        }
    }
    return result;
}
Also used : InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) LinkedHashMap(java.util.LinkedHashMap) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ArrayList(java.util.ArrayList) List(java.util.List) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor)

Example 2 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class XmlConfigurationProvider method addAction.

protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
    String name = actionElement.getAttribute("name");
    String className = actionElement.getAttribute("class");
    // methodName should be null if it's not set
    String methodName = StringUtils.trimToNull(actionElement.getAttribute("method"));
    Location location = DomHelper.getLocationObject(actionElement);
    if (location == null) {
        LOG.warn("Location null for {}", className);
    }
    // use the default-class-ref from the <package/>
    if (StringUtils.isEmpty(className)) {
    // if there is a package default-class-ref use that, otherwise use action support
    /* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) {
                className = packageContext.getDefaultClassRef();
            } else {
                className = ActionSupport.class.getName();
            }*/
    } else {
        if (!verifyAction(className, name, location)) {
            LOG.error("Unable to verify action [{}] with class [{}], from [{}]", name, className, location);
            return;
        }
    }
    Map<String, ResultConfig> results;
    try {
        results = buildResults(actionElement, packageContext);
    } catch (ConfigurationException e) {
        throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
    }
    List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
    List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
    Set<String> allowedMethods = buildAllowedMethods(actionElement, packageContext);
    ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className).methodName(methodName).addResultConfigs(results).addInterceptors(interceptorList).addExceptionMappings(exceptionMappings).addParams(XmlHelper.getParams(actionElement)).setStrictMethodInvocation(packageContext.isStrictMethodInvocation()).addAllowedMethod(allowedMethods).location(location).build();
    packageContext.addActionConfig(name, actionConfig);
    LOG.debug("Loaded {}{} in '{}' package: {}", StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "", name, packageContext.getName(), actionConfig);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) Location(com.opensymphony.xwork2.util.location.Location)

Example 3 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class XmlConfigurationProvider method buildInterceptorList.

protected List<InterceptorMapping> buildInterceptorList(Element element, PackageConfig.Builder context) throws ConfigurationException {
    List<InterceptorMapping> interceptorList = new ArrayList<>();
    NodeList interceptorRefList = element.getElementsByTagName("interceptor-ref");
    for (int i = 0; i < interceptorRefList.getLength(); i++) {
        Element interceptorRefElement = (Element) interceptorRefList.item(i);
        if (interceptorRefElement.getParentNode().equals(element) || interceptorRefElement.getParentNode().getNodeName().equals(element.getNodeName())) {
            List<InterceptorMapping> interceptors = lookupInterceptorReference(context, interceptorRefElement);
            interceptorList.addAll(interceptors);
        }
    }
    return interceptorList;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Example 4 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class XmlConfigurationProvider method loadInterceptorStack.

protected InterceptorStackConfig loadInterceptorStack(Element element, PackageConfig.Builder context) throws ConfigurationException {
    String name = element.getAttribute("name");
    InterceptorStackConfig.Builder config = new InterceptorStackConfig.Builder(name).location(DomHelper.getLocationObject(element));
    NodeList interceptorRefList = element.getElementsByTagName("interceptor-ref");
    for (int j = 0; j < interceptorRefList.getLength(); j++) {
        Element interceptorRefElement = (Element) interceptorRefList.item(j);
        List<InterceptorMapping> interceptors = lookupInterceptorReference(context, interceptorRefElement);
        config.addInterceptors(interceptors);
    }
    return config.build();
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Example 5 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class DefaultActionInvocation method invoke.

/**
 * @throws ConfigurationException If no result can be found with the returned code
 */
public String invoke() throws Exception {
    if (executed) {
        throw new IllegalStateException("Action has already executed");
    }
    if (asyncManager == null || !asyncManager.hasAsyncActionResult()) {
        if (interceptors.hasNext()) {
            final InterceptorMapping interceptorMapping = interceptors.next();
            Interceptor interceptor = interceptorMapping.getInterceptor();
            if (interceptor instanceof WithLazyParams) {
                interceptor = lazyParamInjector.injectParams(interceptor, interceptorMapping.getParams(), invocationContext);
            }
            resultCode = interceptor.intercept(DefaultActionInvocation.this);
        } else {
            resultCode = invokeActionOnly();
        }
    } else {
        Object asyncActionResult = asyncManager.getAsyncActionResult();
        if (asyncActionResult instanceof Throwable) {
            throw new Exception((Throwable) asyncActionResult);
        }
        asyncAction = null;
        resultCode = saveResult(proxy.getConfig(), asyncActionResult);
    }
    if (asyncManager == null || asyncAction == null) {
        // return above and flow through again
        if (!executed) {
            if (preResultListeners != null) {
                LOG.trace("Executing PreResultListeners for result [{}]", result);
                for (Object preResultListener : preResultListeners) {
                    PreResultListener listener = (PreResultListener) preResultListener;
                    listener.beforeResult(this, resultCode);
                }
            }
            // now execute the result, if we're supposed to
            if (proxy.getExecuteResult()) {
                executeResult();
            }
            executed = true;
        }
    } else {
        asyncManager.invokeAsyncAction(asyncAction);
    }
    return resultCode;
}
Also used : WithLazyParams(com.opensymphony.xwork2.interceptor.WithLazyParams) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) NoSuchPropertyException(ognl.NoSuchPropertyException) StrutsException(org.apache.struts2.StrutsException) MethodFailedException(ognl.MethodFailedException)

Aggregations

InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)29 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)12 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)8 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)7 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)7 RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)6 List (java.util.List)6 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)5 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)5 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)4 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)4 Map (java.util.Map)4 SimpleAction (com.opensymphony.xwork2.SimpleAction)3 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)3 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)3 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)3 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)3