Example usage for org.springframework.http HttpStatus REQUESTED_RANGE_NOT_SATISFIABLE

List of usage examples for org.springframework.http HttpStatus REQUESTED_RANGE_NOT_SATISFIABLE

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus REQUESTED_RANGE_NOT_SATISFIABLE.

Prototype

HttpStatus REQUESTED_RANGE_NOT_SATISFIABLE

To view the source code for org.springframework.http HttpStatus REQUESTED_RANGE_NOT_SATISFIABLE.

Click Source Link

Document

416 Requested Range Not Satisfiable .

Usage

From source file:org.eclipse.cft.server.core.internal.CloudErrorUtil.java

public static boolean isRequestedFileRangeNotSatisfiable(CloudFoundryException cfe) {
    return cfe != null && HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.equals(cfe.getStatusCode());
}

From source file:org.cloudfoundry.client.lib.rest.CloudControllerClientImpl.java

private String doGetFileByRange(String urlPath, Object app, String instance, String filePath, int start,
        int end, String range) {

    boolean supportsRanges;
    try {//from   w ww . ja  va2  s .co  m
        supportsRanges = getRestTemplate().execute(getUrl(urlPath), HttpMethod.HEAD, new RequestCallback() {
            public void doWithRequest(ClientHttpRequest request) throws IOException {
                request.getHeaders().set("Range", "bytes=0-");
            }
        }, new ResponseExtractor<Boolean>() {
            public Boolean extractData(ClientHttpResponse response) throws IOException {
                return response.getStatusCode().equals(HttpStatus.PARTIAL_CONTENT);
            }
        }, app, instance, filePath);
    } catch (CloudFoundryException e) {
        if (e.getStatusCode().equals(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE)) {
            // must be a 0 byte file
            return "";
        } else {
            throw e;
        }
    }
    HttpHeaders headers = new HttpHeaders();
    if (supportsRanges) {
        headers.set("Range", range);
    }
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
    ResponseEntity<String> responseEntity = getRestTemplate().exchange(getUrl(urlPath), HttpMethod.GET,
            requestEntity, String.class, app, instance, filePath);
    String response = responseEntity.getBody();
    boolean partialFile = false;
    if (responseEntity.getStatusCode().equals(HttpStatus.PARTIAL_CONTENT)) {
        partialFile = true;
    }
    if (!partialFile && response != null) {
        if (start == -1) {
            return response.substring(response.length() - end);
        } else {
            if (start >= response.length()) {
                if (response.length() == 0) {
                    return "";
                }
                throw new CloudFoundryException(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE,
                        "The starting position " + start + " is past the end of the file content.");
            }
            if (end != -1) {
                if (end >= response.length()) {
                    end = response.length() - 1;
                }
                return response.substring(start, end + 1);
            } else {
                return response.substring(start);
            }
        }
    }
    return response;
}

From source file:org.springframework.http.codec.ResourceHttpMessageWriter.java

@Override
@SuppressWarnings("unchecked")
public Mono<Void> write(Publisher<? extends Resource> inputStream, @Nullable ResolvableType actualType,
        ResolvableType elementType, @Nullable MediaType mediaType, ServerHttpRequest request,
        ServerHttpResponse response, Map<String, Object> hints) {

    HttpHeaders headers = response.getHeaders();
    headers.set(HttpHeaders.ACCEPT_RANGES, "bytes");

    List<HttpRange> ranges;
    try {/*from w w w .  jav  a 2s. c om*/
        ranges = request.getHeaders().getRange();
    } catch (IllegalArgumentException ex) {
        response.setStatusCode(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE);
        return response.setComplete();
    }

    return Mono.from(inputStream).flatMap(resource -> {

        if (ranges.isEmpty()) {
            return writeResource(resource, elementType, mediaType, response, hints);
        }

        response.setStatusCode(HttpStatus.PARTIAL_CONTENT);
        List<ResourceRegion> regions = HttpRange.toResourceRegions(ranges, resource);
        MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints);

        if (regions.size() == 1) {
            ResourceRegion region = regions.get(0);
            headers.setContentType(resourceMediaType);
            long contentLength = lengthOf(resource);
            if (contentLength != -1) {
                long start = region.getPosition();
                long end = start + region.getCount() - 1;
                end = Math.min(end, contentLength - 1);
                headers.add("Content-Range", "bytes " + start + '-' + end + '/' + contentLength);
                headers.setContentLength(end - start + 1);
            }
            return writeSingleRegion(region, response, hints);
        } else {
            String boundary = MimeTypeUtils.generateMultipartBoundaryString();
            MediaType multipartType = MediaType.parseMediaType("multipart/byteranges;boundary=" + boundary);
            headers.setContentType(multipartType);
            Map<String, Object> allHints = Hints.merge(hints, ResourceRegionEncoder.BOUNDARY_STRING_HINT,
                    boundary);
            return encodeAndWriteRegions(Flux.fromIterable(regions), resourceMediaType, response, allHints);
        }
    });
}