diff --git a/org.springframework.test/src/main/java/org/springframework/mock/web/MockMultipartFile.java b/org.springframework.test/src/main/java/org/springframework/mock/web/MockMultipartFile.java index a4b6af7e4c8..e63f63c4315 100644 --- a/org.springframework.test/src/main/java/org/springframework/mock/web/MockMultipartFile.java +++ b/org.springframework.test/src/main/java/org/springframework/mock/web/MockMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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,6 +20,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; +import java.util.Iterator; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; @@ -39,6 +41,8 @@ import org.springframework.web.multipart.MultipartFile; */ public class MockMultipartFile implements MultipartFile { + private static final String CONTENT_TYPE = "Content-Type"; + private final String name; private String originalFilename; @@ -129,4 +133,26 @@ public class MockMultipartFile implements MultipartFile { FileCopyUtils.copy(this.content, dest); } + public String getHeader(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return this.contentType; + } + else { + return null; + } + } + + public String[] getHeaders(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return new String[] {this.contentType}; + } + else { + return null; + } + } + + public Iterator getHeaderNames(String name) { + return Collections.singleton(CONTENT_TYPE).iterator(); + } + } diff --git a/org.springframework.web.portlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java b/org.springframework.web.portlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java index 17fa3373f25..e63f63c4315 100644 --- a/org.springframework.web.portlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java +++ b/org.springframework.web.portlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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,6 +20,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; +import java.util.Iterator; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; @@ -29,16 +31,18 @@ import org.springframework.web.multipart.MultipartFile; * Mock implementation of the {@link org.springframework.web.multipart.MultipartFile} * interface. * - *

Useful in conjunction with a {@link org.springframework.mock.web.MockMultipartHttpServletRequest} + *

Useful in conjunction with a {@link MockMultipartHttpServletRequest} * for testing application controllers that access multipart uploads. * * @author Juergen Hoeller * @author Eric Crampton * @since 2.0 - * @see org.springframework.mock.web.MockMultipartHttpServletRequest + * @see MockMultipartHttpServletRequest */ public class MockMultipartFile implements MultipartFile { + private static final String CONTENT_TYPE = "Content-Type"; + private final String name; private String originalFilename; @@ -61,7 +65,7 @@ public class MockMultipartFile implements MultipartFile { * Create a new MockMultipartFile with the given content. * @param name the name of the file * @param contentStream the content of the file as stream - * @throws java.io.IOException if reading from the stream failed + * @throws IOException if reading from the stream failed */ public MockMultipartFile(String name, InputStream contentStream) throws IOException { this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); @@ -88,7 +92,7 @@ public class MockMultipartFile implements MultipartFile { * @param originalFilename the original filename (as on the client's machine) * @param contentType the content type (if known) * @param contentStream the content of the file as stream - * @throws java.io.IOException if reading from the stream failed + * @throws IOException if reading from the stream failed */ public MockMultipartFile(String name, String originalFilename, String contentType, InputStream contentStream) throws IOException { @@ -129,4 +133,26 @@ public class MockMultipartFile implements MultipartFile { FileCopyUtils.copy(this.content, dest); } + public String getHeader(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return this.contentType; + } + else { + return null; + } + } + + public String[] getHeaders(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return new String[] {this.contentType}; + } + else { + return null; + } + } + + public Iterator getHeaderNames(String name) { + return Collections.singleton(CONTENT_TYPE).iterator(); + } + } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java index e258c0eb9f5..c3a31abbcab 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/mock/web/MockMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2011 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,6 +20,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; +import java.util.Iterator; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; @@ -39,6 +41,8 @@ import org.springframework.web.multipart.MultipartFile; */ public class MockMultipartFile implements MultipartFile { + private static final String CONTENT_TYPE = "Content-Type"; + private final String name; private String originalFilename; @@ -129,4 +133,26 @@ public class MockMultipartFile implements MultipartFile { FileCopyUtils.copy(this.content, dest); } + public String getHeader(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return this.contentType; + } + else { + return null; + } + } + + public String[] getHeaders(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return new String[] {this.contentType}; + } + else { + return null; + } + } + + public Iterator getHeaderNames(String name) { + return Collections.singleton(CONTENT_TYPE).iterator(); + } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/multipart/MultipartFile.java b/org.springframework.web/src/main/java/org/springframework/web/multipart/MultipartFile.java index b9979083e3a..85978cf7a02 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/multipart/MultipartFile.java +++ b/org.springframework.web/src/main/java/org/springframework/web/multipart/MultipartFile.java @@ -19,6 +19,7 @@ package org.springframework.web.multipart; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Iterator; /** * A representation of an uploaded file received in a multipart request. @@ -101,4 +102,28 @@ public interface MultipartFile { */ void transferTo(File dest) throws IOException, IllegalStateException; + /** + * Return the first value associated with the given header name, if any. + * @param name the name of the header + * @return the first header value, or null if no such header was found + * @since 3.1 + */ + String getHeader(String name); + + /** + * Return all values associated with the given header name, if any. + * @param name the name of the header + * @return the header values as an array, or null if no such header was found + * @since 3.1 + */ + String[] getHeaders(String name); + + /** + * Return an {@link java.util.Iterator} of Strings containing the + * names of headers associated with this file. + * @return the names of the headers + * @since 3.1 + */ + Iterator getHeaderNames(String name); + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java b/org.springframework.web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java index 8c042953eef..0d7dd52eb6d 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java +++ b/org.springframework.web/src/main/java/org/springframework/web/multipart/commons/CommonsMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2006 the original author or authors. + * Copyright 2002-2011 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. @@ -21,6 +21,8 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.util.Collections; +import java.util.Iterator; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; @@ -33,10 +35,6 @@ import org.springframework.web.multipart.MultipartFile; /** * MultipartFile implementation for Jakarta Commons FileUpload. * - *

NOTE: As of Spring 2.0, this class requires Commons FileUpload 1.1 - * or higher. The implementation does not use any deprecated FileUpload 1.0 API - * anymore, to be compatible with future Commons FileUpload releases. - * * @author Trevor D. Cook * @author Juergen Hoeller * @since 29.09.2003 @@ -44,6 +42,8 @@ import org.springframework.web.multipart.MultipartFile; */ public class CommonsMultipartFile implements MultipartFile, Serializable { + private static final String CONTENT_TYPE = "Content-Type"; + protected static final Log logger = LogFactory.getLog(CommonsMultipartFile.class); private final FileItem fileItem; @@ -191,4 +191,26 @@ public class CommonsMultipartFile implements MultipartFile, Serializable { } } + public String getHeader(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return this.fileItem.getContentType(); + } + else { + return null; + } + } + + public String[] getHeaders(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return new String[] {this.fileItem.getContentType()}; + } + else { + return null; + } + } + + public Iterator getHeaderNames(String name) { + return Collections.singleton(CONTENT_TYPE).iterator(); + } + } diff --git a/org.springframework.web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java b/org.springframework.web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java index ec3fa03fcda..6a3d9308993 100644 --- a/org.springframework.web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java +++ b/org.springframework.web/src/main/java/org/springframework/web/multipart/support/StandardServletMultipartResolver.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Collection; +import java.util.Iterator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.Part; @@ -29,6 +30,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.util.FileCopyUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartException; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; @@ -149,6 +151,19 @@ public class StandardServletMultipartResolver implements MultipartResolver { public void transferTo(File dest) throws IOException, IllegalStateException { this.part.write(dest.getPath()); } + + public String getHeader(String name) { + return this.part.getHeader(name); + } + + public String[] getHeaders(String name) { + Collection headers = this.part.getHeaders(name); + return (headers != null ? StringUtils.toStringArray(headers) : null); + } + + public Iterator getHeaderNames(String name) { + return this.part.getHeaderNames().iterator(); + } } } diff --git a/org.springframework.web/src/test/java/org/springframework/mock/web/MockMultipartFile.java b/org.springframework.web/src/test/java/org/springframework/mock/web/MockMultipartFile.java index 3bf5523c6ab..e63f63c4315 100644 --- a/org.springframework.web/src/test/java/org/springframework/mock/web/MockMultipartFile.java +++ b/org.springframework.web/src/test/java/org/springframework/mock/web/MockMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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,6 +20,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.util.Collections; +import java.util.Iterator; import org.springframework.util.Assert; import org.springframework.util.FileCopyUtils; @@ -39,6 +41,8 @@ import org.springframework.web.multipart.MultipartFile; */ public class MockMultipartFile implements MultipartFile { + private static final String CONTENT_TYPE = "Content-Type"; + private final String name; private String originalFilename; @@ -61,7 +65,7 @@ public class MockMultipartFile implements MultipartFile { * Create a new MockMultipartFile with the given content. * @param name the name of the file * @param contentStream the content of the file as stream - * @throws java.io.IOException if reading from the stream failed + * @throws IOException if reading from the stream failed */ public MockMultipartFile(String name, InputStream contentStream) throws IOException { this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); @@ -88,7 +92,7 @@ public class MockMultipartFile implements MultipartFile { * @param originalFilename the original filename (as on the client's machine) * @param contentType the content type (if known) * @param contentStream the content of the file as stream - * @throws java.io.IOException if reading from the stream failed + * @throws IOException if reading from the stream failed */ public MockMultipartFile(String name, String originalFilename, String contentType, InputStream contentStream) throws IOException { @@ -129,4 +133,26 @@ public class MockMultipartFile implements MultipartFile { FileCopyUtils.copy(this.content, dest); } -} \ No newline at end of file + public String getHeader(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return this.contentType; + } + else { + return null; + } + } + + public String[] getHeaders(String name) { + if (CONTENT_TYPE.equalsIgnoreCase(name)) { + return new String[] {this.contentType}; + } + else { + return null; + } + } + + public Iterator getHeaderNames(String name) { + return Collections.singleton(CONTENT_TYPE).iterator(); + } + +}