You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2006/08/07 15:11:46 UTC

svn commit: r429330 - in /cocoon/whiteboard/processor: ./ src/main/java/org/apache/cocoon/processing/ src/main/java/org/apache/cocoon/resources/

Author: cziegeler
Date: Mon Aug  7 06:11:45 2006
New Revision: 429330

URL: http://svn.apache.org/viewvc?rev=429330&view=rev
Log:
Correct svn:ignore
Start new cocoon protocol implementation

Added:
    cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/
    cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java   (with props)
    cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java   (with props)
    cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java   (with props)
Modified:
    cocoon/whiteboard/processor/   (props changed)
    cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java

Propchange: cocoon/whiteboard/processor/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Mon Aug  7 06:11:45 2006
@@ -1,4 +1,9 @@
-
-.classpath
-.fbprefs
-.project
+target
+.classpath
+.project
+.wtpmodules
+.deployables
+*.ipr
+*.iml
+*.iws
+.fbprefs

Modified: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java?rev=429330&r1=429329&r2=429330&view=diff
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java (original)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/processing/SAXAwareHttpServletResponseWrapper.java Mon Aug  7 06:11:45 2006
@@ -32,18 +32,30 @@
     extends HttpServletResponseWrapper
     implements SAXAwareHttpServletResponse {
 
-    protected final ContentHandler handler;
+    protected ContentHandler handler;
+
+    public SAXAwareHttpServletResponseWrapper(HttpServletResponse response) {
+        super(response);
+    }
 
     public SAXAwareHttpServletResponseWrapper(HttpServletResponse response, ContentHandler handler) {
         super(response);
-        this.handler = handler;
+        this.setContentHandler(handler);
     }
 
     public SAXAwareHttpServletResponseWrapper(HttpServletResponse response,
                                               ContentHandler      handler, 
                                               LexicalHandler      lexicalHandler) {
         super(response);
-        if ( handler != lexicalHandler ) {
+        this.setContentHandler(handler, lexicalHandler);
+    }
+
+    public void setContentHandler(ContentHandler handler) {
+        this.handler = handler;
+    }
+
+    public void setContentHandler(ContentHandler handler, LexicalHandler lexicalHandler) {
+        if ( handler != lexicalHandler && lexicalHandler != null ) {
             this.handler = new ContentHandlerWrapper(handler, lexicalHandler);
         } else {
             this.handler = handler;

Added: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java?rev=429330&view=auto
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java (added)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java Mon Aug  7 06:11:45 2006
@@ -0,0 +1,244 @@
+/*
+ * Copyright 1999-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.resources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.cocoon.environment.Environment;
+import org.apache.cocoon.environment.http.HttpEnvironment;
+import org.apache.cocoon.environment.internal.EnvironmentHelper;
+import org.apache.cocoon.processing.SAXAwareHttpServletResponseWrapper;
+import org.apache.cocoon.servlet.RequestUtil;
+import org.apache.cocoon.xml.SaxBuffer;
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceException;
+import org.apache.excalibur.source.SourceValidity;
+import org.apache.excalibur.xml.sax.XMLizable;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Implementation of a {@link Source} that gets its content
+ * by using the request dispatcher.
+ *
+ * @version $Id$
+ */
+public final class WebappSource
+        extends AbstractLogEnabled
+        implements Source, XMLizable {
+
+    /** The servlet context. */
+    protected final ServletContext servletContext;
+
+    /** The system id */
+    protected final String systemId;
+
+    /** The used protocol */
+    protected final String protocol;
+
+    protected String mimeType;
+
+    protected boolean needsRefresh = true;
+
+    protected HttpServletRequest request;
+
+    protected SAXAwareHttpServletResponseWrapper response;
+
+    protected final String path;
+
+    protected Exception e;
+
+    protected SaxBuffer saxBuffer;
+
+    /**
+     * Construct a new object
+     */
+    public WebappSource(ServletContext servletContext,
+                        String         uri,
+                        Map            parameters,
+                        Logger         logger)
+    throws MalformedURLException {
+        this.servletContext = servletContext;
+
+        Environment env = EnvironmentHelper.getCurrentEnvironment();
+        if ( env == null ) {
+            throw new MalformedURLException("The cocoon protocol can not be used outside an environment.");
+        }
+        this.enableLogging(logger);
+
+        WebappSourceInfo info = WebappSourceInfo.parseURI(env, uri);
+        this.protocol = info.protocol;
+
+        final HttpServletRequest oldRequest = (HttpServletRequest) env.getObjectModel().get(HttpEnvironment.HTTP_REQUEST_OBJECT);
+        final HttpServletResponse oldResponse = (HttpServletResponse) env.getObjectModel().get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
+        this.request = RequestUtil.createRequestForUri(oldRequest, info.requestURI, null);
+        this.response = new SAXAwareHttpServletResponseWrapper(oldResponse);
+
+        this.systemId = info.systemId;
+        this.path = info.requestURI;
+    }
+
+    /**
+     * Return the protocol identifier.
+     */
+    public String getScheme() {
+        return this.protocol;
+    }
+
+    /**
+     * Get the content length of the source or -1 if it
+     * is not possible to determine the length.
+     */
+    public long getContentLength() {
+        return -1;
+    }
+
+    /**
+     * Get the last modification date.
+     * @return The last modification in milliseconds since January 1, 1970 GMT
+     *         or 0 if it is unknown
+     */
+    public long getLastModified() {
+        return 0;
+    }
+
+    /**
+     * Return an <code>InputStream</code> object to read from the source.
+     */
+    public InputStream getInputStream()
+    throws IOException, SourceException {
+        this.checkRefresh();
+        return null;
+    }
+
+    /**
+     * Returns the unique identifer for this source
+     */
+    public String getURI() {
+        return this.systemId;
+    }
+
+    /**
+     * Returns true always.
+     * @see org.apache.excalibur.source.Source#exists()
+     */
+    public boolean exists() {
+        return true;
+    }
+
+    /**
+     * Get the validity object. This wraps validity of the enclosed event
+     * pipeline. If pipeline is not cacheable, <code>null</code> is returned.
+     */
+    public SourceValidity getValidity() {
+        return null;
+    }
+
+    /**
+     * The mime-type of the content described by this object.
+     * If the source is not able to determine the mime-type by itself
+     * this can be null.
+     */
+     public String getMimeType() {
+         return this.mimeType;
+     }
+
+    /**
+     * Refresh this object and update the last modified date
+     * and content length.
+     *
+     * @see org.apache.excalibur.source.Source#refresh()
+     */
+    public void refresh() {
+        this.e = null;
+        try {
+            this.saxBuffer = new SaxBuffer();
+            this.response.setContentHandler(this.saxBuffer);
+            final RequestDispatcher dispatcher = this.servletContext.getRequestDispatcher(this.path);
+            if ( dispatcher == null ) {
+                this.e = new IOException("Dispatcher not found for source: " + this.systemId);
+            } else {
+                dispatcher.include(this.request, this.response);
+            }
+        } catch (Exception ex) {
+            this.e = ex;
+        } finally {
+            this.response.setContentHandler(null);
+        }
+        this.needsRefresh = false;
+    }
+
+    protected void checkRefresh() {
+        if ( this.needsRefresh ) {
+            this.refresh();
+        }
+    }
+
+    /**
+     * Stream content to the content handler
+     */
+    public void toSAX(ContentHandler contentHandler)
+    throws SAXException {
+        this.checkRefresh();
+        if ( this.e != null ) {
+            this.saxBuffer.toSAX(contentHandler);
+        } else {
+            if ( this.e instanceof SAXException ) {
+                throw (SAXException)this.e;
+            } else {
+                throw new SAXException(this.e);
+            }
+        }
+    }
+
+    /**
+     * Get the value of a parameter.
+     * Using this it is possible to get custom information provided by the
+     * source implementation, like an expires date, HTTP headers etc.
+     */
+    public String getParameter(String name) {
+        return null;
+    }
+
+    /**
+     * Get the value of a parameter.
+     * Using this it is possible to get custom information provided by the
+     * source implementation, like an expires date, HTTP headers etc.
+     */
+    public long getParameterAsLong(String name) {
+        return 0;
+    }
+
+    /**
+     * Get parameter names
+     * Using this it is possible to get custom information provided by the
+     * source implementation, like an expires date, HTTP headers etc.
+     */
+    public Iterator getParameterNames() {
+        return java.util.Collections.EMPTY_LIST.iterator();
+    }
+}

Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSource.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java?rev=429330&view=auto
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java (added)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java Mon Aug  7 06:11:45 2006
@@ -0,0 +1,80 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.resources;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+
+import org.apache.excalibur.source.Source;
+import org.apache.excalibur.source.SourceFactory;
+import org.apache.excalibur.source.URIAbsolutizer;
+import org.apache.excalibur.source.SourceUtil;
+import org.springframework.web.context.ServletContextAware;
+
+/**
+ * This class implements the cocoon: protocol.
+ *
+ * @version $Id$
+ */
+public final class WebappSourceFactory
+    extends AbstractLogEnabled
+    implements SourceFactory, URIAbsolutizer, ServletContextAware {
+    
+    /** The <code>ServiceManager</code> */
+    protected ServletContext servletContext;
+
+    /**
+     * @see org.apache.excalibur.source.SourceFactory#getSource(java.lang.String, java.util.Map)
+     */
+    public Source getSource( String location, Map parameters )
+        throws MalformedURLException, IOException {
+        if( getLogger().isDebugEnabled() ) {
+            getLogger().debug( "Creating webapp source object for " + location );
+        }
+
+        return new WebappSource( this.servletContext, location, parameters, getLogger());
+    }
+    
+    /**
+     * @see org.apache.excalibur.source.SourceFactory#release(org.apache.excalibur.source.Source)
+     */
+    public void release( Source source ) {
+        if ( null != source ) {
+            if ( this.getLogger().isDebugEnabled() ) {
+                this.getLogger().debug("Releasing webapp source " + source.getURI());
+            }
+        }
+    }
+
+    /**
+     * @see org.apache.excalibur.source.URIAbsolutizer#absolutize(java.lang.String, java.lang.String)
+     */
+    public String absolutize(String baseURI, String location) {
+        return SourceUtil.absolutize(baseURI, location, true);
+    }
+
+    /**
+     * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
+     */
+    public void setServletContext(ServletContext servletContext) {
+        this.servletContext = servletContext;
+    }
+}

Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java
URL: http://svn.apache.org/viewvc/cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java?rev=429330&view=auto
==============================================================================
--- cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java (added)
+++ cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java Mon Aug  7 06:11:45 2006
@@ -0,0 +1,152 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.resources;
+
+import java.net.MalformedURLException;
+
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.environment.Environment;
+
+/**
+ * This is a helper class for the cocoon protocol.
+ *
+ * @version $Id$
+ */
+public final class WebappSourceInfo {
+    
+    /**
+     * <ul>
+     * <li><code>true</code> if the sitemap URI uses the <code>raw:</code> subprotocol,
+     * which means that request parameters of the original request are not forwarded,</li>
+     * <li><code>false</code> otherwise.
+     * </ul>
+     */
+    public boolean rawMode;
+    
+    /** The protocol used in the sitemap URI, up to and excluding the colon. */
+    public String protocol;
+    
+    /** The request URI, relative to the context. */
+    public String requestURI;
+    
+    /** The system ID: &lt;protocol&gt;&lt;request-uri&gt;[?&lt;query-string&gt;]. */
+    public String systemId;
+    
+    /** The Cocoon view used in the sitemap URI or <code>null</code> if no view is used.  */
+    public String view;
+    
+    /**
+     * The prefix of the URI in progress for <code>cocoon:/</code> requests,
+     * or an empty string for <code>cocoon://</code> requests.
+     */
+    public String prefix;
+    
+    /** The query string of the sitemap URI. */
+    public String queryString;
+    
+    /** The sitemap URI without protocol identifier and query string. */
+    public String uri;
+
+    /**
+     * Determine the initial processor for the cocoon protocol request.
+     * <ul>
+     * <li><code>true</code> - start in the root sitemap (<code>cocoon://</code>)</li>
+     * <li><code>false</code> - start in the current sitemap (<code>cocoon:/</code>)</li>
+     * </ul>
+     */
+    public boolean processFromRoot;
+
+    public static WebappSourceInfo parseURI(Environment env, String sitemapURI) 
+    throws MalformedURLException {
+        WebappSourceInfo info = new WebappSourceInfo();
+        info.rawMode = false;
+
+        // remove the protocol
+        int position = sitemapURI.indexOf(':') + 1;
+        if (position != 0) {
+            info.protocol = sitemapURI.substring(0, position-1);
+            // check for subprotocol
+            if (sitemapURI.startsWith("raw:", position)) {
+                position += 4;
+                info.rawMode = true;
+            }
+        } else {
+            throw new MalformedURLException("No protocol found for sitemap source in " + sitemapURI);
+        }
+
+        // does the uri point to this sitemap or to the root sitemap?
+        if (sitemapURI.startsWith("//", position)) {
+            position += 2;
+            info.prefix = "";
+            info.processFromRoot = true;
+        } else if (sitemapURI.startsWith("/", position)) {
+            position ++;
+            info.prefix = env.getURIPrefix();
+            info.processFromRoot = false;
+        } else {
+            throw new MalformedURLException("Malformed cocoon URI: " + sitemapURI);
+        }
+
+        // create the queryString (if available)
+        int queryStringPos = sitemapURI.indexOf('?', position);
+        if (queryStringPos != -1) {
+            info.queryString = sitemapURI.substring(queryStringPos + 1);
+            info.uri = sitemapURI.substring(position, queryStringPos);
+        } else if (position > 0) {
+            info.uri = sitemapURI.substring(position);
+        }
+
+        
+        // determine if the queryString specifies a cocoon-view
+        info.view = getView(info.queryString, env);
+
+        // build the request uri which is relative to the context
+        info.requestURI = info.prefix + info.uri;
+
+        // create system ID
+        final StringBuffer buffer = new StringBuffer(info.protocol);
+        buffer.append("://").append(info.requestURI);
+        if (info.queryString != null ) {
+            buffer.append('?').append(info.queryString);
+        }
+        info.systemId = buffer.toString();
+
+        return info;
+    }
+
+    public static String getView(String query, Environment env) {
+        if (query != null) {
+            int index = query.indexOf(Constants.VIEW_PARAM);
+            if (index != -1 
+                    && (index == 0 || query.charAt(index-1) == '&')
+                    && query.length() > index + Constants.VIEW_PARAM.length() 
+                    && query.charAt(index+Constants.VIEW_PARAM.length()) == '=') {
+                
+                String tmp = query.substring(index+Constants.VIEW_PARAM.length()+1);
+                index = tmp.indexOf('&');
+                if (index != -1) {
+                    return tmp.substring(0,index);
+                } else {
+                    return tmp;
+                }
+            } else {
+                return env.getView();
+            }
+        } else {
+            return env.getView();
+        }
+    }
+}

Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/whiteboard/processor/src/main/java/org/apache/cocoon/resources/WebappSourceInfo.java
------------------------------------------------------------------------------
    svn:keywords = Id