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

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

Introduction

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

Prototype

V get(Object key);

Source Link

Document

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Usage

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

/**
 * <p>Create (if necessary) and cache an <code>Action</code> for this
 * request.</p>//from w  w w.  j  a v  a2  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.CreateActionForm.java

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

    // Is there a form bean associated with this ActionConfig?
    ActionConfig actionConfig = (ActionConfig) context.get(getActionConfigKey());
    String name = actionConfig.getName();
    if (name == null) {
        context.remove(getActionFormKey());
        return (false);
    }

    log.trace("Look up form-bean " + name);

    // Look up the corresponding FormBeanConfig (if any)
    FormBeanConfig formBeanConfig = actionConfig.getModuleConfig().findFormBeanConfig(name);
    if (formBeanConfig == null) {
        log.warn("No FormBeanConfig found in module " + actionConfig.getModuleConfig().getPrefix()
                + " under name " + name);
        context.remove(getActionFormKey());
        return (false);
    }

    // Look up the session scope ActionForm instance (if any)
    WebContext wcontext = (WebContext) context;
    ActionForm instance = null;
    if ("session".equals(actionConfig.getScope())) {
        instance = (ActionForm) wcontext.getSessionScope().get(actionConfig.getAttribute());
    }

    // Can we recycle the existing instance (if any)?
    if (instance != null) {
        log.trace("Found an instance in the session; test for reusability");
        if (formBeanConfig.getDynamic()) {
            String className = ((DynaBean) instance).getDynaClass().getName();
            if (className.equals(formBeanConfig.getName())) {
                wcontext.put(getActionFormKey(), instance);
                /* It should already be in session scope
                if ("session".equals(actionConfig.getScope())) {
                wcontext.getSessionScope().put
                    (actionConfig.getAttribute(), instance);
                }
                */
                log.debug("Using existing instance (dynamic)");
                return (false);
            }
        } else {
            try {
                Class configClass = ClassUtils.getApplicationClass(formBeanConfig.getType());
                if (configClass.isAssignableFrom(instance.getClass())) {
                    wcontext.put(getActionFormKey(), instance);
                    /* It should already be in session scope
                       if ("session".equals(actionConfig.getScope())) {
                       wcontext.getSessionScope().put
                       (actionConfig.getAttribute(), instance);
                       }
                    */
                    log.debug("Using existing instance (non-dynamic)");
                    return (false);
                }
            } catch (Exception e) {
                log.debug("Error testing existing instance for reusability; just create a new instance", e);
            }
        }
    }

    log.trace("Make a new instance of: " + formBeanConfig);
    // Create a new form bean instance
    if (formBeanConfig.getDynamic()) {
        ModuleConfig moduleConfig = (ModuleConfig) wcontext.get(getModuleConfigKey());
        DynaActionFormClass dynaClass = DynaActionFormClass.createDynaActionFormClass(formBeanConfig);
        instance = (ActionForm) dynaClass.newInstance();
        ((DynaActionForm) instance).initialize((ActionMapping) actionConfig);
    } else {
        instance = (ActionForm) ClassUtils.getApplicationInstance(formBeanConfig.getType());
    }

    // Configure and cache the new instance
    ActionServlet servlet = (ActionServlet) wcontext.get(getActionServletKey());
    instance.setServlet(servlet);
    wcontext.put(getActionFormKey(), instance);
    if ("session".equals(actionConfig.getScope())) {
        wcontext.getSessionScope().put(actionConfig.getAttribute(), instance);
    } else if ("request".equals(actionConfig.getScope())) {
        wcontext.getRequestScope().put(actionConfig.getAttribute(), instance);
    }

    return (false);

}

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
 *//*from  w  w  w  . j  a va2 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
 *//*w w w.j  a  v a 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
    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 va  2 s . c o m
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);

}

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  va  2s. 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.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 ww  .  j  av a  2 s.  c o  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.exoplatform.calendar.service.test.LastUpdateAction.java

public boolean execute(Context context) throws Exception {
    try {/*from   w  w  w  .  ja  va  2s  .  co m*/
        Node node = (Node) context.get("currentItem");
        if (!node.isNodeType("exo:datetime")) {
            if (node.canAddMixin("exo:datetime")) {
                node.addMixin("exo:datetime");
            }
            node.setProperty("exo:dateCreated", new GregorianCalendar());
            node.setProperty("exo:dateModified", new GregorianCalendar());
        } else
            node.setProperty("exo:dateModified", new GregorianCalendar());
    } catch (ClassCastException e) {
        PropertyImpl property = (PropertyImpl) context.get("currentItem");
        NodeImpl parent = (NodeImpl) property.getParent();
        if (!parent.isNodeType("exo:calendarEvent"))
            throw new Exception("incoming node is not exo:calendarEvent");
        parent.setProperty("exo:dateModified", new GregorianCalendar());
    }
    return false;
}

From source file:org.exoplatform.clouddrive.ecms.action.RemoveCloudFileLinkAction.java

/**
 * {@inheritDoc}/*from ww  w  . j  a  va 2 s . co m*/
 */
@Override
public boolean execute(Context context) throws Exception {
    Node linkNode = (Node) context.get(InvocationContext.CURRENT_ITEM);
    // we work only with node removal (no matter what set in the action config)
    if (ExtendedEvent.NODE_REMOVED == (Integer) context.get(InvocationContext.EVENT)) {
        TrashService trash = getComponent(context, TrashService.class);
        // Don't care about removal from Trash: it can be Cloud Drive itself when removing cloud file by sync op.
        if (!trash.getTrashHomeNode().isSame(linkNode.getParent())) {
            CloudFileActionService actions = getComponent(context, CloudFileActionService.class);
            Node targetNode = actions.markRemoveLink(linkNode);
            if (targetNode != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Cloud File link marked for removal: " + linkNode.getPath() + " -> "
                            + targetNode.getPath());
                }
            }
        }
    } else {
        LOG.warn(RemoveCloudFileLinkAction.class.getName()
                + " supports only node removal. Check configuration. Item skipped: " + linkNode.getPath());
    }
    return false;
}

From source file:org.exoplatform.clouddrive.jcr.AbstractJCRAction.java

/**
 * Get a component instance from the context's container.
 * //from w w w  .ja v a2  s.c o  m
 * @param context
 * @param type
 * @return
 */
protected <C> C getComponent(Context context, Class<C> type) {
    ExoContainer container = (ExoContainer) context.get(InvocationContext.EXO_CONTAINER);
    return type.cast(container.getComponentInstanceOfType(type));
}