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.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   ww  w . jav  a 2  s.com
 *
 * @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.frameworks.jcr.command.core.AddNodeCommand.java

public boolean execute(Context context) throws Exception {

    Session session = ((JCRAppContext) context).getSession();

    Node parentNode = (Node) session.getItem((String) context.get(currentNodeKey));
    String relPath = (String) context.get(pathKey);
    if (context.containsKey(nodeTypeKey))
        context.put(resultKey, parentNode.addNode(relPath, (String) context.get(nodeTypeKey)));
    else//  w  ww.ja  v  a  2s.c  o  m
        context.put(resultKey, parentNode.addNode(relPath));

    return true;
}

From source file:org.exoplatform.frameworks.jcr.command.core.GetChildNodesCommand.java

public boolean execute(Context context) throws Exception {
    Session session = ((JCRAppContext) context).getSession();
    Node curNode = (Node) session.getItem((String) context.get(currentNodeKey));
    NodeIterator nodes = curNode.getNodes();
    context.put(resultKey, nodes);
    return true;/*from w w  w  .  j a  va  2  s.c om*/
}

From source file:org.exoplatform.frameworks.jcr.command.core.GetChildPropertiesCommand.java

public boolean execute(Context context) throws Exception {
    Session session = ((JCRAppContext) context).getSession();
    Node curNode = (Node) session.getItem((String) context.get(currentNodeKey));
    PropertyIterator props = curNode.getProperties();
    context.put(resultKey, props);
    return true;/*from   w  ww . j  a v a  2 s.  c o  m*/
}

From source file:org.exoplatform.frameworks.jcr.command.core.GetNodeCommand.java

public boolean execute(Context context) throws Exception {

    String wsName = (String) context.get(workspaceKey);
    if (wsName != null)
        ((JCRAppContext) context).setCurrentWorkspace(wsName);
    Session session = ((JCRAppContext) context).getSession();
    String relPath = (String) context.get(pathKey);

    Node parentNode = (Node) session.getItem((String) context.get(currentNodeKey));

    context.put(resultKey, parentNode.getNode(relPath));
    return false;
}

From source file:org.exoplatform.frameworks.jcr.command.core.GetPropertyCommand.java

public boolean execute(Context context) throws Exception {
    Session session = ((JCRAppContext) context).getSession();
    String relPath = (String) context.get(pathKey);
    Node parentNode = (Node) session.getItem((String) context.get(currentNodeKey));

    context.put(resultKey, parentNode.getProperty(relPath));
    return false;
}

From source file:org.exoplatform.frameworks.jcr.command.core.SetPropertyCommand.java

public boolean execute(Context context) throws Exception {

    Session session = ((JCRAppContext) context).getSession();

    Node parentNode = (Node) session.getItem((String) context.get(currentNodeKey));
    String name = (String) context.get(nameKey);

    int type = PropertyType.valueFromName((String) context.get(propertyTypeKey));
    boolean multi;// = ((Boolean)context.get(multiValuedKey)).booleanValue();
    if (context.get(multiValuedKey).equals("true")) {
        multi = true;//w  ww .  j a va 2s  .co  m
    } else {
        multi = false;
    }
    Object values = context.get(valuesKey);
    if (values instanceof String)
        context.put(resultKey, parentNode.setProperty(name, (String) values, type));
    else if (values instanceof String[])
        context.put(resultKey, parentNode.setProperty(name, (String[]) values, type));
    else if (values instanceof InputStream)
        context.put(resultKey, parentNode.setProperty(name, (InputStream) values));
    else
        throw new Exception("Values other than String, String[], InputStream is not supported");

    return false;
}

From source file:org.exoplatform.frameworks.jcr.command.ext.AddResourceFileCommand.java

public boolean execute(Context context) throws Exception {

    Session session = ((JCRAppContext) context).getSession();

    Node parentNode = (Node) session.getItem((String) context.get(currentNodeKey));
    String relPath = (String) context.get(pathKey);
    Object data = context.get(dataKey);
    String mimeType = (String) context.get(mimeTypeKey);

    Node file = JCRCommandHelper.createResourceFile(parentNode, relPath, data, mimeType);

    // Node file = parentNode.addNode(relPath, "nt:file");
    // Node contentNode = file.addNode("jcr:content", "nt:resource");
    // if(data instanceof InputStream)
    // contentNode.setProperty("jcr:data", (InputStream)data);
    // else//from ww w . j ava 2s  . c  o m
    // contentNode.setProperty("jcr:data", (String)data);
    // contentNode.setProperty("jcr:mimeType",
    // (String)context.get(mimeTypeKey));
    // contentNode.setProperty("jcr:lastModified", session
    // .getValueFactory().createValue(Calendar.getInstance()));

    context.put(resultKey, file);
    return true;
}

From source file:org.exoplatform.frameworks.jcr.command.ext.FilterNodesByTypesCommand.java

public boolean execute(Context context) throws Exception {
    Object obj = context.get(incomingNodesKey);
    if (obj == null || !(obj instanceof NodeIterator)) {
        throw new IllegalArgumentException("Invalid incoming nodes iterator " + obj);
    }//from www  .j a  v  a2 s  .co  m
    NodeIterator nodes = (NodeIterator) obj;

    obj = context.get(typesKey);
    if (obj == null || !(obj instanceof String[])) {
        throw new IllegalArgumentException("Invalid node types object, expected String[] " + obj);
    }
    String[] nts = (String[]) context.get(typesKey);

    List nodes1 = new ArrayList();
    while (nodes.hasNext()) {
        Node n = nodes.nextNode();
        for (int i = 0; i < nts.length; i++) {
            if (n.isNodeType(nts[i])) {
                nodes1.add(n);
            }
        }
    }
    // context.put(resultKey, new EntityCollection(nodes1));
    context.put(resultKey, nodes1);

    return false;
}

From source file:org.exoplatform.frameworks.jcr.command.GetNodeAsXMLCommand.java

public boolean execute(Context context) throws Exception {
    Object obj = context.get(incomingNodeKey);
    if (obj == null || !(obj instanceof Node))
        throw new Exception("Invalid incoming node " + obj);
    Node node = (Node) context.get(incomingNodeKey);
    String xml = "<node path='" + node.getPath() + "'/>";
    context.put(resultKey, xml);
    return false;
}