Search in sources :

Example 31 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class DefaultResultMapBuilderTest method testActionLevelMultipleResultAnnotation.

public void testActionLevelMultipleResultAnnotation() 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(ActionLevelResultsAction.class, getAnnotation(ActionLevelResultsAction.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("org.apache.struts2.result.ServletDispatcherResult", results.get("error").getClassName());
    assertEquals("/WEB-INF/location/namespace/error.jsp", results.get("error").getParams().get("location"));
    assertEquals("value", results.get("success").getParams().get("key"));
    assertEquals("value1", results.get("success").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(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 32 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class DefaultResultMapBuilderTest method testBuild.

public void testBuild() throws Exception {
    ServletContext context = mockServletContext("/WEB-INF/location");
    // 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);
    verify(context, "/WEB-INF/location", results, false);
    // Test without a slash
    context = mockServletContext("/WEB-INF/location");
    packageConfig = createPackageConfigBuilder("namespace");
    this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
    builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
    results = builder.build(NoAnnotationAction.class, null, "action", packageConfig);
    verify(context, "/WEB-INF/location", results, false);
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ServletContext(javax.servlet.ServletContext) NoAnnotationAction(org.apache.struts2.convention.actions.NoAnnotationAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 33 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class DefaultResultMapBuilderTest method testGlobalResultOverride.

public void testGlobalResultOverride() throws Exception {
    ServletContext context = EasyMock.createStrictMock(ServletContext.class);
    String resultPath = "/WEB-INF/location";
    // Setup some mock jsps
    Set<String> resources = new HashSet<>();
    resources.add(resultPath + "/namespace/action.jsp");
    resources.add(resultPath + "/namespace/action-success.jsp");
    resources.add(resultPath + "/namespace/action-error.jsp");
    EasyMock.expect(context.getResourcePaths(resultPath + "/namespace/")).andReturn(resources);
    EasyMock.replay(context);
    this.conventionsService = new ConventionsServiceImpl("/WEB-INF/location");
    DefaultResultMapBuilder builder = new DefaultResultMapBuilder(context, container, "dispatcher,velocity,freemarker");
    ResultTypeConfig resultType = new ResultTypeConfig.Builder("dispatcher", ServletDispatcherResult.class.getName()).addParam("key", "value").addParam("key1", "value1").defaultResultParam("location").build();
    ResultConfig globalError = new ResultConfig.Builder("error", ServletDispatcherResult.class.getName()).addParam("location", "/globalError.jsp").build();
    PackageConfig packageConfig = new PackageConfig.Builder("package").namespace("/namespace").defaultResultType("dispatcher").addResultTypeConfig(resultType).addGlobalResultConfig(globalError).build();
    Map<String, ResultConfig> results = builder.build(GlobalResultOverrideAction.class, null, "action", packageConfig);
    ResultConfig result = results.get("error");
    assertNotNull(result);
    assertEquals(resultPath + "/namespace/action-error.jsp", result.getParams().get("location"));
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) ServletDispatcherResult(org.apache.struts2.result.ServletDispatcherResult) ResultTypeConfig(com.opensymphony.xwork2.config.entities.ResultTypeConfig) ServletContext(javax.servlet.ServletContext) HashSet(java.util.HashSet)

Example 34 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class DefaultResultMapBuilderTest method testFromServletContext.

public void testFromServletContext() throws Exception {
    ServletContext context = EasyMock.createStrictMock(ServletContext.class);
    // Setup some mock jsps
    Set<String> resources = new HashSet<>();
    resources.add("/WEB-INF/location/namespace/no-annotation.ftl");
    resources.add("/WEB-INF/location/namespace/no-annotation-success.jsp");
    resources.add("/WEB-INF/location/namespace/no-annotation-failure.jsp");
    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(4, results.size());
    assertEquals("success", results.get("success").getName());
    assertEquals(3, results.get("success").getParams().size());
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("success").getClassName());
    assertEquals("/WEB-INF/location/namespace/no-annotation-success.jsp", results.get("success").getParams().get("location"));
    assertEquals(1, results.get("input").getParams().size());
    assertEquals("org.apache.struts2.views.freemarker.FreemarkerResult", results.get("input").getClassName());
    assertEquals("/WEB-INF/location/namespace/no-annotation.ftl", results.get("input").getParams().get("location"));
    assertEquals(1, results.get("error").getParams().size());
    assertEquals("org.apache.struts2.views.freemarker.FreemarkerResult", results.get("error").getClassName());
    assertEquals("/WEB-INF/location/namespace/no-annotation.ftl", results.get("error").getParams().get("location"));
    assertEquals(3, results.get("failure").getParams().size());
    assertEquals("org.apache.struts2.result.ServletDispatcherResult", results.get("success").getClassName());
    assertEquals("/WEB-INF/location/namespace/no-annotation-failure.jsp", results.get("failure").getParams().get("location"));
    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 35 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class JSPLoader method compileJava.

/**
 * Compiles the given source code into java bytecode
 */
private void compileJava(String className, final String source, Set<String> extraClassPath) throws IOException {
    LOG.trace("Compiling [{}], source: [{}]", className, source);
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    // the generated bytecode is fed to the class loader
    JavaFileManager jfm = new ForwardingJavaFileManager<StandardJavaFileManager>(compiler.getStandardFileManager(diagnostics, null, null)) {

        @Override
        public JavaFileObject getJavaFileForOutput(Location location, String name, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
            MemoryJavaFileObject fileObject = new MemoryJavaFileObject(name, kind);
            classLoader.addMemoryJavaFileObject(name, fileObject);
            return fileObject;
        }
    };
    // read java source code from memory
    String fileName = className.replace('.', '/') + ".java";
    SimpleJavaFileObject sourceCodeObject = new SimpleJavaFileObject(toURI(fileName), JavaFileObject.Kind.SOURCE) {

        @Override
        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException, IllegalStateException, UnsupportedOperationException {
            return source;
        }
    };
    // build classpath
    // some entries will be added multiple times, hence the set
    List<String> optionList = new ArrayList<String>();
    Set<String> classPath = new HashSet<String>();
    // find available jars
    ClassLoaderInterface classLoaderInterface = getClassLoaderInterface();
    UrlSet urlSet = new UrlSet(classLoaderInterface);
    // find jars
    List<URL> urls = urlSet.getUrls();
    if (urls != null && urls.size() > 0) {
        final FileManagerFactory fileManagerFactoryGetInstance = ServletActionContext.getActionContext().getInstance(FileManagerFactory.class);
        final FileManagerFactory contextFileManagerFactory = (fileManagerFactoryGetInstance != null ? fileManagerFactoryGetInstance : (FileManagerFactory) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER_FACTORY));
        final FileManagerFactory fileManagerFactory = (contextFileManagerFactory != null ? contextFileManagerFactory : new DefaultFileManagerFactory());
        final FileManager fileManagerGetInstance = fileManagerFactory.getFileManager();
        final FileManager contextFileManager = (fileManagerGetInstance != null ? fileManagerGetInstance : (FileManager) ServletActionContext.getActionContext().get(StrutsConstants.STRUTS_FILE_MANAGER));
        final FileManager fileManager = (contextFileManager != null ? contextFileManager : new DefaultFileManager());
        for (URL url : urls) {
            URL normalizedUrl = fileManager.normalizeToFileProtocol(url);
            File file = FileUtils.toFile(ObjectUtils.defaultIfNull(normalizedUrl, url));
            if (file.exists())
                classPath.add(file.getAbsolutePath());
        }
    }
    // these should be in the list already, but I am feeling paranoid
    // this jar
    classPath.add(getJarUrl(EmbeddedJSPResult.class));
    // servlet api
    classPath.add(getJarUrl(Servlet.class));
    // jsp api
    classPath.add(getJarUrl(JspPage.class));
    try {
        Class instanceManager = Class.forName("org.apache.tomcat.InstanceManager");
        classPath.add(getJarUrl(instanceManager));
    } catch (ClassNotFoundException e) {
    // ok ignore
    }
    // add extra classpath entries (jars where tlds were found will be here)
    for (Iterator<String> iterator = extraClassPath.iterator(); iterator.hasNext(); ) {
        String entry = iterator.next();
        classPath.add(entry);
    }
    String classPathString = StringUtils.join(classPath, File.pathSeparator);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Compiling [#0] with classpath [#1]", className, classPathString);
    }
    optionList.addAll(Arrays.asList("-classpath", classPathString));
    // compile
    JavaCompiler.CompilationTask task = compiler.getTask(null, jfm, diagnostics, optionList, null, Arrays.asList(sourceCodeObject));
    if (!task.call()) {
        throw new StrutsException("Compilation failed:" + diagnostics.getDiagnostics().get(0).toString());
    }
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) ArrayList(java.util.ArrayList) JspPage(javax.servlet.jsp.JspPage) URL(java.net.URL) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) UrlSet(com.opensymphony.xwork2.util.finder.UrlSet) Servlet(javax.servlet.Servlet) DiagnosticCollector(javax.tools.DiagnosticCollector) FileObject(javax.tools.FileObject) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) MemoryJavaFileObject(org.apache.struts2.compiler.MemoryJavaFileObject) HashSet(java.util.HashSet) FileManagerFactory(com.opensymphony.xwork2.FileManagerFactory) DefaultFileManagerFactory(com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ClassLoaderInterface(com.opensymphony.xwork2.util.finder.ClassLoaderInterface) JavaCompiler(javax.tools.JavaCompiler) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) JavaFileManager(javax.tools.JavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) FileManager(com.opensymphony.xwork2.FileManager) StandardJavaFileManager(javax.tools.StandardJavaFileManager) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) DefaultFileManager(com.opensymphony.xwork2.util.fs.DefaultFileManager) File(java.io.File)

Aggregations

ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)32 ServletContext (javax.servlet.ServletContext)22 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)21 HashSet (java.util.HashSet)15 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)11 ActionContext (com.opensymphony.xwork2.ActionContext)10 ValueStack (com.opensymphony.xwork2.util.ValueStack)10 HashMap (java.util.HashMap)9 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)8 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)8 ServletActionContext (org.apache.struts2.ServletActionContext)8 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)7 Location (com.opensymphony.xwork2.util.location.Location)7 ArrayList (java.util.ArrayList)7 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 NodeList (org.w3c.dom.NodeList)6 ActionProxy (com.opensymphony.xwork2.ActionProxy)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5