Search in sources :

Example 1 with ExceptionMappingConfig

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

the class XmlConfigurationProvider method addAction.

protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
    String name = actionElement.getAttribute("name");
    String className = actionElement.getAttribute("class");
    // methodName should be null if it's not set
    String methodName = StringUtils.trimToNull(actionElement.getAttribute("method"));
    Location location = DomHelper.getLocationObject(actionElement);
    if (location == null) {
        LOG.warn("Location null for {}", className);
    }
    // use the default-class-ref from the <package/>
    if (StringUtils.isEmpty(className)) {
    // if there is a package default-class-ref use that, otherwise use action support
    /* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) {
                className = packageContext.getDefaultClassRef();
            } else {
                className = ActionSupport.class.getName();
            }*/
    } else {
        if (!verifyAction(className, name, location)) {
            LOG.error("Unable to verify action [{}] with class [{}], from [{}]", name, className, location);
            return;
        }
    }
    Map<String, ResultConfig> results;
    try {
        results = buildResults(actionElement, packageContext);
    } catch (ConfigurationException e) {
        throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
    }
    List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
    List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
    Set<String> allowedMethods = buildAllowedMethods(actionElement, packageContext);
    ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className).methodName(methodName).addResultConfigs(results).addInterceptors(interceptorList).addExceptionMappings(exceptionMappings).addParams(XmlHelper.getParams(actionElement)).setStrictMethodInvocation(packageContext.isStrictMethodInvocation()).addAllowedMethod(allowedMethods).location(location).build();
    packageContext.addActionConfig(name, actionConfig);
    LOG.debug("Loaded {}{} in '{}' package: {}", StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "", name, packageContext.getName(), actionConfig);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) Location(com.opensymphony.xwork2.util.location.Location)

Example 2 with ExceptionMappingConfig

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

the class XmlConfigurationProvider method buildExceptionMappings.

/**
 * Build a list of exception mapping objects from below a given XML element.
 *
 * @param element the given XML element
 * @param packageContext the package context
 *
 * @return list of exception mapping config objects
 */
protected List<ExceptionMappingConfig> buildExceptionMappings(Element element, PackageConfig.Builder packageContext) {
    NodeList exceptionMappingEls = element.getElementsByTagName("exception-mapping");
    List<ExceptionMappingConfig> exceptionMappings = new ArrayList<>();
    for (int i = 0; i < exceptionMappingEls.getLength(); i++) {
        Element ehElement = (Element) exceptionMappingEls.item(i);
        if (ehElement.getParentNode().equals(element) || ehElement.getParentNode().getNodeName().equals(element.getNodeName())) {
            String emName = ehElement.getAttribute("name");
            String exceptionClassName = ehElement.getAttribute("exception");
            String exceptionResult = ehElement.getAttribute("result");
            Map<String, String> params = XmlHelper.getParams(ehElement);
            if (StringUtils.isEmpty(emName)) {
                emName = exceptionResult;
            }
            ExceptionMappingConfig ehConfig = new ExceptionMappingConfig.Builder(emName, exceptionClassName, exceptionResult).addParams(params).location(DomHelper.getLocationObject(ehElement)).build();
            exceptionMappings.add(ehConfig);
        }
    }
    return exceptionMappings;
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig)

Example 3 with ExceptionMappingConfig

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

the class XmlConfigurationProviderExceptionMappingsTest method testActions.

public void testActions() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-exception-mappings.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    List<ExceptionMappingConfig> exceptionMappings = new ArrayList<>();
    HashMap<String, String> parameters = new HashMap<>();
    HashMap<String, ResultConfig> results = new HashMap<>();
    exceptionMappings.add(new ExceptionMappingConfig.Builder("spooky-result", "com.opensymphony.xwork2.SpookyException", "spooky-result").build());
    results.put("spooky-result", new ResultConfig.Builder("spooky-result", MockResult.class.getName()).build());
    Map<String, String> resultParams = new HashMap<>();
    resultParams.put("actionName", "bar.vm");
    results.put("specificLocationResult", new ResultConfig.Builder("specificLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
    ActionConfig expectedAction = new ActionConfig.Builder("default", "Bar", SimpleAction.class.getName()).addParams(parameters).addResultConfigs(results).addExceptionMappings(exceptionMappings).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(1, actionConfigs.size());
    ActionConfig action = (ActionConfig) actionConfigs.get("Bar");
    assertEquals(expectedAction, action);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ExceptionMappingConfig

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

the class ActionConfigMatcherTest method testCheckSubstitutionsMatch.

public void testCheckSubstitutionsMatch() {
    ActionConfig m = matcher.match("foo/class/method");
    assertTrue("Class hasn't been replaced", "foo.bar.classAction".equals(m.getClassName()));
    assertTrue("Method hasn't been replaced", "domethod".equals(m.getMethodName()));
    assertTrue("Package isn't correct", "package-class".equals(m.getPackageName()));
    assertTrue("First param isn't correct", "class".equals(m.getParams().get("first")));
    assertTrue("Second param isn't correct", "method".equals(m.getParams().get("second")));
    ExceptionMappingConfig ex = m.getExceptionMappings().get(0);
    assertTrue("Wrong name, was " + ex.getName(), "fooclass".equals(ex.getName()));
    assertTrue("Wrong result", "successclass".equals(ex.getResult()));
    assertTrue("Wrong exception", "java.lang.methodException".equals(ex.getExceptionClassName()));
    assertTrue("First param isn't correct", "class".equals(ex.getParams().get("first")));
    assertTrue("Second param isn't correct", "method".equals(ex.getParams().get("second")));
    ResultConfig result = m.getResults().get("successclass");
    assertTrue("Wrong name, was " + result.getName(), "successclass".equals(result.getName()));
    assertTrue("Wrong classname", "foo.method".equals(result.getClassName()));
    assertTrue("First param isn't correct", "class".equals(result.getParams().get("first")));
    assertTrue("Second param isn't correct", "method".equals(result.getParams().get("second")));
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig)

Example 5 with ExceptionMappingConfig

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

the class XmlConfigurationProviderActionsTest method testActions.

public void testActions() throws Exception {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    // bar action is very simple, just two params
    params.put("foo", "17");
    params.put("bar", "23");
    params.put("testXW412", "foo.jspa?fooID=${fooID}&something=bar");
    params.put("testXW412Again", "something");
    ActionConfig barAction = new ActionConfig.Builder("", "Bar", SimpleAction.class.getName()).addParams(params).build();
    // foo action is a little more complex, two params, a result and an interceptor stack
    results = new HashMap<>();
    params = new HashMap<>();
    params.put("foo", "18");
    params.put("bar", "24");
    results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
    InterceptorConfig noopInterceptorConfig = new InterceptorConfig.Builder("noop", NoOpInterceptor.class.getName()).build();
    interceptors.add(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptorConfig, new HashMap<String, String>())));
    ActionConfig fooAction = new ActionConfig.Builder("", "Foo", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).build();
    // wildcard action is simple wildcard example
    results = new HashMap<>();
    results.put("*", new ResultConfig.Builder("*", MockResult.class.getName()).build());
    ActionConfig wildcardAction = new ActionConfig.Builder("", "WildCard", SimpleAction.class.getName()).addResultConfigs(results).addInterceptors(interceptors).build();
    // fooBar action is a little more complex, two params, a result and an interceptor stack
    params = new HashMap<String, String>();
    params.put("foo", "18");
    params.put("bar", "24");
    results = new HashMap<>();
    results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
    ExceptionMappingConfig exceptionConfig = new ExceptionMappingConfig.Builder("runtime", "java.lang.RuntimeException", "exception").build();
    exceptionMappings.add(exceptionConfig);
    ActionConfig fooBarAction = new ActionConfig.Builder("", "FooBar", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).addExceptionMappings(exceptionMappings).build();
    // TestInterceptorParam action tests that an interceptor worked
    HashMap<String, String> interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue");
    interceptorParams.put("foo", MockInterceptor.DEFAULT_FOO_VALUE);
    InterceptorConfig mockInterceptorConfig = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).build();
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
    ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // TestInterceptorParamOverride action tests that an interceptor with a param override worked
    interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue");
    interceptorParams.put("foo", "foo123");
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
    ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(7, actionConfigs.size());
    assertEquals(barAction, actionConfigs.get("Bar"));
    assertEquals(fooAction, actionConfigs.get("Foo"));
    assertEquals(wildcardAction, actionConfigs.get("WildCard"));
    assertEquals(fooBarAction, actionConfigs.get("FooBar"));
    assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
    assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
}
Also used : MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) SimpleAction(com.opensymphony.xwork2.SimpleAction) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ExceptionMappingConfig (com.opensymphony.xwork2.config.entities.ExceptionMappingConfig)7 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)4 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)4 ArrayList (java.util.ArrayList)3 SimpleAction (com.opensymphony.xwork2.SimpleAction)2 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)2 MockResult (com.opensymphony.xwork2.mock.MockResult)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Configuration (com.opensymphony.xwork2.config.Configuration)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)1 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)1 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)1 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)1 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)1 Location (com.opensymphony.xwork2.util.location.Location)1 LinkedHashMap (java.util.LinkedHashMap)1 ServletContext (javax.servlet.ServletContext)1 DefaultResultPathAction (org.apache.struts2.convention.actions.DefaultResultPathAction)1