cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dani...@apache.org
Subject svn commit: r367085 - in /cocoon/trunk/cocoon-core/src: main/java/org/apache/cocoon/blocks/ main/java/org/apache/cocoon/blocks/util/ main/java/org/apache/cocoon/components/source/impl/ test/java/org/apache/cocoon/test/blocks/ test/resources/org/apache/...
Date Sun, 08 Jan 2006 20:15:45 GMT
Author: danielf
Date: Sun Jan  8 12:15:23 2006
New Revision: 367085

URL: http://svn.apache.org/viewcvs?rev=367085&view=rev
Log:
Refactored the BlockManager from being a Processor to become a Servlet. Refactored the BlocksSource
to call Servlets rather than Processors. Created a util sub package and moved some utility
classes to it. Removed the BlockManagerTestCase as it wasn't relevant anymore.

Added:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java
  (with props)
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java
  (with props)
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/CoreUtil.java
      - copied, changed from r366826, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/CoreUtil.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/LoggerUtil.java
      - copied, changed from r366794, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/LoggerUtil.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletConfigurationWrapper.java
      - copied, changed from r366826, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletConfigurationWrapper.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletContextWrapper.java
      - copied, changed from r366826, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletContextWrapper.java
Removed:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/CoreUtil.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/LoggerUtil.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletConfigurationWrapper.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletContextWrapper.java
    cocoon/trunk/cocoon-core/src/test/java/org/apache/cocoon/test/blocks/BlockManagerTestCase.java
    cocoon/trunk/cocoon-core/src/test/resources/org/apache/cocoon/test/blocks/BlockManagerTestCase.xconf
Modified:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/Block.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockContext.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockManager.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksContext.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksManager.java
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSource.java

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/Block.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/Block.java?rev=367085&r1=367084&r2=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/Block.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/Block.java Sun Jan  8
12:15:23 2006
@@ -26,7 +26,7 @@
 /**
  * @version $Id$
  */
-public interface Block extends Processor, Servlet { 
+public interface Block extends Servlet { 
 
     public static String NAME = Block.class.getName() + "-name";
     public static String SUPER = "super";

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockContext.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockContext.java?rev=367085&r1=367084&r2=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockContext.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockContext.java Sun
Jan  8 12:15:23 2006
@@ -29,6 +29,8 @@
 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 
+import org.apache.cocoon.blocks.util.ServletContextWrapper;
+
 /**
 * @version $Id$
 */

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockManager.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockManager.java?rev=367085&r1=367084&r2=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockManager.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlockManager.java Sun
Jan  8 12:15:23 2006
@@ -42,13 +42,13 @@
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.Constants;
 import org.apache.cocoon.Processor;
+import org.apache.cocoon.blocks.util.CoreUtil;
+import org.apache.cocoon.blocks.util.ServletConfigurationWrapper;
 import org.apache.cocoon.components.ContextHelper;
 import org.apache.cocoon.components.LifecycleHelper;
 import org.apache.cocoon.components.container.CocoonServiceManager;
 import org.apache.cocoon.components.container.ComponentContext;
 import org.apache.cocoon.core.container.CoreServiceManager;
-import org.apache.cocoon.environment.Environment;
-import org.apache.cocoon.environment.SourceResolver;
 import org.apache.cocoon.environment.http.HttpContext;
 import org.apache.cocoon.environment.http.HttpEnvironment;
 
@@ -75,17 +75,6 @@
     private Blocks blocks;
     private String contextURL;
 
-    public void init(ServletConfig servletConfig) throws ServletException {
-    	this.servletConfig = servletConfig;
-    	this.servletContext = servletConfig.getServletContext();
-        this.containerEncoding = this.servletConfig.getInitParameter("container-encoding");
-        if (this.containerEncoding == null) {
-        	this.containerEncoding = "ISO-8859-1";
-        }
-    }
-    
-    // Life cycle
-
     public void contextualize(Context context) throws ContextException {
         this.context = context;
     }
@@ -296,162 +285,104 @@
         }
         return uri;
     }
+    
+    // Servlet methods
 
-    // The Processor methods
+	public void init(ServletConfig servletConfig) throws ServletException {
+    	this.servletConfig = servletConfig;
+    	this.servletContext = servletConfig.getServletContext();
+        this.containerEncoding = this.servletConfig.getInitParameter("container-encoding");
+        if (this.containerEncoding == null) {
+        	this.containerEncoding = "ISO-8859-1";
+        }
+    }
 
-    public boolean process(Environment environment) throws Exception {
-        String blockName = (String)environment.getAttribute(Block.NAME);
+    public ServletConfig getServletConfig() {
+		return this.servletConfig;
+	}
+    
+	public void service(ServletRequest request0, ServletResponse response0) throws ServletException,
IOException {
+		HttpServletRequest request = (HttpServletRequest)request0;
+		HttpServletResponse response =(HttpServletResponse)response0;
+
+        String blockName = (String) request.getAttribute(Block.NAME);
 
         if (blockName != null) {
             // Request to other block.
-        	String blockId = this.blockWiring.getBlockId(blockName);
-        	boolean superCall = false;
+            String blockId = this.blockWiring.getBlockId(blockName);
+            boolean superCall = false;
             // Call to named block
             if (blockId != null && !Block.SUPER.equals(blockName)) {
                 // The block name should not be used in the recieving block.
-                environment.removeAttribute(Block.NAME);
+                request.removeAttribute(Block.NAME);
             } else {
-            	if (Block.SUPER.equals(blockName)) {
-            		// Explicit call to super block
-            		// The block name should not be used in the recieving block.
-            		environment.removeAttribute(Block.NAME);
-            	} else if (blockId == null) {
-            		// If there is a super block, the connection might
-            		// be defined there instead.
-            		blockId = this.blockWiring.getBlockId(Block.SUPER);
-            	}
-        		superCall = true;
+                if (Block.SUPER.equals(blockName)) {
+                    // Explicit call to super block
+                    // The block name should not be used in the recieving block.
+                    request.removeAttribute(Block.NAME);
+                } else if (blockId == null) {
+                    // If there is a super block, the connection might
+                    // be defined there instead.
+                    blockId = this.blockWiring.getBlockId(Block.SUPER);
+                }
+                superCall = true;
             }
             Block block = this.blocks.getBlock(blockId);
-    		if (block == null) {
-    			return false;
-    		}
+            if (block == null)
+                throw new ServletException("No block with name=" + blockName +
+                        " id=" + blockId);
             this.getLogger().debug("Enter processing in block " + blockName);
             try {
-				// A super block should be called in the context of
-				// the called block to get polymorphic calls resolved
-				// in the right way. Therefore no new current block is
-				// set.
-            	if (!superCall) {
-                	// It is important to set the current block each time
-                	// a new block is entered, this is used for the block
-                	// protocol
-            		BlockEnvironmentHelper.enterBlock(block);
-            	}
-            	return block.process(environment);
+                // A super block should be called in the context of
+                // the called block to get polymorphic calls resolved
+                // in the right way. Therefore no new current block is
+                // set.
+                if (!superCall) {
+                    // It is important to set the current block each time
+                    // a new block is entered, this is used for the block
+                    // protocol
+                    BlockEnvironmentHelper.enterBlock(block);
+                }
+                block.service(request, response);
             } finally {
-            	if (!superCall) {
-            		BlockEnvironmentHelper.leaveBlock();
-            	}
-            	this.getLogger().debug("Leaving processing in block " + blockName);
-			}            	
+                if (!superCall) {
+                    BlockEnvironmentHelper.leaveBlock();
+                }
+                this.getLogger().debug("Leaving processing in block " + blockName);
+            }               
 
         } else {
             // Request to the own block
-            boolean result = this.blockProcessor.process(environment);
-
-            return result;
-
-            // Pipelines seem to throw an exception instead of
-            // returning false when the pattern is not found. For the
-            // moment an explicit call of the super block is called in
-            // the end of the sitemap. It might be better to be
-            // explicit about it anyway.
-
-//             if (result) {
-//                 return true;
-//             } else if (this.superId != null) {
-//                 // Wasn't defined in the current block try super block
-//                 return this.process(this.superId, environment, true);
-//             } else {
-//                 return false;
-//             }
-        }
-    }
-
-    // FIXME: Not consistently supported for blocks yet. Most of the
-    // code just use process.
-    public InternalPipelineDescription buildPipeline(Environment environment)
-        throws Exception {
-        return this.blockProcessor.buildPipeline(environment);
-    }
-
-    public Configuration[] getComponentConfigurations() {
-        return this.blockProcessor.getComponentConfigurations();
-    }
+            String uri = request.getPathInfo();
 
-    // A block is supposed to be an isolated unit so it should not have
-    // any direct access to the global root sitemap
-    public Processor getRootProcessor() {
-        return this.blockProcessor;
-    }
-    
-    public SourceResolver getSourceResolver() {
-        return this.blockProcessor.getSourceResolver();
-    }
-    
-    public String getContext() {
-        return this.blockProcessor.getContext();
-    }
-
-    /**
-     * @see org.apache.cocoon.Processor#getAttribute(java.lang.String)
-     */
-    public Object getAttribute(String name) {
-        return this.blockProcessor.getAttribute(name);
-    }
-
-    /**
-     * @see org.apache.cocoon.Processor#removeAttribute(java.lang.String)
-     */
-    public Object removeAttribute(String name) {
-        return this.blockProcessor.removeAttribute(name);
-    }
-
-    /**
-     * @see org.apache.cocoon.Processor#setAttribute(java.lang.String, java.lang.Object)
-     */
-    public void setAttribute(String name, Object value) {
-        this.blockProcessor.setAttribute(name, value);
-    }
-
-	public ServletConfig getServletConfig() {
-		return this.servletConfig;
-	}
-
-	public void service(ServletRequest request0, ServletResponse response0) throws ServletException,
IOException {
-		HttpServletRequest request = (HttpServletRequest)request0;
-		HttpServletResponse response =(HttpServletResponse)response0;
-
-        String uri = request.getPathInfo();
-
-        if (uri.charAt(0) == '/') {
-        	uri = uri.substring(1);
-        }
+            if (uri.charAt(0) == '/') {
+                uri = uri.substring(1);
+            }
 
-        String formEncoding = request.getParameter("cocoon-form-encoding");
-        if (formEncoding == null) {
-        	formEncoding = "ISO-8859-1";
-            // FIXME formEncoding = this.settings.getFormEncoding();
+            String formEncoding = request.getParameter("cocoon-form-encoding");
+            if (formEncoding == null) {
+                formEncoding = "ISO-8859-1";
+                // FIXME formEncoding = this.settings.getFormEncoding();
+            }
+            HttpEnvironment env =
+                new HttpEnvironment(uri,
+                        this.contextURL,
+                        request,
+                        response,
+                        this.servletContext,
+                        new HttpContext(this.servletContext),
+                        this.containerEncoding,
+                        formEncoding);
+            env.enableLogging(getLogger());
+            
+            try {
+                this.blockProcessor.process(env);
+            } catch (Exception e) {
+                e.printStackTrace();
+                throw new ServletException(e);
+            }
+            env.commitResponse();       
         }
-        HttpEnvironment env =
-        	new HttpEnvironment(uri,
-        			this.contextURL,
-        			request,
-        			response,
-        			this.servletContext,
-        			new HttpContext(this.servletContext),
-        			this.containerEncoding,
-        			formEncoding);
-        env.enableLogging(getLogger());
-		
-        try {
-	        this.process(env);
-		} catch (Exception e) {
-			e.printStackTrace();
-			throw new ServletException(e);
-		}
-		env.commitResponse();		
 	}
 
 	public String getServletInfo() {

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksContext.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksContext.java?rev=367085&r1=367084&r2=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksContext.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksContext.java Sun
Jan  8 12:15:23 2006
@@ -23,6 +23,8 @@
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 
+import org.apache.cocoon.blocks.util.ServletContextWrapper;
+
 /**
  * @version $Id$
  */

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksManager.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksManager.java?rev=367085&r1=367084&r2=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksManager.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/BlocksManager.java Sun
Jan  8 12:15:23 2006
@@ -36,6 +36,8 @@
 import org.apache.cocoon.Constants;
 import org.apache.cocoon.Modifiable;
 import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.blocks.util.CoreUtil;
+import org.apache.cocoon.blocks.util.LoggerUtil;
 import org.apache.cocoon.components.LifecycleHelper;
 import org.apache.cocoon.components.source.SourceUtil;
 import org.apache.cocoon.components.source.impl.DelayedRefreshSourceWrapper;

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java?rev=367085&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java
(added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java
Sun Jan  8 12:15:23 2006
@@ -0,0 +1,149 @@
+/*
+ * Copyright 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.blocks.util;
+
+import java.net.URI;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+import org.apache.cocoon.environment.wrapper.RequestParameters;
+
+/**
+ * Wraps the request object of the environment. All URI methods are overrided to reflect
the
+ * used block URI. Request parameters and attributes have local values as well, no fallback
+ * to the original request, (is that needed?).
+ * 
+ * @version $Id$
+ */
+public class BlockHttpServletRequestWrapper extends HttpServletRequestWrapper {
+    
+    private URI uri;
+    private Hashtable attributes = new Hashtable();
+    private RequestParameters parameters;
+
+    public BlockHttpServletRequestWrapper(HttpServletRequest request, URI uri) {
+        super(request);
+        this.uri = uri;
+        this.parameters = new RequestParameters(this.uri.getQuery());
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletRequestWrapper#getContextPath()
+     */
+    public String getContextPath() {
+        return "";
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletRequestWrapper#getPathInfo()
+     */
+    public String getPathInfo() {
+        return this.uri.getPath();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletRequestWrapper#getQueryString()
+     */
+    public String getQueryString() {
+        return this.uri.getQuery();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletRequestWrapper#getRequestURI()
+     */
+    public String getRequestURI() {
+        return this.getContextPath() + this.getServletPath() + this.getPathInfo();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletRequestWrapper#getRequestURL()
+     */
+    public StringBuffer getRequestURL() {
+        return new StringBuffer(this.uri.getScheme()).append(':').append(this.getRequestURI());
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletRequestWrapper#getServletPath()
+     */
+    public String getServletPath() {
+        return "";
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#getAttribute(java.lang.String)
+     */
+    public Object getAttribute(String name) {
+        return this.attributes.get(name);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#getAttributeNames()
+     */
+    public Enumeration getAttributeNames() {
+        return this.attributes.keys();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#getParameter(java.lang.String)
+     */
+    public String getParameter(String name) {
+        return this.parameters.getParameter(name);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#getParameterMap()
+     */
+    public Map getParameterMap() {
+        // TODO Implement this
+        throw new UnsupportedOperationException();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#getParameterNames()
+     */
+    public Enumeration getParameterNames() {
+        return this.parameters.getParameterNames();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#getParameterValues(java.lang.String)
+     */
+    public String[] getParameterValues(String name) {
+        return this.parameters.getParameterValues(name);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#removeAttribute(java.lang.String)
+     */
+    public void removeAttribute(String name) {
+        this.attributes.remove(name);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletRequestWrapper#setAttribute(java.lang.String, java.lang.Object)
+     */
+    public void setAttribute(String name, Object value) {
+        if (value != null)
+            this.attributes.put(name, value);
+        else
+            this.removeAttribute(name);
+    }
+}
\ No newline at end of file

Propchange: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletRequestWrapper.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java?rev=367085&view=auto
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java
(added)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java
Sun Jan  8 12:15:23 2006
@@ -0,0 +1,184 @@
+/*
+ * Copyright 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.blocks.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+/**
+ * Wraps the response. Modification of headers and status is ignored. The output
+ * stream and the methods for it are overridden.
+ * 
+ * @version $Id$
+ */
+public class BlockHttpServletResponseWrapper extends HttpServletResponseWrapper {
+
+    private OutputStream outputStream;
+    private ServletOutputStream servletStream;
+    private OutputStreamWriter writer;
+    private boolean committed;
+    
+    public BlockHttpServletResponseWrapper(HttpServletResponse response) {
+        super(response);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#addDateHeader(java.lang.String,
long)
+     */
+    public void addDateHeader(String name, long date) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#addHeader(java.lang.String, java.lang.String)
+     */
+    public void addHeader(String name, String value) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#addIntHeader(java.lang.String,
int)
+     */
+    public void addIntHeader(String name, int value) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#setDateHeader(java.lang.String,
long)
+     */
+    public void setDateHeader(String name, long date) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#setHeader(java.lang.String, java.lang.String)
+     */
+    public void setHeader(String name, String value) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#setIntHeader(java.lang.String,
int)
+     */
+    public void setIntHeader(String name, int value) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int, java.lang.String)
+     */
+    public void setStatus(int sc, String sm) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.http.HttpServletResponseWrapper#setStatus(int)
+     */
+    public void setStatus(int sc) {
+        // Ignore
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#flushBuffer()
+     */
+    public void flushBuffer() throws IOException {
+        this.committed = true;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#getBufferSize()
+     */
+    public int getBufferSize() {
+        return 0;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#getOutputStream()
+     */
+    public ServletOutputStream getOutputStream() throws IOException {
+        if (this.writer != null)
+            throw new IllegalStateException( "Tried to create output stream; writer already
exists" );
+
+        return new ServletOutputStream() {
+
+            /* (non-Javadoc)
+             * @see java.io.OutputStream#write(int)
+             */
+            public void write(int b) throws IOException {
+                BlockHttpServletResponseWrapper.this.outputStream.write(b);
+            }
+        };
+    }
+    
+    public void setOutputStream(OutputStream outputStream) {
+        this.outputStream = outputStream;        }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#getWriter()
+     */
+    public PrintWriter getWriter() throws IOException {
+        if (this.servletStream != null)
+            throw new IllegalStateException( "Tried to create writer; output stream already
exists" );
+
+        return new PrintWriter(new OutputStreamWriter(this.outputStream, this.getCharacterEncoding()));
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#isCommitted()
+     */
+    public boolean isCommitted() {
+        return this.committed;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#reset()
+     */
+    public void reset() {
+        this.resetBuffer();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#resetBuffer()
+     */
+    public void resetBuffer() {
+        if (this.committed)
+            throw new IllegalStateException( "May not resetBuffer after response is committed"
);
+        this.outputStream = null;
+        this.servletStream = null;
+        this.writer = null;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#setBufferSize(int)
+     */
+    public void setBufferSize(int size) {
+        // TODO Implement buffering, for the moment ignore.
+    }
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletResponseWrapper#setContentLength(int)
+     */
+    public void setContentLength(int len) {
+        // Ignore
+    }
+}

Propchange: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/BlockHttpServletResponseWrapper.java
------------------------------------------------------------------------------
    svn:keywords = Id

Copied: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/CoreUtil.java
(from r366826, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/CoreUtil.java)
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/CoreUtil.java?p2=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/CoreUtil.java&p1=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/CoreUtil.java&r1=366826&r2=367085&rev=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/CoreUtil.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/CoreUtil.java Sun
Jan  8 12:15:23 2006
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.cocoon.blocks;
+package org.apache.cocoon.blocks.util;
 
 import java.io.File;
 import java.io.FileInputStream;

Copied: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/LoggerUtil.java
(from r366794, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/LoggerUtil.java)
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/LoggerUtil.java?p2=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/LoggerUtil.java&p1=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/LoggerUtil.java&r1=366794&r2=367085&rev=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/LoggerUtil.java (original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/LoggerUtil.java Sun
Jan  8 12:15:23 2006
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.cocoon.blocks;
+package org.apache.cocoon.blocks.util;
 
 import java.io.File;
 import java.io.IOException;

Copied: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletConfigurationWrapper.java
(from r366826, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletConfigurationWrapper.java)
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletConfigurationWrapper.java?p2=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletConfigurationWrapper.java&p1=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletConfigurationWrapper.java&r1=366826&r2=367085&rev=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletConfigurationWrapper.java
(original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletConfigurationWrapper.java
Sun Jan  8 12:15:23 2006
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.cocoon.blocks;
+package org.apache.cocoon.blocks.util;
 
 import java.util.Enumeration;
 

Copied: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletContextWrapper.java
(from r366826, cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletContextWrapper.java)
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletContextWrapper.java?p2=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletContextWrapper.java&p1=cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletContextWrapper.java&r1=366826&r2=367085&rev=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/ServletContextWrapper.java
(original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/blocks/util/ServletContextWrapper.java
Sun Jan  8 12:15:23 2006
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.cocoon.blocks;
+package org.apache.cocoon.blocks.util;
 
 import java.io.InputStream;
 import java.net.MalformedURLException;

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSource.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSource.java?rev=367085&r1=367084&r2=367085&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSource.java
(original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/components/source/impl/BlockSource.java
Sun Jan  8 12:15:23 2006
@@ -24,14 +24,18 @@
 import java.net.URISyntaxException;
 import java.util.Map;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 import org.apache.avalon.framework.logger.Logger;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.cocoon.blocks.Block;
 import org.apache.cocoon.blocks.BlockEnvironmentHelper;
+import org.apache.cocoon.blocks.util.BlockHttpServletRequestWrapper;
+import org.apache.cocoon.blocks.util.BlockHttpServletResponseWrapper;
 import org.apache.cocoon.environment.Environment;
-import org.apache.cocoon.environment.ObjectModelHelper;
+import org.apache.cocoon.environment.http.HttpEnvironment;
 import org.apache.cocoon.environment.internal.EnvironmentHelper;
-import org.apache.cocoon.environment.wrapper.EnvironmentWrapper;
 import org.apache.excalibur.source.Source;
 import org.apache.excalibur.source.SourceException;
 import org.apache.excalibur.source.impl.AbstractSource;
@@ -45,14 +49,19 @@
 public final class BlockSource
     extends AbstractSource {
 
-    /** The environment */
-    private final EnvironmentWrapper environment;
+    /** Wrapped request */
+    private BlockHttpServletRequestWrapper wrappedRequest;
+    
+    /** Wrapped response */
+    private BlockHttpServletResponseWrapper wrappedResponse;
 
     /** The name of the called block */
     private String blockName;
 
     /** The current block */
     private final Block block;
+    
+    private String systemId;
 
     /**
      * Construct a new object
@@ -64,35 +73,41 @@
         throws MalformedURLException {
 
         Environment env = EnvironmentHelper.getCurrentEnvironment();
-        if ( env == null ) {
+        if (env == null) {
             throw new MalformedURLException("The block protocol can not be used outside an
environment.");
         }
         this.block = BlockEnvironmentHelper.getCurrentBlock();
         if (this.block == null)
             throw new MalformedURLException("Must be used in a block context " + this.getURI());
 
-        SitemapSourceInfo info = null;
+        URI blockURI = null;
         try {
-            info = parseBlockURI(env, uri);
+            blockURI = parseBlockURI(env, uri);
         } catch (URISyntaxException e) {
             throw new MalformedURLException("Malformed URI in block source " +
                                             e.getMessage());
         }
-        setScheme(info.protocol);
-        setSystemId(info.systemId);
+        setScheme(blockURI.getScheme());
+        setSystemId(this.systemId);
 
-        // create environment...
-        this.environment = new EnvironmentWrapper(env, info, logger);
+        // wrap the request
+        HttpServletRequest originalRequest =
+            (HttpServletRequest) env.getObjectModel().get(HttpEnvironment.HTTP_REQUEST_OBJECT);
+        if (originalRequest == null)
+            throw new MalformedURLException("Blocks only work in an HttpEnvironment");
+        
+        this.wrappedRequest = new BlockHttpServletRequestWrapper(originalRequest, blockURI);
 
-        // ...and put information passed from the parent request to the internal request
-        if ( null != parameters ) {
-            this.environment.getObjectModel().put(ObjectModelHelper.PARENT_CONTEXT, parameters);
-        } else {
-            this.environment.getObjectModel().remove(ObjectModelHelper.PARENT_CONTEXT);
-        }
+        // indicate what block that is called 
+        this.wrappedRequest.setAttribute(Block.NAME, this.blockName);
 
-        this.environment.setURI(info.prefix, info.uri);
-        this.environment.setAttribute(Block.NAME, this.blockName);
+        // wrap the response
+        HttpServletResponse originalResponse =
+            (HttpServletResponse) env.getObjectModel().get(HttpEnvironment.HTTP_RESPONSE_OBJECT);
+        if (originalResponse == null)
+            throw new MalformedURLException("Blocks only work in an HttpEnvironment");
+        
+        this.wrappedResponse = new BlockHttpServletResponseWrapper(originalResponse);
     }
 
     /**
@@ -102,18 +117,16 @@
         throws IOException, SourceException {
 
         ByteArrayOutputStream os = new ByteArrayOutputStream();
-        this.environment.setOutputStream(os);
+        this.wrappedResponse.setOutputStream(os);
 
         try {
-            this.block.process(this.environment);
+            this.block.service(this.wrappedRequest, this.wrappedResponse);
+            this.wrappedResponse.flushBuffer();
             
             return new ByteArrayInputStream(os.toByteArray());
 
         } catch (Exception e) {
             throw new SourceException("Exception during processing of " + this.getURI(),
e);
-        } finally {
-            // Unhide wrapped environment output stream
-            this.environment.setOutputStream(null);
         }
     }
 
@@ -132,13 +145,9 @@
     }
 
     // Parse the block protocol.
-    private SitemapSourceInfo parseBlockURI(Environment env, String blockURI) 
+    private URI parseBlockURI(Environment env, String blockURI) 
         throws URISyntaxException {
 
-        SitemapSourceInfo info = new SitemapSourceInfo();
-        // Maybe rawMode should be available for the block protocol.
-        info.rawMode = false;
-
         URI uri = new URI(blockURI);
 
         // Can't happen
@@ -146,7 +155,7 @@
             throw new URISyntaxException(blockURI,
                                          "Only absolute URIs are allowed for the block protocol.");
         }
-        info.protocol = uri.getScheme();
+        String scheme = uri.getScheme();
 
         String baseURI = env.getURIPrefix();
         if (baseURI.length() == 0 || !baseURI.startsWith("/"))
@@ -156,22 +165,15 @@
                                     new URI(null, null, baseURI, null));
         
         this.blockName = uri.getScheme();
-        info.uri = uri.getPath();
-        // Sub sitemap URI parsing doesn't like URIs starting with "/"
-        if (info.uri.length() != 0 && info.uri.startsWith("/"))
-            info.uri = info.uri.substring(1);
+        String path = uri.getPath();
         // All URIs, also relative are resolved and processed from the block manager
-        info.processFromRoot = true;
-        info.prefix = "";
-        info.requestURI = info.uri;
-        info.queryString = uri.getQuery();
-        info.view = SitemapSourceInfo.getView(info.queryString, env);
+        String queryString = uri.getQuery();
         
         // FIXME: This will not be a system global id, as the blockName is block local.
-        String ssp = (new URI(this.blockName, null, uri.getPath(), info.queryString, null)).toString();
-        info.systemId = (new URI(info.protocol, ssp, null)).toString();
+        String ssp = (new URI(this.blockName, null, path, queryString, null)).toString();
+        this.systemId = (new URI(scheme, ssp, null)).toString();
         
-        return info;
+        return new URI(scheme, null, path, queryString, null);
     }
 }
 



Mime
View raw message