Search in sources :

Example 1 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project entando-core by entando.

the class TestCustomTokenInterceptor method testExecuteValidation_1.

public void testExecuteValidation_1() throws Exception {
    ActionInvocation invocation = this.prepareAction();
    CustomTokenInterceptor interceptor = new CustomTokenInterceptor();
    String result = interceptor.intercept(invocation);
    assertEquals(Action.SUCCESS, result);
    String newResult = interceptor.intercept(invocation);
    assertEquals(CustomTokenInterceptor.INVALID_TOKEN_CODE, newResult);
    ActionSupport action = super.getAction();
    assertEquals(0, action.getActionErrors().size());
    assertEquals(0, action.getActionMessages().size());
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 2 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project entando-core by entando.

the class TestCustomTokenInterceptor method testExecuteValidation_2.

public void testExecuteValidation_2() throws Exception {
    ActionInvocation invocation = this.prepareAction();
    CustomTokenInterceptor interceptor = new CustomTokenInterceptor();
    interceptor.setTypeMessages(CustomTokenInterceptor.TYPE_RETURN_ACTION_ERROR_MESSAGE);
    String result = interceptor.intercept(invocation);
    assertEquals(Action.SUCCESS, result);
    String newResult = interceptor.intercept(invocation);
    assertEquals(CustomTokenInterceptor.INVALID_TOKEN_CODE, newResult);
    ActionSupport action = super.getAction();
    assertEquals(1, action.getActionErrors().size());
    assertEquals(0, action.getActionMessages().size());
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 3 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor 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 4 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor 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 5 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class XmlConfigurationProvider method addPackage.

/**
 * Create a PackageConfig from an XML element representing it.
 *
 * @param packageElement the given XML element
 * @return the package config
 * @throws ConfigurationException in case of configuration errors
 */
protected PackageConfig addPackage(Element packageElement) throws ConfigurationException {
    String packageName = packageElement.getAttribute("name");
    PackageConfig packageConfig = configuration.getPackageConfig(packageName);
    if (packageConfig != null) {
        LOG.debug("Package [{}] already loaded, skipping re-loading it and using existing PackageConfig [{}]", packageName, packageConfig);
        return packageConfig;
    }
    PackageConfig.Builder newPackage = buildPackageContext(packageElement);
    if (newPackage.isNeedsRefresh()) {
        return newPackage.build();
    }
    LOG.debug("Loaded {}", newPackage);
    // add result types (and default result) to this package
    addResultTypes(newPackage, packageElement);
    // load the interceptors and interceptor stacks for this package
    loadInterceptors(newPackage, packageElement);
    // load the default interceptor reference for this package
    loadDefaultInterceptorRef(newPackage, packageElement);
    // load the default class ref for this package
    loadDefaultClassRef(newPackage, packageElement);
    // load the global result list for this package
    loadGlobalResults(newPackage, packageElement);
    loadGlobalAllowedMethods(newPackage, packageElement);
    // load the global exception handler list for this package
    loadGlobalExceptionMappings(newPackage, packageElement);
    // get actions
    NodeList actionList = packageElement.getElementsByTagName("action");
    for (int i = 0; i < actionList.getLength(); i++) {
        Element actionElement = (Element) actionList.item(i);
        addAction(actionElement, newPackage);
    }
    // load the default action reference for this package
    loadDefaultActionRef(newPackage, packageElement);
    PackageConfig cfg = newPackage.build();
    configuration.addPackageConfig(cfg.getName(), cfg);
    return cfg;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Aggregations

ActionInvocation (com.opensymphony.xwork2.ActionInvocation)32 HashMap (java.util.HashMap)28 ActionContext (com.opensymphony.xwork2.ActionContext)20 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)19 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)19 ServletActionContext (org.apache.struts2.ServletActionContext)14 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 LinkedHashMap (java.util.LinkedHashMap)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)10 DefaultAcceptedPatternsChecker (com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker)10 DefaultExcludedPatternsChecker (com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)10 Cookie (javax.servlet.http.Cookie)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Mock (com.mockobjects.dynamic.Mock)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)8