Search in sources :

Example 6 with ForwardConfig

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

the class AbstractPerformForward method execute.

// ---------------------------------------------------------- Public Methods
/**
     * <p>Perform forwarding or redirection based on the specified
     * <code>ActionForward</code> (if any).</p>
     *
     * @param actionCtx The <code>Context</code> for the current request
     * @return <code>true</code> so that processing completes
     * @throws Exception if thrown by the <code>Action</code>
     */
public boolean execute(ActionContext actionCtx) throws Exception {
    // Is there a ForwardConfig to be performed?
    ForwardConfig forwardConfig = actionCtx.getForwardConfig();
    if (forwardConfig == null) {
        return (false);
    }
    // Perform the appropriate processing on this ActionForward
    perform(actionCtx, forwardConfig);
    return (true);
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 7 with ForwardConfig

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

the class AbstractSelectForward method execute.

// ---------------------------------------------------------- Public Methods
/**
     * <p>Select and cache the <code>ActionForward</code> for this
     * <code>ActionConfig</code> if specified.</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 not 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();
    ForwardConfig forwardConfig = null;
    String forward = actionConfig.getForward();
    if (forward != null) {
        forwardConfig = forward(actionCtx, moduleConfig, forward);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Forwarding 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)

Example 8 with ForwardConfig

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

the class TestMockBase method setUpThirdApp.

// Set up third app for testing URL mapping
protected void setUpThirdApp() {
    ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
    moduleConfig3 = factoryObject.createModuleConfig("/3");
    context.setAttribute(Globals.MODULE_KEY + "/3", moduleConfig3);
    // Instantiate the controller configuration for this app
    ControllerConfig controller = new ControllerConfig();
    moduleConfig3.setControllerConfig(controller);
    // Configure the properties we will be testing
    controller.setForwardPattern("/forwarding$M$P");
    controller.setInputForward(true);
    controller.setPagePattern("/paging$M$P");
    // Configure global forward declarations
    moduleConfig3.addForwardConfig(new ForwardConfig("moduleForward", "/module/forward", // No redirect, same module
    false));
    moduleConfig3.addForwardConfig(new ForwardConfig("moduleRedirect", "/module/redirect", // Redirect, same module
    true));
    moduleConfig3.addForwardConfig(new ForwardConfig("contextForward", // No redirect
    "/forward", // No redirect
    false, // Specify module
    "/context"));
    moduleConfig3.addForwardConfig(new ForwardConfig("contextRedirect", // Redirect
    "/redirect", // Redirect
    true, // Specify module
    "/context"));
    moduleConfig3.addForwardConfig(new ForwardConfig("moduleNoslash", "module/noslash", // No redirect, same module
    false));
    moduleConfig3.addForwardConfig(new ForwardConfig("contextNoslash", // No redirect
    "noslash", // No redirect
    false, // specify module
    "/context"));
}
Also used : ModuleConfigFactory(org.apache.struts.config.ModuleConfigFactory) ControllerConfig(org.apache.struts.config.ControllerConfig) ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 9 with ForwardConfig

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

the class TestRequestUtils method testForwardURLa.

// Cross module forwards
public void testForwardURLa() {
    request.setAttribute(Globals.MODULE_KEY, moduleConfig);
    request.setPathElements("/myapp", "/action.do", null, null);
    ForwardConfig forward = null;
    String result = null;
    // redirect=false, contextRelative=false, link to module 3
    forward = moduleConfig3.findForwardConfig("moduleForward");
    assertNotNull("moduleForward found", forward);
    result = RequestUtils.forwardURL(request, forward, moduleConfig3);
    assertNotNull("moduleForward computed", result);
    assertEquals("moduleForward value", "/forwarding/3/module/forward", result);
    // redirect=true, contextRelative=false, link to module 3
    forward = moduleConfig3.findForwardConfig("moduleRedirect");
    assertNotNull("moduleRedirect found", forward);
    result = RequestUtils.forwardURL(request, forward, moduleConfig3);
    assertNotNull("moduleRedirect computed", result);
    assertEquals("moduleRedirect value", "/forwarding/3/module/redirect", result);
    // redirect=false, module=/context
    forward = moduleConfig3.findForwardConfig("contextForward");
    assertNotNull("contextForward found", forward);
    result = RequestUtils.forwardURL(request, forward, moduleConfig3);
    assertNotNull("contextForward computed", result);
    assertEquals("contextForward value", "/forwarding/context/forward", result);
    // redirect=true, module=/context
    forward = moduleConfig3.findForwardConfig("contextRedirect");
    assertNotNull("contextRedirect found", forward);
    result = RequestUtils.forwardURL(request, forward, moduleConfig3);
    assertNotNull("contextRedirect computed", result);
    assertEquals("contextRedirct value", "/forwarding/context/redirect", result);
    // noslash, contextRelative=false, link to module 3
    forward = moduleConfig3.findForwardConfig("moduleNoslash");
    assertNotNull("moduleNoslash found", forward);
    result = RequestUtils.forwardURL(request, forward, moduleConfig3);
    assertNotNull("moduleNoslash computed", result);
    assertEquals("moduleNoslash value", "/forwarding/3/module/noslash", result);
    // noslash, module=/
    forward = moduleConfig3.findForwardConfig("contextNoslash");
    assertNotNull("contextNoslash found", forward);
    result = RequestUtils.forwardURL(request, forward, moduleConfig3);
    assertNotNull("contextNoslash computed", result);
    assertEquals("contextNoslash value", "/forwarding/context/noslash", result);
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 10 with ForwardConfig

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

the class TagUtils method computeURLWithCharEncoding.

/**
     * Compute a hyperlink URL based on the <code>forward</code>,
     * <code>href</code>, <code>action</code> or <code>page</code> parameter
     * that is not null. The returned URL will have already been passed to
     * <code>response.encodeURL()</code> for adding a session identifier.
     *
     * @param pageContext      PageContext for the tag making this call
     * @param forward          Logical forward name for which to look up the
     *                         context-relative URI (if specified)
     * @param href             URL to be utilized unmodified (if specified)
     * @param page             Module-relative page for which a URL should be
     *                         created (if specified)
     * @param action           Logical action name for which to look up the
     *                         context-relative URI (if specified)
     * @param params           Map of parameters to be dynamically included
     *                         (if any)
     * @param anchor           Anchor to be dynamically included (if any)
     * @param redirect         Is this URL for a <code>response.sendRedirect()</code>?
     * @param encodeSeparator  This is only checked if redirect is set to
     *                         false (never encoded for a redirect).  If true,
     *                         query string parameter separators are encoded
     *                         as &gt;amp;, else &amp; is used.
     * @param useLocalEncoding If set to true, urlencoding is done on the
     *                         bytes of character encoding from
     *                         ServletResponse#getCharacterEncoding. Use UTF-8
     *                         otherwise.
     * @return URL with session identifier
     * @throws java.net.MalformedURLException if a URL cannot be created for
     *                                        the specified parameters
     */
public String computeURLWithCharEncoding(PageContext pageContext, String forward, String href, String page, String action, String module, Map params, String anchor, boolean redirect, boolean encodeSeparator, boolean useLocalEncoding) throws MalformedURLException {
    String charEncoding = "UTF-8";
    if (useLocalEncoding) {
        charEncoding = pageContext.getResponse().getCharacterEncoding();
    }
    // TODO All the computeURL() methods need refactoring!
    // Validate that exactly one specifier was included
    int n = 0;
    if (forward != null) {
        n++;
    }
    if (href != null) {
        n++;
    }
    if (page != null) {
        n++;
    }
    if (action != null) {
        n++;
    }
    if (n != 1) {
        throw new MalformedURLException(messages.getMessage("computeURL.specifier"));
    }
    // Look up the module configuration for this request
    ModuleConfig moduleConfig = getModuleConfig(module, pageContext);
    // Calculate the appropriate URL
    StringBuffer url = new StringBuffer();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    if (forward != null) {
        ForwardConfig forwardConfig = moduleConfig.findForwardConfig(forward);
        if (forwardConfig == null) {
            throw new MalformedURLException(messages.getMessage("computeURL.forward", forward));
        }
        if (forwardConfig.getPath().startsWith("/")) {
            url.append(request.getContextPath());
            url.append(RequestUtils.forwardURL(request, forwardConfig, moduleConfig));
        } else {
            url.append(forwardConfig.getPath());
        }
    } else if (href != null) {
        url.append(href);
    } else if (action != null) {
        ActionServlet servlet = (ActionServlet) pageContext.getServletContext().getAttribute(Globals.ACTION_SERVLET_KEY);
        String actionIdPath = RequestUtils.actionIdURL(action, moduleConfig, servlet);
        if (actionIdPath != null) {
            action = actionIdPath;
            url.append(request.getContextPath());
            url.append(actionIdPath);
        } else {
            url.append(instance.getActionMappingURL(action, module, pageContext, false));
        }
    } else /* if (page != null) */
    {
        url.append(request.getContextPath());
        url.append(this.pageURL(request, page, moduleConfig));
    }
    // Add anchor if requested (replacing any existing anchor)
    if (anchor != null) {
        String temp = url.toString();
        int hash = temp.indexOf('#');
        if (hash >= 0) {
            url.setLength(hash);
        }
        url.append('#');
        url.append(this.encodeURL(anchor, charEncoding));
    }
    // Add dynamic parameters if requested
    if ((params != null) && (params.size() > 0)) {
        // Save any existing anchor
        String temp = url.toString();
        int hash = temp.indexOf('#');
        if (hash >= 0) {
            anchor = temp.substring(hash + 1);
            url.setLength(hash);
            temp = url.toString();
        } else {
            anchor = null;
        }
        // Define the parameter separator
        String separator = null;
        if (redirect) {
            separator = "&";
        } else if (encodeSeparator) {
            separator = "&amp;";
        } else {
            separator = "&";
        }
        // Add the required request parameters
        boolean question = temp.indexOf('?') >= 0;
        Iterator keys = params.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            Object value = params.get(key);
            if (value == null) {
                if (!question) {
                    url.append('?');
                    question = true;
                } else {
                    url.append(separator);
                }
                url.append(this.encodeURL(key, charEncoding));
                // Interpret null as "no value"
                url.append('=');
            } else if (value instanceof String) {
                if (!question) {
                    url.append('?');
                    question = true;
                } else {
                    url.append(separator);
                }
                url.append(this.encodeURL(key, charEncoding));
                url.append('=');
                url.append(this.encodeURL((String) value, charEncoding));
            } else if (value instanceof String[]) {
                String[] values = (String[]) value;
                for (int i = 0; i < values.length; i++) {
                    if (!question) {
                        url.append('?');
                        question = true;
                    } else {
                        url.append(separator);
                    }
                    url.append(this.encodeURL(key, charEncoding));
                    url.append('=');
                    url.append(this.encodeURL(values[i], charEncoding));
                }
            } else /* Convert other objects to a string */
            {
                if (!question) {
                    url.append('?');
                    question = true;
                } else {
                    url.append(separator);
                }
                url.append(this.encodeURL(key, charEncoding));
                url.append('=');
                url.append(this.encodeURL(value.toString(), charEncoding));
            }
        }
        // Re-add the saved anchor (if any)
        if (anchor != null) {
            url.append('#');
            url.append(this.encodeURL(anchor, charEncoding));
        }
    }
    // but only if url is not an external URL
    if ((href == null) && (pageContext.getSession() != null)) {
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
        if (redirect) {
            return (response.encodeRedirectURL(url.toString()));
        }
        return (response.encodeURL(url.toString()));
    }
    return (url.toString());
}
Also used : MalformedURLException(java.net.MalformedURLException) ModuleConfig(org.apache.struts.config.ModuleConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionServlet(org.apache.struts.action.ActionServlet) HttpServletRequest(javax.servlet.http.HttpServletRequest) Iterator(java.util.Iterator) 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