package org.sonar.server.ws;
import static com.google.common.base.MoreObjects.firstNonNull;
+import static java.util.Locale.ENGLISH;
import static org.apache.commons.lang.StringUtils.substringAfterLast;
+import static org.apache.tomcat.util.http.fileupload.FileUploadBase.MULTIPART;
import com.google.common.collect.ImmutableMap;
import com.google.common.net.HttpHeaders;
import java.io.InputStream;
-import java.util.Locale;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.servlet.http.HttpServletRequest;
@Override
protected InputStream readInputStreamParam(String key) {
try {
+ if (!isMultipartContent()) {
+ return null;
+ }
Part part = source.getPart(key);
- return part == null ? null : part.getInputStream();
+ if (part == null || part.getSize() == 0) {
+ return null;
+ }
+ return part.getInputStream();
} catch (Exception e) {
throw new IllegalStateException("Can't read file part", e);
}
}
+ private boolean isMultipartContent() {
+ String contentType = source.getContentType();
+ return contentType != null && contentType.toLowerCase(ENGLISH).startsWith(MULTIPART);
+ }
+
@Override
public String toString() {
StringBuffer url = source.getRequestURL();
@CheckForNull
private static String mediaTypeFromUrl(String url) {
String formatSuffix = substringAfterLast(url, ".");
- return SUPPORTED_MEDIA_TYPES_BY_URL_SUFFIX.get(formatSuffix.toLowerCase(Locale.ENGLISH));
+ return SUPPORTED_MEDIA_TYPES_BY_URL_SUFFIX.get(formatSuffix.toLowerCase(ENGLISH));
}
@Override
- public String getPath(){
+ public String getPath() {
return source.getRequestURI().replaceFirst(source.getContextPath(), "");
}
}
@Test
public void read_input_stream() throws Exception {
+ when(source.getContentType()).thenReturn("multipart/form-data");
InputStream file = mock(InputStream.class);
Part part = mock(Part.class);
when(part.getInputStream()).thenReturn(file);
+ when(part.getSize()).thenReturn(10L);
when(source.getPart("param1")).thenReturn(part);
ServletRequest request = new ServletRequest(source);
assertThat(request.readInputStreamParam("param2")).isNull();
}
+ @Test
+ public void read_no_input_stream_when_part_size_is_zero() throws Exception {
+ when(source.getContentType()).thenReturn("multipart/form-data");
+ InputStream file = mock(InputStream.class);
+ Part part = mock(Part.class);
+ when(part.getInputStream()).thenReturn(file);
+ when(part.getSize()).thenReturn(0L);
+ when(source.getPart("param1")).thenReturn(part);
+
+ ServletRequest request = new ServletRequest(source);
+ assertThat(request.readInputStreamParam("param1")).isNull();
+ }
+
+ @Test
+ public void return_no_input_stream_when_content_type_is_not_multipart() throws Exception {
+ when(source.getContentType()).thenReturn("multipart/form-data");
+ ServletRequest request = new ServletRequest(source);
+ assertThat(request.readInputStreamParam("param1")).isNull();
+ }
+
+ @Test
+ public void return_no_input_stream_when_content_type_is_null() throws Exception {
+ when(source.getContentType()).thenReturn(null);
+ ServletRequest request = new ServletRequest(source);
+ assertThat(request.readInputStreamParam("param1")).isNull();
+ }
+
@Test
public void throw_ISE_when_invalid_part() throws Exception {
+ when(source.getContentType()).thenReturn("multipart/form-data");
InputStream file = mock(InputStream.class);
Part part = mock(Part.class);
+ when(part.getSize()).thenReturn(0L);
when(part.getInputStream()).thenReturn(file);
doThrow(IllegalArgumentException.class).when(source).getPart("param1");
ServletRequest request = new ServletRequest(source);