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

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

Introduction

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

Prototype

public ServletContext getContext() 

Source Link

Document

Return the ServletContext for this context.

Usage

From source file:com.liulangf.pattern.gof.behavioral.chain.jakarta.simple.ForwardCommand.java

/**
 * <p>Execute the command.</p>
 *
 * @param context The {@link Context} we are operating on
 * @return <code>false</code> so that processng will continue
 * @throws Exception If an error occurs during execution.
 *//*from   w  w  w. j  av  a  2 s  .  c  o m*/
public boolean execute(Context context) throws Exception {

    String uri = getForward(context);
    if (uri != null) {
        if (log.isDebugEnabled()) {
            log.debug("Forwarding to " + uri);
        }
        ServletWebContext swcontext = (ServletWebContext) context;
        RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri);
        rd.forward(swcontext.getRequest(), swcontext.getResponse());
        return true;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("No forward found");
        }
        return false;
    }
}

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

/**
 * <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
 *//*w  w  w  .  j  a v a  2  s  . c  o  m*/
protected void perform(Context context, ForwardConfig forwardConfig) throws Exception {

    ServletWebContext swcontext = (ServletWebContext) context;
    String forwardPath = forwardConfig.getPath();
    String uri = null;

    ModuleConfig moduleConfig = (ModuleConfig) context.get(getModuleConfigKey());
    // Resolve module-relative paths
    if (forwardPath.startsWith("/")) {
        uri = RequestUtils.forwardURL(swcontext.getRequest(), forwardConfig, moduleConfig);
    } else {
        uri = forwardPath;
    }

    // Get the underlying request in the case of a multipart wrapper
    HttpServletRequest request = swcontext.getRequest();
    if (request instanceof MultipartRequestWrapper) {
        request = ((MultipartRequestWrapper) request).getRequest();
    }

    // Perform redirect or forward
    if (forwardConfig.getRedirect()) {
        if (uri.startsWith("/")) {
            uri = request.getContextPath() + uri;
        }
        swcontext.getResponse().sendRedirect(swcontext.getResponse().encodeRedirectURL(uri));
    } else {
        RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri);
        rd.forward(request, swcontext.getResponse());
    }

}

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

/**
 * <p>Perform the appropriate processing on the specified
 * include uri.</p>//from   w ww .j  a v a 2 s. c om
 *
 * @param context The context for this request
 * @param uri The uri to be included
 */
protected void perform(Context context, String uri) throws Exception {

    ServletWebContext swcontext = (ServletWebContext) context;

    // Get the underlying request in the case of a multipart wrapper
    HttpServletRequest request = swcontext.getRequest();
    if (request instanceof MultipartRequestWrapper) {
        request = ((MultipartRequestWrapper) request).getRequest();
    }

    RequestDispatcher rd = swcontext.getContext().getRequestDispatcher(uri);
    rd.forward(request, swcontext.getResponse());
}

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>//from   w  w w.  j av  a2s  .  c  om
 *
 * @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
 *///w  w  w .  j a v a2s  . 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);
}