incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1229187 - in /sling/trunk/contrib/extensions/startup-filter/src: main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
Date Mon, 09 Jan 2012 15:05:57 GMT
Author: bdelacretaz
Date: Mon Jan  9 15:05:56 2012
New Revision: 1229187

URL: http://svn.apache.org/viewvc?rev=1229187&view=rev
Log:
SLING-2347 - use response.setStatus to avoid triggering the container's error pages during
startup

Modified:
    sling/trunk/contrib/extensions/startup-filter/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
    sling/trunk/contrib/extensions/startup-filter/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java

Modified: sling/trunk/contrib/extensions/startup-filter/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/startup-filter/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java?rev=1229187&r1=1229186&r2=1229187&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/startup-filter/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
(original)
+++ sling/trunk/contrib/extensions/startup-filter/src/main/java/org/apache/sling/startupfilter/impl/StartupFilterImpl.java
Mon Jan  9 15:05:56 2012
@@ -72,7 +72,7 @@ public class StartupFilterImpl implement
     private String defaultMessage;
     
     /** @inheritDoc */
-    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
+    public void doFilter(ServletRequest request, ServletResponse sr, FilterChain chain) throws
IOException, ServletException {
         updateProviders();
         
         final StringBuilder sb = new StringBuilder();
@@ -81,7 +81,15 @@ public class StartupFilterImpl implement
             sb.append('\n');
             sb.append(p.getProgressInfo());
         }
-        ((HttpServletResponse)response).sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
sb.toString());
+
+        // Do not use setError to avoid triggering the container's error page,
+        // as that might cascade other errors during startup
+        final HttpServletResponse response = (HttpServletResponse)sr;
+        response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
+        response.setContentType("text/plain");
+        response.setCharacterEncoding("UTF-8");
+        response.getWriter().write(sb.toString());
+        response.getWriter().flush();
     }
     
     @Override

Modified: sling/trunk/contrib/extensions/startup-filter/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/startup-filter/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java?rev=1229187&r1=1229186&r2=1229187&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/startup-filter/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
(original)
+++ sling/trunk/contrib/extensions/startup-filter/src/test/java/org/apache/sling/startupfilter/impl/StartupFilterImplTest.java
Mon Jan  9 15:05:56 2012
@@ -20,6 +20,8 @@ package org.apache.sling.startupfilter.i
 
 import static org.junit.Assert.assertEquals;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -116,7 +118,7 @@ public class StartupFilterImplTest {
     private HttpServletResponse response;
     private FilterChain chain;
     private int lastReturnedStatus;
-    private String lastReturnedMessage;
+    private StringWriter messageWriter;
     private AtomicInteger activeFilterCount;
     private ServiceRegistration serviceRegistration;
 
@@ -135,17 +137,19 @@ public class StartupFilterImplTest {
         final BundleContext bundleContext = mockery.mock(BundleContext.class); 
         final ComponentContext componentContext = mockery.mock(ComponentContext.class); 
         
-        final Action storeResponse = new Action() {
+        final Action storeStatus = new Action() {
             public void describeTo(Description d) {
                 d.appendText("Store HTTP response values");
             }
 
             public Object invoke(Invocation invocation) throws Throwable {
                 lastReturnedStatus = (Integer)invocation.getParameter(0);
-                lastReturnedMessage = (String)invocation.getParameter(1);
                 return null;
             }
         };
+
+        messageWriter = new StringWriter();
+        final PrintWriter responseWriter = new PrintWriter(messageWriter); 
         
         final Dictionary<String, Object> props = new Hashtable<String, Object>();
         props.put(StartupFilterImpl.ACTIVE_BY_DEFAULT_PROP, Boolean.TRUE);
@@ -173,8 +177,14 @@ public class StartupFilterImplTest {
                     returnValue(serviceRegistration)
                     ));
             
-            allowing(response).sendError(with(any(Integer.class)), with(any(String.class)));
-            will(storeResponse);
+            allowing(response).setStatus((with(any(Integer.class))));
+            will(storeStatus);
+            
+            allowing(response).setContentType("text/plain");
+            
+            allowing(response).getWriter();
+            will(returnValue(responseWriter));
+            allowing(response).setCharacterEncoding(with(any(String.class)));
             
             allowing(serviceRegistration).unregister();
             will(new ChangeInteger(activeFilterCount, false));
@@ -184,16 +194,17 @@ public class StartupFilterImplTest {
     }
     
     private void assertRequest(final int expectedStatus, final String expectedMessage) throws
Exception {
-        lastReturnedMessage = null;
         lastReturnedStatus = -1;
         
         filter.doFilter(request, response, chain);
         
+        final String responseText = messageWriter.toString();
+        
         // status 0 means we expect the request to go through
         assertEquals("Expecting status to match", 
                 expectedStatus, lastReturnedStatus);
         assertEquals("Expecting message to match", 
-                expectedMessage, lastReturnedMessage);
+                expectedMessage, responseText);
     }
     
     @Test
@@ -224,4 +235,4 @@ public class StartupFilterImplTest {
         filter.disable();
         assertEquals("Expecting filter service to be gone", 0, activeFilterCount.get());
     }
-}
+}
\ No newline at end of file



Mime
View raw message