Example usage for org.springframework.http HttpHeaders getContentLength

List of usage examples for org.springframework.http HttpHeaders getContentLength

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders getContentLength.

Prototype

public long getContentLength() 

Source Link

Document

Return the length of the body in bytes, as specified by the Content-Length header.

Usage

From source file:ar.com.aleatoria.ue.rest.AbstractBufferingClientHttpRequest.java

@Override
protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
    byte[] bytes = this.bufferedOutput.toByteArray();
    if (headers.getContentLength() == -1) {
        headers.setContentLength(bytes.length);
    }//from   www  .  j a  v  a2s  . c o  m
    ClientHttpResponse result = executeInternal(headers, bytes);
    this.bufferedOutput = null;
    return result;
}

From source file:grails.plugin.cloudfoundry.GrailsHttpRequest.java

protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
    byte[] bytes = bufferedOutput.toByteArray();
    if (headers.getContentLength() == -1) {
        headers.setContentLength(bytes.length);
    }//from   w ww. java  2 s  .c om
    ClientHttpResponse result = executeInternal(headers, bytes);
    bufferedOutput = null;
    return result;
}

From source file:org.aksw.gerbil.datasets.datahub.DatahubNIFLoader.java

private void getNIFDataSetsMetaInformation(List<String> nifDataSets) {
    datasets = Maps.newHashMap();//from  ww w .jav  a  2 s  . c om
    String nifCorpusMetaInfURL = GerbilConfiguration.getInstance()
            .getString(DATAHUB_NIF_CORPUS_META_INF_URL_PROPERTY_NAME);
    if (nifCorpusMetaInfURL == null) {
        LOGGER.error("Couldn't load the needed property \"{}\". Aborting.",
                DATAHUB_NIF_CORPUS_META_INF_URL_PROPERTY_NAME);
        return;
    }
    // go through all datasets tagged with nif
    for (String d : nifDataSets) {
        ResponseEntity<Dataset.Response> entity = rt.getForEntity(nifCorpusMetaInfURL + d,
                Dataset.Response.class);
        if (entity.getStatusCode().equals(HttpStatus.OK)) {
            Dataset.Response body = entity.getBody();
            List<Resource> resources = body.getResult().getResources();
            // go through the downloadable Resources
            for (Resource r : resources) {
                String url = r.getUrl();
                LOGGER.debug("checking {}", url);
                HttpHeaders headers = rt.headForHeaders(url);
                long contentLength = headers.getContentLength();
                LOGGER.debug("{} bytes", contentLength);
                // FIXME - put the magic number in application.properties
                // add if less than 20mb ends with ttl (turtle) but not with dataid.ttl (we aint gonna need it yet)
                if (contentLength < 20_000_000 && url.endsWith(".ttl") && !url.endsWith("dataid.ttl")) {
                    LOGGER.debug("{}: {} has less than 20mb and is turtle > add to Dataset", d, url);
                    datasets.put(d, url);
                }
            }
        }
    }

}

From source file:org.meteogrou.jbrotli.servlet.HelloBrotliHttpControllerTest.java

@Test
public void content_length_is_NOT_set__OR_is_zero() throws Exception {
    // given/*ww w. j a  v  a  2 s  .  c o m*/
    String textFileUrl = root_url + "/canterbury-corpus/asyoulik.txt";

    // when
    restTemplate.setInterceptors(createAcceptBrotliEncodingInterceptor());
    HttpHeaders headers = restTemplate.getForEntity(textFileUrl, String.class).getHeaders();

    // then
    assertThat(headers.getContentLength()).describedAs("Content length should be unknown").isEqualTo(-1L);
}

From source file:com.iflytek.edu.cloud.frame.spring.RequestResponseBodyMethodProcessorExt.java

private void write(final String content, MediaType contentType, ServletServerHttpResponse outputMessage)
        throws IOException, HttpMessageNotWritableException {
    final HttpHeaders headers = outputMessage.getHeaders();
    headers.setContentType(contentType);
    if (headers.getContentLength() == -1) {
        Long contentLength = getContentLength(content, headers.getContentType());
        if (contentLength != null) {
            headers.setContentLength(contentLength);
        }/*from ww  w  .ja  v a2 s  . co m*/
    }

    StreamUtils.copy(content, charset, outputMessage.getBody());
    outputMessage.getBody().flush();
}

From source file:org.greencheek.spring.rest.SSLCachingHttpComponentsClientHttpRequest.java

protected ClientHttpResponse executeInternal(HttpHeaders headers) throws IOException {
    byte[] bytes = this.bufferedOutput.toByteArray();
    if (headers.getContentLength() == -1) {
        headers.setContentLength(bytes.length);
    }/*from w w  w .  ja v  a 2 s  .c  o  m*/
    ClientHttpResponse result = executeInternal(headers, bytes);
    this.bufferedOutput = null;
    return result;
}

From source file:ar.com.aleatoria.ue.rest.SimpleStreamingClientHttpRequest.java

@Override
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
    if (this.body == null) {
        int contentLength = (int) headers.getContentLength();
        if (contentLength >= 0 && !olderThanFroyo) {
            this.connection.setFixedLengthStreamingMode(contentLength);
        } else {/*from   w  ww  .j  av a2s  .  c  o  m*/
            this.connection.setChunkedStreamingMode(this.chunkSize);
        }
        writeHeaders(headers);
        this.connection.connect();
        this.body = this.connection.getOutputStream();
    }
    return new NonClosingOutputStream(this.body);
}

From source file:cz.jirutka.spring.http.client.cache.DefaultCachingPolicy.java

protected boolean isResponseCacheable(ClientHttpResponse response) {

    boolean cacheable = false;
    HttpHeaders headers = response.getHeaders();

    try {/*from   www  .  ja  v  a 2  s .c o  m*/
        int status = response.getRawStatusCode();
        if (isImplicitlyCacheableStatus(status)) {
            cacheable = true; //MAY be cached

        } else if (isUncacheableStatus(status)) {
            log.trace("Response with status code {} is not cacheable", status);
            return false;
        }
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }

    if (isExplicitlyNonCacheable(response)) {
        log.trace("Response with Cache-Control: '{}' is not cacheable", headers.getCacheControl());
        return false;
    }

    if (headers.getContentLength() > maxBodySizeBytes) {
        log.debug("Response with Content-Lenght {} > {} is not cacheable", headers.getContentLength(),
                maxBodySizeBytes);
        return false;
    }

    try {
        if (response.getHeaders().getDate() < 0) {
            log.debug("Response without a valid Date header is not cacheable");
            return false;
        }
    } catch (IllegalArgumentException ex) {
        return false;
    }

    // dunno how to properly handle Vary
    if (headers.containsKey("Vary")) {
        log.trace("Response with Vary header is not cacheable");
        return false;
    }

    return (cacheable || isExplicitlyCacheable(response));
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaController.java

protected HttpHeaders getResponseHeaders(HttpHeaders headers) {
    // Some of the headers coming back are poisonous apparently
    // (content-length?)...
    HttpHeaders outgoingHeaders = new HttpHeaders();
    outgoingHeaders.putAll(headers);//w w  w.j  a  va  2 s  .  c o  m
    if (headers.getContentLength() >= 0) {
        outgoingHeaders.remove(CONTENT_LENGTH);
        outgoingHeaders.remove(CONTENT_LENGTH.toLowerCase());
    }
    if (headers.containsKey(TRANSFER_ENCODING)) {
        outgoingHeaders.remove(TRANSFER_ENCODING);
        outgoingHeaders.remove(TRANSFER_ENCODING.toLowerCase());
    }
    return outgoingHeaders;
}

From source file:org.springframework.cloud.stream.app.http.source.DefaultMixedCaseContentTypeHttpHeaderMapper.java

private Object getHttpHeader(HttpHeaders source, String name) {
    if (ACCEPT.equalsIgnoreCase(name)) {
        return source.getAccept();
    } else if (ACCEPT_CHARSET.equalsIgnoreCase(name)) {
        return source.getAcceptCharset();
    } else if (ALLOW.equalsIgnoreCase(name)) {
        return source.getAllow();
    } else if (CACHE_CONTROL.equalsIgnoreCase(name)) {
        String cacheControl = source.getCacheControl();
        return (StringUtils.hasText(cacheControl)) ? cacheControl : null;
    } else if (CONTENT_LENGTH.equalsIgnoreCase(name)) {
        long contentLength = source.getContentLength();
        return (contentLength > -1) ? contentLength : null;
    } else if (CONTENT_TYPE.equalsIgnoreCase(name)) {
        return source.getContentType();
    } else if (DATE.equalsIgnoreCase(name)) {
        long date = source.getDate();
        return (date > -1) ? date : null;
    } else if (ETAG.equalsIgnoreCase(name)) {
        String eTag = source.getETag();
        return (StringUtils.hasText(eTag)) ? eTag : null;
    } else if (EXPIRES.equalsIgnoreCase(name)) {
        try {/*from   w w  w .  ja va 2 s. co m*/
            long expires = source.getExpires();
            return (expires > -1) ? expires : null;
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug(e.getMessage());
            }
            // According to RFC 2616
            return null;
        }
    } else if (IF_NONE_MATCH.equalsIgnoreCase(name)) {
        return source.getIfNoneMatch();
    } else if (IF_MODIFIED_SINCE.equalsIgnoreCase(name)) {
        long modifiedSince = source.getIfModifiedSince();
        return (modifiedSince > -1) ? modifiedSince : null;
    } else if (IF_UNMODIFIED_SINCE.equalsIgnoreCase(name)) {
        String unmodifiedSince = source.getFirst(IF_UNMODIFIED_SINCE);
        return unmodifiedSince != null ? this.getFirstDate(unmodifiedSince, IF_UNMODIFIED_SINCE) : null;
    } else if (LAST_MODIFIED.equalsIgnoreCase(name)) {
        long lastModified = source.getLastModified();
        return (lastModified > -1) ? lastModified : null;
    } else if (LOCATION.equalsIgnoreCase(name)) {
        return source.getLocation();
    } else if (PRAGMA.equalsIgnoreCase(name)) {
        String pragma = source.getPragma();
        return (StringUtils.hasText(pragma)) ? pragma : null;
    }
    return source.get(name);
}