Support filename hint for client side too

Closes gh-25516
This commit is contained in:
Rossen Stoyanchev
2020-08-14 08:15:49 +01:00
parent 36553264ce
commit a7f71f4d9b
3 changed files with 96 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -96,13 +96,15 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
@Override
public Flux<T> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
MediaType contentType = getContentType(message);
return this.decoder.decode(message.getBody(), elementType, contentType, hints);
Map<String, Object> allHints = Hints.merge(hints, getReadHints(elementType, message));
return this.decoder.decode(message.getBody(), elementType, contentType, allHints);
}
@Override
public Mono<T> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
MediaType contentType = getContentType(message);
return this.decoder.decodeToMono(message.getBody(), elementType, contentType, hints);
Map<String, Object> allHints = Hints.merge(hints, getReadHints(elementType, message));
return this.decoder.decodeToMono(message.getBody(), elementType, contentType, allHints);
}
/**
@@ -118,6 +120,14 @@ public class DecoderHttpMessageReader<T> implements HttpMessageReader<T> {
return (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
}
/**
* Get additional hints for decoding based on the input HTTP message.
* @since 5.3
*/
protected Map<String, Object> getReadHints(ResolvableType elementType, ReactiveHttpInputMessage message) {
return Hints.none();
}
// Server-side only...

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -22,6 +22,7 @@ import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Hints;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.io.Resource;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.StringUtils;
@@ -45,12 +46,18 @@ public class ResourceHttpMessageReader extends DecoderHttpMessageReader<Resource
}
@Override
protected Map<String, Object> getReadHints(ResolvableType elementType, ReactiveHttpInputMessage message) {
String filename = message.getHeaders().getContentDisposition().getFilename();
return (StringUtils.hasText(filename) ?
Hints.from(ResourceDecoder.FILENAME_HINT, filename) : Hints.none());
}
@Override
protected Map<String, Object> getReadHints(ResolvableType actualType, ResolvableType elementType,
ServerHttpRequest request, ServerHttpResponse response) {
String name = request.getHeaders().getContentDisposition().getFilename();
return StringUtils.hasText(name) ? Hints.from(ResourceDecoder.FILENAME_HINT, name) : Hints.none();
return getReadHints(elementType, request);
}
}