ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1544238 - in /ace/trunk/org.apache.ace.deployment: src/org/apache/ace/deployment/servlet/ContentRangeResponseWrapper.java test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
Date Thu, 21 Nov 2013 16:11:27 GMT
Author: jawi
Date: Thu Nov 21 16:11:27 2013
New Revision: 1544238

URL: http://svn.apache.org/r1544238
Log:
ACE-435 - Content-Length not always set for deployment packages:

- when downloading a DP, the content length header was not always
  set. The agent could therefore not determine a priori how many
  bytes it should download;
- the solution is to leverage the ContentRangeResponseWrapper for
  non-range requests as well, making it possible to exactly 
  determine the content length *before* sending it to the agent.


Modified:
    ace/trunk/org.apache.ace.deployment/src/org/apache/ace/deployment/servlet/ContentRangeResponseWrapper.java
    ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java

Modified: ace/trunk/org.apache.ace.deployment/src/org/apache/ace/deployment/servlet/ContentRangeResponseWrapper.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.deployment/src/org/apache/ace/deployment/servlet/ContentRangeResponseWrapper.java?rev=1544238&r1=1544237&r2=1544238&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.deployment/src/org/apache/ace/deployment/servlet/ContentRangeResponseWrapper.java
(original)
+++ ace/trunk/org.apache.ace.deployment/src/org/apache/ace/deployment/servlet/ContentRangeResponseWrapper.java
Thu Nov 21 16:11:27 2013
@@ -47,16 +47,23 @@ public class ContentRangeResponseWrapper
         private static final int BUFFER_SIZE = 32 * 1024; // kB
 
         private final HttpServletResponse m_response;
+        private final boolean m_streamAll;
         private final long m_requestFirstBytePos;
         private final long m_requestLastBytePos;
-
         private final FileOutputStream m_os;
         private final File m_file;
 
         private final AtomicLong m_instanceLen = new AtomicLong(0);
 
+        public ContentRangeOutputStreamWrapper(HttpServletResponse response) throws IOException
{
+            this(response, 0, Long.MAX_VALUE);
+        }
+
         public ContentRangeOutputStreamWrapper(HttpServletResponse response, long firstBytePos,
long lastBytePos) throws IOException {
+            this(response, firstBytePos, lastBytePos, (firstBytePos == 0 && lastBytePos
== Long.MAX_VALUE));
+        }
 
+        private ContentRangeOutputStreamWrapper(HttpServletResponse response, long firstBytePos,
long lastBytePos, boolean streamAll) throws IOException {
             assert response != null;
             assert firstBytePos >= 0;
             assert lastBytePos > firstBytePos;
@@ -64,6 +71,7 @@ public class ContentRangeResponseWrapper
             m_response = response;
             m_requestFirstBytePos = firstBytePos;
             m_requestLastBytePos = lastBytePos;
+            m_streamAll = streamAll;
 
             // We use a file to buffer because Deployment Packages can be big and the current
common ACE Agent case it a
             // range request for some start position up-to EOF.
@@ -99,9 +107,11 @@ public class ContentRangeResponseWrapper
                     long lastBytePos = instanceLastBytePos < m_requestLastBytePos ? instanceLastBytePos
: m_requestLastBytePos;
                     long contentLength = lastBytePos - firstBytePos + 1;
 
-                    m_response.setStatus(SC_PARTIAL_CONTENT);
+                    m_response.setStatus(m_streamAll ? SC_OK : SC_PARTIAL_CONTENT);
                     m_response.setHeader("Content-Length", String.valueOf(contentLength));
-                    m_response.setHeader("Content-Range", String.format("bytes %d-%d/%d",
firstBytePos, lastBytePos, instanceLength));
+                    if (!m_streamAll) {
+                        m_response.setHeader("Content-Range", String.format("bytes %d-%d/%d",
firstBytePos, lastBytePos, instanceLength));
+                    }
 
                     byte[] buffer = new byte[BUFFER_SIZE];
                     is = new FileInputStream(m_file);
@@ -132,7 +142,7 @@ public class ContentRangeResponseWrapper
     }
 
     private final HttpServletResponse m_response;
-    private ServletOutputStream m_outputStream;
+    private final ServletOutputStream m_outputStream;
 
     public ContentRangeResponseWrapper(HttpServletRequest request, HttpServletResponse response)
throws IOException {
         super(response);
@@ -145,19 +155,20 @@ public class ContentRangeResponseWrapper
 
         // If a valid Range request is present we install the ContentRangeOutputStreamWrapper.
Otherwise we do not touch
         // the response ServletOutputStream until we have to in #getOutputStream().
+        ContentRangeOutputStreamWrapper wrapper = null;
         long[] requestRange = getRequestRange(request);
         if (requestRange != null) {
-            m_outputStream = new ContentRangeOutputStreamWrapper(response, requestRange[0],
requestRange[1]);
+            wrapper = new ContentRangeOutputStreamWrapper(response, requestRange[0], requestRange[1]);
+        }
+        if (wrapper == null) {
+            // Assume a range of "bytes=0-", which simply streams everything. This solves
ACE-435...
+            wrapper = new ContentRangeOutputStreamWrapper(response);
         }
+        m_outputStream = wrapper;
     }
 
     @Override
     public ServletOutputStream getOutputStream() throws IOException {
-        // If a ContentRangeOutputStreamWrapper is installed we return it. Otherwise we simply
delegate to the original
-        // response directly.
-        if (m_outputStream == null) {
-            return m_response.getOutputStream();
-        }
         return m_outputStream;
     }
 

Modified: ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java?rev=1544238&r1=1544237&r2=1544238&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
(original)
+++ ace/trunk/org.apache.ace.deployment/test/org/apache/ace/deployment/servlet/DeploymentServletTest.java
Thu Nov 21 16:11:27 2013
@@ -96,6 +96,7 @@ public class DeploymentServletTest {
         m_requestPathInfo = "/existing/versions/2.0.0";
         m_servlet.doGet(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseHeaderValue("Content-Length", "100");
         assertResponseOutputSize(100);
         assertGeneratorTargetId("existing");
         assertGeneratorToVersion("2.0.0");
@@ -128,6 +129,7 @@ public class DeploymentServletTest {
         m_requestRangeHeader = "bytes=a-1";
         m_servlet.doGet(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseHeaderValue("Content-Length", "100");
         assertResponseOutputSize(100);
     }
 
@@ -197,6 +199,7 @@ public class DeploymentServletTest {
         m_requestRangeHeader = "bytes=2-1";
         m_servlet.doGet(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_OK);
+        assertResponseHeaderValue("Content-Length", "100");
         assertResponseOutputSize(100);
     }
 
@@ -208,6 +211,7 @@ public class DeploymentServletTest {
         m_servlet.doGet(m_request, m_response);
         assertResponseCode(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
         assertResponseHeaderValue("Content-Range", "bytes */100");
+        assertResponseHeaderNotPresent("Content-Length");
         assertResponseOutputSize(0);
     }
 



Mime
View raw message