Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reactive multipart request support #1201

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.gradle
Expand Up @@ -89,6 +89,7 @@ configure(allprojects) { project ->
ext.servletVersion = "3.1.0"
ext.slf4jVersion = "1.7.25"
ext.snakeyamlVersion = "1.18"
ext.nioMultipartVersion = "1.0.2"
ext.testngVersion = "6.11"
ext.tiles3Version = "3.0.7"
ext.tomcatVersion = "8.5.14"
Expand Down Expand Up @@ -747,6 +748,7 @@ project("spring-web") {
optional("javax.xml.bind:jaxb-api:${jaxbVersion}")
optional("javax.xml.ws:jaxws-api:${jaxwsVersion}")
optional("javax.mail:javax.mail-api:${javamailVersion}")
optional("org.synchronoss.cloud:nio-multipart-parser:${nioMultipartVersion}")
optional("org.jetbrains.kotlin:kotlin-stdlib-jre8:${kotlinVersion}")
testCompile(project(":spring-context-support")) // for JafMediaTypeFactory
testCompile("io.projectreactor.addons:reactor-test")
Expand Down Expand Up @@ -838,6 +840,10 @@ project("spring-webflux") {
testRuntime("org.jetbrains.kotlin:kotlin-compiler:${kotlinVersion}")
testCompile("org.jetbrains.kotlin:kotlin-script-runtime:${kotlinVersion}")
testRuntime("org.jetbrains.kotlin:kotlin-script-util:${kotlinVersion}")
testRuntime("org.synchronoss.cloud:nio-multipart-parser:${nioMultipartVersion}")
testRuntime("com.sun.mail:javax.mail:${javamailVersion}")
testRuntime("com.sun.xml.bind:jaxb-core:${jaxbVersion}")
testRuntime("com.sun.xml.bind:jaxb-impl:${jaxbVersion}")
}

if (JavaVersion.current().java9Compatible) {
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.mock.http.server.reactive;

import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.server.ServerWebExchangeDecorator;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import org.springframework.web.server.session.DefaultWebSessionManager;
Expand All @@ -35,7 +36,8 @@ public class MockServerWebExchange extends ServerWebExchangeDecorator {

public MockServerWebExchange(MockServerHttpRequest request) {
super(new DefaultServerWebExchange(
request, new MockServerHttpResponse(), new DefaultWebSessionManager()));
request, new MockServerHttpResponse(), new DefaultWebSessionManager(),
ServerCodecConfigurer.create()));
}


Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.http.codec.json.Jackson2JsonDecoder;
import org.springframework.http.codec.multipart.MultipartHttpMessageWriter;

/**
* Default implementation of {@link ClientCodecConfigurer}.
Expand Down Expand Up @@ -57,6 +58,7 @@ public void serverSentEventDecoder(Decoder<?> decoder) {
protected void addTypedWritersTo(List<HttpMessageWriter<?>> result) {
super.addTypedWritersTo(result);
addWriterTo(result, FormHttpMessageWriter::new);
addWriterTo(result, MultipartHttpMessageWriter::new);
}

@Override
Expand Down
Expand Up @@ -21,6 +21,8 @@
import org.springframework.core.codec.Encoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.http.codec.multipart.SynchronossMultipartHttpMessageReader;
import org.springframework.util.ClassUtils;

/**
* Default implementation of {@link ServerCodecConfigurer}.
Expand All @@ -30,6 +32,11 @@
*/
class DefaultServerCodecConfigurer extends DefaultCodecConfigurer implements ServerCodecConfigurer {

static final boolean synchronossMultipartPresent =
ClassUtils.isPresent("org.synchronoss.cloud.nio.multipart.NioMultipartParser",
org.springframework.http.codec.DefaultCodecConfigurer.class.getClassLoader());


public DefaultServerCodecConfigurer() {
super(new DefaultServerDefaultCodecsConfigurer());
}
Expand Down Expand Up @@ -57,6 +64,9 @@ public void serverSentEventEncoder(Encoder<?> encoder) {
public void addTypedReadersTo(List<HttpMessageReader<?>> result) {
super.addTypedReadersTo(result);
addReaderTo(result, FormHttpMessageReader::new);
if (synchronossMultipartPresent) {
addReaderTo(result, SynchronossMultipartHttpMessageReader::new);
}
}

@Override
Expand Down
@@ -0,0 +1,58 @@
/*
* Copyright 2002-2017 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
*
* http://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.multipart;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import reactor.core.publisher.Flux;

import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.util.MultiValueMap;

/**
* Interface for reading multipart HTML forms with {@code "multipart/form-data"} media
* type in accordance with <a href="https://tools.ietf.org/html/rfc7578">RFC 7578</a>.
*
* @author Sebastien Deleuze
* @since 5.0
*/
public interface MultipartHttpMessageReader extends HttpMessageReader<MultiValueMap<String, Part>> {

ResolvableType MULTIPART_VALUE_TYPE =
ResolvableType.forClassWithGenerics(MultiValueMap.class, String.class, Part.class);

@Override
default List<MediaType> getReadableMediaTypes() {
return Collections.singletonList(MediaType.MULTIPART_FORM_DATA);
}

@Override
default boolean canRead(ResolvableType elementType, MediaType mediaType) {
return MULTIPART_VALUE_TYPE.isAssignableFrom(elementType) &&
(mediaType == null || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType));
}

@Override
default Flux<MultiValueMap<String, Part>> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
return Flux.from(readMono(elementType, message, hints));
}
}