Search in sources :

Example 6 with InterceptorMapping

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

the class Form method evaluateClientSideJsEnablement.

/**
 * Evaluate client side JavaScript Enablement.
 * @param actionName the actioName to check for
 * @param namespace the namespace to check for
 * @param actionMethod the method to ckeck for
 */
protected void evaluateClientSideJsEnablement(String actionName, String namespace, String actionMethod) {
    // Only evaluate if Client-Side js is to be enable when validate=true
    Boolean validate = (Boolean) getParameters().get("validate");
    if (validate != null && validate) {
        addParameter("performValidation", Boolean.FALSE);
        RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration();
        ActionConfig actionConfig = runtimeConfiguration.getActionConfig(namespace, actionName);
        if (actionConfig != null) {
            List<InterceptorMapping> interceptors = actionConfig.getInterceptors();
            for (InterceptorMapping interceptorMapping : interceptors) {
                if (ValidationInterceptor.class.isInstance(interceptorMapping.getInterceptor())) {
                    ValidationInterceptor validationInterceptor = (ValidationInterceptor) interceptorMapping.getInterceptor();
                    Set excludeMethods = validationInterceptor.getExcludeMethodsSet();
                    Set includeMethods = validationInterceptor.getIncludeMethodsSet();
                    if (MethodFilterInterceptorUtil.applyMethod(excludeMethods, includeMethods, actionMethod)) {
                        addParameter("performValidation", Boolean.TRUE);
                    }
                    return;
                }
            }
        }
    }
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) Set(java.util.Set) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 7 with InterceptorMapping

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

the class XmlConfigurationProviderInterceptorParamOverridingTest method testInterceptorParamOveriding.

public void testInterceptorParamOveriding() throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    final XmlConfigurationProvider p = new StrutsXmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-param-overriding.xml");
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    factory.setContainer(container);
    factory.setFileManager(new DefaultFileManager());
    p.setFileManagerFactory(factory);
    conf.reloadContainer(new ArrayList<ContainerProvider>() {

        {
            add(new StrutsDefaultConfigurationProvider());
            add(p);
        }
    });
    RuntimeConfiguration rtConf = conf.getRuntimeConfiguration();
    ActionConfig actionOne = rtConf.getActionConfig("", "actionOne");
    ActionConfig actionTwo = rtConf.getActionConfig("", "actionTwo");
    List<InterceptorMapping> actionOneInterceptors = actionOne.getInterceptors();
    List<InterceptorMapping> actionTwoInterceptors = actionTwo.getInterceptors();
    assertNotNull(actionOne);
    assertNotNull(actionTwo);
    assertNotNull(actionOneInterceptors);
    assertNotNull(actionTwoInterceptors);
    assertEquals(actionOneInterceptors.size(), 3);
    assertEquals(actionTwoInterceptors.size(), 3);
    InterceptorMapping actionOneInterceptorMapping1 = actionOneInterceptors.get(0);
    InterceptorMapping actionOneInterceptorMapping2 = actionOneInterceptors.get(1);
    InterceptorMapping actionOneInterceptorMapping3 = actionOneInterceptors.get(2);
    InterceptorMapping actionTwoInterceptorMapping1 = actionTwoInterceptors.get(0);
    InterceptorMapping actionTwoInterceptorMapping2 = actionTwoInterceptors.get(1);
    InterceptorMapping actionTwoInterceptorMapping3 = actionTwoInterceptors.get(2);
    assertNotNull(actionOneInterceptorMapping1);
    assertNotNull(actionOneInterceptorMapping2);
    assertNotNull(actionOneInterceptorMapping3);
    assertNotNull(actionTwoInterceptorMapping1);
    assertNotNull(actionTwoInterceptorMapping2);
    assertNotNull(actionTwoInterceptorMapping3);
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamOne(), "i1p1");
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamTwo(), "i1p2");
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamOne(), "i2p1");
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamTwo(), null);
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamOne(), null);
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamTwo(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamOne(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamTwo(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamOne(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamTwo(), "i2p2");
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamOne(), "i3p1");
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamTwo(), "i3p2");
}
Also used : DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) ContainerProvider(com.opensymphony.xwork2.config.ContainerProvider) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) DefaultConfiguration(com.opensymphony.xwork2.config.impl.DefaultConfiguration) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 8 with InterceptorMapping

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

the class XmlConfigurationProviderInterceptorStackParamOverridingTest method testInterceptorStackParamOveriding.

public void testInterceptorStackParamOveriding() throws Exception {
    DefaultConfiguration conf = new DefaultConfiguration();
    final XmlConfigurationProvider p = new StrutsXmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-stack-param-overriding.xml");
    DefaultFileManagerFactory factory = new DefaultFileManagerFactory();
    factory.setContainer(container);
    factory.setFileManager(new DefaultFileManager());
    p.setFileManagerFactory(factory);
    configurationManager.addContainerProvider(p);
    conf.reloadContainer(new ArrayList<ContainerProvider>() {

        {
            add(new StrutsDefaultConfigurationProvider());
            add(p);
        }
    });
    RuntimeConfiguration rtConf = conf.getRuntimeConfiguration();
    ActionConfig actionOne = rtConf.getActionConfig("", "actionOne");
    ActionConfig actionTwo = rtConf.getActionConfig("", "actionTwo");
    List actionOneInterceptors = actionOne.getInterceptors();
    List actionTwoInterceptors = actionTwo.getInterceptors();
    assertNotNull(actionOne);
    assertNotNull(actionTwo);
    assertNotNull(actionOneInterceptors);
    assertNotNull(actionTwoInterceptors);
    assertEquals(actionOneInterceptors.size(), 3);
    assertEquals(actionTwoInterceptors.size(), 3);
    InterceptorMapping actionOneInterceptorMapping1 = (InterceptorMapping) actionOneInterceptors.get(0);
    InterceptorMapping actionOneInterceptorMapping2 = (InterceptorMapping) actionOneInterceptors.get(1);
    InterceptorMapping actionOneInterceptorMapping3 = (InterceptorMapping) actionOneInterceptors.get(2);
    InterceptorMapping actionTwoInterceptorMapping1 = (InterceptorMapping) actionTwoInterceptors.get(0);
    InterceptorMapping actionTwoInterceptorMapping2 = (InterceptorMapping) actionTwoInterceptors.get(1);
    InterceptorMapping actionTwoInterceptorMapping3 = (InterceptorMapping) actionTwoInterceptors.get(2);
    assertNotNull(actionOneInterceptorMapping1);
    assertNotNull(actionOneInterceptorMapping2);
    assertNotNull(actionOneInterceptorMapping3);
    assertNotNull(actionTwoInterceptorMapping1);
    assertNotNull(actionTwoInterceptorMapping2);
    assertNotNull(actionTwoInterceptorMapping3);
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamOne(), "i1p1");
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping1.getInterceptor()).getParamTwo(), "i1p2");
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamOne(), "i2p1");
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping2.getInterceptor()).getParamTwo(), null);
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamOne(), null);
    assertEquals(((InterceptorForTestPurpose) actionOneInterceptorMapping3.getInterceptor()).getParamTwo(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamOne(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping1.getInterceptor()).getParamTwo(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamOne(), null);
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping2.getInterceptor()).getParamTwo(), "i2p2");
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamOne(), "i3p1");
    assertEquals(((InterceptorForTestPurpose) actionTwoInterceptorMapping3.getInterceptor()).getParamTwo(), "i3p2");
}
Also used : DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) ContainerProvider(com.opensymphony.xwork2.config.ContainerProvider) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) DefaultConfiguration(com.opensymphony.xwork2.config.impl.DefaultConfiguration) List(java.util.List) ArrayList(java.util.ArrayList) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 9 with InterceptorMapping

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

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

the class XmlConfigurationProviderMultilevelTest method testMultiLevelInheritance.

/**
 * attempt to load an xwork.xml file that has multilevel levels of inheritance and verify that the interceptors are
 * correctly propagated through.
 *
 * @throws Exception
 */
public void testMultiLevelInheritance() throws Exception {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-multilevel.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    provider.init(configuration);
    provider.loadPackages();
    /**
     * for this test, we expect the action named, action3, in the namespace, namespace3, to have a single
     * ParameterInterceptor.  The ParameterInterceptor, param, has been defined far up namespace3's parentage ...
     * namespace3 -> namespace2 -> namespace1 -> default
     */
    PackageConfig packageConfig = configuration.getPackageConfig("namespace3");
    Assert.assertNotNull(packageConfig);
    assertEquals(2, packageConfig.getAllInterceptorConfigs().size());
    ActionConfig actionConfig = packageConfig.getActionConfigs().get("action3");
    assertNotNull(actionConfig);
    assertNotNull(actionConfig.getInterceptors());
    assertEquals(2, actionConfig.getInterceptors().size());
    assertEquals(ParametersInterceptor.class, ((InterceptorMapping) actionConfig.getInterceptors().get(0)).getInterceptor().getClass());
    assertNotNull(actionConfig.getResults());
    assertEquals(1, actionConfig.getResults().size());
    assertTrue(actionConfig.getResults().containsKey("success"));
    ResultConfig resultConfig = (ResultConfig) actionConfig.getResults().get("success");
    assertEquals(ActionChainResult.class.getName(), resultConfig.getClassName());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionChainResult(com.opensymphony.xwork2.ActionChainResult) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

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