cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gkossakow...@apache.org
Subject svn commit: r572015 - in /cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src: changes/ main/java/org/apache/cocoon/servletservice/ test/java/org/apache/cocoon/servletservice/
Date Sun, 02 Sep 2007 16:17:43 GMT
Author: gkossakowski
Date: Sun Sep  2 09:17:42 2007
New Revision: 572015

URL: http://svn.apache.org/viewvc?rev=572015&view=rev
Log:
Fixes for COCOON-2038 and COCOON-1939.
* Implemented true Object Oriented approach for handling servlet calls. This change removes
the need for explicit super calls.
* Fixed handling of multilevel inheritance that could cause stack overflow.
* Added test cases that cover added and fixed functionality.

Modified:
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/changes/changes.xml
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
    cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTestCase.java

Modified: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/changes/changes.xml?rev=572015&r1=572014&r2=572015&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/changes/changes.xml
(original)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/changes/changes.xml
Sun Sep  2 09:17:42 2007
@@ -24,8 +24,15 @@
     +-->
 <document>
   <body>
-    <release version="1.0.0-RC1-SNAPSHOT" date="2007-00-00" description="unreleased">
 
-      <action dev="gkossakowski" type="fix" issues="COCOON-2121">
+    <release version="1.0.0-RC1-SNAPSHOT" date="2007-00-00" description="unreleased">
+      <action dev="gkossakowski" type="add" issue="COCOON-2038">
+        Implemented true Object Oriented approach for handling servlet calls.
+        This change removes the need for explicit super calls. 
+      </action>
+      <action dev="gkossakowski" type="fix" issue="COCOON-1939">
+        Fixed handling of multilevel inheritance that could cause stack overflow. 
+      </action>
+      <action dev="gkossakowski" type="fix" issue="COCOON-2121">
         Fixed bug in DispatcherServlet that caused servlet (blocks) mounted at "/" to be
handled improperly. 
       </action>
     </release>

Modified: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java?rev=572015&r1=572014&r2=572015&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
(original)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/main/java/org/apache/cocoon/servletservice/ServletServiceContext.java
Sun Sep  2 09:17:42 2007
@@ -41,6 +41,7 @@
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
 
 import org.apache.cocoon.servletservice.util.ServletContextWrapper;
 import org.apache.commons.logging.Log;
@@ -413,11 +414,22 @@
         if (this.connections == null) {
             return null;
         }
-
-        Servlet servlet = (Servlet) this.connections.get(name);
+        
+        Servlet servlet =
+            (Servlet) this.connections.get(name);
+        if (servlet == null && !name.equals(SUPER)) {
+        	Servlet _super = ((Servlet)this.connections.get(SUPER));
+        	if (_super != null) {
+        		ServletContext c = _super.getServletConfig().getServletContext();
+        		if (c instanceof ServletServiceContext)
+        			return ((ServletServiceContext)c).getNamedContext(name);
+        		
+        		return null;
+        	}
+        }        
         return servlet != null ? servlet.getServletConfig().getServletContext() : null;
     }
-
+    
     /**
      * @param mountPath The mountPath to set.
      */
@@ -466,16 +478,6 @@
 
             // Call to a named servlet service that exists in the current context
             this.context = ServletServiceContext.this.getNamedContext(this.servletServiceName);
-            if (this.context == null) {
-                // If there is a super servlet service, the connection might
-                // be defined there instead.
-                ServletServiceContext superContext =
-                        (ServletServiceContext) ServletServiceContext.this.getNamedContext(SUPER);
-                if (superContext != null) {
-                    this.context = superContext.getNamedContext(this.servletServiceName);
-                    this.superCall = true;
-                }
-            }
         }
 
         protected boolean exists() {
@@ -545,19 +547,37 @@
         protected void forward(ServletRequest request, ServletResponse response, boolean
superCall)
         throws ServletException, IOException {
             try {
+                StatusRetrievableWrappedResponse wrappedResponse = new StatusRetrievableWrappedResponse((HttpServletResponse)response);
                 if (!superCall) {
                     // It is important to set the current context each time
                     // a new context is entered, this is used for the servlet
                     // protocol
-                    CallStackHelper.enterServlet(ServletServiceContext.this, (HttpServletRequest)request,
(HttpServletResponse)response);
+                    CallStackHelper.enterServlet(ServletServiceContext.this, (HttpServletRequest)request,
(HttpServletResponse)wrappedResponse);
                 } else {
                     // A super servlet service should be called in the context of
                     // the called servlet service to get polymorphic calls resolved
                     // in the right way. We still need to register the
                     // current context for resolving super calls relative it.
-                    CallStackHelper.enterSuperServlet(ServletServiceContext.this, (HttpServletRequest)request,
(HttpServletResponse)response);
+                    CallStackHelper.enterSuperServlet(ServletServiceContext.this, (HttpServletRequest)request,
(HttpServletResponse)wrappedResponse);
+                }
+                ServletException se = null;
+                try {
+                	ServletServiceContext.this.servlet.service(request, wrappedResponse);
                 }
-                ServletServiceContext.this.servlet.service(request, response);
+                catch (ServletException e) {
+                	se = e;
+                }
+               	int status = wrappedResponse.getStatus();
+               	if (se != null || (status < 200 || status >= 400)) {
+               		wrappedResponse.reset();
+               		NamedDispatcher _super = (NamedDispatcher) ServletServiceContext.this.getNamedDispatcher(SUPER);
+               		if (_super != null) {
+               			_super.forward(request, wrappedResponse);
+               		}
+               		else
+               			throw se;
+               	}
+
             } finally {
                 CallStackHelper.leaveServlet();
             }
@@ -570,4 +590,38 @@
             throw new UnsupportedOperationException();
         }
     }
+
+    private static class StatusRetrievableWrappedResponse extends HttpServletResponseWrapper
{
+    	
+       	private int status;
+    
+       	public StatusRetrievableWrappedResponse(HttpServletResponse wrapped) {
+       		super(wrapped);
+       	}
+       	
+    	public void setStatus(int sc, String sm) {
+    		this.status = sc;
+    		super.setStatus(sc, sm);
+    	}
+    
+    	public void setStatus(int sc) {
+    		this.status = sc;
+    		super.setStatus(sc);
+    	}
+    	
+    	public int getStatus() {
+    		return this.status;
+    	}
+    	
+    	public void sendError(int errorCode) throws IOException {
+    		this.status = errorCode;
+    		super.sendError(errorCode);	
+    	}
+    	
+    	public void sendError(int errorCode, String errorMessage) throws IOException {
+    		this.status = errorCode;
+    		super.sendError(errorCode, errorMessage);	
+    	}		
+    }
+    
 }

Modified: cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTestCase.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTestCase.java?rev=572015&r1=572014&r2=572015&view=diff
==============================================================================
--- cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTestCase.java
(original)
+++ cocoon/trunk/core/cocoon-servlet-service/cocoon-servlet-service-impl/src/test/java/org/apache/cocoon/servletservice/ServletServiceContextTestCase.java
Sun Sep  2 09:17:42 2007
@@ -175,8 +175,6 @@
             
             protected void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
                 super.service(req, response);
-                assertEquals(request, req);
-                assertEquals(response, res);
                 assertEquals(servletCContext, CallStackHelper.getBaseServletContext());
                 assertEquals(servletCContext, CallStackHelper.getCurrentServletContext());
                 res.setStatus(200);
@@ -236,8 +234,6 @@
             
             protected void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
                 super.service(req, response);
-                assertEquals(request, req);
-                assertEquals(response, res);
                 assertEquals(servletCContext, CallStackHelper.getBaseServletContext());
                 assertEquals(servletCContext, CallStackHelper.getCurrentServletContext());
                 res.setStatus(200);
@@ -256,6 +252,66 @@
         dispatcher.forward(request, response);
         assertEquals(200, response.getStatus());
     }
+    
+    /**
+     * <p>This test is for bug COCOON-1939 for more than 2 level of inheritance.
+     *        
+     * <p>Servlets are connected that way:</p>
+     * <pre>
+     * 	  SerlvetC
+     * 		 ^
+     * 		 |
+     *    ServletB
+     *       ^
+     *       |
+     *    ServletA
+     * </pre>
+     * 
+     * @throws Exception
+     */
+    public void testThreeLevelInheritance() throws Exception {
+        servletA = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletAContext);
}
+
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+                super.service(request, response);
+                response.setStatus(500);
+            }
+
+        };
+        
+        servletB = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletBContext);
}
+            
+            protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
+            	super.service(request, response);
+                response.setStatus(500);
+            }
+        };
+        
+        servletC = new HttpServlet() {
+            public ServletConfig getServletConfig() { return new ServletConfigWithContext(servletCContext);
}
+            
+            protected void service(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException {
+                super.service(req, response);
+                assertEquals(servletAContext, CallStackHelper.getBaseServletContext());
+                assertEquals(servletCContext, CallStackHelper.getCurrentServletContext());
+                res.setStatus(200);
+            }
+        };
+        servletAContext.setServlet(servletA);
+        servletBContext.setServlet(servletB);
+        servletCContext.setServlet(servletC);
+        
+        //connecting servlets
+        setMainConnection(servletA, "servletA");
+        connectServlets(servletA, servletB, "super");
+        connectServlets(servletB, servletC, "super");
+
+        RequestDispatcher dispatcher = mainContext.getNamedDispatcher("servletA");
+        dispatcher.forward(request, response);
+        assertEquals(200, response.getStatus());
+    }    
     
     //-----------------------------------------------------------------------------------------
     



Mime
View raw message