Example usage for org.springframework.http HttpStatus PARTIAL_CONTENT

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

Introduction

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

Prototype

HttpStatus PARTIAL_CONTENT

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

Click Source Link

Document

206 Partial Content .

Usage

From source file:br.com.uol.runas.controller.JUnitController.java

private ResponseEntity<String> runJUnit(String path, String[] suites) throws Exception {
    final JUnitServiceResponse response = jUnitService.runTests(path, suites);

    HttpStatus status;//from   w  w  w  .  ja v  a  2 s  .  c  om

    if (response.getResult().wasSuccessful() && response.getResult().getIgnoreCount() == 0) {
        status = HttpStatus.OK;
    } else if (response.getResult().getFailureCount() > 0) {
        status = HttpStatus.EXPECTATION_FAILED;
    } else {
        status = HttpStatus.PARTIAL_CONTENT;
    }

    return ResponseEntity.status(status).contentType(response.getMediaType()).body(response.getLog());
}

From source file:fr.olympicinsa.riocognized.exception.MyExceptionHandler.java

@ExceptionHandler(NullPointerException.class)
@ResponseBody/*from  ww  w  . j a  v a2s.com*/
@ResponseStatus(HttpStatus.PARTIAL_CONTENT)
public ErrorMessage handleNullPointerException(NullPointerException e, HttpServletRequest req) {
    return new ErrorMessage("ENTITY_HAS_UNALLOWED_NULL_VALUE");
}

From source file:org.kurento.repository.test.RangeGetTests.java

@Test
public void test() throws Exception {

    String id = "logo.png";

    RepositoryItem item;//from  ww w  . j  a  v a  2  s.  c  o m
    try {
        item = getRepository().findRepositoryItemById(id);
    } catch (NoSuchElementException e) {
        item = getRepository().createRepositoryItem(id);
        uploadFile(new File("test-files/" + id), item);
    }

    RepositoryHttpPlayer player = item.createRepositoryHttpPlayer();

    String url = player.getURL();

    player.setAutoTerminationTimeout(100000);

    // Following sample
    // http://stackoverflow.com/questions/8293687/sample-http-range-request-session

    RestTemplate httpClient = getRestTemplate();

    {
        HttpHeaders requestHeaders = new HttpHeaders();

        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                postParameters, requestHeaders);

        ResponseEntity<byte[]> response = httpClient.exchange(url, HttpMethod.GET, requestEntity, byte[].class);

        System.out.println(response);

        assertTrue("The server doesn't accept ranges", response.getHeaders().containsKey("Accept-ranges"));
        assertTrue("The server doesn't accept ranges with bytes",
                response.getHeaders().get("Accept-ranges").contains("bytes"));
    }

    long fileLength = 0;

    {
        // Range: bytes=0-

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("Range", "bytes=0-");

        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                postParameters, requestHeaders);

        ResponseEntity<byte[]> response = httpClient.exchange(url, HttpMethod.GET, requestEntity, byte[].class);

        System.out.println(response);

        assertEquals("The server doesn't respond with http status code 206 to a request with ranges",
                HttpStatus.PARTIAL_CONTENT, response.getStatusCode());

        fileLength = Long.parseLong(response.getHeaders().get("Content-Length").get(0));

    }

    {
        HttpHeaders requestHeaders = new HttpHeaders();

        long firstByte = fileLength - 3000;
        long lastByte = fileLength - 1;
        long numBytes = lastByte - firstByte + 1;

        requestHeaders.set("Range", "bytes=" + firstByte + "-" + lastByte);

        MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();

        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
                postParameters, requestHeaders);

        ResponseEntity<byte[]> response = httpClient.exchange(url, HttpMethod.GET, requestEntity, byte[].class);

        System.out.println(response);

        assertEquals("The server doesn't respond with http status code 206 to a request with ranges",
                response.getStatusCode(), HttpStatus.PARTIAL_CONTENT);

        long responseContentLength = Long.parseLong(response.getHeaders().get("Content-Length").get(0));
        assertEquals("The server doesn't send the requested bytes", numBytes, responseContentLength);

        assertEquals("The server doesn't send the requested bytes", responseContentLength,
                response.getBody().length);

    }
}

From source file:bjerne.gallery.controller.GalleryController.java

/**
 * Method used to return the binary of a gallery file (
 * {@link GalleryFile#getActualFile()} ). This method handles 304 redirects
 * (if file has not changed) and range headers if requested by browser. The
 * range parts is particularly important for videos. The correct response
 * status is set depending on the circumstances.
 * <p>/*  w  ww .  j  a  v a2 s. c o  m*/
 * NOTE: the range logic should NOT be considered a complete implementation
 * - it's a bare minimum for making requests for byte ranges work.
 * 
 * @param request
 *            Request
 * @param galleryFile
 *            Gallery file
 * @return The binary of the gallery file, or a 304 redirect, or a part of
 *         the file.
 * @throws IOException
 *             If there is an issue accessing the binary file.
 */
private ResponseEntity<InputStreamResource> returnResource(WebRequest request, GalleryFile galleryFile)
        throws IOException {
    LOG.debug("Entering returnResource()");
    if (request.checkNotModified(galleryFile.getActualFile().lastModified())) {
        return null;
    }
    File file = galleryFile.getActualFile();
    String contentType = galleryFile.getContentType();
    String rangeHeader = request.getHeader(HttpHeaders.RANGE);
    long[] ranges = getRangesFromHeader(rangeHeader);
    long startPosition = ranges[0];
    long fileTotalSize = file.length();
    long endPosition = ranges[1] != 0 ? ranges[1] : fileTotalSize - 1;
    long contentLength = endPosition - startPosition + 1;
    LOG.debug("contentLength: {}, file length: {}", contentLength, fileTotalSize);

    LOG.debug("Returning resource {} as inputstream. Start position: {}", file.getCanonicalPath(),
            startPosition);
    InputStream boundedInputStream = new BoundedInputStream(new FileInputStream(file), endPosition + 1);

    InputStream is = new BufferedInputStream(boundedInputStream, 65536);
    InputStreamResource inputStreamResource = new InputStreamResource(is);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentLength(contentLength);
    responseHeaders.setContentType(MediaType.valueOf(contentType));
    responseHeaders.add(HttpHeaders.ACCEPT_RANGES, "bytes");
    if (StringUtils.isNotBlank(rangeHeader)) {
        is.skip(startPosition);
        String contentRangeResponseHeader = "bytes " + startPosition + "-" + endPosition + "/" + fileTotalSize;
        responseHeaders.add(HttpHeaders.CONTENT_RANGE, contentRangeResponseHeader);
        LOG.debug("{} was not null but {}. Adding header {} to response: {}", HttpHeaders.RANGE, rangeHeader,
                HttpHeaders.CONTENT_RANGE, contentRangeResponseHeader);
    }
    HttpStatus status = (startPosition == 0 && contentLength == fileTotalSize) ? HttpStatus.OK
            : HttpStatus.PARTIAL_CONTENT;
    LOG.debug("Returning {}. Status: {}, content-type: {}, {}: {}, contentLength: {}", file, status,
            contentType, HttpHeaders.CONTENT_RANGE, responseHeaders.get(HttpHeaders.CONTENT_RANGE),
            contentLength);
    return new ResponseEntity<InputStreamResource>(inputStreamResource, responseHeaders, status);
}

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  v  a 2  s.c o 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 .j  a va2s  .  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);
        }
    });
}