Search in sources :

Example 51 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.

the class TestRequestUtils method testForwardURL2.

// Second module (default forwardPattern)
public void testForwardURL2() {
    request.setAttribute(Globals.MODULE_KEY, moduleConfig2);
    request.setPathElements("/myapp", "/2/action.do", null, null);
    ForwardConfig forward = null;
    String result = null;
    // redirect=false, module=null
    forward = moduleConfig2.findForwardConfig("moduleForward");
    assertNotNull("moduleForward found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("moduleForward computed", result);
    assertEquals("moduleForward value", "/2/module/forward", result);
    // redirect=true, module=null
    forward = moduleConfig2.findForwardConfig("moduleRedirect");
    assertNotNull("moduleRedirect found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("moduleRedirect computed", result);
    assertEquals("moduleRedirect value", "/2/module/redirect", result);
    // redirect=false, module=/context
    forward = moduleConfig2.findForwardConfig("contextForward");
    assertNotNull("contextForward found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("contextForward computed", result);
    assertEquals("contextForward value", "/context/forward", result);
    // redirect=true, module=/context
    forward = moduleConfig2.findForwardConfig("contextRedirect");
    assertNotNull("contextRedirect found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("contextRedirect computed", result);
    assertEquals("contextRedirct value", "/context/redirect", result);
    // noslash, module=null
    forward = moduleConfig2.findForwardConfig("moduleNoslash");
    assertNotNull("moduleNoslash found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("moduleNoslash computed", result);
    assertEquals("moduleNoslash value", "/2/module/noslash", result);
    // noslash, module=/
    forward = moduleConfig2.findForwardConfig("contextNoslash");
    assertNotNull("contextNoslash found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("contextNoslash computed", result);
    assertEquals("contextNoslash value", "/context/noslash", result);
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 52 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.

the class PerformForward method perform.

// ------------------------------------------------------- Protected Methods
/**
 * <p>Perform the appropriate processing on the specified
 * <code>ForwardConfig</code>.</p>
 *
 * @param context       The context for this request
 * @param forwardConfig The forward to be performed
 */
protected void perform(ActionContext context, ForwardConfig forwardConfig) throws Exception {
    ServletActionContext sacontext = (ServletActionContext) context;
    String uri = forwardConfig.getPath();
    if (uri == null) {
        ActionServlet servlet = sacontext.getActionServlet();
        MessageResources resources = servlet.getInternal();
        throw new IllegalArgumentException(resources.getMessage("forwardPathNull"));
    }
    HttpServletRequest request = sacontext.getRequest();
    ServletContext servletContext = sacontext.getContext();
    HttpServletResponse response = sacontext.getResponse();
    // If the forward can be unaliased into an action, then use the path of the action
    String actionIdPath = RequestUtils.actionIdURL(forwardConfig, sacontext.getRequest(), sacontext.getActionServlet());
    if (actionIdPath != null) {
        uri = actionIdPath;
        ForwardConfig actionIdForwardConfig = new ForwardConfig(forwardConfig);
        actionIdForwardConfig.setPath(actionIdPath);
        forwardConfig = actionIdForwardConfig;
    }
    if (uri.startsWith("/")) {
        uri = resolveModuleRelativePath(forwardConfig, servletContext, request);
    }
    if (response.isCommitted() && !forwardConfig.getRedirect()) {
        handleAsInclude(uri, servletContext, request, response);
    } else if (forwardConfig.getRedirect()) {
        handleAsRedirect(uri, request, response);
    } else {
        handleAsForward(uri, servletContext, request, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MessageResources(org.apache.struts.util.MessageResources) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionServlet(org.apache.struts.action.ActionServlet)

Example 53 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.

the class TestMockBase method setUpSecondApp.

protected void setUpSecondApp() {
    ActionFormBean formBean = null;
    ActionMapping mapping = null;
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    moduleConfig2 = factoryObject.createModuleConfig("/2");
    context.setAttribute(Globals.MODULE_KEY + "/2", moduleConfig2);
    // Forward "external" to "http://jakarta.apache.org/"
    moduleConfig2.addForwardConfig(new ActionForward("external", "http://jakarta.apache.org/", false));
    // Forward "foo" to "/baz.jsp" (different from default)
    moduleConfig2.addForwardConfig(new ActionForward("foo", "/baz.jsp", false));
    // Forward "relative1" to "relative.jsp" non-context-relative
    moduleConfig2.addForwardConfig(new ActionForward("relative1", "relative.jsp", false));
    // Forward "relative2" to "relative.jsp" context-relative
    moduleConfig2.addForwardConfig(new ActionForward("relative2", "relative.jsp", false));
    // Form Bean "static" is a standard ActionForm subclass (same as default)
    formBean = new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
    moduleConfig2.addFormBeanConfig(formBean);
    // Action "/static" uses the "static" form bean in request scope (same as default)
    mapping = new ActionMapping();
    mapping.setInput("/static.jsp");
    mapping.setName("static");
    mapping.setPath("/static");
    mapping.setScope("request");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig2.addActionConfig(mapping);
    // Form Bean "dynamic2" is a DynaActionForm with the same properties
    formBean = new ActionFormBean("dynamic2", "org.apache.struts.action.DynaActionForm");
    formBean.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "boolean", "false"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringProperty", "java.lang.String", null));
    moduleConfig2.addFormBeanConfig(formBean);
    // Action "/dynamic2" uses the "dynamic2" form bean in session scope
    mapping = new ActionMapping();
    mapping.setInput("/dynamic2.jsp");
    mapping.setName("dynamic2");
    mapping.setPath("/dynamic2");
    mapping.setScope("session");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig2.addActionConfig(mapping);
    // Action "/noform" has no form bean associated with it (same as default)
    mapping = new ActionMapping();
    mapping.setPath("/noform");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig2.addActionConfig(mapping);
    // Configure global forward declarations
    moduleConfig2.addForwardConfig(new ForwardConfig("moduleForward", "/module/forward", // No redirect, same module
    false));
    moduleConfig2.addForwardConfig(new ForwardConfig("moduleRedirect", "/module/redirect", // Redirect, same module
    true));
    moduleConfig2.addForwardConfig(new ForwardConfig("contextForward", // No redirect
    "/forward", // No redirect
    false, // Specify module
    "/context"));
    moduleConfig2.addForwardConfig(new ForwardConfig("contextRedirect", // Redirect
    "/redirect", // Redirect
    true, // Specify module
    "/context"));
    moduleConfig2.addForwardConfig(new ForwardConfig("moduleNoslash", "module/noslash", // No redirect, same module
    false));
    moduleConfig2.addForwardConfig(new ForwardConfig("contextNoslash", // No redirect
    "noslash", // No redirect
    false, // Specify module
    "/context"));
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) ActionMapping(org.apache.struts.action.ActionMapping) ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionForward(org.apache.struts.action.ActionForward) ActionFormBean(org.apache.struts.action.ActionFormBean)

Example 54 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.

the class TestMockBase method setUpDefaultApp.

protected void setUpDefaultApp() {
    ActionFormBean formBean = null;
    ActionMapping mapping = null;
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    moduleConfig = factoryObject.createModuleConfig("");
    context.setAttribute(Globals.MODULE_KEY, moduleConfig);
    // Forward "external" to "http://jakarta.apache.org/"
    moduleConfig.addForwardConfig(new ActionForward("external", "http://jakarta.apache.org/", false));
    // Forward "foo" to "/bar.jsp"
    moduleConfig.addForwardConfig(new ActionForward("foo", "/bar.jsp", false));
    // Forward "relative1" to "relative.jsp" non-context-relative
    moduleConfig.addForwardConfig(new ActionForward("relative1", "relative.jsp", false));
    // Forward "relative2" to "relative.jsp" context-relative
    moduleConfig.addForwardConfig(new ActionForward("relative2", "relative.jsp", false));
    // Form Bean "static" is a standard ActionForm subclass
    formBean = new ActionFormBean("static", "org.apache.struts.mock.MockFormBean");
    moduleConfig.addFormBeanConfig(formBean);
    // Action "/static" uses the "static" form bean in request scope
    mapping = new ActionMapping();
    mapping.setInput("/static.jsp");
    mapping.setName("static");
    mapping.setPath("/static");
    mapping.setScope("request");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Form Bean "dynamic" is a DynaActionForm with the same properties
    formBean = new ActionFormBean("dynamic", "org.apache.struts.action.DynaActionForm");
    formBean.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "boolean", "false"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringProperty", "java.lang.String", null));
    moduleConfig.addFormBeanConfig(formBean);
    // Action "/dynamic" uses the "dynamic" form bean in session scope
    mapping = new ActionMapping();
    mapping.setInput("/dynamic.jsp");
    mapping.setName("dynamic");
    mapping.setPath("/dynamic");
    mapping.setScope("session");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Form Bean "/dynamic0" is a DynaActionForm with initializers
    formBean = new ActionFormBean("dynamic0", "org.apache.struts.action.DynaActionForm");
    formBean.addFormPropertyConfig(new FormPropertyConfig("booleanProperty", "boolean", "true"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringProperty", "java.lang.String", "String Property"));
    formBean.addFormPropertyConfig(new FormPropertyConfig("intArray1", "int[]", "{1,2,3}", // 4 should be ignored
    4));
    formBean.addFormPropertyConfig(new FormPropertyConfig("intArray2", "int[]", null, // 5 should be respected
    5));
    formBean.addFormPropertyConfig(new FormPropertyConfig("principal", "org.apache.struts.mock.MockPrincipal", null));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray1", "java.lang.String[]", "{aaa,bbb,ccc}", // 2 should be ignored
    2));
    formBean.addFormPropertyConfig(new FormPropertyConfig("stringArray2", "java.lang.String[]", null, // 3 should be respected
    3));
    moduleConfig.addFormBeanConfig(formBean);
    // Action "/dynamic0" uses the "dynamic0" form bean in request scope
    mapping = new ActionMapping();
    mapping.setName("dynamic0");
    mapping.setPath("/dynamic0");
    mapping.setScope("request");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Action "/noform" has no form bean associated with it
    mapping = new ActionMapping();
    mapping.setPath("/noform");
    mapping.setType("org.apache.struts.mock.MockAction");
    moduleConfig.addActionConfig(mapping);
    // Configure global forward declarations
    moduleConfig.addForwardConfig(new ForwardConfig("moduleForward", "/module/forward", // No redirect, same module
    false));
    moduleConfig.addForwardConfig(new ForwardConfig("moduleRedirect", "/module/redirect", // Redirect, same module
    true));
    moduleConfig.addForwardConfig(new ForwardConfig("contextForward", // No redirect
    "/forward", // No redirect
    false, // Specify module
    "/context"));
    moduleConfig.addForwardConfig(new ForwardConfig("contextRedirect", // Redirect
    "/redirect", // Redirect
    true, // Specify module
    "/context"));
    moduleConfig.addForwardConfig(new ForwardConfig("moduleNoslash", "module/noslash", // No redirect, same module
    false));
    moduleConfig.addForwardConfig(new ForwardConfig("contextNoslash", // No redirect
    "noslash", // No redirect
    false, // Specify module
    "/context"));
}
Also used : FormPropertyConfig(org.apache.struts.config.FormPropertyConfig) ActionMapping(org.apache.struts.action.ActionMapping) ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) ForwardConfig(org.apache.struts.config.ForwardConfig) ActionForward(org.apache.struts.action.ActionForward) ActionFormBean(org.apache.struts.action.ActionFormBean)

Example 55 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonar-java by SonarSource.

the class AbstractSelectInput method execute.

// ---------------------------------------------------------- Public Methods
/**
 * <p>Select and cache a <code>ForwardConfig</code> for the input page for
 * the current request.</p>
 *
 * @param actionCtx The <code>Context</code> for the current request
 * @return <code>false</code> so that processing continues
 * @throws Exception if thrown by the Action class
 */
public boolean execute(ActionContext actionCtx) throws Exception {
    // Skip processing if the current request is valid
    Boolean valid = actionCtx.getFormValid();
    if ((valid != null) && valid.booleanValue()) {
        return (false);
    }
    // Acquire configuration objects that we need
    ActionConfig actionConfig = actionCtx.getActionConfig();
    ModuleConfig moduleConfig = actionConfig.getModuleConfig();
    // Cache an ForwardConfig back to our input page
    ForwardConfig forwardConfig;
    String input = actionConfig.getInput();
    if (moduleConfig.getControllerConfig().getInputForward()) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Finding ForwardConfig for '" + input + "'");
        }
        forwardConfig = actionConfig.findForwardConfig(input);
        if (forwardConfig == null) {
            forwardConfig = moduleConfig.findForwardConfig(input);
        }
    } else {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Delegating to forward() for '" + input + "'");
        }
        forwardConfig = forward(actionCtx, moduleConfig, input);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Forwarding back to " + forwardConfig);
    }
    actionCtx.setForwardConfig(forwardConfig);
    return (false);
}
Also used : ActionConfig(org.apache.struts.config.ActionConfig) ModuleConfig(org.apache.struts.config.ModuleConfig) ForwardConfig(org.apache.struts.config.ForwardConfig)

Aggregations

ForwardConfig (org.apache.struts.config.ForwardConfig)63 ActionConfig (org.apache.struts.config.ActionConfig)14 UnavailableException (javax.servlet.UnavailableException)12 ServletException (javax.servlet.ServletException)10 ExceptionConfig (org.apache.struts.config.ExceptionConfig)10 MalformedURLException (java.net.MalformedURLException)8 ModuleConfig (org.apache.struts.config.ModuleConfig)8 IOException (java.io.IOException)6 MissingResourceException (java.util.MissingResourceException)6 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)6 SAXException (org.xml.sax.SAXException)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ActionFormBean (org.apache.struts.action.ActionFormBean)4 ActionForward (org.apache.struts.action.ActionForward)4 ActionMapping (org.apache.struts.action.ActionMapping)4 ActionServlet (org.apache.struts.action.ActionServlet)4 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)4 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)3 ArrayList (java.util.ArrayList)2