Search in sources :

Example 11 with InterceptorStackConfig

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

the class XmlConfigurationProvider method loadInterceptorStacks.

protected void loadInterceptorStacks(Element element, PackageConfig.Builder context) throws ConfigurationException {
    NodeList interceptorStackList = element.getElementsByTagName("interceptor-stack");
    for (int i = 0; i < interceptorStackList.getLength(); i++) {
        Element interceptorStackElement = (Element) interceptorStackList.item(i);
        InterceptorStackConfig config = loadInterceptorStack(interceptorStackElement, context);
        context.addInterceptorStackConfig(config);
    }
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 12 with InterceptorStackConfig

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

the class FetchMetadataInterceptorTest method testSetExemptedPathsInjectionIndirectly.

public void testSetExemptedPathsInjectionIndirectly() throws Exception {
    // Perform a multi-step test to confirm (indirectly) that the method parameter injection of setExemptedPaths() for
    // the FetchMetadataInterceptor is functioning as expected, when configured appropriately.
    // Ensure we're using the specific test configuration, not the default simple configuration.
    XmlConfigurationProvider configurationProvider = new StrutsXmlConfigurationProvider("struts-testing.xml");
    container.inject(configurationProvider);
    loadConfigurationProviders(configurationProvider);
    // The test configuration in "struts-testing.xml" should define a "default" package.  That "default" package should contain a "defaultInterceptorStack" containing
    // a "fetchMetadata" interceptor parameter "fetchMetadata.setExemptedPaths".  If the parameter method injection is working correctly for the FetchMetadataInterceptor,
    // the exempted paths should be set appropriately for the interceptor instances, once the configuration is loaded into the container.
    final PackageConfig defaultPackageConfig = configuration.getPackageConfig("default");
    final InterceptorStackConfig defaultInterceptorStackConfig = (InterceptorStackConfig) defaultPackageConfig.getInterceptorConfig("defaultInterceptorStack");
    final Collection<InterceptorMapping> defaultInterceptorStackInterceptors = defaultInterceptorStackConfig.getInterceptors();
    assertFalse("'defaultInterceptorStack' interceptors in struts-testing.xml is empty ?", defaultInterceptorStackInterceptors.isEmpty());
    InterceptorMapping configuredFetchMetadataInterceptorMapping = null;
    Iterator<InterceptorMapping> interceptorIterator = defaultInterceptorStackInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping not present after loading 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping loaded from 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    FetchMetadataInterceptor configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, "foo");
    request.setContextPath("/foo");
    assertEquals("Expected interceptor to NOT accept this request [/foo]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    request.setContextPath("/fetchMetadataExemptedGlobal");
    assertNotEquals("Expected interceptor to accept this request [/fetchMetadataExemptedGlobal]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    request.setContextPath("/someOtherPath");
    assertNotEquals("Expected interceptor to accept this request [/someOtherPath]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // The test configuration in "struts-testing.xml" should also contain three actions configured differently for the "fetchMetadata" interceptor.
    // "fetchMetadataExempted" has an override exemption matching its action name, "fetchMetadataNotExempted" has an override exemption NOT matching its action name,
    // and "fetchMetadataExemptedGlobal" has an action name matching an exemption defined in "defaultInterceptorStack".
    final RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration();
    final ActionConfig fetchMetadataExemptedActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataExempted");
    final ActionConfig fetchMetadataNotExemptedActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataNotExempted");
    final ActionConfig fetchMetadataExemptedGlobalActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataExemptedGlobal");
    assertNotNull("'fetchMetadataExempted' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    assertNotNull("'fetchMetadataNotExempted' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    assertNotNull("'fetchMetadataExemptedGlobal' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    // Test fetchMetadata interceptor for the "fetchMetadataExempted" action.
    Collection<InterceptorMapping> currentActionInterceptors = fetchMetadataExemptedActionConfig.getInterceptors();
    assertFalse("'fetchMetadataExempted' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataExempted' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping for action 'fetchMetadataExempted' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataExemptedActionConfig.getName());
    request.setContextPath("/" + fetchMetadataExemptedActionConfig.getName());
    assertNotEquals("Expected interceptor to accept this request [" + "/" + fetchMetadataExemptedActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // Test fetchMetadata interceptor for the "fetchMetadataNotExempted" action.
    currentActionInterceptors = fetchMetadataNotExemptedActionConfig.getInterceptors();
    assertFalse("'fetchMetadataNotExempted' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataNotExempted' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping 'fetchMetadataExempted' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataNotExemptedActionConfig.getName());
    request.setContextPath("/" + fetchMetadataNotExemptedActionConfig.getName());
    assertEquals("Expected interceptor to NOT accept this request [" + "/" + fetchMetadataNotExemptedActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // Test fetchMetadata interceptor for the "fetchMetadataExemptedGlobal" action.
    currentActionInterceptors = fetchMetadataExemptedGlobalActionConfig.getInterceptors();
    assertFalse("'fetchMetadataExemptedGlobal' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataExemptedGlobal' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping 'fetchMetadataExemptedGlobal' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataExemptedGlobalActionConfig.getName());
    request.setContextPath("/" + fetchMetadataExemptedGlobalActionConfig.getName());
    assertNotEquals("Expected interceptor to accept this request [" + "/" + fetchMetadataExemptedGlobalActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 13 with InterceptorStackConfig

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

the class XmlConfigurationProviderInterceptorsTest method testInterceptorParamOverriding.

public void testInterceptorParamOverriding() throws Exception {
    Map<String, String> params = new HashMap<>();
    params.put("foo", "expectedFoo");
    params.put("expectedFoo", "expectedFooValue");
    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();
    ArrayList<InterceptorMapping> interceptors = new ArrayList<>();
    interceptors.addAll(defaultStack.getInterceptors());
    ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // TestInterceptorParamOverride action tests that an interceptor with a param override worked
    HashMap<String, String> interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue2");
    interceptorParams.put("foo", "foo123");
    InterceptorStackConfig defaultStack2 = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, interceptorParams))).build();
    interceptors = new ArrayList<>();
    interceptors.addAll(defaultStack2.getInterceptors());
    ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-params.xml");
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(2, actionConfigs.size());
    assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
    assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
    ActionConfig ac = (ActionConfig) actionConfigs.get("TestInterceptorParamOverride");
    assertEquals(defaultStack.getInterceptors(), ac.getInterceptors());
    ActionConfig ac2 = (ActionConfig) actionConfigs.get("TestInterceptorParam");
    assertEquals(defaultStack2.getInterceptors(), ac2.getInterceptors());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with InterceptorStackConfig

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

the class XmlConfigurationProviderInterceptorsTest method testInterceptorInheritance.

public void testInterceptorInheritance() throws ConfigurationException {
    // expectations - the inherited interceptor stack
    InterceptorStackConfig inheritedStack = new InterceptorStackConfig.Builder("subDefaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).build();
    ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-inheritance.xml");
    // assertions
    PackageConfig defaultPkg = configuration.getPackageConfig("default");
    assertEquals(2, defaultPkg.getInterceptorConfigs().size());
    PackageConfig subPkg = configuration.getPackageConfig("subPackage");
    assertEquals(1, subPkg.getInterceptorConfigs().size());
    assertEquals(3, subPkg.getAllInterceptorConfigs().size());
    assertEquals(inheritedStack, subPkg.getInterceptorConfigs().get("subDefaultStack"));
    // expectations - the inherited interceptor stack
    inheritedStack = new InterceptorStackConfig.Builder("subSubDefaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).build();
    PackageConfig subSubPkg = configuration.getPackageConfig("subSubPackage");
    assertEquals(1, subSubPkg.getInterceptorConfigs().size());
    assertEquals(4, subSubPkg.getAllInterceptorConfigs().size());
    assertEquals(inheritedStack, subSubPkg.getInterceptorConfigs().get("subSubDefaultStack"));
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 15 with InterceptorStackConfig

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

the class InterceptorBuilderTest method testBuildInterceptor_2.

/**
 * Try to test this
 * <interceptor-ref name="interceptorStack1">
 * <param name="interceptorStack2.interceptor1.param1">interceptor1_value1</param>
 * <param name="interceptorStack2.interceptor1.param2">interceptor1_value2</param>
 * <param name="interceptorStack3.interceptor2.param1">interceptor2_value1</param>
 * <param name="interceptorStack3.interceptor2.param2">interceptor2_value2</param>
 * </interceptor-ref>
 *
 * @throws Exception
 */
public void testBuildInterceptor_2() throws Exception {
    InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("interceptorStack1").build();
    InterceptorStackConfig interceptorStackConfig2 = new InterceptorStackConfig.Builder("interceptorStack2").build();
    InterceptorStackConfig interceptorStackConfig3 = new InterceptorStackConfig.Builder("interceptorStack3").build();
    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();
    PackageConfig packageConfig = new PackageConfig.Builder("package1").namespace("/namspace").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorStackConfig(interceptorStackConfig1).addInterceptorStackConfig(interceptorStackConfig2).addInterceptorStackConfig(interceptorStackConfig3).build();
    List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "interceptorStack1", new LinkedHashMap<String, String>() {

        private static final long serialVersionUID = -5819935102242042570L;

        {
            put("interceptorStack2.interceptor1.param1", "interceptor1_value1");
            put("interceptorStack2.interceptor1.param2", "interceptor1_value2");
            put("interceptorStack3.interceptor2.param1", "interceptor2_value1");
            put("interceptorStack3.interceptor2.param2", "interceptor2_value2");
        }
    }, null, objectFactory);
    assertEquals(interceptorMappings.size(), 2);
    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(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam1(), "interceptor2_value1");
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam2(), "interceptor2_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