Example usage for org.apache.commons.chain Context put

List of usage examples for org.apache.commons.chain Context put

Introduction

In this page you can find the example usage for org.apache.commons.chain Context put.

Prototype

V put(K key, V value);

Source Link

Document

Associates the specified value with the specified key in this map (optional operation).

Usage

From source file:org.apache.struts.chain.AbstractPopulateActionForm.java

protected void handleCancel(Context context, ActionConfig actionConfig, ActionForm actionForm)
        throws Exception {
    WebContext wcontext = (WebContext) context;
    Map paramValues = wcontext.getParamValues();

    // Set the cancellation attribute if appropriate
    if ((paramValues.get(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY) != null)
            || (paramValues.get(org.apache.struts.taglib.html.Constants.CANCEL_PROPERTY_X) != null)) {
        context.put(getCancelKey(), Boolean.TRUE);
        wcontext.getRequestScope().put(Globals.CANCEL_KEY, Boolean.TRUE);
    } else {// w w  w  .j a va 2 s .c om
        context.put(getCancelKey(), Boolean.FALSE);
    }
}

From source file:org.apache.struts.chain.AbstractSelectForward.java

/**
 * <p>Select and cache the <code>ActionForward</code> for this
 * <code>ActionConfig</code> if specified.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues
 */// ww  w. j  a  v a  2  s.  c  o  m
public boolean execute(Context context) throws Exception {

    // Skip processing if the current request is not valid
    Boolean valid = (Boolean) context.get(getValidKey());
    if ((valid == null) || !valid.booleanValue()) {
        return (false);
    }

    // Acquire configuration objects that we need
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());
    ModuleConfig moduleConfig = actionConfig.getModuleConfig();

    ForwardConfig forwardConfig = null;
    String forward = actionConfig.getForward();
    if (forward != null) {
        forwardConfig = forward(context, moduleConfig, forward);
        if (log.isDebugEnabled()) {
            log.debug("Forwarding to " + forwardConfig);
        }
        context.put(getForwardConfigKey(), forwardConfig);
    }
    return (false);

}

From source file:org.apache.struts.chain.AbstractSelectInput.java

/**
 * <p>Select and cache a <code>ForwardConfig</code> for the input page
 * for the current request.</p>//from w w  w  . j av  a 2  s . c om
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues
 */
public boolean execute(Context context) throws Exception {

    // Skip processing if the current request is valid
    Boolean valid = (Boolean) context.get(getValidKey());
    if ((valid != null) && valid.booleanValue()) {
        return (false);
    }

    // Acquire configuration objects that we need
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());
    ModuleConfig moduleConfig = actionConfig.getModuleConfig();

    // Cache an ForwardConfig back to our input page
    ForwardConfig forwardConfig = null;
    String input = actionConfig.getInput();
    if (moduleConfig.getControllerConfig().getInputForward()) {
        if (log.isTraceEnabled()) {
            log.trace("Finding ForwardConfig for '" + input + "'");
        }
        forwardConfig = actionConfig.findForwardConfig(input);
        if (forwardConfig == null) {
            forwardConfig = moduleConfig.findForwardConfig(input);
        }
    } else {
        if (log.isTraceEnabled()) {
            log.trace("Delegating to forward() for '" + input + "'");
        }
        forwardConfig = forward(context, moduleConfig, input);
    }
    if (log.isDebugEnabled()) {
        log.debug("Forwarding back to " + forwardConfig);
    }
    context.put(getForwardConfigKey(), forwardConfig);
    return (false);

}

From source file:org.apache.struts.chain.AbstractSelectLocale.java

/**
 * <p>Select the <code>Locale</code> to be used for this request.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues
 *//*from   w w w  .  j  a v  a2 s .  com*/
public boolean execute(Context context) throws Exception {

    // Are we configured to select Locale automatically?
    ModuleConfig moduleConfig = (ModuleConfig) context.get(getModuleConfigKey());
    if (!moduleConfig.getControllerConfig().getLocale()) {
        return (false);
    }

    // Retrieve and cache appropriate Locale for this request
    Locale locale = getLocale(context);
    context.put(getLocaleKey(), locale);

    return (false);

}

From source file:org.apache.struts.chain.AbstractValidateActionForm.java

/**
 * <p>Validate the properties of the form bean for this request.  If
 * there are any validation errors, execute the child commands in our
 * chain; otherwise, proceed normally.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues, if there are
 *  no validation errors; otherwise <code>true</code>
 */// w  w w  .ja va  2s . c  o m
public boolean execute(Context context) throws Exception {

    // Is there a form bean for this request?
    ActionForm actionForm = (ActionForm) context.get(getActionFormKey());
    if (actionForm == null) {
        context.put(getValidKey(), Boolean.TRUE);
        return (false);
    }

    // Was this request cancelled?
    Boolean cancel = (Boolean) context.get(getCancelKey());
    if ((cancel != null) && cancel.booleanValue()) {
        context.put(getValidKey(), Boolean.TRUE);
        return (false);
    }

    // Is validation disabled on this request?
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());
    if (!actionConfig.getValidate()) {
        context.put(getValidKey(), Boolean.TRUE);
        return (false);
    }

    // Call the validate() method of this form bean
    ActionErrors errors = validate(context, actionConfig, actionForm);

    // If there were no errors, proceed normally
    if ((errors == null) || (errors.isEmpty())) {
        context.put(getValidKey(), Boolean.TRUE);
        return (false);
    }

    // Flag the validation failure and proceed
    context.put(getValidKey(), Boolean.FALSE);
    return (false);

}

From source file:org.apache.struts.chain.CreateAction.java

/**
 * <p>Create (if necessary) and cache an <code>Action</code> for this
 * request.</p>/*from ww  w  .j  a  va  2 s. c  o  m*/
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues
 */
public boolean execute(Context context) throws Exception {

    // Skip processing if the current request is not valid
    Boolean valid = (Boolean) context.get(getValidKey());
    if ((valid == null) || !valid.booleanValue()) {
        return (false);
    }

    // Check to see if an action has already been created
    if (context.get(getActionKey()) != null) {
        return (false);
    }

    // Look up the class name for the desired Action
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());
    String type = actionConfig.getType();

    if (type == null) {
        return (false);
    }

    // Create (if necessary) and cache an Action instance
    Action action = null;
    Map actions = getActions(context, actionConfig.getModuleConfig());
    synchronized (actions) {
        action = (Action) actions.get(type);
        if (action == null) {
            log.info("Initialize action of type: " + type + " for actionConfig " + actionConfig);
            action = (Action) ClassUtils.getApplicationInstance(type);
            ActionServlet actionServlet = (ActionServlet) context.get(getActionServletKey());
            action.setServlet(actionServlet);
            actions.put(type, action);
        }
    }
    context.put(getActionKey(), action);

    return (false);

}

From source file:org.apache.struts.chain.ExceptionCatcher.java

/**
 * <p>If an exception was thrown by a subsequent <code>Command</code>,
 * pass it on to the specified exception handling chain.  Otherwise,
 * do nothing.</p>//w  ww  .j a  v  a 2 s  .c om
 *
 * @param context The {@link Context} to be processed by this
 *  {@link Filter}
 * @param exception The <code>Exception</code> (if any) that was thrown
 *  by the last {@link Command} that was executed; otherwise
 *  <code>null</code>
 */
public boolean postprocess(Context context, Exception exception) {

    // Do nothing if there was no exception thrown
    if (exception == null) {
        return (false);
    }

    // Stash the exception in the specified context attribute
    if (log.isDebugEnabled()) {
        log.debug("Attempting to handle a thrown exception");
    }
    context.put(getExceptionKey(), exception);

    // Execute the specified command
    try {
        String catalogName = getCatalogName();
        Catalog catalog = null;
        if (catalogName == null) {
            catalog = CatalogFactory.getInstance().getCatalog();
            if (catalog == null) {
                log.error("Cannot find default catalog");
                throw new IllegalArgumentException("Cannot find default catalog");
            }
        } else {
            catalog = CatalogFactory.getInstance().getCatalog(catalogName);
            if (catalog == null) {
                log.error("Cannot find catalog '" + catalogName + "'");
                throw new IllegalArgumentException("Cannot find catalog '" + catalogName + "'");
            }
        }
        String exceptionCommand = getExceptionCommand();
        if (exceptionCommand == null) {
            log.error("No exceptionCommand property specified");
            throw new IllegalStateException("No exceptionCommand property specfied");
        }
        Command command = catalog.getCommand(exceptionCommand);
        if (command == null) {
            log.error("Cannot find exceptionCommand '" + exceptionCommand + "'");
            throw new IllegalStateException("Cannot find exceptionCommand '" + exceptionCommand + "'");
        }
        if (log.isTraceEnabled()) {
            log.trace("Calling exceptionCommand '" + exceptionCommand + "'");
        }
        command.execute(context);
    } catch (Exception e) {
        log.warn("Exception from exceptionCommand '" + exceptionCommand + "'", e);
        throw new IllegalStateException("Exception chain threw exception");
    }
    return (true);

}

From source file:org.apache.struts.chain.legacy.ChainAction.java

/**
 * <p>Delegate to the command chain specified in our configuration.</p>
 *
 * @param mapping <code>ActionMapping</code> configuring this action
 * @param form <code>ActionForm</code> for this request (if any)
 * @param request <code>HttpServletRequest</code> we are processing
 * @param response <code>HttpServletResponse</code> we are creating
 *///ww w  .j a va  2 s  .  c  o m
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    // Set up a context for this request
    Context context = new ServletWebContext(getServlet().getServletContext(), request, response);
    context.put("mapping", mapping);
    context.put("form", form);

    // Delegate to the specified command
    Command command = getCatalog().getCommand(mapping.getParameter());
    command.execute(context); // Ignore return state

    // Return results as appropriate
    Exception exception = null;
    try {
        exception = (Exception) context.get("exception");
        if (exception != null) {
            throw exception;
        }
    } catch (ClassCastException e) {
        ; // It is not an Exception
    }
    ActionForward forward = null;
    try {
        forward = (ActionForward) context.get("forward");
    } catch (ClassCastException e) {
        forward = null; // It is not an ActionForward
    }
    return forward;

}

From source file:org.apache.struts.chain.legacy.DispatchAction.java

/**
 * <p>Delegate to the command chain specified in our configuration.</p>
 *
 * @param mapping <code>ActionMapping</code> configuring this action
 * @param form <code>ActionForm</code> for this request (if any)
 * @param request <code>HttpServletRequest</code> we are processing
 * @param response <code>HttpServletResponse</code> we are creating
 *//*from  ww w. j a  v a  2 s  .c om*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    // Set up a context for this request
    Context context = new ServletWebContext(getServlet().getServletContext(), request, response);
    context.put("mapping", mapping);
    context.put("form", form);

    // Delegate to the specified command
    String name = mapping.getParameter();
    Command command = getCatalog().getCommand(request.getParameter(name));
    command.execute(context); // Ignore return state

    // Return results as appropriate
    Exception exception = null;
    try {
        exception = (Exception) context.get("exception");
        if (exception != null) {
            throw exception;
        }
    } catch (ClassCastException e) {
        ; // It is not an Exception
    }
    ActionForward forward = null;
    try {
        forward = (ActionForward) context.get("forward");
    } catch (ClassCastException e) {
        forward = null; // It is not an ActionForward
    }
    return forward;

}

From source file:org.apache.struts.chain.SelectInclude.java

/**
 * <p>Select and cache the include uri for this
 * <code>ActionConfig</code> if specified.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> so that processing continues
 *//*  w  w  w  . j a  v  a  2  s . c  om*/
public boolean execute(Context context) throws Exception {

    // Acquire configuration objects that we need
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());

    // Cache an include uri if found
    String include = actionConfig.getInclude();
    if (include != null) {
        if (log.isDebugEnabled()) {
            log.debug("Including " + include);
        }
        context.put(getIncludeKey(), include);
    }
    return (false);

}