Search in sources :

Example 26 with ForwardConfig

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

the class TestActionServlet method testProcessForwardConfigClassOverriddenSubConfigClass.

/**
 * Test the case where the subconfig has already specified its own config
 * class.  If the code still attempts to create a new instance, an error
 * will be thrown.
 */
public void testProcessForwardConfigClassOverriddenSubConfigClass() throws Exception {
    moduleConfig.addForwardConfig(baseForward);
    ForwardConfig customSub = new CustomForwardConfigArg("failure", "/failure.jsp");
    customSub.setExtends("success");
    moduleConfig.addForwardConfig(customSub);
    try {
        actionServlet.processForwardConfigClass(customSub, moduleConfig, null);
    } catch (Exception e) {
        fail("Exception should not be thrown");
    }
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig) ServletException(javax.servlet.ServletException) UnavailableException(javax.servlet.UnavailableException)

Example 27 with ForwardConfig

use of org.apache.struts.config.ForwardConfig in project sonar-java 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 28 with ForwardConfig

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

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

the class RequestProcessor method processValidate.

/**
     * <p>If this request was not cancelled, and the request's {@link
     * ActionMapping} has not disabled validation, call the
     * <code>validate</code> method of the specified {@link ActionForm}, and
     * forward to the input path if there were any errors. Return
     * <code>true</code> if we should continue processing, or
     * <code>false</code> if we have already forwarded control back to the
     * input form.</p>
     *
     * @param request  The servlet request we are processing
     * @param response The servlet response we are creating
     * @param form     The ActionForm instance we are populating
     * @param mapping  The ActionMapping we are using
     * @return <code>true</code> to continue normal processing;
     *         <code>false</code> if a response has been created.
     * @throws IOException      if an input/output error occurs
     * @throws ServletException if a servlet exception occurs
     * @throws InvalidCancelException if a cancellation is attempted
     *         without the proper action configuration.
     */
protected boolean processValidate(HttpServletRequest request, HttpServletResponse response, ActionForm form, ActionMapping mapping) throws IOException, ServletException, InvalidCancelException {
    if (form == null) {
        return (true);
    }
    // Has validation been turned off for this mapping?
    if (!mapping.getValidate()) {
        return (true);
    }
    // error or a spoof.
    if (request.getAttribute(Globals.CANCEL_KEY) != null) {
        if (mapping.getCancellable()) {
            if (log.isDebugEnabled()) {
                log.debug(" Cancelled transaction, skipping validation");
            }
            return (true);
        } else {
            request.removeAttribute(Globals.CANCEL_KEY);
            throw new InvalidCancelException();
        }
    }
    // Call the form bean's validation method
    if (log.isDebugEnabled()) {
        log.debug(" Validating input form properties");
    }
    ActionMessages errors = form.validate(mapping, request);
    if ((errors == null) || errors.isEmpty()) {
        if (log.isTraceEnabled()) {
            log.trace("  No errors detected, accepting input");
        }
        return (true);
    }
    // Special handling for multipart request
    if (form.getMultipartRequestHandler() != null) {
        if (log.isTraceEnabled()) {
            log.trace("  Rolling back multipart request");
        }
        form.getMultipartRequestHandler().rollback();
    }
    // Was an input path (or forward) specified for this mapping?
    String input = mapping.getInput();
    if (input == null) {
        if (log.isTraceEnabled()) {
            log.trace("  Validation failed but no input form available");
        }
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, getInternal().getMessage("noInput", mapping.getPath()));
        return (false);
    }
    // Save our error messages and return to the input form if possible
    if (log.isDebugEnabled()) {
        log.debug(" Validation failed, returning to '" + input + "'");
    }
    request.setAttribute(Globals.ERROR_KEY, errors);
    if (moduleConfig.getControllerConfig().getInputForward()) {
        ForwardConfig forward = mapping.findForward(input);
        processForwardConfig(request, response, forward);
    } else {
        internalModuleRelativeForward(input, request, response);
    }
    return (false);
}
Also used : ForwardConfig(org.apache.struts.config.ForwardConfig)

Example 30 with ForwardConfig

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

the class ActionServlet method processForwardConfigClass.

/**
     * <p>Checks if the current forwardConfig is using the correct class based
     * on the class of its configuration ancestor.  If actionConfig is
     * provided, then this method will process the forwardConfig as part
     * of that actionConfig.  If actionConfig is null, the forwardConfig
     * will be processed as a global forward.</p>
     *
     * @param forwardConfig The forward to check.
     * @param moduleConfig  The config for the current module.
     * @param actionConfig  If applicable, the config for the current action.
     * @return The forward config using the correct class as determined by the
     *         config's ancestor and its own overridden value.
     * @throws UnavailableException if an instance of the forward config class
     *                              cannot be created.
     * @throws ServletException     on class creation error
     */
protected ForwardConfig processForwardConfigClass(ForwardConfig forwardConfig, ModuleConfig moduleConfig, ActionConfig actionConfig) throws ServletException {
    String ancestor = forwardConfig.getExtends();
    if (ancestor == null) {
        // Nothing to do, then
        return forwardConfig;
    }
    // Make sure that this config is of the right class
    ForwardConfig baseConfig = null;
    if (actionConfig != null) {
        // Look for this in the actionConfig
        baseConfig = actionConfig.findForwardConfig(ancestor);
    }
    if (baseConfig == null) {
        // Either this is a forwardConfig that inherits a global config,
        //  or actionConfig is null
        baseConfig = moduleConfig.findForwardConfig(ancestor);
    }
    if (baseConfig == null) {
        throw new UnavailableException("Unable to find " + "forward '" + ancestor + "' to extend.");
    }
    // Was our forwards's class overridden already?
    if (forwardConfig.getClass().equals(ActionForward.class)) {
        // Ensure that our forward is using the correct class
        if (!baseConfig.getClass().equals(forwardConfig.getClass())) {
            // Replace the config with an instance of the correct class
            ForwardConfig newForwardConfig = null;
            String baseConfigClassName = baseConfig.getClass().getName();
            try {
                newForwardConfig = (ForwardConfig) RequestUtils.applicationInstance(baseConfigClassName);
                // copy the values
                BeanUtils.copyProperties(newForwardConfig, forwardConfig);
            } catch (Exception e) {
                handleCreationException(baseConfigClassName, e);
            }
            // replace forwardConfig with newForwardConfig
            if (actionConfig != null) {
                actionConfig.removeForwardConfig(forwardConfig);
                actionConfig.addForwardConfig(newForwardConfig);
            } else {
                // this is a global forward
                moduleConfig.removeForwardConfig(forwardConfig);
                moduleConfig.addForwardConfig(newForwardConfig);
            }
            forwardConfig = newForwardConfig;
        }
    }
    return forwardConfig;
}
Also used : UnavailableException(javax.servlet.UnavailableException) ForwardConfig(org.apache.struts.config.ForwardConfig) ServletException(javax.servlet.ServletException) MissingResourceException(java.util.MissingResourceException) SAXException(org.xml.sax.SAXException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException)

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