Search in sources :

Example 11 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonarqube by SonarSource.

the class TestActionServlet method testProcessForwardConfigClassSubConfigCustomClass.

/**
     * Make sure processForwardConfigClass() returns the same class instance
     * if the base config isn't using a custom class.
     */
public void testProcessForwardConfigClassSubConfigCustomClass() throws Exception {
    moduleConfig.addForwardConfig(baseForward);
    ForwardConfig customSub = new ActionForward();
    customSub.setName("failure");
    customSub.setExtends("success");
    moduleConfig.addForwardConfig(customSub);
    ForwardConfig result = actionServlet.processForwardConfigClass(customSub, moduleConfig, null);
    assertSame("The instance returned should be the param given it.", customSub, result);
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 12 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonarqube by SonarSource.

the class TestRequestUtils method testForwardURL1.

// ----------------------------------------------------------- forwardURL()
// Default module (default forwardPattern)
public void testForwardURL1() {
    request.setAttribute(Globals.MODULE_KEY, moduleConfig);
    request.setPathElements("/myapp", "/action.do", null, null);
    ForwardConfig forward = null;
    String result = null;
    // redirect=false, module=null
    forward = moduleConfig.findForwardConfig("moduleForward");
    assertNotNull("moduleForward found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("moduleForward computed", result);
    assertEquals("moduleForward value", "/module/forward", result);
    // redirect=true, module=null
    forward = moduleConfig.findForwardConfig("moduleRedirect");
    assertNotNull("moduleRedirect found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("moduleRedirect computed", result);
    assertEquals("moduleRedirect value", "/module/redirect", result);
    // redirect=false, module=/context
    forward = moduleConfig.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 = moduleConfig.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 = moduleConfig.findForwardConfig("moduleNoslash");
    assertNotNull("moduleNoslash found", forward);
    result = RequestUtils.forwardURL(request, forward, null);
    assertNotNull("moduleNoslash computed", result);
    assertEquals("moduleNoslash value", "/module/noslash", result);
    // noslash, module=/
    forward = moduleConfig.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 13 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonarqube 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 14 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonarqube by SonarSource.

the class TestPerformForward method testNullForwardPath.

public void testNullForwardPath() throws Exception {
    ForwardConfig config = new ForwardConfig();
    config.setPath(null);
    try {
        command.perform(saContext, config);
        fail("Didn't throw an illegal argument exception on null forward path");
    } catch (IllegalArgumentException ex) {
        System.out.println("exception: " + ex.getMessage());
    // Do nothing, the test passed
    }
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 15 with ForwardConfig

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

the class TilesPreProcessor method execute.

// ---------------------------------------------------------- Public Methods
/**
 * <p>If the current <code>ForwardConfig</code> is using "tiles",
 * perform necessary pre-processing to set up the <code>TilesContext</code>
 * and substitute a new <code>ForwardConfig</code> which is understandable
 * to a <code>RequestDispatcher</code>.</p>
 *
 * <p>Note that if the command finds a previously existing
 * <code>ComponentContext</code> in the request, then it
 * infers that it has been called from within another tile,
 * so instead of changing the <code>ForwardConfig</code> in the chain
 * <code>Context</code>, the command uses <code>RequestDispatcher</code>
 * to <em>include</em> the tile, and returns true, indicating that the processing
 * chain is complete.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> in most cases, but true if we determine
 * that we're processing in "include" mode.
 */
public boolean execute(Context context) throws Exception {
    // Is there a Tiles Definition to be processed?
    ServletActionContext sacontext = (ServletActionContext) context;
    ForwardConfig forwardConfig = sacontext.getForwardConfig();
    if (forwardConfig == null || forwardConfig.getPath() == null) {
        log.debug("No forwardConfig or no path, so pass to next command.");
        return (false);
    }
    ComponentDefinition definition = null;
    try {
        definition = TilesUtil.getDefinition(forwardConfig.getPath(), sacontext.getRequest(), sacontext.getContext());
    } catch (FactoryNotFoundException ex) {
        // this is not a serious error, so log at low priority
        log.debug("Tiles DefinitionFactory not found, so pass to next command.");
        return false;
    } catch (NoSuchDefinitionException ex) {
        // ignore not found
        log.debug("NoSuchDefinitionException " + ex.getMessage());
    }
    // Do we do a forward (original behavior) or an include ?
    boolean doInclude = false;
    ComponentContext tileContext = null;
    // Get current tile context if any.
    // If context exists, or if the response has already been committed we will do an include
    tileContext = ComponentContext.getContext(sacontext.getRequest());
    doInclude = (tileContext != null || sacontext.getResponse().isCommitted());
    // Controller associated to a definition, if any
    Controller controller = null;
    // Computed uri to include
    String uri = null;
    if (definition != null) {
        // We have a "forward config" definition.
        // We use it to complete missing attribute in context.
        // We also get uri, controller.
        uri = definition.getPath();
        controller = definition.getOrCreateController();
        if (tileContext == null) {
            tileContext = new ComponentContext(definition.getAttributes());
            ComponentContext.setContext(tileContext, sacontext.getRequest());
        } else {
            tileContext.addMissing(definition.getAttributes());
        }
    }
    // Process definition set in Action, if any.  This may override the
    // values for uri or controller found using the ForwardConfig, and
    // may augment the tileContext with additional attributes.
    // :FIXME: the class DefinitionsUtil is deprecated, but I can't find
    // the intended alternative to use.
    definition = DefinitionsUtil.getActionDefinition(sacontext.getRequest());
    if (definition != null) {
        // We also overload uri and controller if set in definition.
        if (definition.getPath() != null) {
            log.debug("Override forward uri " + uri + " with action uri " + definition.getPath());
            uri = definition.getPath();
        }
        if (definition.getOrCreateController() != null) {
            log.debug("Override forward controller with action controller");
            controller = definition.getOrCreateController();
        }
        if (tileContext == null) {
            tileContext = new ComponentContext(definition.getAttributes());
            ComponentContext.setContext(tileContext, sacontext.getRequest());
        } else {
            tileContext.addMissing(definition.getAttributes());
        }
    }
    if (uri == null) {
        log.debug("no uri computed, so pass to next command");
        return false;
    }
    // Execute controller associated to definition, if any.
    if (controller != null) {
        log.trace("Execute controller: " + controller);
        controller.execute(tileContext, sacontext.getRequest(), sacontext.getResponse(), sacontext.getContext());
    }
    if (doInclude) {
        log.info("Tiles process complete; doInclude with " + uri);
        doInclude(sacontext, uri);
    } else {
        log.info("Tiles process complete; forward to " + uri);
        doForward(sacontext, uri);
    }
    log.debug("Tiles processed, so clearing forward config from context.");
    sacontext.setForwardConfig(null);
    return (false);
}
Also used : FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) NoSuchDefinitionException(org.apache.struts.tiles.NoSuchDefinitionException) ComponentContext(org.apache.struts.tiles.ComponentContext) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ForwardConfig(org.apache.struts.config.ForwardConfig) Controller(org.apache.struts.tiles.Controller) ComponentDefinition(org.apache.struts.tiles.ComponentDefinition)

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