@ExtendWith({ RepositoryManagementTestExecutionListener.class, ArtifactManagementTestExecutionListener.class }) @Test void handlePartialDownloadWithMultipleRanges(@MavenRepository(repositoryId = REPOSITORY_RELEASES_4) Repository repository, @MavenTestArtifact(repositoryId = REPOSITORY_RELEASES_4, id = "org.carlspring.strongbox:partial-multiple", versions = "1.0") Path artifactPath) throws IOException { RepositoryPath artifactRepositoryPath = (RepositoryPath) artifactPath.normalize(); try(InputStream is = artifactResolutionService.getInputStream(artifactRepositoryPath)) { HttpHeaders httpHeaders = getHttpHeaders("0-500,510-519"); HttpServletResponse response = new MockHttpServletResponse(); response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(Files.size(artifactRepositoryPath))); ArtifactControllerHelper.handlePartialDownload(is, httpHeaders, response); assertThat(response.getStatus()).isEqualTo(HttpStatus.PARTIAL_CONTENT.value()); assertThat(response.getContentType()).isEqualTo("multipart/byteranges; boundary=" + MULTIPART_BOUNDARY); } }
private static void handlePartialDownloadWithMultipleRanges(InputStream is, List<ByteRange> byteRanges, HttpServletResponse response) throws IOException { ByteRangeInputStream bris = StreamUtils.findSource(ByteRangeInputStream.class, is); long length = bris != null ? StreamUtils.getLength(bris) : 0; boolean anyByteRangeNotSatisfiable = byteRanges.stream() .anyMatch(byteRange -> byteRange.getOffset() >= length); if (anyByteRangeNotSatisfiable) { setRangeNotSatisfiable(response, length); } else { final String rangesContentType = response.getContentType(); prepareResponseBuilderForPartialRequestWithMultipleRanges(response); copyPartialMultipleRangeToResponse(is, response, byteRanges, rangesContentType); } }
ArtifactControllerHelper { private static void handlePartialDownloadWithMultipleRanges(InputStream is, List<ByteRange> byteRanges, HttpServletResponse response) throws IOException { ByteRangeInputStream bris = StreamUtils.findSource(ByteRangeInputStream.class, is); long length = bris != null ? StreamUtils.getLength(bris) : 0; boolean anyByteRangeNotSatisfiable = byteRanges.stream() .anyMatch(byteRange -> byteRange.getOffset() >= length); if (anyByteRangeNotSatisfiable) { setRangeNotSatisfiable(response, length); } else { final String rangesContentType = response.getContentType(); prepareResponseBuilderForPartialRequestWithMultipleRanges(response); copyPartialMultipleRangeToResponse(is, response, byteRanges, rangesContentType); } } }
ArtifactControllerHelper { private static void handlePartialDownloadWithMultipleRanges(InputStream is, List<ByteRange> byteRanges, HttpServletResponse response) throws IOException { ByteRangeInputStream bris = StreamUtils.findSource(ByteRangeInputStream.class, is); long length = bris != null ? StreamUtils.getLength(bris) : 0; boolean anyByteRangeNotSatisfiable = byteRanges.stream() .anyMatch(byteRange -> byteRange.getOffset() >= length); if (anyByteRangeNotSatisfiable) { setRangeNotSatisfiable(response, length); } else { final String rangesContentType = response.getContentType(); prepareResponseBuilderForPartialRequestWithMultipleRanges(response); copyPartialMultipleRangeToResponse(is, response, byteRanges, rangesContentType); } } private  ArtifactControllerHelper(); }
ArtifactControllerHelper { private static void handlePartialDownloadWithMultipleRanges(InputStream is, List<ByteRange> byteRanges, HttpServletResponse response) throws IOException { ByteRangeInputStream bris = StreamUtils.findSource(ByteRangeInputStream.class, is); long length = bris != null ? StreamUtils.getLength(bris) : 0; boolean anyByteRangeNotSatisfiable = byteRanges.stream() .anyMatch(byteRange -> byteRange.getOffset() >= length); if (anyByteRangeNotSatisfiable) { setRangeNotSatisfiable(response, length); } else { final String rangesContentType = response.getContentType(); prepareResponseBuilderForPartialRequestWithMultipleRanges(response); copyPartialMultipleRangeToResponse(is, response, byteRanges, rangesContentType); } } private  ArtifactControllerHelper(); static void handlePartialDownload(InputStream is,
                                             HttpHeaders headers,
                                             HttpServletResponse response); static boolean isRangedRequest(HttpHeaders headers); static void provideArtifactHeaders(HttpServletResponse response,
                                              RepositoryPath path); }
ArtifactControllerHelper { private static void handlePartialDownloadWithMultipleRanges(InputStream is, List<ByteRange> byteRanges, HttpServletResponse response) throws IOException { ByteRangeInputStream bris = StreamUtils.findSource(ByteRangeInputStream.class, is); long length = bris != null ? StreamUtils.getLength(bris) : 0; boolean anyByteRangeNotSatisfiable = byteRanges.stream() .anyMatch(byteRange -> byteRange.getOffset() >= length); if (anyByteRangeNotSatisfiable) { setRangeNotSatisfiable(response, length); } else { final String rangesContentType = response.getContentType(); prepareResponseBuilderForPartialRequestWithMultipleRanges(response); copyPartialMultipleRangeToResponse(is, response, byteRanges, rangesContentType); } } private  ArtifactControllerHelper(); static void handlePartialDownload(InputStream is,
                                             HttpHeaders headers,
                                             HttpServletResponse response); static boolean isRangedRequest(HttpHeaders headers); static void provideArtifactHeaders(HttpServletResponse response,
                                              RepositoryPath path); static final String MULTIPART_BOUNDARY; }