revised Servlet 3.0 based StandardServletMultipartResolver for correct param/file distinction; added multipart content type and headers access to MultipartRequest (dropping the previous header access solution on MultipartFile); MultipartFilter uses a Servlet 3.0 based StandardServletMultipartResolver by default

This commit is contained in:
Juergen Hoeller
2011-07-20 20:46:53 +00:00
parent 21f3f59cb7
commit 571535352b
30 changed files with 619 additions and 412 deletions

View File

@@ -41,8 +41,6 @@ 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;
@@ -133,26 +131,4 @@ 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<String> getHeaderNames() {
return Collections.singleton(CONTENT_TYPE).iterator();
}
}

View File

@@ -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.
@@ -17,10 +17,13 @@
package org.springframework.mock.web;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
@@ -89,4 +92,40 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
}
public String getMultipartContentType(String paramOrFileName) {
MultipartFile file = getFile(paramOrFileName);
if (file != null) {
return file.getContentType();
}
else {
return null;
}
}
public HttpMethod getRequestMethod() {
return HttpMethod.valueOf(getMethod());
}
public HttpHeaders getRequestHeaders() {
HttpHeaders headers = new HttpHeaders();
Enumeration<String> headerNames = getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
headers.put(headerName, Collections.list(getHeaders(headerName)));
}
return headers;
}
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
String contentType = getMultipartContentType(paramOrFileName);
if (contentType != null) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", contentType);
return headers;
}
else {
return null;
}
}
}

View File

@@ -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.
@@ -72,7 +72,7 @@ public class MockMultipartActionRequest extends MockActionRequest implements Mul
else {
return Collections.emptyList();
}
}
}
public Map<String, MultipartFile> getFileMap() {
return this.multipartFiles.toSingleValueMap();
@@ -82,4 +82,14 @@ public class MockMultipartActionRequest extends MockActionRequest implements Mul
return new LinkedMultiValueMap<String, MultipartFile>(this.multipartFiles);
}
public String getMultipartContentType(String paramOrFileName) {
MultipartFile file = getFile(paramOrFileName);
if (file != null) {
return file.getContentType();
}
else {
return null;
}
}
}