Add way to override default multipartReader
This commit introduces a way to override the default multipart reader, for instance to the SynchronossPartHttpMessageReader.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.http.codec;
|
||||
|
||||
import org.springframework.core.codec.Encoder;
|
||||
import org.springframework.http.codec.multipart.Part;
|
||||
|
||||
/**
|
||||
* Extension of {@link CodecConfigurer} for HTTP message reader and writer
|
||||
@@ -79,10 +80,21 @@ public interface ServerCodecConfigurer extends CodecConfigurer {
|
||||
/**
|
||||
* Configure the {@code Encoder} to use for Server-Sent Events.
|
||||
* <p>By default if this is not set, and Jackson is available, the
|
||||
* {@link #jackson2JsonEncoder} override is used instead. Use this property
|
||||
* if you want to further customize the SSE encoder.
|
||||
* {@link #jackson2JsonEncoder} override is used instead. Use this method
|
||||
* to customize the SSE encoder.
|
||||
*/
|
||||
void serverSentEventEncoder(Encoder<?> encoder);
|
||||
|
||||
/**
|
||||
* Configure the {@code HttpMessageReader} to use for multipart messages
|
||||
* (i.e. file uploads).
|
||||
* <p>By default if this is not set, the
|
||||
* {@link org.springframework.http.codec.multipart.DefaultMultipartMessageReader} is used.
|
||||
* Use this method to customize the multipart reader, for instance to use
|
||||
* {@link org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader}
|
||||
* instead.
|
||||
*/
|
||||
void multipartReader(HttpMessageReader<Part> multipartReader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* 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.
|
||||
@@ -20,12 +20,13 @@ import java.util.List;
|
||||
import org.springframework.core.codec.Encoder;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.HttpMessageWriter;
|
||||
import org.springframework.http.codec.LoggingCodecSupport;
|
||||
import org.springframework.http.codec.ServerCodecConfigurer;
|
||||
import org.springframework.http.codec.ServerSentEventHttpMessageWriter;
|
||||
import org.springframework.http.codec.multipart.DefaultMultipartMessageReader;
|
||||
import org.springframework.http.codec.multipart.MultipartHttpMessageReader;
|
||||
import org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader;
|
||||
import org.springframework.http.codec.multipart.Part;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link ServerCodecConfigurer.ServerDefaultCodecs}.
|
||||
@@ -34,34 +35,41 @@ import org.springframework.util.ClassUtils;
|
||||
*/
|
||||
class ServerDefaultCodecsImpl extends BaseDefaultCodecs implements ServerCodecConfigurer.ServerDefaultCodecs {
|
||||
|
||||
private static final boolean synchronossMultipartPresent =
|
||||
ClassUtils.isPresent("org.synchronoss.cloud.nio.multipart.NioMultipartParser",
|
||||
DefaultServerCodecConfigurer.class.getClassLoader());
|
||||
|
||||
|
||||
@Nullable
|
||||
private Encoder<?> sseEncoder;
|
||||
|
||||
@Nullable
|
||||
private HttpMessageReader<Part> multipartReader;
|
||||
|
||||
@Override
|
||||
public void serverSentEventEncoder(Encoder<?> encoder) {
|
||||
this.sseEncoder = encoder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void multipartReader(HttpMessageReader<Part> multipartReader) {
|
||||
this.multipartReader = multipartReader;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void extendTypedReaders(List<HttpMessageReader<?>> typedReaders) {
|
||||
if (synchronossMultipartPresent) {
|
||||
boolean enable = isEnableLoggingRequestDetails();
|
||||
|
||||
SynchronossPartHttpMessageReader partReader = new SynchronossPartHttpMessageReader();
|
||||
partReader.setEnableLoggingRequestDetails(enable);
|
||||
typedReaders.add(partReader);
|
||||
HttpMessageReader<Part> partReader = getMultipartReader();
|
||||
|
||||
MultipartHttpMessageReader reader = new MultipartHttpMessageReader(partReader);
|
||||
reader.setEnableLoggingRequestDetails(enable);
|
||||
typedReaders.add(reader);
|
||||
boolean logRequestDetails = isEnableLoggingRequestDetails();
|
||||
if (partReader instanceof LoggingCodecSupport) {
|
||||
((LoggingCodecSupport) partReader).setEnableLoggingRequestDetails(logRequestDetails);
|
||||
}
|
||||
typedReaders.add(partReader);
|
||||
|
||||
MultipartHttpMessageReader reader = new MultipartHttpMessageReader(partReader);
|
||||
reader.setEnableLoggingRequestDetails(logRequestDetails);
|
||||
typedReaders.add(reader);
|
||||
}
|
||||
|
||||
private HttpMessageReader<Part> getMultipartReader() {
|
||||
return this.multipartReader != null ? this.multipartReader : new DefaultMultipartMessageReader();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user