Search in sources :

Example 1 with InterceptorConfig

use of com.opensymphony.xwork2.config.entities.InterceptorConfig 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 InterceptorConfig

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

the class XmlConfigurationProvider method loadInterceptors.

protected void loadInterceptors(PackageConfig.Builder context, Element element) throws ConfigurationException {
    NodeList interceptorList = element.getElementsByTagName("interceptor");
    for (int i = 0; i < interceptorList.getLength(); i++) {
        Element interceptorElement = (Element) interceptorList.item(i);
        String name = interceptorElement.getAttribute("name");
        String className = interceptorElement.getAttribute("class");
        Map<String, String> params = XmlHelper.getParams(interceptorElement);
        InterceptorConfig config = new InterceptorConfig.Builder(name, className).addParams(params).location(DomHelper.getLocationObject(interceptorElement)).build();
        context.addInterceptorConfig(config);
    }
    loadInterceptorStacks(element, context);
}
Also used : InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 3 with InterceptorConfig

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

the class DefaultInterceptorFactory method buildInterceptor.

public Interceptor buildInterceptor(InterceptorConfig interceptorConfig, Map<String, String> interceptorRefParams) throws ConfigurationException {
    String interceptorClassName = interceptorConfig.getClassName();
    Map<String, String> thisInterceptorClassParams = interceptorConfig.getParams();
    Map<String, String> params = (thisInterceptorClassParams == null) ? new HashMap<String, String>() : new HashMap<>(thisInterceptorClassParams);
    params.putAll(interceptorRefParams);
    String message;
    Throwable cause;
    try {
        // interceptor instances are long-lived and used across user sessions, so don't try to pass in any extra context
        Object o = objectFactory.buildBean(interceptorClassName, null);
        if (o instanceof WithLazyParams) {
            LOG.debug("Interceptor {} is marked with interface {} and params will be set during action invocation", interceptorClassName, WithLazyParams.class.getName());
        } else {
            reflectionProvider.setProperties(params, o);
        }
        if (o instanceof Interceptor) {
            Interceptor interceptor = (Interceptor) o;
            interceptor.init();
            return interceptor;
        }
        throw new ConfigurationException("Class [" + interceptorClassName + "] does not implement Interceptor", interceptorConfig);
    } catch (InstantiationException e) {
        cause = e;
        message = "Unable to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
    } catch (IllegalAccessException e) {
        cause = e;
        message = "IllegalAccessException while attempting to instantiate an instance of Interceptor class [" + interceptorClassName + "].";
    } catch (ClassCastException e) {
        cause = e;
        message = "Class [" + interceptorClassName + "] does not implement com.opensymphony.xwork2.interceptor.Interceptor";
    } catch (Exception e) {
        cause = e;
        message = "Caught Exception while registering Interceptor class " + interceptorClassName;
    } catch (NoClassDefFoundError e) {
        cause = e;
        message = "Could not load class " + interceptorClassName + ". Perhaps it exists but certain dependencies are not available?";
    }
    throw new ConfigurationException(message, cause, interceptorConfig);
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) WithLazyParams(com.opensymphony.xwork2.interceptor.WithLazyParams) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor)

Example 4 with InterceptorConfig

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

the class XmlConfigurationProviderInterceptorsTest method testBasicInterceptors.

public void testBasicInterceptors() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-interceptors-basic.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    // the test interceptor with a parameter
    Map<String, String> params = new HashMap<>();
    params.put("foo", "expectedFoo");
    InterceptorConfig paramsInterceptor = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).addParams(params).build();
    // the default interceptor stack
    InterceptorStackConfig defaultStack = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).build();
    // the derivative interceptor stack
    InterceptorStackConfig derivativeStack = new InterceptorStackConfig.Builder("derivativeStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).addInterceptor(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>()))).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map interceptorConfigs = pkg.getInterceptorConfigs();
    // assertions for size
    assertEquals(5, interceptorConfigs.size());
    // assertions for interceptors
    assertEquals(noopInterceptor, interceptorConfigs.get("noop"));
    assertEquals(loggingInterceptor, interceptorConfigs.get("logging"));
    assertEquals(paramsInterceptor, interceptorConfigs.get("test"));
    // assertions for interceptor stacks
    assertEquals(defaultStack, interceptorConfigs.get("defaultStack"));
    assertEquals(derivativeStack, interceptorConfigs.get("derivativeStack"));
}
Also used : HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with InterceptorConfig

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

the class InterceptorBuilderTest method testMultipleSameInterceptors.

public void testMultipleSameInterceptors() throws Exception {
    InterceptorConfig interceptorConfig1 = new InterceptorConfig.Builder("interceptor1", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor1").build();
    InterceptorConfig interceptorConfig2 = new InterceptorConfig.Builder("interceptor2", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor2").build();
    InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("multiStack").addInterceptor(new InterceptorMapping(interceptorConfig1.getName(), objectFactory.buildInterceptor(interceptorConfig1, Collections.<String, String>emptyMap()))).addInterceptor(new InterceptorMapping(interceptorConfig2.getName(), objectFactory.buildInterceptor(interceptorConfig2, Collections.<String, String>emptyMap()))).addInterceptor(new InterceptorMapping(interceptorConfig1.getName(), objectFactory.buildInterceptor(interceptorConfig1, Collections.<String, String>emptyMap()))).build();
    PackageConfig packageConfig = new PackageConfig.Builder("package1").namespace("/namespace").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorConfig(interceptorConfig1).addInterceptorStackConfig(interceptorStackConfig1).build();
    List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "multiStack", new LinkedHashMap<String, String>() {

        {
            put("interceptor1.param1", "interceptor1_value1");
            put("interceptor1.param2", "interceptor1_value2");
        }
    }, null, objectFactory);
    assertEquals(interceptorMappings.size(), 3);
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getName(), "interceptor1");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor().getClass(), MockInterceptor1.class);
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam1(), "interceptor1_value1");
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam2(), "interceptor1_value2");
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getName(), "interceptor2");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor().getClass(), MockInterceptor2.class);
    assertEquals(((InterceptorMapping) interceptorMappings.get(2)).getName(), "interceptor1");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(2)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(2)).getInterceptor().getClass(), MockInterceptor1.class);
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(2)).getInterceptor()).getParam1(), "interceptor1_value1");
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(2)).getInterceptor()).getParam2(), "interceptor1_value2");
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) List(java.util.List) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Aggregations

InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)11 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)7 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)7 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)6 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)6 List (java.util.List)5 Map (java.util.Map)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 HashMap (java.util.HashMap)3 ModelDrivenInterceptor (com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor)2 ArrayList (java.util.ArrayList)2 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)1 WithLazyParams (com.opensymphony.xwork2.interceptor.WithLazyParams)1 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)1 MockResult (com.opensymphony.xwork2.mock.MockResult)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)1