From: Julien Lancelot Date: Wed, 29 Jun 2016 08:51:20 +0000 (+0200) Subject: SONAR-7776 Do not fail to get uploaded file when not multipart X-Git-Tag: 6.0-RC1~224 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e27375ac9be0d8bbf5eb7bd71cf65c949ab208ce;p=sonarqube.git SONAR-7776 Do not fail to get uploaded file when not multipart --- 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); @@ -99,10 +101,39 @@ public class ServletRequestTest { 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);