]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7776 Do not fail to get uploaded file when not multipart
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 29 Jun 2016 08:51:20 +0000 (10:51 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 29 Jun 2016 08:52:52 +0000 (10:52 +0200)
server/sonar-server/src/main/java/org/sonar/server/ws/ServletRequest.java
server/sonar-server/src/test/java/org/sonar/server/ws/ServletRequestTest.java

index 287f8379f20b8fb5752df3fd7fece3c4eec5d0ca..998b64d70f7447af2323a627f293bd6a9bf1884a 100644 (file)
 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(), "");
   }
 }
index 23f033eeefb371e8763eb524b4a353eedc3679f7..9b7f108fdd485bf619cbe951759ba18c1b650971 100644 (file)
@@ -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);