SPR-5904 - Multipart/mixed requests using RestTemplate

This commit is contained in:
Arjen Poutsma
2010-03-09 16:15:41 +00:00
parent 21fd150894
commit 0efb9d8023
11 changed files with 683 additions and 1 deletions

View File

@@ -0,0 +1,103 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
/**
* <p>Inspired by {@link org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}.
*
* @author Arjen Poutsma
* @since 3.0.2
*/
abstract class AbstractPart implements Part {
private static final byte[] CONTENT_DISPOSITION =
new byte[]{'C', 'o', 'n', 't', 'e', 'n', 't', '-', 'D', 'i', 's', 'p', 'o', 's', 'i', 't', 'i', 'o', 'n',
':', ' ', 'f', 'o', 'r', 'm', '-', 'd', 'a', 't', 'a', ';', ' ', 'n', 'a', 'm', 'e', '='};
private static final byte[] CONTENT_TYPE =
new byte[]{'C', 'o', 'n', 't', 'e', 'n', 't', '-', 'T', 'y', 'p', 'e', ':', ' '};
private final MediaType contentType;
protected AbstractPart(MediaType contentType) {
Assert.notNull(contentType, "'contentType' must not be null");
this.contentType = contentType;
}
public final void write(byte[] boundary, String name, OutputStream os) throws IOException {
writeBoundary(boundary, os);
writeContentDisposition(name, os);
writeContentType(os);
writeEndOfHeader(os);
writeData(os);
writeEnd(os);
}
protected void writeBoundary(byte[] boundary, OutputStream os) throws IOException {
os.write('-');
os.write('-');
os.write(boundary);
writeNewLine(os);
}
protected void writeContentDisposition(String name, OutputStream os) throws IOException {
os.write(CONTENT_DISPOSITION);
os.write('"');
os.write(getAsciiBytes(name));
os.write('"');
}
protected void writeContentType(OutputStream os) throws IOException {
writeNewLine(os);
os.write(CONTENT_TYPE);
os.write(getAsciiBytes(contentType.toString()));
}
protected byte[] getAsciiBytes(String name) {
try {
return name.getBytes("US-ASCII");
}
catch (UnsupportedEncodingException ex) {
// should not happen, US-ASCII is always supported
throw new IllegalStateException(ex);
}
}
protected void writeEndOfHeader(OutputStream os) throws IOException {
writeNewLine(os);
writeNewLine(os);
}
protected void writeEnd(OutputStream os) throws IOException {
writeNewLine(os);
}
private void writeNewLine(OutputStream os) throws IOException {
os.write('\r');
os.write('\n');
}
protected abstract void writeData(OutputStream os) throws IOException;
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.IOException;
import java.io.OutputStream;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/**
* @author Arjen Poutsma
* @since 3.0.2
*/
class ByteArrayPart extends AbstractPart {
private final byte[] value;
public ByteArrayPart(byte[] value, MediaType contentType) {
super(contentType);
Assert.isTrue(value != null && value.length != 0, "'value' must not be null");
this.value = value;
}
@Override
protected void writeData(OutputStream os) throws IOException {
FileCopyUtils.copy(value, os);
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
/**
* <p>Inspired by {@link org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity}.
*
* @author Arjen Poutsma
* @since 3.0.2
*/
public class MultipartHttpMessageConverter extends AbstractHttpMessageConverter<MultipartMap> {
private static final byte[] BOUNDARY_CHARS =
new byte[]{'-', '_',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
private final Random rnd = new Random();
public MultipartHttpMessageConverter() {
super(new MediaType("multipart", "form-data"));
}
@Override
protected boolean supports(Class<?> clazz) {
return MultipartMap.class.isAssignableFrom(clazz);
}
@Override
protected void writeInternal(MultipartMap map, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
byte[] boundary = generateBoundary();
HttpHeaders headers = outputMessage.getHeaders();
MediaType contentType = headers.getContentType();
if (contentType != null) {
String boundaryString = new String(boundary, "US-ASCII");
Map<String, String> params = Collections.singletonMap("boundary", boundaryString);
contentType = new MediaType(contentType.getType(), contentType.getSubtype(), params);
headers.setContentType(contentType);
}
OutputStream os = outputMessage.getBody();
for (Map.Entry<String, List<Part>> entry : map.entrySet()) {
String name = entry.getKey();
for (Part part : entry.getValue()) {
part.write(boundary, name, os);
}
}
os.write('-');
os.write('-');
os.write(boundary);
os.write('-');
os.write('-');
os.write('\r');
os.write('\n');
}
/**
* Generate a multipart boundary.
*
* <p>Default implementation returns a random boundary.
*/
protected byte[] generateBoundary() {
byte[] boundary = new byte[rnd.nextInt(11) + 30];
for (int i = 0; i < boundary.length; i++) {
boundary[i] = BOUNDARY_CHARS[rnd.nextInt(BOUNDARY_CHARS.length)];
}
return boundary;
}
@Override
public boolean canRead(Class<?> clazz, MediaType mediaType) {
// reading not supported yet
return false;
}
@Override
protected MultipartMap readInternal(Class<? extends MultipartMap> clazz, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.File;
import java.nio.charset.Charset;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
/**
* @author Arjen Poutsma
* @since 3.0.2
*/
public class MultipartMap extends LinkedMultiValueMap<String, Part> {
public void addTextPart(String name, String value) {
Assert.hasText(name, "'name' must not be empty");
add(name, new StringPart(value));
}
public void addTextPart(String name, String value, Charset charset) {
Assert.hasText(name, "'name' must not be empty");
add(name, new StringPart(value, charset));
}
public void addBinaryPart(String name, Resource resource) {
Assert.hasText(name, "'name' must not be empty");
add(name, new ResourcePart(resource));
}
public void addBinaryPart(Resource resource) {
Assert.notNull(resource, "'resource' must not be null");
addBinaryPart(resource.getFilename(), resource);
}
public void addBinaryPart(String name, File file) {
addBinaryPart(name, new FileSystemResource(file));
}
public void addBinaryPart(File file) {
addBinaryPart(new FileSystemResource(file));
}
public void addPart(String name, byte[] value, MediaType contentType) {
Assert.hasText(name, "'name' must not be empty");
add(name, new ByteArrayPart(value, contentType));
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author Arjen Poutsma
* @since 3.0.2
*/
public interface Part {
void write(byte[] boundary, String name, OutputStream os) throws IOException;
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.IOException;
import java.io.OutputStream;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/** @author Arjen Poutsma */
class ResourcePart extends AbstractPart {
private static final byte[] FILE_NAME = new byte[]{';', ' ', 'f', 'i', 'l', 'e', 'n', 'a', 'm', 'e', '='};
private final Resource resource;
public ResourcePart(Resource resource) {
super(new MediaType("application", "octet-stream"));
Assert.notNull(resource, "'resource' must not be null");
Assert.isTrue(resource.exists(), "'" + resource + "' does not exist");
this.resource = resource;
}
@Override
protected void writeContentDisposition(String name, OutputStream os) throws IOException {
super.writeContentDisposition(name, os);
String filename = resource.getFilename();
if (StringUtils.hasLength(filename)) {
os.write(FILE_NAME);
os.write('"');
os.write(getAsciiBytes(filename));
os.write('"');
}
}
@Override
protected void writeData(OutputStream os) throws IOException {
FileCopyUtils.copy(resource.getInputStream(), os);
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2010 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.converter.multipart;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
/** @author Arjen Poutsma */
class StringPart extends AbstractPart {
private static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
private final String value;
private final Charset charset;
public StringPart(String value) {
this(value, DEFAULT_CHARSET);
}
public StringPart(String value, Charset charset) {
super(new MediaType("text", "plain", charset));
Assert.hasText(value, "'value' must not be null");
Assert.notNull(charset, "'charset' must not be null");
this.value = value;
this.charset = charset;
}
@Override
protected void writeData(OutputStream os) throws IOException {
FileCopyUtils.copy(value, new OutputStreamWriter(os, charset));
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2002-2010 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.
*/
/**
*
* Provides a HttpMessageConverter implementations for handling multipart data.
*
*/
package org.springframework.http.converter.multipart;

View File

@@ -37,6 +37,7 @@ import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
import org.springframework.http.converter.multipart.MultipartHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.util.Assert;
@@ -122,6 +123,7 @@ public class RestTemplate extends HttpAccessor implements RestOperations {
public RestTemplate() {
this.messageConverters.add(new ByteArrayHttpMessageConverter());
this.messageConverters.add(new StringHttpMessageConverter());
this.messageConverters.add(new MultipartHttpMessageConverter());
this.messageConverters.add(new FormHttpMessageConverter());
this.messageConverters.add(new SourceHttpMessageConverter());
if (jaxb2Present) {