org.springframework.http.codec.DecoderHttpMessageReader Maven / Gradle / Ivy
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.http.codec;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.AbstractDecoder;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Hints;
import org.springframework.http.HttpLogging;
import org.springframework.http.HttpMessage;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* {@code HttpMessageReader} that wraps and delegates to a {@link Decoder}.
*
* Also a {@code HttpMessageReader} that pre-resolves decoding hints
* from the extra information available on the server side such as the request
* or controller method parameter annotations.
*
* @author Arjen Poutsma
* @author Sebastien Deleuze
* @author Rossen Stoyanchev
* @since 5.0
* @param the type of objects in the decoded output stream
*/
public class DecoderHttpMessageReader implements HttpMessageReader {
private final Decoder decoder;
private final List mediaTypes;
/**
* Create an instance wrapping the given {@link Decoder}.
*/
public DecoderHttpMessageReader(Decoder decoder) {
Assert.notNull(decoder, "Decoder is required");
initLogger(decoder);
this.decoder = decoder;
this.mediaTypes = MediaType.asMediaTypes(decoder.getDecodableMimeTypes());
}
private static void initLogger(Decoder> decoder) {
if (decoder instanceof AbstractDecoder &&
decoder.getClass().getName().startsWith("org.springframework.core.codec")) {
Log logger = HttpLogging.forLog(((AbstractDecoder>) decoder).getLogger());
((AbstractDecoder>) decoder).setLogger(logger);
}
}
/**
* Return the {@link Decoder} of this reader.
*/
public Decoder getDecoder() {
return this.decoder;
}
@Override
public List getReadableMediaTypes() {
return this.mediaTypes;
}
@Override
public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType) {
return this.decoder.canDecode(elementType, mediaType);
}
@Override
public Flux read(ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) {
MediaType contentType = getContentType(message);
return this.decoder.decode(message.getBody(), elementType, contentType, hints);
}
@Override
public Mono readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map hints) {
MediaType contentType = getContentType(message);
return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints);
}
/**
* Determine the Content-Type of the HTTP message based on the
* "Content-Type" header or otherwise default to
* {@link MediaType#APPLICATION_OCTET_STREAM}.
* @param inputMessage the HTTP message
* @return the MediaType, possibly {@code null}.
*/
@Nullable
protected MediaType getContentType(HttpMessage inputMessage) {
MediaType contentType = inputMessage.getHeaders().getContentType();
return (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
}
// Server-side only...
@Override
public Flux read(ResolvableType actualType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response, Map hints) {
Map allHints = Hints.merge(hints,
getReadHints(actualType, elementType, request, response));
return read(elementType, request, allHints);
}
@Override
public Mono readMono(ResolvableType actualType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response, Map hints) {
Map allHints = Hints.merge(hints,
getReadHints(actualType, elementType, request, response));
return readMono(elementType, request, allHints);
}
/**
* Get additional hints for decoding for example based on the server request
* or annotations from controller method parameters. By default, delegate to
* the decoder if it is an instance of {@link HttpMessageDecoder}.
*/
protected Map getReadHints(ResolvableType actualType,
ResolvableType elementType, ServerHttpRequest request, ServerHttpResponse response) {
if (this.decoder instanceof HttpMessageDecoder) {
HttpMessageDecoder> decoder = (HttpMessageDecoder>) this.decoder;
return decoder.getDecodeHints(actualType, elementType, request, response);
}
return Hints.none();
}
}