Example usage for org.apache.commons.chain.web.servlet ServletWebContext getRequest

List of usage examples for org.apache.commons.chain.web.servlet ServletWebContext getRequest

Introduction

In this page you can find the example usage for org.apache.commons.chain.web.servlet ServletWebContext getRequest.

Prototype

public HttpServletRequest getRequest() 

Source Link

Document

<p>Return the HttpServletRequest for this context.</p>

Usage

From source file:org.apache.struts.chain.servlet.SelectModule.java

protected String getPrefix(Context context) {

    // Identify the URI from which we will match a module prefix
    ServletWebContext swcontext = (ServletWebContext) context;
    HttpServletRequest request = swcontext.getRequest();
    String uri = (String) request.getAttribute(Constants.INCLUDE_SERVLET_PATH);
    if (uri == null) {
        uri = request.getServletPath();/*from w w  w.  j  a  v a  2 s  .  c  o  m*/
    }
    if (uri == null) {
        throw new IllegalArgumentException("No path information in request");
    }

    // Identify the module prefix for the current module
    String prefix = ""; // Initialize to default prefix
    String prefixes[] = (String[]) swcontext.getApplicationScope().get(Globals.MODULE_PREFIXES_KEY);
    int lastSlash = 0;
    while (prefix.equals("") && ((lastSlash = uri.lastIndexOf("/")) > 0)) {
        uri = uri.substring(0, lastSlash);
        for (int i = 0; i < prefixes.length; i++) {
            if (uri.equals(prefixes[i])) {
                prefix = prefixes[i];
                break;
            }
        }
    }

    return (prefix);

}

From source file:org.apache.struts.chain.servlet.TilesPreProcessor.java

/**
 * <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>/* w w w  .ja v a  2 s  . co m*/
 *
 * @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?
    ForwardConfig forwardConfig = (ForwardConfig) context.get(getForwardConfigKey());
    if (forwardConfig == null || forwardConfig.getPath() == null) {
        log.debug("No forwardConfig or no path, so pass to next command.");
        return (false);
    }

    ServletWebContext swcontext = (ServletWebContext) context;

    ComponentDefinition definition = null;
    try {
        definition = TilesUtil.getDefinition(forwardConfig.getPath(), swcontext.getRequest(),
                swcontext.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;
    }

    // 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, we will do an include
    tileContext = ComponentContext.getContext(swcontext.getRequest());
    doInclude = (tileContext != null);

    // 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, swcontext.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(swcontext.getRequest());
    if (definition != null) { // We have a definition.
        // We use it to complete missing attribute in context.
        // 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, swcontext.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, swcontext.getRequest(), swcontext.getResponse(),
                swcontext.getContext());
    }

    // If request comes from a previous Tile, do an include.
    // This allows to insert an action in a Tile.

    if (doInclude) {
        log.info("Tiles process complete; doInclude with " + uri);
        doInclude(swcontext, uri);
        return (true);
    } else {
        // create an "instant" forward config which can be used
        // by an AbstractPerformForward later as if our ForwardConfig
        // were the one actually returned by an executing Action
        log.info("Tiles process complete; forward to " + uri);
        // :FIXME: How do we need to coordinate the "context-relative" value
        // with other places it might be set.  For now, hardcode to true.
        context.put(getForwardConfigKey(), new ForwardConfig("tiles-chain", uri, false, true));
        return (false);
    }
}

From source file:org.apache.struts.chain.servlet.TilesPreProcessor.java

/**
 * <p>Do an include of specified URI using a <code>RequestDispatcher</code>.</p>
 *
 * @param swcontext a chain servlet/web context
 * @param uri Context-relative URI to include
 *///  ww  w.j  a va 2s.c  o m
protected void doInclude(ServletWebContext swcontext, String uri) throws IOException, ServletException {

    HttpServletRequest request = swcontext.getRequest();

    // Unwrap the multipart request, if there is one.
    if (request instanceof MultipartRequestWrapper) {
        request = ((MultipartRequestWrapper) request).getRequest();
    }

    HttpServletResponse response = swcontext.getResponse();
    RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri);
    if (rd == null) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Error getting RequestDispatcher for " + uri);
        return;
    }
    rd.include(request, response);
}

From source file:org.apache.struts.chain.servlet.ValidateActionForm.java

/**
 * <p>Call the <code>validate()</code> method of the specified form bean,
 * and return the resulting <code>ActionErrors</code> object.</p>
 *
 * @param context The context for this request
 * @param actionForm The form bean for this request
 *//*  w  ww. ja  va2  s . c  o  m*/
protected ActionErrors validate(Context context, ActionConfig actionConfig, ActionForm actionForm) {

    ServletWebContext swcontext = (ServletWebContext) context;
    ActionErrors errors = (actionForm.validate((ActionMapping) actionConfig, swcontext.getRequest()));

    // Special handling for multipart request
    if (errors != null && !errors.isEmpty()) {
        if (actionForm.getMultipartRequestHandler() != null) {
            if (log.isTraceEnabled()) {
                log.trace("  Rolling back multipart request");
            }
            actionForm.getMultipartRequestHandler().rollback();
        }
    }

    // Saving the errors is not part of the contract for this method,
    // but the idea of the HttpServletRequest is not present in the
    // abstract parent of this class.  Put this in now so that it
    // at least gets done -- and then see if other developers have
    // opinions about whether this is good, bad, or at least acceptable.
    swcontext.getRequest().setAttribute(Globals.ERROR_KEY, errors);

    return errors;

}