@Test public void testHandle() throws Exception { Request request = createGetRequestBuilder().header(RANGE, RANGE_HEADER).build(); Response response = createOkResponseBuilder() .header(ETAG, ETAG_VALUE).header(LAST_MODIFIED, LAST_MODIFIED_VALUE).payload(PAYLOAD).build(); when(rangeParser.parseRangeSpec(RANGE_HEADER, PAYLOAD.getSize())).thenReturn(singletonList(ZERO_TO_TWO_RANGE)); verifyHandleReturnsPartialResponse(request, response); }
@Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final Response response = context.proceed(); if (!HttpMethods.GET.equals(context.getRequest().getAction())) { return response; } if (response.getStatus().getCode() != HttpStatus.OK) { return response; } final Payload payload = response.getPayload(); if (payload == null) { return response; } if (payload.getSize() == Payload.UNKNOWN_SIZE) { return response; } final String rangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.RANGE); if (rangeHeader == null) { return response; } final List<Range<Long>> ranges = rangeParser.parseRangeSpec(rangeHeader, payload.getSize()); if (ranges == null) { return HttpResponses.rangeNotSatisfiable(payload.getSize()); } if (ranges.isEmpty()) { return response; } if (ranges.size() > 1) { return HttpResponses.notImplemented("Multiple ranges not supported."); } final String ifRangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.IF_RANGE); if (ifRangeHeader != null && !ifRangeHeaderMatches(response, ifRangeHeader)) { return response; } Range<Long> requestedRange = ranges.get(0); return partialResponse(response, payload, requestedRange); }
PartialFetchHandler implements Handler { @Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final Response response = context.proceed(); if (!HttpMethods.GET.equals(context.getRequest().getAction())) { return response; } if (response.getStatus().getCode() != HttpStatus.OK) { return response; } final Payload payload = response.getPayload(); if (payload == null) { return response; } if (payload.getSize() == Payload.UNKNOWN_SIZE) { return response; } final String rangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.RANGE); if (rangeHeader == null) { return response; } final List<Range<Long>> ranges = rangeParser.parseRangeSpec(rangeHeader, payload.getSize()); if (ranges == null) { return HttpResponses.rangeNotSatisfiable(payload.getSize()); } if (ranges.isEmpty()) { return response; } if (ranges.size() > 1) { return HttpResponses.notImplemented("Multiple ranges not supported."); } final String ifRangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.IF_RANGE); if (ifRangeHeader != null && !ifRangeHeaderMatches(response, ifRangeHeader)) { return response; } Range<Long> requestedRange = ranges.get(0); return partialResponse(response, payload, requestedRange); } }
PartialFetchHandler implements Handler { @Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final Response response = context.proceed(); if (!HttpMethods.GET.equals(context.getRequest().getAction())) { return response; } if (response.getStatus().getCode() != HttpStatus.OK) { return response; } final Payload payload = response.getPayload(); if (payload == null) { return response; } if (payload.getSize() == Payload.UNKNOWN_SIZE) { return response; } final String rangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.RANGE); if (rangeHeader == null) { return response; } final List<Range<Long>> ranges = rangeParser.parseRangeSpec(rangeHeader, payload.getSize()); if (ranges == null) { return HttpResponses.rangeNotSatisfiable(payload.getSize()); } if (ranges.isEmpty()) { return response; } if (ranges.size() > 1) { return HttpResponses.notImplemented("Multiple ranges not supported."); } final String ifRangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.IF_RANGE); if (ifRangeHeader != null && !ifRangeHeaderMatches(response, ifRangeHeader)) { return response; } Range<Long> requestedRange = ranges.get(0); return partialResponse(response, payload, requestedRange); } @Inject  PartialFetchHandler(final RangeParser rangeParser); }
PartialFetchHandler implements Handler { @Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final Response response = context.proceed(); if (!HttpMethods.GET.equals(context.getRequest().getAction())) { return response; } if (response.getStatus().getCode() != HttpStatus.OK) { return response; } final Payload payload = response.getPayload(); if (payload == null) { return response; } if (payload.getSize() == Payload.UNKNOWN_SIZE) { return response; } final String rangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.RANGE); if (rangeHeader == null) { return response; } final List<Range<Long>> ranges = rangeParser.parseRangeSpec(rangeHeader, payload.getSize()); if (ranges == null) { return HttpResponses.rangeNotSatisfiable(payload.getSize()); } if (ranges.isEmpty()) { return response; } if (ranges.size() > 1) { return HttpResponses.notImplemented("Multiple ranges not supported."); } final String ifRangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.IF_RANGE); if (ifRangeHeader != null && !ifRangeHeaderMatches(response, ifRangeHeader)) { return response; } Range<Long> requestedRange = ranges.get(0); return partialResponse(response, payload, requestedRange); } @Inject  PartialFetchHandler(final RangeParser rangeParser); @Nonnull @Override Response handle(@Nonnull final Context context); }
PartialFetchHandler implements Handler { @Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final Response response = context.proceed(); if (!HttpMethods.GET.equals(context.getRequest().getAction())) { return response; } if (response.getStatus().getCode() != HttpStatus.OK) { return response; } final Payload payload = response.getPayload(); if (payload == null) { return response; } if (payload.getSize() == Payload.UNKNOWN_SIZE) { return response; } final String rangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.RANGE); if (rangeHeader == null) { return response; } final List<Range<Long>> ranges = rangeParser.parseRangeSpec(rangeHeader, payload.getSize()); if (ranges == null) { return HttpResponses.rangeNotSatisfiable(payload.getSize()); } if (ranges.isEmpty()) { return response; } if (ranges.size() > 1) { return HttpResponses.notImplemented("Multiple ranges not supported."); } final String ifRangeHeader = getHeaderValue(context.getRequest(), HttpHeaders.IF_RANGE); if (ifRangeHeader != null && !ifRangeHeaderMatches(response, ifRangeHeader)) { return response; } Range<Long> requestedRange = ranges.get(0); return partialResponse(response, payload, requestedRange); } @Inject  PartialFetchHandler(final RangeParser rangeParser); @Nonnull @Override Response handle(@Nonnull final Context context);  }