Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4736 50f2f4bb-b051-0410-bef5-90022cba6387pull/2/head
30 changed files with 619 additions and 412 deletions
@ -1,98 +0,0 @@
@@ -1,98 +0,0 @@
|
||||
/* |
||||
* 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. |
||||
* 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.web.multipart; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.net.URI; |
||||
import java.net.URISyntaxException; |
||||
import java.util.Iterator; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpMethod; |
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* {@link ServerHttpRequest} implementation that is based on a part of a {@link MultipartHttpServletRequest}. |
||||
* The part is accessed as {@link MultipartFile} and adapted to the ServerHttpRequest contract. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 3.1 |
||||
*/ |
||||
public class RequestPartServletServerHttpRequest implements ServerHttpRequest { |
||||
|
||||
private final MultipartHttpServletRequest request; |
||||
|
||||
private final MultipartFile multipartFile; |
||||
|
||||
private HttpHeaders headers; |
||||
|
||||
/** |
||||
* Creates a new {@link RequestPartServletServerHttpRequest} instance. |
||||
* |
||||
* @param request the multipart request. |
||||
* @param name the name of the part to adapt to the {@link ServerHttpRequest} contract. |
||||
*/ |
||||
public RequestPartServletServerHttpRequest(MultipartHttpServletRequest request, String name) { |
||||
this.request = request; |
||||
this.multipartFile = request.getFile(name); |
||||
Assert.notNull(multipartFile, "Request part named '" + name + "' not found. " + |
||||
"Available request part names: " + request.getMultiFileMap().keySet()); |
||||
|
||||
} |
||||
|
||||
public HttpMethod getMethod() { |
||||
return HttpMethod.valueOf(this.request.getMethod()); |
||||
} |
||||
|
||||
public URI getURI() { |
||||
try { |
||||
return new URI(this.request.getScheme(), null, this.request.getServerName(), |
||||
this.request.getServerPort(), this.request.getRequestURI(), |
||||
this.request.getQueryString(), null); |
||||
} |
||||
catch (URISyntaxException ex) { |
||||
throw new IllegalStateException("Could not get HttpServletRequest URI: " + ex.getMessage(), ex); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Returns the headers associated with the part of the multi-part request associated with this instance. |
||||
* If the underlying implementation supports access to headers, then all headers are returned. |
||||
* Otherwise, the returned headers will have a 'Content-Type' header in the very least. |
||||
*/ |
||||
public HttpHeaders getHeaders() { |
||||
if (this.headers == null) { |
||||
this.headers = new HttpHeaders(); |
||||
Iterator<String> iterator = this.multipartFile.getHeaderNames(); |
||||
while (iterator.hasNext()) { |
||||
String name = iterator.next(); |
||||
String[] values = this.multipartFile.getHeaders(name); |
||||
for (String value : values) { |
||||
this.headers.add(name, value); |
||||
} |
||||
} |
||||
} |
||||
return this.headers; |
||||
} |
||||
|
||||
public InputStream getBody() throws IOException { |
||||
return this.multipartFile.getInputStream(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,94 @@
@@ -0,0 +1,94 @@
|
||||
/* |
||||
* 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. |
||||
* 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.web.multipart.support; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.server.ServerHttpRequest; |
||||
import org.springframework.http.server.ServletServerHttpRequest; |
||||
import org.springframework.web.multipart.MultipartException; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import org.springframework.web.multipart.MultipartHttpServletRequest; |
||||
|
||||
/** |
||||
* {@link ServerHttpRequest} implementation that is based on a part of a {@link MultipartHttpServletRequest}. |
||||
* The part is accessed as {@link MultipartFile} and adapted to the ServerHttpRequest contract. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Juergen Hoeller |
||||
* @since 3.1 |
||||
*/ |
||||
public class RequestPartServletServerHttpRequest extends ServletServerHttpRequest { |
||||
|
||||
private final MultipartHttpServletRequest multipartRequest; |
||||
|
||||
private final String partName; |
||||
|
||||
private final HttpHeaders headers; |
||||
|
||||
|
||||
/** |
||||
* Create a new {@link RequestPartServletServerHttpRequest} instance. |
||||
* @param request the multipart request |
||||
* @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract |
||||
*/ |
||||
public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName) { |
||||
super(request); |
||||
|
||||
this.multipartRequest = (request instanceof MultipartHttpServletRequest ? |
||||
(MultipartHttpServletRequest) request : new StandardMultipartHttpServletRequest(request)); |
||||
this.partName = partName; |
||||
|
||||
this.headers = this.multipartRequest.getMultipartHeaders(this.partName); |
||||
if (this.headers == null) { |
||||
throw new IllegalArgumentException("No request part found for name '" + this.partName + "'"); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public HttpHeaders getHeaders() { |
||||
return this.headers; |
||||
} |
||||
|
||||
@Override |
||||
public InputStream getBody() throws IOException { |
||||
if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { |
||||
try { |
||||
return this.multipartRequest.getPart(this.partName).getInputStream(); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new MultipartException("Could not parse multipart servlet request", ex); |
||||
} |
||||
} |
||||
else { |
||||
MultipartFile file = this.multipartRequest.getFile(this.partName); |
||||
if (file != null) { |
||||
return file.getInputStream(); |
||||
} |
||||
else { |
||||
String paramValue = this.multipartRequest.getParameter(this.partName); |
||||
return new ByteArrayInputStream(paramValue.getBytes(FORM_CHARSET)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,175 @@
@@ -0,0 +1,175 @@
|
||||
/* |
||||
* 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. |
||||
* 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.web.multipart.support; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.Collection; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.Part; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.util.FileCopyUtils; |
||||
import org.springframework.util.LinkedMultiValueMap; |
||||
import org.springframework.util.MultiValueMap; |
||||
import org.springframework.web.multipart.MultipartException; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
|
||||
/** |
||||
* Spring MultipartHttpServletRequest adapter, wrapping a Servlet 3.0 HttpServletRequest |
||||
* and its Part objects. Parameters get exposed through the native request's getParameter |
||||
* methods - without any custom processing on our side. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 3.1 |
||||
*/ |
||||
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest { |
||||
|
||||
private static final String CONTENT_DISPOSITION = "Content-Disposition"; |
||||
|
||||
private static final String FILENAME_KEY = "filename="; |
||||
|
||||
|
||||
/** |
||||
* Create a new StandardMultipartHttpServletRequest wrapper for the given request. |
||||
* @param request the servlet request to wrap |
||||
* @throws MultipartException if parsing failed |
||||
*/ |
||||
public StandardMultipartHttpServletRequest(HttpServletRequest request) throws MultipartException { |
||||
super(request); |
||||
try { |
||||
Collection<Part> parts = request.getParts(); |
||||
MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<String, MultipartFile>(parts.size()); |
||||
for (Part part : parts) { |
||||
String filename = extractFilename(part.getHeader(CONTENT_DISPOSITION)); |
||||
if (filename != null) { |
||||
files.add(part.getName(), new StandardMultipartFile(part, filename)); |
||||
} |
||||
} |
||||
setMultipartFiles(files); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new MultipartException("Could not parse multipart servlet request", ex); |
||||
} |
||||
} |
||||
|
||||
private String extractFilename(String contentDisposition) { |
||||
if (contentDisposition == null) { |
||||
return null; |
||||
} |
||||
// TODO: can only handle the typical case at the moment
|
||||
int startIndex = contentDisposition.indexOf(FILENAME_KEY); |
||||
if (startIndex == -1) { |
||||
return null; |
||||
} |
||||
String filename = contentDisposition.substring(startIndex + FILENAME_KEY.length()); |
||||
if (filename.startsWith("\"")) { |
||||
int endIndex = filename.indexOf("\"", 1); |
||||
if (endIndex != -1) { |
||||
return filename.substring(1, endIndex); |
||||
} |
||||
} |
||||
else { |
||||
int endIndex = filename.indexOf(";"); |
||||
if (endIndex != -1) { |
||||
return filename.substring(0, endIndex); |
||||
} |
||||
} |
||||
return filename; |
||||
} |
||||
|
||||
|
||||
public String getMultipartContentType(String paramOrFileName) { |
||||
try { |
||||
Part part = getPart(paramOrFileName); |
||||
return (part != null ? part.getContentType() : null); |
||||
} |
||||
catch (Exception ex) { |
||||
throw new MultipartException("Could not access multipart servlet request", ex); |
||||
} |
||||
} |
||||
|
||||
public HttpHeaders getMultipartHeaders(String paramOrFileName) { |
||||
try { |
||||
Part part = getPart(paramOrFileName); |
||||
if (part != null) { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
for (String headerName : part.getHeaderNames()) { |
||||
headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName))); |
||||
} |
||||
return headers; |
||||
} |
||||
else { |
||||
return null; |
||||
} |
||||
} |
||||
catch (Exception ex) { |
||||
throw new MultipartException("Could not access multipart servlet request", ex); |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Spring MultipartFile adapter, wrapping a Servlet 3.0 Part object. |
||||
*/ |
||||
private static class StandardMultipartFile implements MultipartFile { |
||||
|
||||
private final Part part; |
||||
|
||||
private final String filename; |
||||
|
||||
public StandardMultipartFile(Part part, String filename) { |
||||
this.part = part; |
||||
this.filename = filename; |
||||
} |
||||
|
||||
public String getName() { |
||||
return this.part.getName(); |
||||
} |
||||
|
||||
public String getOriginalFilename() { |
||||
return this.filename; |
||||
} |
||||
|
||||
public String getContentType() { |
||||
return this.part.getContentType(); |
||||
} |
||||
|
||||
public boolean isEmpty() { |
||||
return (this.part.getSize() == 0); |
||||
} |
||||
|
||||
public long getSize() { |
||||
return this.part.getSize(); |
||||
} |
||||
|
||||
public byte[] getBytes() throws IOException { |
||||
return FileCopyUtils.copyToByteArray(this.part.getInputStream()); |
||||
} |
||||
|
||||
public InputStream getInputStream() throws IOException { |
||||
return this.part.getInputStream(); |
||||
} |
||||
|
||||
public void transferTo(File dest) throws IOException, IllegalStateException { |
||||
this.part.write(dest.getPath()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue