|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2017 the original author or authors. |
|
|
|
* Copyright 2002-2020 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -22,11 +22,13 @@ import java.io.InputStream; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
import java.nio.charset.Charset; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.http.Part; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.server.ServerHttpRequest; |
|
|
|
import org.springframework.http.server.ServerHttpRequest; |
|
|
|
import org.springframework.http.server.ServletServerHttpRequest; |
|
|
|
import org.springframework.http.server.ServletServerHttpRequest; |
|
|
|
|
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.web.multipart.MultipartException; |
|
|
|
import org.springframework.web.multipart.MultipartException; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest; |
|
|
|
import org.springframework.web.multipart.MultipartHttpServletRequest; |
|
|
|
@ -46,59 +48,79 @@ public class RequestPartServletServerHttpRequest extends ServletServerHttpReques |
|
|
|
|
|
|
|
|
|
|
|
private final MultipartHttpServletRequest multipartRequest; |
|
|
|
private final MultipartHttpServletRequest multipartRequest; |
|
|
|
|
|
|
|
|
|
|
|
private final String partName; |
|
|
|
private final String requestPartName; |
|
|
|
|
|
|
|
|
|
|
|
private final HttpHeaders headers; |
|
|
|
private final HttpHeaders multipartHeaders; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new {@code RequestPartServletServerHttpRequest} instance. |
|
|
|
* Create a new {@code RequestPartServletServerHttpRequest} instance. |
|
|
|
* @param request the current servlet request |
|
|
|
* @param request the current servlet request |
|
|
|
* @param partName the name of the part to adapt to the {@link ServerHttpRequest} contract |
|
|
|
* @param requestPartName the name of the part to adapt to the {@link ServerHttpRequest} contract |
|
|
|
* @throws MissingServletRequestPartException if the request part cannot be found |
|
|
|
* @throws MissingServletRequestPartException if the request part cannot be found |
|
|
|
* @throws MultipartException if MultipartHttpServletRequest cannot be initialized |
|
|
|
* @throws MultipartException if MultipartHttpServletRequest cannot be initialized |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public RequestPartServletServerHttpRequest(HttpServletRequest request, String partName) |
|
|
|
public RequestPartServletServerHttpRequest(HttpServletRequest request, String requestPartName) |
|
|
|
throws MissingServletRequestPartException { |
|
|
|
throws MissingServletRequestPartException { |
|
|
|
|
|
|
|
|
|
|
|
super(request); |
|
|
|
super(request); |
|
|
|
|
|
|
|
|
|
|
|
this.multipartRequest = MultipartResolutionDelegate.asMultipartHttpServletRequest(request); |
|
|
|
this.multipartRequest = MultipartResolutionDelegate.asMultipartHttpServletRequest(request); |
|
|
|
this.partName = partName; |
|
|
|
this.requestPartName = requestPartName; |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = this.multipartRequest.getMultipartHeaders(this.partName); |
|
|
|
HttpHeaders multipartHeaders = this.multipartRequest.getMultipartHeaders(this.requestPartName); |
|
|
|
if (headers == null) { |
|
|
|
if (multipartHeaders == null) { |
|
|
|
throw new MissingServletRequestPartException(partName); |
|
|
|
throw new MissingServletRequestPartException(requestPartName); |
|
|
|
} |
|
|
|
} |
|
|
|
this.headers = headers; |
|
|
|
this.multipartHeaders = multipartHeaders; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public HttpHeaders getHeaders() { |
|
|
|
public HttpHeaders getHeaders() { |
|
|
|
return this.headers; |
|
|
|
return this.multipartHeaders; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public InputStream getBody() throws IOException { |
|
|
|
public InputStream getBody() throws IOException { |
|
|
|
if (this.multipartRequest instanceof StandardMultipartHttpServletRequest) { |
|
|
|
// Prefer Servlet Part resolution to cover file as well as parameter streams
|
|
|
|
try { |
|
|
|
boolean servletParts = (this.multipartRequest instanceof StandardMultipartHttpServletRequest); |
|
|
|
return this.multipartRequest.getPart(this.partName).getInputStream(); |
|
|
|
if (servletParts) { |
|
|
|
} |
|
|
|
Part part = retrieveServletPart(); |
|
|
|
catch (Exception ex) { |
|
|
|
if (part != null) { |
|
|
|
throw new MultipartException("Could not parse multipart servlet request", ex); |
|
|
|
return part.getInputStream(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
MultipartFile file = this.multipartRequest.getFile(this.partName); |
|
|
|
// Spring-style distinction between MultipartFile and String parameters
|
|
|
|
if (file != null) { |
|
|
|
MultipartFile file = this.multipartRequest.getFile(this.requestPartName); |
|
|
|
return file.getInputStream(); |
|
|
|
if (file != null) { |
|
|
|
} |
|
|
|
return file.getInputStream(); |
|
|
|
else { |
|
|
|
} |
|
|
|
String paramValue = this.multipartRequest.getParameter(this.partName); |
|
|
|
String paramValue = this.multipartRequest.getParameter(this.requestPartName); |
|
|
|
return new ByteArrayInputStream(paramValue.getBytes(determineCharset())); |
|
|
|
if (paramValue != null) { |
|
|
|
|
|
|
|
return new ByteArrayInputStream(paramValue.getBytes(determineCharset())); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback: Servlet Part resolution even if not indicated
|
|
|
|
|
|
|
|
if (!servletParts) { |
|
|
|
|
|
|
|
Part part = retrieveServletPart(); |
|
|
|
|
|
|
|
if (part != null) { |
|
|
|
|
|
|
|
return part.getInputStream(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
|
|
|
private Part retrieveServletPart() { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
return this.multipartRequest.getPart(this.requestPartName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) { |
|
|
|
|
|
|
|
throw new MultipartException("Failed to retrieve request part '" + this.requestPartName + "'", ex); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Charset determineCharset() { |
|
|
|
private Charset determineCharset() { |
|
|
|
|