INT-1256, INT-822, INT-823 added MultipartFileReader strategy interface and some implementations

This commit is contained in:
Mark Fisher
2010-07-25 23:28:29 +00:00
parent d7c083128d
commit 184db41bbc
5 changed files with 347 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
/*
* 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.integration.http;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
/**
* {@link MultipartFileReader} implementation that reads the {@link MultipartFile}
* content directly into a new {@link MultipartFile} instance that is not restricted
* to the HTTP request scope.
*
* @author Mark Fisher
* @since 2.0
*/
public class DefaultMultipartFileReader implements MultipartFileReader<MultipartFile> {
public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException {
return new UploadedMultipartFile(multipartFile.getBytes(),
multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename());
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.integration.http;
import java.io.File;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.multipart.MultipartFile;
/**
* {@link MultipartFileReader} implementation that copies the MulitpartFile's
* content to a new temporary File in the specified directory. If no directory
* is provided, the Files will be created in the default temporary directory.
*
* @author Mark Fisher
* @since 2.0
*/
public class FileCopyingMultipartFileReader implements MultipartFileReader<MultipartFile> {
private static final Log logger = LogFactory.getLog(FileCopyingMultipartFileReader.class);
private final File directory;
private volatile String prefix = "si_";
private volatile String suffix = ".tmp";
/**
* Create a {@link FileCopyingMultipartFileReader} that creates temporary
* Files in the default temporary directory.
*/
public FileCopyingMultipartFileReader() {
this(null);
}
/**
* Create a {@link FileCopyingMultipartFileReader} that creates temporary
* Files in the given directory.
*/
public FileCopyingMultipartFileReader(File directory) {
this.directory = directory;
}
/**
* Specify the prefix to use for temporary files.
*/
public void setPrefix(String prefix) {
this.prefix = prefix;
}
/**
* Specify the suffix to use for temporary files.
*/
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public MultipartFile readMultipartFile(MultipartFile multipartFile) throws IOException {
File upload = File.createTempFile(this.prefix, this.suffix, this.directory);
multipartFile.transferTo(upload);
UploadedMultipartFile uploadedMultipartFile = new UploadedMultipartFile(upload, multipartFile.getSize(),
multipartFile.getContentType(), multipartFile.getName(), multipartFile.getOriginalFilename());
if (logger.isDebugEnabled()) {
logger.debug("copied uploaded file [" + multipartFile.getOriginalFilename() +
"] to [" + upload.getAbsolutePath() + "]");
}
return uploadedMultipartFile;
}
}

View File

@@ -0,0 +1,36 @@
/*
* 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.integration.http;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
/**
* Strategy for reading {@link MultipartFile} content.
*
* @author mark Fisher
* @since 2.0
*/
public interface MultipartFileReader<T> {
/**
* Reads {@link MultipartFile} content.
*/
T readMultipartFile(MultipartFile multipartFile) throws IOException;
}

View File

@@ -0,0 +1,62 @@
/*
* 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.integration.http;
import java.io.IOException;
import java.nio.charset.Charset;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.WebUtils;
/**
* {@link MultipartFileReader} implementation that does not maintain metadata from
* the original {@link MultipartFile} instance. Instead this simply reads the file
* content directly as either a String or byte array depending on the Content-Type.
*
* @author Mark Fisher
* @since 2.0
*/
public class SimpleMultipartFileReader implements MultipartFileReader<Object> {
private volatile Charset defaultCharset = Charset.forName(WebUtils.DEFAULT_CHARACTER_ENCODING);
/**
* Specify the default charset name to use when converting multipart file
* content into Strings if the multipart itself does not provide a charset.
*/
public void setDefaultMultipartCharset(String defaultCharset) {
this.defaultCharset = Charset.forName(
defaultCharset != null ? defaultCharset : WebUtils.DEFAULT_CHARACTER_ENCODING);
}
public Object readMultipartFile(MultipartFile multipartFile) throws IOException {
if (multipartFile.getContentType() != null && multipartFile.getContentType().startsWith("text")) {
MediaType contentType = MediaType.parseMediaType(multipartFile.getContentType());
Charset charset = contentType.getCharSet();
if (charset == null) {
charset = defaultCharset;
}
return new String(multipartFile.getBytes(), charset);
}
else {
return multipartFile.getBytes();
}
}
}

View File

@@ -0,0 +1,121 @@
/*
* 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.integration.http;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
/**
* A {@link MultipartFile} implementation that represents an uploaded File.
* The actual file content either exists in memory (in a byte array) or in a File.
*
* @author Mark Fisher
* @since 2.0
*/
public class UploadedMultipartFile implements MultipartFile {
private final File file;
private final byte[] bytes;
private final long size;
private final String contentType;
private final String formParameterName;
private final String originalFilename;
public UploadedMultipartFile(File file, long size, String contentType, String formParameterName, String originalFilename) {
Assert.notNull(file, "file must not be null");
Assert.hasText(contentType, "contentType is required");
Assert.hasText(formParameterName, "formParameterName is required");
Assert.hasText(originalFilename, "originalFilename is required");
this.file = file;
this.size = size;
this.bytes = null;
this.contentType = contentType;
this.formParameterName = formParameterName;
this.originalFilename = originalFilename;
}
public UploadedMultipartFile(byte[] bytes, String contentType, String formParameterName, String originalFilename) {
Assert.notNull(bytes, "bytes must not be null");
Assert.hasText(contentType, "contentType is required");
Assert.hasText(formParameterName, "formParameterName is required");
Assert.hasText(originalFilename, "originalFilename is required");
this.bytes = bytes;
this.size = bytes.length;
this.file = null;
this.contentType = contentType;
this.formParameterName = formParameterName;
this.originalFilename = originalFilename;
}
public String getName() {
return this.formParameterName;
}
public byte[] getBytes() throws IOException {
if (this.bytes != null) {
return this.bytes;
}
return FileCopyUtils.copyToByteArray(this.file);
}
public String getContentType() {
return this.contentType;
}
public InputStream getInputStream() throws IOException {
if (this.bytes != null) {
return new ByteArrayInputStream(this.bytes);
}
return new FileInputStream(this.file);
}
public String getOriginalFilename() {
return this.originalFilename;
}
public long getSize() {
return this.size;
}
public boolean isEmpty() {
return this.size == 0;
}
public void transferTo(File dest) throws IOException, IllegalStateException {
if (this.bytes != null) {
FileCopyUtils.copy(this.bytes, dest);
}
else {
FileCopyUtils.copy(this.file, dest);
}
}
}