Search in sources :

Example 1 with InterceptorStackConfig

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

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

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

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

the class Dispatcher method cleanup.

/**
 * Releases all instances bound to this dispatcher instance.
 */
public void cleanup() {
    // clean up ObjectFactory
    ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class);
    if (objectFactory == null) {
        LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed");
    }
    if (objectFactory instanceof ObjectFactoryDestroyable) {
        try {
            ((ObjectFactoryDestroyable) objectFactory).destroy();
        } catch (Exception e) {
            // catch any exception that may occurred during destroy() and log it
            LOG.error("Exception occurred while destroying ObjectFactory [{}]", objectFactory.toString(), e);
        }
    }
    // clean up Dispatcher itself for this thread
    instance.set(null);
    servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null);
    // clean up DispatcherListeners
    if (!dispatcherListeners.isEmpty()) {
        for (DispatcherListener l : dispatcherListeners) {
            l.dispatcherDestroyed(this);
        }
    }
    // clean up all interceptors by calling their destroy() method
    Set<Interceptor> interceptors = new HashSet<>();
    Collection<PackageConfig> packageConfigs = configurationManager.getConfiguration().getPackageConfigs().values();
    for (PackageConfig packageConfig : packageConfigs) {
        for (Object config : packageConfig.getAllInterceptorConfigs().values()) {
            if (config instanceof InterceptorStackConfig) {
                for (InterceptorMapping interceptorMapping : ((InterceptorStackConfig) config).getInterceptors()) {
                    interceptors.add(interceptorMapping.getInterceptor());
                }
            }
        }
    }
    for (Interceptor interceptor : interceptors) {
        interceptor.destroy();
    }
    // Clear container holder when application is unloaded / server shutdown
    ContainerHolder.clear();
    // cleanup action context
    ActionContext.clear();
    // clean up configuration
    configurationManager.destroyConfiguration();
    configurationManager = null;
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ObjectFactoryDestroyable(org.apache.struts2.util.ObjectFactoryDestroyable) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ServletException(javax.servlet.ServletException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Example 5 with InterceptorStackConfig

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

InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)14 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)7 List (java.util.List)5 HashMap (java.util.HashMap)4 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)4 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 Mock (com.mockobjects.dynamic.Mock)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 ConfigurationManager (com.opensymphony.xwork2.config.ConfigurationManager)1 RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)1 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)1