Package org.apache.cocoon.sunshine.context

Source Code of org.apache.cocoon.sunshine.context.RequestSessionContext

/*

============================================================================
                   The Apache Software License, Version 1.1
============================================================================

Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without modifica-
tion, are permitted provided that the following conditions are met:

1. Redistributions of  source code must  retain the above copyright  notice,
    this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any, must
    include  the following  acknowledgment:  "This product includes  software
    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
    Alternately, this  acknowledgment may  appear in the software itself,  if
    and wherever such third-party acknowledgments normally appear.

4. The names "Apache Cocoon" and  "Apache Software Foundation" must  not  be
    used to  endorse or promote  products derived from  this software without
    prior written permission. For written permission, please contact
    apache@apache.org.

5. Products  derived from this software may not  be called "Apache", nor may
    "Apache" appear  in their name,  without prior written permission  of the
    Apache Software Foundation.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
(INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software  consists of voluntary contributions made  by many individuals
on  behalf of the Apache Software  Foundation and was  originally created by
Stefano Mazzocchi  <stefano@apache.org>. For more  information on the Apache
Software Foundation, please see <http://www.apache.org/>.

*/
package org.apache.cocoon.sunshine.context;

import java.io.*;
import java.util.*;

import org.apache.avalon.excalibur.xml.Parser;
import org.apache.excalibur.source.SourceParameters;
import org.apache.avalon.framework.component.ComponentManager;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.Cookie;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.http.HttpRequest;
import org.apache.cocoon.sunshine.SunShineConstants;
import org.apache.cocoon.sunshine.connector.Resource;
import org.apache.cocoon.xml.IncludeXMLConsumer;
import org.apache.cocoon.sunshine.xml.XMLUtil;

import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

import org.xml.sax.SAXException;
import org.xml.sax.ContentHandler;
import org.xml.sax.ext.LexicalHandler;

/**
* A SessionContext which encapsulates the current Request object.
*
* It is not allowed to change this context.
* The following paths are valid:
* /parameter                  - lists all parameters, parameter names build the
*             elements with the values as text node childs
* /parameter/<parameter_name> - one text node containing the value
* /querystring                - the querystring with a leading '?' or null (the querystring is only for GET)
*
* /parametervalues            - same as /parameter but values are listed as described
*                               below
*                               <sunshine:params>
*                                      <sunshine:param>
*                                              <sunshine:name>parameter name</sunshine:name>
*                                              <sunshine:value>parameter value</sunshine:value>
*                                      </sunshine:param>
*                                       ...
*                                      <sunshine:param>
*                                              <sunshine:name>parameter name</sunshine:name>
*                                              <sunshine:value>parameter value</sunshine:value>
*                                      </sunshine:param>
*                               </sunshine:params>
*                               If a parameter has more than one value for each value a
*                               <sunshine:param> block is generated.
* /attributes - lists all attributes, attribute names build the elements
*               with the values as text node childs
* /headers    - lists all headers, header names build the elements
*               with the values as text node childs
* /cookies ----- <cookie name="...">
*                   <comment/>
*                   <domain/>
*                   <maxAge/>
*                   <name/>
*                   <path/>
*                   <secure/>
*                   <value/>
*                   <version/>
* /characterEncoding
* /contentLength
* /contentType
* /protocol
* /remoteAddress
* /remoteHost
* /scheme
* /serverName
* /serverPort
* /authType        <!-- only for http -->
* /method
* /contextPath
* /pathInfo
* /pathTranslated
* /remoteUser
* /requestedSessionId
* /requestURI
* /servletPath
* /isRequestedSessionIdFromCookie
* /isRequestedSessionIdFromCookie
* /isRequestedSessionIdValid
*
*  The following attributes of the servlet api 2.2 are missing:
*  - getUserPrincipal()
*  - getLocale()
*  - getLocales()
*
* @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
* @version CVS $Id: RequestSessionContext.java,v 1.4.2.1 2002/06/07 09:34:25 cziegeler Exp $
*/
public final class RequestSessionContext
implements SessionContext {

    private String    name;
    transient private Request          request;
    private Document  contextData;

    public void setup(String value, Resource load, Resource save) { name = value; }

    /**
     * Set the Request
     */
    public void setup(Map objectModel, ComponentManager manager)
    throws ProcessingException {
        this.request = ObjectModelHelper.getRequest(objectModel);

        contextData = XMLUtil.createDocument();
        contextData.appendChild(contextData.createElementNS(null, "context"));

        Element root = contextData.getDocumentElement();

        Parser parser = null;
        try {
            parser = (Parser) manager.lookup( Parser.ROLE );
            this.buildParameterXML(root, parser);
        } catch (ComponentException ce) {
            throw new ProcessingException("Unable to lookup parser.", ce);
        } finally {
            manager.release( parser );
        }
        this.buildAttributesXML(root);
        this.buildMiscXML(root);
        this.buildCookiesXML(root);
        this.buildHeadersXML(root);
    }

    /**
     * Get the name of the context
     */
    public String getName() {
        return this.name;
    }

    /**
     * Build path
     */
    private String createPath(String path) {
        if (path == null) path = "/";
        if (path.startsWith("/") == false) path = "/" + path;
        path = "/context" + path;
        if (path.endsWith("/") == true) path = path.substring(0, path.length() - 1);
        return path;
    }

    private Node createTextNode(Document doc, String value) {
        return doc.createTextNode(value != null ? value : "");
    }

   /**
     * Build attributes XML
     */
    private void buildMiscXML(Element root)
    throws ProcessingException {
        Document doc = root.getOwnerDocument();

        Element node;

        node = doc.createElementNS(null, "characterEncoding");
        node.appendChild(this.createTextNode(doc, this.request.getCharacterEncoding()));
        root.appendChild(node);
        node = doc.createElementNS(null, "contentLength");
        node.appendChild(this.createTextNode(doc, "" + this.request.getContentLength()));
        root.appendChild(node);
        node = doc.createElementNS(null, "contentType");
        node.appendChild(this.createTextNode(doc, this.request.getContentType()));
        root.appendChild(node);
        node = doc.createElementNS(null, "protocol");
        node.appendChild(this.createTextNode(doc, this.request.getProtocol()));
        root.appendChild(node);
        node = doc.createElementNS(null, "remoteAddress");
        node.appendChild(this.createTextNode(doc, this.request.getRemoteAddr()));
        root.appendChild(node);
        node = doc.createElementNS(null, "remoteHost");
        node.appendChild(this.createTextNode(doc, this.request.getRemoteHost()));
        root.appendChild(node);
        node = doc.createElementNS(null, "scheme");
        node.appendChild(this.createTextNode(doc, this.request.getRemoteHost()));
        root.appendChild(node);
        node = doc.createElementNS(null, "serverName");
        node.appendChild(this.createTextNode(doc, this.request.getServerName()));
        root.appendChild(node);
        node = doc.createElementNS(null, "serverPort");
        node.appendChild(this.createTextNode(doc, ""+this.request.getServerPort()));
        root.appendChild(node);
        if (this.request instanceof HttpRequest) {
            node = doc.createElementNS(null, "authType");
            node.appendChild(this.createTextNode(doc, ((HttpRequest)this.request).getAuthType()));
            root.appendChild(node);
        }
        node = doc.createElementNS(null, "method");
        node.appendChild(this.createTextNode(doc, this.request.getMethod()));
        root.appendChild(node);
        node = doc.createElementNS(null, "contextPath");
        node.appendChild(this.createTextNode(doc, this.request.getContextPath()));
        root.appendChild(node);
        node = doc.createElementNS(null, "pathInfo");
        node.appendChild(this.createTextNode(doc, this.request.getPathInfo()));
        root.appendChild(node);
        node = doc.createElementNS(null, "pathTranslated");
        node.appendChild(this.createTextNode(doc, this.request.getPathTranslated()));
        root.appendChild(node);
        node = doc.createElementNS(null, "remoteUser");
        node.appendChild(this.createTextNode(doc, this.request.getRemoteUser()));
        root.appendChild(node);
        node = doc.createElementNS(null, "requestedSessionId");
        node.appendChild(this.createTextNode(doc, this.request.getRequestedSessionId()));
        root.appendChild(node);
        node = doc.createElementNS(null, "requestURI");
        node.appendChild(this.createTextNode(doc, this.request.getRequestURI()));
        root.appendChild(node);
        node = doc.createElementNS(null, "servletPath");
        node.appendChild(this.createTextNode(doc, this.request.getServletPath()));
        root.appendChild(node);
        node = doc.createElementNS(null, "isRequestedSessionIdFromCookie");
        node.appendChild(doc.createTextNode(this.request.isRequestedSessionIdFromCookie() ? "true" : "false"));
        root.appendChild(node);
        node = doc.createElementNS(null, "isRequestedSessionIdFromURL");
        node.appendChild(doc.createTextNode(this.request.isRequestedSessionIdFromURL() ? "true" : "false"));
        root.appendChild(node);
        node = doc.createElementNS(null, "isRequestedSessionIdValid");
        node.appendChild(doc.createTextNode(this.request.isRequestedSessionIdValid() ? "true" : "false"));
        root.appendChild(node);
    }

    /**
     * Build attributes XML
     */
    private void buildAttributesXML(Element root)
    throws ProcessingException {
        Document doc = root.getOwnerDocument();
        Element attrElement = doc.createElementNS(null, "attributes");
        String attrName;
        Element attr;

        root.appendChild(attrElement);
        Enumeration all = this.request.getAttributeNames();
        while (all.hasMoreElements() == true) {
            attrName = (String) all.nextElement();
            attr = doc.createElementNS(null, attrName);
            attrElement.appendChild(attr);
            attr.appendChild(this.createTextNode(doc, this.request.getAttribute(attrName).toString()));
        }
    }

    /**
     * Build cookies XML
     */
    private void buildCookiesXML(Element root)
    throws ProcessingException {
        Document doc = root.getOwnerDocument();

        Element cookiesElement = doc.createElementNS(null, "cookies");
        root.appendChild(cookiesElement);

        Cookie[] cookies = this.request.getCookies();
        if (cookies != null) {
            Cookie current;
            Element node;
            Element parent;
            for(int i = 0; i < cookies.length; i++) {
                current = cookies[i];
                parent = doc.createElementNS(null, "cookie");
                parent.setAttributeNS(null, "name", current.getName());
                cookiesElement.appendChild(parent);
                node = doc.createElementNS(null, "comment");
                node.appendChild(this.createTextNode(doc, current.getComment()));
                parent.appendChild(node);
                node = doc.createElementNS(null, "domain");
                node.appendChild(this.createTextNode(doc, current.getDomain()));
                parent.appendChild(node);
                node = doc.createElementNS(null, "maxAge");
                node.appendChild(this.createTextNode(doc, ""+current.getMaxAge()));
                parent.appendChild(node);
                node = doc.createElementNS(null, "name");
                node.appendChild(this.createTextNode(doc, current.getName()));
                parent.appendChild(node);
                node = doc.createElementNS(null, "path");
                node.appendChild(this.createTextNode(doc, current.getPath()));
                parent.appendChild(node);
                node = doc.createElementNS(null, "secure");
                node.appendChild(doc.createTextNode(current.getSecure() ? "true" : "false"));
                parent.appendChild(node);
                node = doc.createElementNS(null, "value");
                node.appendChild(this.createTextNode(doc, current.getValue()));
                parent.appendChild(node);
                node = doc.createElementNS(null, "version");
                node.appendChild(this.createTextNode(doc, ""+current.getVersion()));
                parent.appendChild(node);
            }
        }
    }

    /**
     * Build headers XML
     */
    private void buildHeadersXML(Element root)
    throws ProcessingException {
        Document doc = root.getOwnerDocument();
        Element headersElement = doc.createElementNS(null, "headers");
        String headerName;
        Element header;

        root.appendChild(headersElement);
        Enumeration all = this.request.getHeaderNames();
        while (all.hasMoreElements() == true) {
            headerName = (String) all.nextElement();
            header = doc.createElementNS(null, headerName);
            headersElement.appendChild(header);
            header.appendChild(this.createTextNode(doc, this.request.getHeader(headerName)));
        }
    }

    /**
     * Build parameter XML
     */
    private void buildParameterXML(Element root, Parser parser)
    throws ProcessingException {
        Document doc = root.getOwnerDocument();
        // include all parameters
        // process "/parameter" and "/parametervalues" at the same time
        Element     parameterElement = doc.createElementNS(null, "parameter");
        Element     parameterValuesElement = doc.createElementNS(null, "parametervalues");
        root.appendChild(parameterElement);
        root.appendChild(parameterValuesElement);
        String      parameterName = null;
        Enumeration pars = this.request.getParameterNames();
        Element     parameter;
        Element     element;
        Node        valueNode;
        String[]    values;
        String      parValue;

        element = doc.createElementNS(SunShineConstants.SUNSHINE_NAMESPACE_URI, "sunshine:params");
        parameterValuesElement.appendChild(element);
        parameterValuesElement = element;

        while (pars.hasMoreElements() == true) {
            parameterName = (String)pars.nextElement();
            values = this.request.getParameterValues(parameterName);

            for(int i = 0; i < values.length; i++) {

                // this is a fast test, if the parameter value contains xml!
                parValue = values[i].trim();
                if (parValue.length() > 0 && parValue.charAt(0) == '<') {
                    try {
                        valueNode = XMLUtil.getDocumentFragment(parser, new StringReader(parValue));
                        valueNode = doc.importNode(valueNode, true);
                    } catch (Exception noXMLException) {
                        valueNode = doc.createTextNode(parValue);
                    }
                } else {
                    valueNode = doc.createTextNode(parValue);
                }
                // create "/parameter" entry for first value
                if (i == 0) {
                    try {
                        parameter = doc.createElementNS(null, parameterName);
                        parameter.appendChild(valueNode);
                        parameterElement.appendChild(parameter);
                    } catch (Exception local) {
                        // the exception is ignored and only this parameters is ignored
                    }
                }

                try {
                    // create "/parametervalues" entry
                    element = doc.createElementNS(SunShineConstants.SUNSHINE_NAMESPACE_URI, "sunshine:param");
                    parameterValuesElement.appendChild(element);
                    parameter = element;
                    element = doc.createElementNS(SunShineConstants.SUNSHINE_NAMESPACE_URI, "sunshine:name");
                    parameter.appendChild(element);
                    element.appendChild(doc.createTextNode(parameterName));
                    element = doc.createElementNS(SunShineConstants.SUNSHINE_NAMESPACE_URI, "sunshine:value");
                    parameter.appendChild(element);
                    element.appendChild(valueNode.cloneNode(true));
                } catch (Exception local) {
                    // the exception is ignored and only this parameters is ignored
                }
            }
        }
        // and now the query string
        element = doc.createElementNS(null, "querystring");
        root.appendChild(element);
        String value = request.getQueryString();
        if (value != null) {
            element.appendChild(doc.createTextNode('?' + value));
        }
    }

    /**
     * Get the XML from the request object
     */
    public DocumentFragment getXML(String path)
    throws ProcessingException {
        if (path == null || path.charAt(0) != '/') {
            throw new ProcessingException("Not a valid XPath: " + path);
        }
        path = this.createPath(path);
        DocumentFragment result = null;
        NodeList list;

        try {
            list = XMLUtil.selectNodeList(this.contextData, path);
        } catch (javax.xml.transform.TransformerException localException) {
            throw new ProcessingException("Exception: " + localException, localException);
        }
        if (list != null && list.getLength() > 0) {
            result = XMLUtil.getOwnerDocument(contextData).createDocumentFragment();
            for(int i = 0; i < list.getLength(); i++) {

                // the found node is either an attribute or an element
                if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
                    // if it is an attribute simple create a new text node with the value of the attribute
                    result.appendChild(XMLUtil.getOwnerDocument(contextData).createTextNode(list.item(i).getNodeValue()));
                } else {
                    // now we have an element
                    // copy all children of this element in the resulting tree
                    NodeList childs = list.item(i).getChildNodes();
                    if (childs != null) {
                        for(int m = 0; m < childs.getLength(); m++) {
                            result.appendChild(XMLUtil.getOwnerDocument(contextData).importNode(childs.item(m), true));
                        }
                    }
                }
            }
        }

        return result;
    }

    /**
     * Setting of xml is not possible for the request context
     */
    public void setXML(String path, DocumentFragment fragment)
    throws ProcessingException {
        throw new ProcessingException("RequestSessionContext: Setting of xml not allowed");
    }

    /**
     * Setting of xml is not possible for the request context
     */
    public void setValueOfNode(String path, String value)
    throws ProcessingException {
        throw new ProcessingException("RequestSessionContext: Setting of xml not allowed");
    }

    /**
     * Append a document fragment is not possible for the request context.
     */
    public void appendXML(String path, DocumentFragment fragment)
    throws ProcessingException {
        throw new ProcessingException("RequestSessionContext: Appending of xml not allowed");
    }

    /**
     * Removing is not possible for the request context.
     */
    public void removeXML(String path)
    throws ProcessingException {
        throw new ProcessingException("RequestSessionContext: Removing of xml not allowed");
    }

    /**
     * Set a context attribute. If value is null the attribute is removed.
     */
    public void setAttribute(String key, Object value)
    throws ProcessingException {
        if (value == null) {
            this.request.removeAttribute(key);
        } else {
            this.request.setAttribute(key, value);
        }
    }

    /**
     * Get a context attribute. If the attribute is not available return null
     */
    public Object getAttribute(String key)
    throws ProcessingException {
        return this.request.getAttribute(key);
    }

    /**
     * Get a context attribute. If the attribute is not available the defaultObject is returned
     */
    public Object getAttribute(String key, Object defaultObject)
    throws ProcessingException {
        Object obj = this.getAttribute(key);
        return (obj != null ? obj : defaultObject);
    }

    /**
     * Get a copy the first node specified by the path.
     */
    public Node getSingleNode(String path)
    throws ProcessingException {
        path = this.createPath(path);
        Node node = null;

        try {
            node = XMLUtil.getSingleNode(this.contextData, path);
        } catch (javax.xml.transform.TransformerException localException) {
            throw new ProcessingException("Exception: " + localException, localException);
        }
        return node;
    }

    /**
     * Get a copy all the nodes specified by the path.
     */
    public NodeList getNodeList(String path)
    throws ProcessingException {
        path = this.createPath(path);
        NodeList list = null;

        try {
            list = XMLUtil.selectNodeList(this.contextData, path);
        } catch (javax.xml.transform.TransformerException localException) {
            throw new ProcessingException("Exception: " + localException, localException);
        }
        return list;
    }

    /**
     * Set the value of a node. The node is copied before insertion.
     */
    public void setNode(String path, Node node)
    throws ProcessingException {
        throw new ProcessingException("RequestSessionContext: Setting of XML not allowed");
    }

    /**
     * Get the value of this node. This is similiar to the xsl:value-of
     * function. If the node does not exist, <code>null</code> is returned.
     */
    public String getValueOfNode(String path)
    throws ProcessingException {
        String value = null;
        Node node = this.getSingleNode(path);
        if (node != null) {
            value = XMLUtil.getValueOfNode(node);
        }

        return value;
    }

    /**
     * Stream the XML directly to the handler. This streams the contents of getXML()
     * to the given handler without creating a DocumentFragment containing a copy
     * of the data
     */
    public boolean streamXML(String path,
                             ContentHandler contentHandler,
                             LexicalHandler lexicalHandler)
    throws SAXException, ProcessingException {
        boolean result = false;
        NodeList list;

        try {
            list = XMLUtil.selectNodeList(this.contextData, this.createPath(path));
        } catch (javax.xml.transform.TransformerException local) {
            throw new ProcessingException("TransformerException: " + local, local);
        }
        if (list != null && list.getLength() > 0) {
            result = true;
            for(int i = 0; i < list.getLength(); i++) {

                // the found node is either an attribute or an element
                if (list.item(i).getNodeType() == Node.ATTRIBUTE_NODE) {
                    // if it is an attribute simple create a new text node with the value of the attribute
                    String value = list.item(i).getNodeValue();
                    contentHandler.characters(value.toCharArray(), 0, value.length());
                } else {
                    // now we have an element
                    // stream all children of this element to the resulting tree
                    NodeList childs = list.item(i).getChildNodes();
                    if (childs != null) {
                        for(int m = 0; m < childs.getLength(); m++) {
                            IncludeXMLConsumer.includeNode(childs.item(m), contentHandler, lexicalHandler);
                        }
                    }
                }
            }
        }

        return result;
    }

    /**
     * Get the request parameter as xml
     */
    public DocumentFragment getParameterAsXML(final String parameterName)
    throws ProcessingException {
        return this.getXML("/parameter/"+parameterName);
    }

    /**
     * Get the request parameter as a String
     */
    public String getParameter(final String parameterName)
    throws ProcessingException {
        return this.request.getParameter(parameterName);
    }

    /**
     * Try to load XML into the context.
     * If the context does not provide the ability of loading,
     * an exception is thrown.
     */
    public void loadXML(String path,
                        SourceParameters parameters,
                        Map                objectModel,
                        SourceResolver     resolver,
                        ComponentManager   manager)
    throws SAXException, ProcessingException, IOException {
        throw new ProcessingException("The context " + this.name + " does not support loading.");
    }

    /**
     * Try to save XML from the context.
     * If the context does not provide the ability of saving,
     * an exception is thrown.
     */
    public void saveXML(String path,
                        SourceParameters parameters,
                        Map                objectModel,
                        SourceResolver     resolver,
                        ComponentManager   manager)
    throws SAXException, ProcessingException, IOException {
        throw new ProcessingException("The context " + this.name + " does not support saving.");
    }

}
TOP

Related Classes of org.apache.cocoon.sunshine.context.RequestSessionContext

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.