Search in sources :

Example 36 with Location

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

the class VelocityResult method doExecute.

/**
 * Creates a Velocity context from the action, loads a Velocity template and executes the
 * template. Output is written to the servlet output stream.
 *
 * @param finalLocation the location of the Velocity template
 * @param invocation    an encapsulation of the action execution state.
 * @throws Exception if an error occurs when creating the Velocity context, loading or executing
 *                   the template or writing output to the servlet response stream.
 */
public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    ValueStack stack = ActionContext.getContext().getValueStack();
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    ServletContext servletContext = ServletActionContext.getServletContext();
    Servlet servlet = JspSupportServlet.jspSupportServlet;
    velocityManager.init(servletContext);
    boolean usedJspFactory = false;
    PageContext pageContext = (PageContext) ActionContext.getContext().getPageContext();
    if (pageContext == null && servlet != null) {
        pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true);
        ActionContext.getContext().withPageContext(pageContext);
        usedJspFactory = true;
    }
    try {
        String encoding = getEncoding(finalLocation);
        String contentType = getContentType(finalLocation);
        if (encoding != null) {
            contentType = contentType + ";charset=" + encoding;
        }
        Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);
        Context context = createContext(velocityManager, stack, request, response, finalLocation);
        Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
        response.setContentType(contentType);
        t.merge(context, writer);
        // always flush the writer (we used to only flush it if this was a jspWriter, but someone asked
        // to do it all the time (WW-829). Since Velocity support is being deprecated, we'll oblige :)
        writer.flush();
    } catch (Exception e) {
        LOG.error("Unable to render velocity template: '{}'", finalLocation, e);
        throw e;
    } finally {
        if (usedJspFactory) {
            jspFactory.releasePageContext(pageContext);
        }
    }
}
Also used : PageContext(javax.servlet.jsp.PageContext) ServletActionContext(org.apache.struts2.ServletActionContext) Context(org.apache.velocity.context.Context) ServletContext(javax.servlet.ServletContext) ActionContext(com.opensymphony.xwork2.ActionContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) HttpServletResponse(javax.servlet.http.HttpServletResponse) Template(org.apache.velocity.Template) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) JspSupportServlet(org.apache.struts2.views.JspSupportServlet) Servlet(javax.servlet.Servlet) PageContext(javax.servlet.jsp.PageContext) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 37 with Location

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

the class VelocityResultTest method testCanResolveLocationUsingOgnl.

public void testCanResolveLocationUsingOgnl() throws Exception {
    TestResult result = new TestResult();
    String location = "/myaction.action";
    Bean bean = new Bean();
    bean.setLocation(location);
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(bean);
    TestCase.assertEquals(location, stack.findValue("location"));
    result.setLocation("${location}");
    result.execute(actionInvocation);
    TestCase.assertEquals(location, result.finalLocation);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 38 with Location

use of com.opensymphony.xwork2.util.location.Location 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 39 with Location

use of com.opensymphony.xwork2.util.location.Location 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 40 with Location

use of com.opensymphony.xwork2.util.location.Location 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)

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