Search in sources :

Example 26 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project tis by qlangtech.

the class TisPackageBasedActionConfigBuilder method getPackageConfig.

@Override
protected Builder getPackageConfig(Map<String, Builder> packageConfigs, String actionNamespace, String actionPackage, Class<?> actionClass, Action action) {
    Matcher matcher = NAMESPACE_PATTERN.matcher(actionClass.getName());
    // 解析struts2的命名空间
    String name = null;
    if (// || (matcher = NAMESPACE_TIS_PATTERN.matcher(actionClass.getName())).matches()
    matcher.matches()) {
        name = '/' + matcher.group(1) + StringUtils.replace(matcher.group(3), ".", "/") + "#" + matcher.group(2);
    } else {
        throw new IllegalStateException("actionPackage:" + actionPackage + " is not a valid actionPackage");
    }
    PackageConfig.Builder pkgConfig = packageConfigs.get(name);
    if (pkgConfig == null) {
        pkgConfig = new PackageConfig.Builder(name).namespace(name).addParent(this.parentPkgConfig);
        // add by baisui: 2020/7/13
        pkgConfig.strictMethodInvocation(false);
        packageConfigs.put(name, pkgConfig);
        // check for @DefaultInterceptorRef in the package
        DefaultInterceptorRef defaultInterceptorRef = AnnotationUtils.findAnnotation(actionClass, DefaultInterceptorRef.class);
        if (defaultInterceptorRef != null) {
            pkgConfig.defaultInterceptorRef(defaultInterceptorRef.value());
            if (LOG.isTraceEnabled())
                LOG.debug("Setting [#0] as the default interceptor ref for [#1]", defaultInterceptorRef.value(), pkgConfig.getName());
        }
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Created package config named [#0] with a namespace [#1]", name, actionNamespace);
    }
    return pkgConfig;
}
Also used : Builder(com.opensymphony.xwork2.config.entities.PackageConfig.Builder) Matcher(java.util.regex.Matcher) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) DefaultInterceptorRef(org.apache.struts2.convention.annotation.DefaultInterceptorRef)

Example 27 with Builder

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

the class DefaultResultMapBuilderTest method testFromServletContextWithBadNames.

public void testFromServletContextWithBadNames() throws Exception {
    ServletContext context = EasyMock.createStrictMock(ServletContext.class);
    // Setup some mock jsps
    Set<String> resources = new HashSet<>();
    resources.add("/WEB-INF/location/namespace/.something");
    resources.add("/WEB-INF/location/namespace/.somethingelse/");
    EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(resources);
    EasyMock.replay(context);
    PackageConfig packageConfig = createPackageConfigBuilder("/namespace");
    this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
    DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
    Map<String, ResultConfig> results = builder.build(NoAnnotationAction.class, null, "no-annotation", packageConfig);
    assertEquals(0, results.size());
    EasyMock.verify(context);
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ServletContext(javax.servlet.ServletContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Example 28 with Builder

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

the class DefaultResultMapBuilderTest method testActionLevelMultipleResultNamesAnnotation.

public void testActionLevelMultipleResultNamesAnnotation() throws Exception {
    ServletContext context = EasyMock.createStrictMock(ServletContext.class);
    // Setup some mock jsps
    Set<String> resources = new HashSet<>();
    EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(resources);
    EasyMock.replay(context);
    PackageConfig packageConfig = createPackageConfigBuilder("/namespace");
    this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
    DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
    Map<String, ResultConfig> results = builder.build(ActionLevelResultsNamesAction.class, getAnnotation(ActionLevelResultsNamesAction.class, "execute", Action.class), "action-level-results", packageConfig);
    assertEquals(4, results.size());
    assertEquals("error", results.get("error").getName());
    assertEquals("input", results.get("input").getName());
    assertEquals("success", results.get("success").getName());
    assertEquals("failure", results.get("failure").getName());
    assertEquals(3, results.get("error").getParams().size());
    assertEquals("/WEB-INF/location/namespace/error.jsp", results.get("error").getParams().get("location"));
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("error").getClassName());
    assertEquals("value", results.get("success").getParams().get("key"));
    assertEquals("value1", results.get("success").getParams().get("key1"));
    assertEquals(3, results.get("input").getParams().size());
    assertEquals("/WEB-INF/location/namespace/error.jsp", results.get("input").getParams().get("location"));
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("input").getClassName());
    assertEquals(3, results.get("failure").getParams().size());
    assertEquals("/WEB-INF/location/namespace/action-failure.jsp", results.get("failure").getParams().get("location"));
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("failure").getClassName());
    assertEquals(3, results.get("success").getParams().size());
    assertEquals("/WEB-INF/location/namespace/action-success.jsp", results.get("success").getParams().get("location"));
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("success").getClassName());
    EasyMock.verify(context);
}
Also used : ClassLevelResultPathAction(org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction) NoAnnotationAction(org.apache.struts2.convention.actions.NoAnnotationAction) Action(org.apache.struts2.convention.annotation.Action) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ServletContext(javax.servlet.ServletContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Example 29 with Builder

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

the class DefaultResultMapBuilderTest method testNull.

public void testNull() throws Exception {
    ServletContext context = EasyMock.createStrictMock(ServletContext.class);
    EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(null);
    EasyMock.replay(context);
    // Test with a slash
    PackageConfig packageConfig = createPackageConfigBuilder("/namespace");
    this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
    DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
    Map<String, ResultConfig> results = builder.build(NoAnnotationAction.class, null, "action", packageConfig);
    assertEquals(0, results.size());
    EasyMock.verify(context);
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ServletContext(javax.servlet.ServletContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 30 with Builder

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

the class DefaultResultMapBuilderTest method testClassLevelMultipleResultAnnotation.

public void testClassLevelMultipleResultAnnotation() throws Exception {
    ServletContext context = EasyMock.createStrictMock(ServletContext.class);
    // Setup some mock jsps
    Set<String> resources = new HashSet<>();
    EasyMock.expect(context.getResourcePaths("/WEB-INF/location/namespace/")).andReturn(resources);
    EasyMock.replay(context);
    PackageConfig packageConfig = createPackageConfigBuilder("/namespace");
    this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
    DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
    Map<String, ResultConfig> results = builder.build(ClassLevelResultsAction.class, null, "class-level-results", packageConfig);
    assertEquals(4, results.size());
    assertEquals("error", results.get("error").getName());
    assertEquals("input", results.get("input").getName());
    assertEquals("success", results.get("success").getName());
    assertEquals("failure", results.get("failure").getName());
    assertEquals(3, results.get("error").getParams().size());
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("error").getClassName());
    assertEquals("/WEB-INF/location/namespace/error.jsp", results.get("error").getParams().get("location"));
    assertEquals("ann-value", results.get("error").getParams().get("key"));
    assertEquals("ann-value1", results.get("error").getParams().get("key1"));
    assertEquals(1, results.get("input").getParams().size());
    assertEquals("foo.action", results.get("input").getParams().get("actionName"));
    assertEquals("org.apache.struts2.result.ServletActionRedirectResult", results.get("input").getClassName());
    assertEquals(3, results.get("failure").getParams().size());
    assertEquals("/WEB-INF/location/namespace/action-failure.jsp", results.get("failure").getParams().get("location"));
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("failure").getClassName());
    assertEquals("value", results.get("failure").getParams().get("key"));
    assertEquals("value1", results.get("failure").getParams().get("key1"));
    assertEquals(3, results.get("success").getParams().size());
    assertEquals("/WEB-INF/location/namespace/action-success.jsp", results.get("success").getParams().get("location"));
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("success").getClassName());
    assertEquals("value", results.get("success").getParams().get("key"));
    assertEquals("value1", results.get("success").getParams().get("key1"));
    EasyMock.verify(context);
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ServletContext(javax.servlet.ServletContext) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Aggregations

PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)23 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)21 ServletContext (javax.servlet.ServletContext)21 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)19 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)18 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)17 HashSet (java.util.HashSet)14 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)10 NoAnnotationAction (org.apache.struts2.convention.actions.NoAnnotationAction)7 ClassLevelResultPathAction (org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction)6 Action (org.apache.struts2.convention.annotation.Action)6 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)4 Configuration (com.opensymphony.xwork2.config.Configuration)4 Container (com.opensymphony.xwork2.inject.Container)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)3 StrutsDefaultConfigurationProvider (com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)3