上一篇章稍微研究了参数解析器(HandlerMethodArgumentResolver),本篇稍微研究返回值处理器(HandlerMethodReturnValueHandler)。返回值处理器主要用于根据目标方法的返回类型描述(比如参数类型/方法的注解),把返回值写入到各种http对象中(比如response/session);
1.1.类结构 1.2.功能说明(重点)名称以ReturnValueHandler结尾的是单一的返回值处理器,名称以MethodProcessor结尾的既是参数解析器,也是返回值处理器;
分类 | 名称 | 支持的类型 | 功能 |
---|---|---|---|
组合模式 | HandlerMethodReturnValueHandlerComposite 组合模式,调用其它返回值处理器 | 内部引用的返回值处理器支持的返回类型 | 返回值处理器 |
处理异步结果 | AsyncHandlerMethodReturnValueHandler 自定义异步返回值处理器接口 | 支持自定义的异步返回类型 | 返回值处理器 |
ResponseBodyEmitterReturnValueHandler 封装成DeferredResult,处理异步结果 | 1.ResponseBodyEmitter/SseEmitter(异步)返回类型 2.Reactive返回类型(Spring5引入的响应式编程) 3.ResponseEntity返回类型(泛型是以上2种类型) | 返回值处理器 | |
DeferredResultMethodReturnValueHandler 封装成DeferredResult,处理异步结果 | 1.DeferredResult返回类型 2.ListenableFuture返回类型 3.CompletionStage返回类型 | 返回值处理器 | |
AsyncTaskMethodReturnValueHandler 执行WebAsyncTask中的Callable异步任务,处理异步结果 | WebAsyncTask返回类型 | 返回值处理器 | |
StreamingResponseBodyReturnValueHandler 把StreamingResponseBody封装成Callable,然后执行Callable异步任务,处理异步结果 | 1.StreamingResponseBody返回类型 2.ResponseEntity返回类型(泛型是StreamingResponseBody) | 返回值处理器 | |
CallableMethodReturnValueHandler 执行Callable异步任务,处理异步结果 | Callable返回类型 | 返回值处理器 | |
写入httpHeader | HttpHeadersReturnValueHandler 写入到response的header | HttpHeaders返回类型 | 返回值处理器 |
写入springModel | MapMethodProcessor 写入model | 1.Map类型参数(没有注解) 2.Map返回类型 | 参数解析器&返回值处理器 |
ModelAttributeMethodProcessor 写入model | 1.@ModelAttribute标注的参数 2.@ModelAttribute标注的方法 3.所有非简单类型/简单类型数组(annotationNotRequired=true) | 参数解析器&返回值处理器 | |
ModelMethodProcessor 写入model | 1.Model类型参数 2.Model返回类型 | 参数解析器&返回值处理器 | |
写入httpBody | RequestResponseBodyMethodProcessor 写入到response的body | 1.@RequestBody标注的参数 2.@ResponseBody标注的方法 | 参数解析器&返回值处理器 |
HttpEntityMethodProcessor 写入到response的body | 1.HttpEntity和RequestEntity类型的参数 2.HttpEntity和ResponseEntity返回类型 | 参数解析器&返回值处理器 |
扩展把目标方法的返回值写入body的功能(复杂的逻辑是处理各种类型响应数据的http头信息)。所有把目标方法的返回值写入body的返回值处理器都是它的子类。具体过程如下:
- 获取安全的文件扩展名(默认为请求header中Accept配置的文件扩展名+常见的安全文件扩展名);
- 获取返回值的class对象和具体类型(如果返回值是泛型,则获取具体的泛型类型);
- 如果是资源文件支持分段下载;
- 获取合适的MediaType。如果找不到 & 返回值不为空,则报错;
- 根据MediaType和返回类型的class对象找到支持的消息转换器。如果找不到 & 返回值不为空,则报错;
- 调用RequestResponseBodyAdviceChain.beforeBodyWrite写入之前进行预处理;
- 添加http header头信息;
- 调用HttpMessageConverter把目标方法的返回值写入body;
public abstract class AbstractMessageConverterMethodProcessor extends AbstractMessageConverterMethodArgumentResolver
implements HandlerMethodReturnValueHandler {
//**常见的安全文件扩展名
private static final Set<String> SAFE_EXTENSIONS = new HashSet<>(Arrays.asList(
"txt", "text", "yml", "properties", "csv",
"json", "xml", "atom", "rss",
"png", "jpe", "jpeg", "jpg", "gif", "wbmp", "bmp"));
//**安全的文件类型
private static final Set<String> SAFE_MEDIA_BASE_TYPES = new HashSet<>(
Arrays.asList("audio", "image", "video"));
//**表示所有MediaType的常量
private static final List<MediaType> ALL_APPLICATION_MEDIA_TYPES =
Arrays.asList(MediaType.ALL, new MediaType("application"));
private static final Type RESOURCE_REGION_LIST_TYPE =
new ParameterizedTypeReference<List<ResourceRegion>>() { }.getType();
private final ContentNegotiationManager contentNegotiationManager;
//**安全的文件扩展名
private final Set<String> safeExtensions = new HashSet<>();
//**创建实例时,获取安全的文件扩展名(默认为请求header中Accept配置的文件扩展名+常见的安全文件扩展名)
protected AbstractMessageConverterMethodProcessor(List<HttpMessageConverter<?>> converters,
@Nullable ContentNegotiationManager manager, @Nullable List<Object> requestResponseBodyAdvice) {
super(converters, requestResponseBodyAdvice);
this.contentNegotiationManager = (manager != null ? manager : new ContentNegotiationManager());
this.safeExtensions.addAll(this.contentNegotiationManager.getAllFileExtensions());
this.safeExtensions.addAll(SAFE_EXTENSIONS);
}
//**根据NativeWebRequest创建ServletServerHttpResponse
protected ServletServerHttpResponse createOutputMessage(NativeWebRequest webRequest) {
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
Assert.state(response != null, "No HttpServletResponse");
return new ServletServerHttpResponse(response);
}
//**把目标方法的返回值写入ServletServerHttpResponse
protected <T> void writeWithMessageConverters(T value, MethodParameter returnType, NativeWebRequest webRequest)
throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
writeWithMessageConverters(value, returnType, inputMessage, outputMessage);
}
//**把目标方法的返回值写入ServletServerHttpResponse
@SuppressWarnings({"rawtypes", "unchecked"})
protected <T> void writeWithMessageConverters(@Nullable T value, MethodParameter returnType,
ServletServerHttpRequest inputMessage, ServletServerHttpResponse outputMessage)
throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
Object body; //**body内容
Class<?> valueType; //**返回值的class对象
Type targetType; //**返回值类型(如果返回值是泛型,则获取具体的泛型类型)
if (value instanceof CharSequence) { //**如果返回值是字符串,返回值的类型为String
body = value.toString();
valueType = String.class;
targetType = String.class;
}
else { //**否则,获取返回值的类型
body = value;
valueType = getReturnValueType(body, returnType);
targetType = GenericTypeResolver.resolveType(getGenericType(returnType), returnType.getContainingClass());
}
/**
* 资源文件支持分段下载
* 请求头信息:Range: bytes=100-200
* 响应头信息:Accept-Ranges: bytes
* 响应状态码:206
*/
if (isResourceType(value, returnType)) {
outputMessage.getHeaders().set(HttpHeaders.ACCEPT_RANGES, "bytes");
if (value != null && inputMessage.getHeaders().getFirst(HttpHeaders.RANGE) != null &&
outputMessage.getServletResponse().getStatus() == 200) {
Resource resource = (Resource) value;
try {
List<HttpRange> httpRanges = inputMessage.getHeaders().getRange();
outputMessage.getServletResponse().setStatus(HttpStatus.PARTIAL_CONTENT.value());
body = HttpRange.toResourceRegions(httpRanges, resource);
valueType = body.getClass();
targetType = RESOURCE_REGION_LIST_TYPE;
}
catch (IllegalArgumentException ex) {
outputMessage.getHeaders().set(HttpHeaders.CONTENT_RANGE, "bytes */" + resource.contentLength());
outputMessage.getServletResponse().setStatus(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE.value());
}
}
}
//**获取合适的MediaType
MediaType selectedMediaType = null;
MediaType contentType = outputMessage.getHeaders().getContentType();
boolean isContentTypePreset = contentType != null && contentType.isConcrete();
if (isContentTypePreset) {
if (logger.isDebugEnabled()) {
logger.debug("Found 'Content-Type:" + contentType + "' in response");
}
selectedMediaType = contentType;
}
else {
HttpServletRequest request = inputMessage.getServletRequest();
List<MediaType> acceptableTypes;
try {
acceptableTypes = getAcceptableMediaTypes(request);
}
catch (HttpMediaTypeNotAcceptableException ex) {
int series = outputMessage.getServletResponse().getStatus() / 100;
if (body == null || series == 4 || series == 5) {
if (logger.isDebugEnabled()) {
logger.debug("Ignoring error response content (if any). " + ex);
}
return;
}
throw ex;
}
List<MediaType> producibleTypes = getProducibleMediaTypes(request, valueType, targetType);
if (body != null && producibleTypes.isEmpty()) {
throw new HttpMessageNotWritableException(
"No converter found for return value of type: " + valueType);
}
List<MediaType> mediaTypesToUse = new ArrayList<>();
for (MediaType requestedType : acceptableTypes) {
for (MediaType producibleType : producibleTypes) {
if (requestedType.isCompatibleWith(producibleType)) {
mediaTypesToUse.add(getMostSpecificMediaType(requestedType, producibleType));
}
}
}
if (mediaTypesToUse.isEmpty()) {
if (body != null) {
throw new HttpMediaTypeNotAcceptableException(producibleTypes);
}
if (logger.isDebugEnabled()) {
logger.debug("No match for " + acceptableTypes + ", supported: " + producibleTypes);
}
return;
}
MediaType.sortBySpecificityAndQuality(mediaTypesToUse);
for (MediaType mediaType : mediaTypesToUse) {
if (mediaType.isConcrete()) {
selectedMediaType = mediaType;
break;
}
else if (mediaType.isPresentIn(ALL_APPLICATION_MEDIA_TYPES)) {
selectedMediaType = MediaType.APPLICATION_OCTET_STREAM;
break;
}
}
if (logger.isDebugEnabled()) {
logger.debug("Using '" + selectedMediaType + "', given " +
acceptableTypes + " and supported " + producibleTypes);
}
}
/**
* 如果获取到了正确的MediaType & HttpMessageConverter能处理目标方法的返回值
* 1.调用RequestResponseBodyAdviceChain.beforeBodyWrite写入之前进行预处理
* 2.添加头信息
* 3.调用HttpMessageConverter把目标方法的返回值写入body
*/
if (selectedMediaType != null) {
selectedMediaType = selectedMediaType.removeQualityValue();
for (HttpMessageConverter<?> converter : this.messageConverters) {
GenericHttpMessageConverter genericConverter = (converter instanceof GenericHttpMessageConverter ?
(GenericHttpMessageConverter<?>) converter : null);
if (genericConverter != null ?
((GenericHttpMessageConverter) converter).canWrite(targetType, valueType, selectedMediaType) :
converter.canWrite(valueType, selectedMediaType)) {
body = getAdvice().beforeBodyWrite(body, returnType, selectedMediaType,
(Class<? extends HttpMessageConverter<?>>) converter.getClass(),
inputMessage, outputMessage);
if (body != null) {
Object theBody = body;
LogFormatUtils.traceDebug(logger, traceOn ->
"Writing [" + LogFormatUtils.formatValue(theBody, !traceOn) + "]");
addContentDispositionHeader(inputMessage, outputMessage);
if (genericConverter != null) {
genericConverter.write(body, targetType, selectedMediaType, outputMessage);
}
else {
((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
}
}
else {
if (logger.isDebugEnabled()) {
logger.debug("Nothing to write: null body");
}
}
return;
}
}
}
if (body != null) {
Set<MediaType> producibleMediaTypes =
(Set<MediaType>) inputMessage.getServletRequest()
.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
if (isContentTypePreset || !CollectionUtils.isEmpty(producibleMediaTypes)) {
throw new HttpMessageNotWritableException(
"No converter for [" + valueType + "] with preset Content-Type '" + contentType + "'");
}
throw new HttpMediaTypeNotAcceptableException(getSupportedMediaTypes(body.getClass()));
}
}
//**获取目标方法返回值的类型
protected Class<?> getReturnValueType(@Nullable Object value, MethodParameter returnType) {
return (value != null ? value.getClass() : returnType.getParameterType());
}
//目标方法返回值是否是Resource类型
protected boolean isResourceType(@Nullable Object value, MethodParameter returnType) {
Class<?> clazz = getReturnValueType(value, returnType);
return clazz != InputStreamResource.class && Resource.class.isAssignableFrom(clazz);
}
//**根据目标方法返回类型获取MediaType列表
private Type getGenericType(MethodParameter returnType) {
if (HttpEntity.class.isAssignableFrom(returnType.getParameterType())) {
return ResolvableType.forType(returnType.getGenericParameterType()).getGeneric().getType();
}
else {
return returnType.getGenericParameterType();
}
}
//**根据目标方法返回类型获取MediaType列表
@SuppressWarnings("unused")
protected List<MediaType> getProducibleMediaTypes(HttpServletRequest request, Class<?> valueClass) {
return getProducibleMediaTypes(request, valueClass, null);
}
//**获取目标方法返回值的MediaType
@SuppressWarnings("unchecked")
protected List<MediaType> getProducibleMediaTypes(
HttpServletRequest request, Class<?> valueClass, @Nullable Type targetType) {
Set<MediaType> mediaTypes =
(Set<MediaType>) request.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
if (!CollectionUtils.isEmpty(mediaTypes)) {
return new ArrayList<>(mediaTypes);
}
List<MediaType> result = new ArrayList<>();
for (HttpMessageConverter<?> converter : this.messageConverters) {
if (converter instanceof GenericHttpMessageConverter && targetType != null) {
if (((GenericHttpMessageConverter<?>) converter).canWrite(targetType, valueClass, null)) {
result.addAll(converter.getSupportedMediaTypes(valueClass));
}
}
else if (converter.canWrite(valueClass, null)) {
result.addAll(converter.getSupportedMediaTypes(valueClass));
}
}
return (result.isEmpty() ? Collections.singletonList(MediaType.ALL) : result);
}
private List<MediaType> getAcceptableMediaTypes(HttpServletRequest request)
throws HttpMediaTypeNotAcceptableException {
return this.contentNegotiationManager.resolveMediaTypes(new ServletWebRequest(request));
}
//**获取具体的MediaType
private MediaType getMostSpecificMediaType(MediaType acceptType, MediaType produceType) {
MediaType produceTypeToUse = produceType.copyQualityValue(acceptType);
return (MediaType.SPECIFICITY_COMPARATOR.compare(acceptType, produceTypeToUse) <= 0 ? acceptType : produceTypeToUse);
}
//**如果是不安全的文件扩展名 & 状态码为2xx,则header添加("Content-disposition", "inline;filename=f.txt");
private void addContentDispositionHeader(ServletServerHttpRequest request, ServletServerHttpResponse response) {
HttpHeaders headers = response.getHeaders();
if (headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
return;
}
try {
int status = response.getServletResponse().getStatus();
if (status < 200 || (status > 299 && status < 400)) {
return;
}
}
catch (Throwable ex) {
// ignore
}
HttpServletRequest servletRequest = request.getServletRequest();
String requestUri = UrlPathHelper.rawPathInstance.getOriginatingRequestUri(servletRequest);
int index = requestUri.lastIndexOf('/') + 1;
String filename = requestUri.substring(index);
String pathParams = "";
index = filename.indexOf(';');
if (index != -1) {
pathParams = filename.substring(index);
filename = filename.substring(0, index);
}
filename = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, filename);
String ext = StringUtils.getFilenameExtension(filename);
pathParams = UrlPathHelper.defaultInstance.decodeRequestString(servletRequest, pathParams);
String extInPathParams = StringUtils.getFilenameExtension(pathParams);
if (!safeExtension(servletRequest, ext) || !safeExtension(servletRequest, extInPathParams)) {
headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=f.txt");
}
}
//**判断文件扩展名是否安全
@SuppressWarnings("unchecked")
private boolean safeExtension(HttpServletRequest request, @Nullable String extension) {
if (!StringUtils.hasText(extension)) {
return true;
}
extension = extension.toLowerCase(Locale.ENGLISH);
if (this.safeExtensions.contains(extension)) {
return true;
}
String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (pattern != null && pattern.endsWith("." + extension)) {
return true;
}
if (extension.equals("html")) {
String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
Set<MediaType> mediaTypes = (Set<MediaType>) request.getAttribute(name);
if (!CollectionUtils.isEmpty(mediaTypes) && mediaTypes.contains(MediaType.TEXT_HTML)) {
return true;
}
}
MediaType mediaType = resolveMediaType(request, extension);
return (mediaType != null && (safeMediaType(mediaType)));
}
//**根据文件扩展名获取MediaType
@Nullable
private MediaType resolveMediaType(ServletRequest request, String extension) {
MediaType result = null;
String rawMimeType = request.getServletContext().getMimeType("file." + extension);
if (StringUtils.hasText(rawMimeType)) {
result = MediaType.parseMediaType(rawMimeType);
}
if (result == null || MediaType.APPLICATION_OCTET_STREAM.equals(result)) {
result = MediaTypeFactory.getMediaType("file." + extension).orElse(null);
}
return result;
}
//**判断MediaType是否安全
private boolean safeMediaType(MediaType mediaType) {
return (SAFE_MEDIA_BASE_TYPES.contains(mediaType.getType()) ||
mediaType.getSubtype().endsWith("+xml"));
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)