List of usage examples for org.springframework.http HttpHeaders getContentLength
public long getContentLength()
From source file:org.springframework.http.codec.EncoderHttpMessageWriter.java
@SuppressWarnings("unchecked") @Override// www . j a v a2 s . c o m public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType elementType, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { MediaType contentType = updateContentType(message, mediaType); Flux<DataBuffer> body = this.encoder.encode(inputStream, message.bufferFactory(), elementType, contentType, hints); if (inputStream instanceof Mono) { HttpHeaders headers = message.getHeaders(); if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) { return Mono.from(body).flatMap(dataBuffer -> { headers.setContentLength(dataBuffer.readableByteCount()); return message.writeWith(Mono.just(dataBuffer)); }); } } return (isStreamingMediaType(contentType) ? message.writeAndFlushWith(body.map(Flux::just)) : message.writeWith(body)); }
From source file:org.springframework.http.codec.ResourceHttpMessageWriter.java
private Mono<Void> writeResource(Resource resource, ResolvableType type, @Nullable MediaType mediaType, ReactiveHttpOutputMessage message, Map<String, Object> hints) { HttpHeaders headers = message.getHeaders(); MediaType resourceMediaType = getResourceMediaType(mediaType, resource, hints); headers.setContentType(resourceMediaType); if (headers.getContentLength() < 0) { long length = lengthOf(resource); if (length != -1) { headers.setContentLength(length); }/* www.j a v a 2 s. c om*/ } return zeroCopy(resource, null, message, hints).orElseGet(() -> { Mono<Resource> input = Mono.just(resource); DataBufferFactory factory = message.bufferFactory(); Flux<DataBuffer> body = this.encoder.encode(input, factory, type, resourceMediaType, hints); return message.writeWith(body); }); }
From source file:org.springframework.http.converter.AbstractHttpMessageConverter.java
/** * Add default headers to the output message. * <p>This implementation delegates to {@link #getDefaultContentType(Object)} if a * content type was not provided, set if necessary the default character set, calls * {@link #getContentLength}, and sets the corresponding headers. * @since 4.2/*from w w w . ja v a 2s. c o m*/ */ protected void addDefaultHeaders(HttpHeaders headers, T t, @Nullable MediaType contentType) throws IOException { if (headers.getContentType() == null) { MediaType contentTypeToUse = contentType; if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype()) { contentTypeToUse = getDefaultContentType(t); } else if (MediaType.APPLICATION_OCTET_STREAM.equals(contentType)) { MediaType mediaType = getDefaultContentType(t); contentTypeToUse = (mediaType != null ? mediaType : contentTypeToUse); } if (contentTypeToUse != null) { if (contentTypeToUse.getCharset() == null) { Charset defaultCharset = getDefaultCharset(); if (defaultCharset != null) { contentTypeToUse = new MediaType(contentTypeToUse, defaultCharset); } } headers.setContentType(contentTypeToUse); } } if (headers.getContentLength() < 0 && !headers.containsKey(HttpHeaders.TRANSFER_ENCODING)) { Long contentLength = getContentLength(t, headers.getContentType()); if (contentLength != null) { headers.setContentLength(contentLength); } } }
From source file:org.springframework.http.server.reactive.ServletServerHttpRequest.java
private static HttpHeaders initHeaders(HttpServletRequest request) { HttpHeaders headers = new HttpHeaders(); for (Enumeration<?> names = request.getHeaderNames(); names.hasMoreElements();) { String name = (String) names.nextElement(); for (Enumeration<?> values = request.getHeaders(name); values.hasMoreElements();) { headers.add(name, (String) values.nextElement()); }/* w w w . ja v a 2 s .c o m*/ } MediaType contentType = headers.getContentType(); if (contentType == null) { String requestContentType = request.getContentType(); if (StringUtils.hasLength(requestContentType)) { contentType = MediaType.parseMediaType(requestContentType); headers.setContentType(contentType); } } if (contentType != null && contentType.getCharset() == null) { String encoding = request.getCharacterEncoding(); if (StringUtils.hasLength(encoding)) { Charset charset = Charset.forName(encoding); Map<String, String> params = new LinkedCaseInsensitiveMap<>(); params.putAll(contentType.getParameters()); params.put("charset", charset.toString()); headers.setContentType(new MediaType(contentType.getType(), contentType.getSubtype(), params)); } } if (headers.getContentLength() == -1) { int contentLength = request.getContentLength(); if (contentLength != -1) { headers.setContentLength(contentLength); } } return headers; }
From source file:org.springframework.integration.http.support.DefaultHttpHeaderMapper.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 {//w w w . j a v a2 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_UNMODIFIED_SINCE.equalsIgnoreCase(name)) { long unmodifiedSince = source.getIfNotModifiedSince(); return (unmodifiedSince > -1) ? unmodifiedSince : 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); }