diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-06-29 10:51:20 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-06-29 10:52:52 +0200 |
commit | e27375ac9be0d8bbf5eb7bd71cf65c949ab208ce (patch) | |
tree | 98e22b655b6a9dce6b4301fca92bbdbc1084c828 /server | |
parent | 9d6b5bde5cf6be16f6547100d16e67de8b52252d (diff) | |
download | sonarqube-e27375ac9be0d8bbf5eb7bd71cf65c949ab208ce.tar.gz sonarqube-e27375ac9be0d8bbf5eb7bd71cf65c949ab208ce.zip |
SONAR-7776 Do not fail to get uploaded file when not multipart
Diffstat (limited to 'server')
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java | 20 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java | 31 |
2 files changed, 47 insertions, 4 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java index 287f8379f20..998b64d70f7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java @@ -20,12 +20,13 @@ 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; @@ -73,13 +74,24 @@ public class ServletRequest extends ValidatingRequest { @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(); @@ -98,11 +110,11 @@ public class ServletRequest extends ValidatingRequest { @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(), ""); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java b/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java index 23f033eeefb..9b7f108fdd4 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java @@ -88,9 +88,11 @@ public class ServletRequestTest { @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); @@ -100,9 +102,38 @@ public class ServletRequestTest { } @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); |