Repository: wicket Updated Branches: refs/heads/master 792a0a413 -> a1d0df298
WICKET-5819 Support for HTML 5 media tags (audio / video) Add unit tests for the new ranges support. Fix some corner cases Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/4a36aa74 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/4a36aa74 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/4a36aa74 Branch: refs/heads/master Commit: 4a36aa74718739c44db925af4ff34b6f37cb75fd Parents: f623cea Author: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Authored: Thu Mar 26 21:39:56 2015 +0200 Committer: Martin Tzvetanov Grigorov <mgrigo...@apache.org> Committed: Mon Mar 30 21:59:14 2015 +0300 ---------------------------------------------------------------------- .../request/resource/PartWriterCallback.java | 47 ++++++--------- .../resource/PackageResourceReferenceTest.java | 63 ++++++++++++++++++++ 2 files changed, 80 insertions(+), 30 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/4a36aa74/wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java index ca70470..c0c7a63 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PartWriterCallback.java @@ -104,22 +104,31 @@ public class PartWriterCallback extends WriteCallback endbyte = contentLength; } + // The read bytes in the current buffer + int readBytes; + + // The total bytes read long totalBytes = 0; - int actualReadBytes; - while ((actualReadBytes = inputStream.read(buffer)) != -1) + while ((readBytes = inputStream.read(buffer)) != -1) { - totalBytes = totalBytes + buffer.length; - long lowerBuffer = endbyte - totalBytes; - if (lowerBuffer <= 0) + totalBytes += readBytes; + + // Check if the end byte is reached + if (endbyte - totalBytes < 0) { - buffer = resizeArray(buffer, actualReadBytes); - outputStream.write(buffer); + // calculate the bytes left to be read in the current buffer + // can be casted to int, because the the previous chunks are + // subtracted - so it can't exceed buffer size + int leftBytesToBeRead = (int)(totalBytes - startbyte) - + (int)(totalBytes - endbyte); + outputStream.write(buffer, 0, leftBytesToBeRead); break; } else { - outputStream.write(buffer); + // If the end byte is not reached read the full buffer + outputStream.write(buffer, 0, readBytes); } } } @@ -139,28 +148,6 @@ public class PartWriterCallback extends WriteCallback } /** - * Reallocates an array with a new size, and copies the contents of the old array to the new - * array. - * - * @param oldArray - * the old array, to be reallocated. - * @param newSize - * the new array size. - * @return A new array with the same contents. - */ - private static byte[] resizeArray(byte[] oldArray, int newSize) - { - int oldSize = oldArray.length; - byte[] newArray = new byte[newSize]; - int minLength = Math.min(oldSize, newSize); - if (minLength > 0) - { - System.arraycopy(oldArray, 0, newArray, 0, minLength); - } - return newArray; - } - - /** * Sets the buffer size used to send the data to the client * * @return the buffer size used to send the data to the client (default is 4096) http://git-wip-us.apache.org/repos/asf/wicket/blob/4a36aa74/wicket-core/src/test/java/org/apache/wicket/request/resource/PackageResourceReferenceTest.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/test/java/org/apache/wicket/request/resource/PackageResourceReferenceTest.java b/wicket-core/src/test/java/org/apache/wicket/request/resource/PackageResourceReferenceTest.java index 4fcbd09..a58a2c9 100644 --- a/wicket-core/src/test/java/org/apache/wicket/request/resource/PackageResourceReferenceTest.java +++ b/wicket-core/src/test/java/org/apache/wicket/request/resource/PackageResourceReferenceTest.java @@ -20,6 +20,11 @@ import java.util.Locale; import org.apache.wicket.Application; import org.apache.wicket.WicketTestCase; +import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; +import org.apache.wicket.protocol.http.mock.MockHttpServletResponse; +import org.apache.wicket.request.Request; +import org.apache.wicket.request.Response; +import org.apache.wicket.request.resource.AbstractResource.ContentRangeType; import org.apache.wicket.request.resource.IResource.Attributes; import org.apache.wicket.request.resource.ResourceReference.UrlAttributes; import org.apache.wicket.response.ByteArrayResponse; @@ -237,4 +242,62 @@ public class PackageResourceReferenceTest extends WicketTestCase final CssPackageResource alreadyMinifiedResource = alreadyMinified.getResource(); Assert.assertFalse("Already minified resource should got its compress flag set to false", alreadyMinifiedResource.getCompress()); } + + /** + * See WICKET-5819 - Media tags + */ + @Test + public void testContentRange() + { + // Test range + Assert.assertEquals("resource", makeRangeRequest("bytes=0-8")); + Assert.assertEquals("ource", makeRangeRequest("bytes=3-8")); + Assert.assertEquals("resource_var_style_en.txt", makeRangeRequest("bytes=0-")); + Assert.assertEquals("var_style_en.txt", makeRangeRequest("bytes=9-")); + Assert.assertEquals("resource_var_style_en.txt", makeRangeRequest("bytes=-")); + Assert.assertEquals("resource_var_style_en.txt", makeRangeRequest("bytes=-25")); + } + + private String makeRangeRequest(String range) + { + ResourceReference reference = new PackageResourceReference(scope, "resource.txt", + locales[1], styles[1], variations[1]); + + ByteArrayResponse byteResponse = new ByteArrayResponse(); + + Request request = tester.getRequestCycle().getRequest(); + MockHttpServletRequest mockHttpServletRequest = (MockHttpServletRequest)request.getContainerRequest(); + mockHttpServletRequest.setHeader("range", range); + Attributes mockAttributes = new Attributes(request, byteResponse); + reference.getResource().respond(mockAttributes); + return new String(byteResponse.getBytes()); + } + + /** + * See WICKET-5819 - Media tags + */ + @Test + public void testContentRangeHeaders() + { + // Test header fields + ResourceReference reference = new PackageResourceReference(scope, "resource.txt", + locales[1], styles[1], variations[1]); + Request request = tester.getRequestCycle().getRequest(); + Response response = tester.getRequestCycle().getResponse(); + MockHttpServletResponse mockHttpServletResponse = (MockHttpServletResponse)response.getContainerResponse(); + Attributes mockAttributes = new Attributes(request, response); + reference.getResource().respond(mockAttributes); + Assert.assertEquals(ContentRangeType.BYTES.getTypeName(), + mockHttpServletResponse.getHeader("Accept-Range")); + // For normal: If a resource supports content range no content is delivered + // if no "Range" header is given, but we have to deliver it, because + // other resources then media should get the content. (e.g. CSS, JS, etc.) Browsers + // detecting media requests and automatically add the "Range" header for + // partial content and they don't make an initial request to detect if a media + // resource supports Content-Range (by the Accept-Range header) + Assert.assertEquals("resource_var_style_en.txt", + new String(mockHttpServletResponse.getBinaryContent())); + } + + }