You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2014/10/28 10:39:54 UTC

[1/2] git commit: [OLINGO-469] Fix: Batch boundary content type with multiple parameters

Repository: olingo-odata2
Updated Branches:
  refs/heads/master 9dd4de726 -> 053b324c0


[OLINGO-469] Fix: Batch boundary content type with multiple parameters

Signed-off-by: Christian Amend <ch...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/7df31f55
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/7df31f55
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/7df31f55

Branch: refs/heads/master
Commit: 7df31f5578f5cf922df4803ec5c534b0cf62cd5a
Parents: 9dd4de7
Author: Christian Holzer <c....@sap.com>
Authored: Mon Oct 27 17:25:16 2014 +0100
Committer: Christian Amend <ch...@apache.org>
Committed: Tue Oct 28 10:19:20 2014 +0100

----------------------------------------------------------------------
 .../odata2/core/batch/v2/BatchBodyPart.java     |  2 +-
 .../odata2/core/batch/v2/BatchParserCommon.java | 32 +++++++++++---------
 .../core/batch/v2/BatchTransformatorCommon.java |  2 +-
 .../core/batch/BatchRequestParserTest.java      | 15 ++++++++-
 .../olingo/odata2/core/batch/HeaderTest.java    |  4 +--
 5 files changed, 36 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7df31f55/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchBodyPart.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchBodyPart.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchBodyPart.java
index d60c29a..bfadac9 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchBodyPart.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchBodyPart.java
@@ -70,7 +70,7 @@ public class BatchBodyPart implements BatchPart {
   }
 
   private boolean isContentTypeMultiPartMixed(final String contentType) {
-    return BatchParserCommon.PATTERN_MULTIPART_BOUNDARY.matcher(contentType).matches();
+    return BatchParserCommon.PATTERN_MULTIPART_MIXED.matcher(contentType).matches();
   }
 
   private List<BatchQueryOperation> consumeRequest(final List<Line> remainingMessage) throws BatchException {

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7df31f55/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchParserCommon.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchParserCommon.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchParserCommon.java
index c2751ee..b36f873 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchParserCommon.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchParserCommon.java
@@ -45,10 +45,9 @@ public class BatchParserCommon {
       "([a-zA-Z0-9_\\-\\.'\\+]{1,70})|\"([a-zA-Z0-9_\\-\\.'\\+\\s\\" +
           "(\\),/:=\\?]{1,69}[a-zA-Z0-9_\\-\\.'\\+\\(\\),/:=\\?])\""; // See RFC 2046
 
-  private static final String REX_EX_MULTIPART_BOUNDARY = "multipart/mixed;\\s*boundary=(.+)";
-  private static final String REG_EX_APPLICATION_HTTP = "application/http";
-  public static final Pattern PATTERN_MULTIPART_BOUNDARY = Pattern.compile(REX_EX_MULTIPART_BOUNDARY,
-      Pattern.CASE_INSENSITIVE);
+  public static final Pattern PATTERN_MULTIPART_MIXED = Pattern
+      .compile("multipart/mixed(.*)", Pattern.CASE_INSENSITIVE);
+  final static String REG_EX_APPLICATION_HTTP = "application/http";
   public static final Pattern PATTERN_HEADER_LINE = Pattern.compile("([a-zA-Z\\-]+):\\s?(.*)\\s*");
   public static final Pattern PATTERN_CONTENT_TYPE_APPLICATION_HTTP = Pattern.compile(REG_EX_APPLICATION_HTTP,
       Pattern.CASE_INSENSITIVE);
@@ -204,18 +203,23 @@ public class BatchParserCommon {
   }
 
   public static String getBoundary(final String contentType, final int line) throws BatchException {
-    final Matcher boundaryMatcher = PATTERN_MULTIPART_BOUNDARY.matcher(contentType);
+    if (contentType.toLowerCase(Locale.ENGLISH).startsWith("multipart/mixed")) {
+      final String[] parameter = contentType.split(";");
+
+      for (final String pair : parameter) {
+
+        final String[] attrValue = pair.split("=");
+        if (attrValue.length == 2 && "boundary".equals(attrValue[0].trim().toLowerCase(Locale.ENGLISH))) {
+          if (attrValue[1].matches(REG_EX_BOUNDARY)) {
+            return trimQuota(attrValue[1].trim());
+          } else {
+            throw new BatchException(BatchException.INVALID_BOUNDARY.addContent(line));
+          }
+        }
 
-    if (boundaryMatcher.matches()) {
-      final String boundary = boundaryMatcher.group(1);
-      if (boundary.matches(REG_EX_BOUNDARY)) {
-        return trimQuota(boundary);
-      } else {
-        throw new BatchException(BatchException.INVALID_BOUNDARY.addContent(line));
       }
-    } else {
-      throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED));
     }
+    throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED));
   }
 
   private static String trimQuota(String boundary) {
@@ -225,7 +229,7 @@ public class BatchParserCommon {
 
     return boundary;
   }
-  
+
   public static Map<String, List<String>> parseQueryParameter(final Line httpRequest) {
     Map<String, List<String>> queryParameter = new HashMap<String, List<String>>();
 

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7df31f55/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchTransformatorCommon.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchTransformatorCommon.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchTransformatorCommon.java
index c07c06f..a0e05c5 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchTransformatorCommon.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/v2/BatchTransformatorCommon.java
@@ -47,7 +47,7 @@ public class BatchTransformatorCommon {
     if (contentTypes.size() == 0) {
       throw new BatchException(BatchException.MISSING_CONTENT_TYPE);
     }
-    if (!headers.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_BOUNDARY)
+    if (!headers.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_MIXED)
       & !headers.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_CONTENT_TYPE_APPLICATION_HTTP)) {
       throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(
           HttpContentType.MULTIPART_MIXED + " or " + HttpContentType.APPLICATION_HTTP));

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7df31f55/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
index 447049e..69ca695 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
@@ -354,7 +354,20 @@ public class BatchRequestParserTest {
         + "--batch_8194-cf13-1f56--";
     parseInvalidBatchBody(batch);
   }
-
+  
+  @Test
+  public void testContentTypeCharset() throws BatchException {
+    final String contentType = "multipart/mixed; charset=UTF-8;boundary=batch_14d1-b293-b99a";
+    final String batch = ""
+                    + "--batch_14d1-b293-b99a" + CRLF
+                    + GET_REQUEST
+                    + "--batch_14d1-b293-b99a--";
+    final BatchParser parser = new BatchParser(contentType, batchProperties, true);
+    final List<BatchRequestPart> parts = parser.parseBatchRequest(new ByteArrayInputStream(batch.getBytes()));
+    
+    assertEquals(1, parts.size());
+  }
+  
   @Test(expected = BatchException.class)
   public void testNoContentType() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7df31f55/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java
index 9561567..e630a54 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java
@@ -76,7 +76,7 @@ public class HeaderTest {
     Header header = new Header(1);
     header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + ";boundary=123", 1);
 
-    assertTrue(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_BOUNDARY));
+    assertTrue(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_MIXED));
   }
 
   @Test
@@ -110,7 +110,7 @@ public class HeaderTest {
   public void testMatcherNoHeader() {
     Header header = new Header(1);
 
-    assertFalse(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_BOUNDARY));
+    assertFalse(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE,  BatchParserCommon.PATTERN_MULTIPART_MIXED));
   }
 
   @Test


[2/2] git commit: [OLINGO-469] Additional negative tests

Posted by ch...@apache.org.
[OLINGO-469] Additional negative tests


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/053b324c
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/053b324c
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/053b324c

Branch: refs/heads/master
Commit: 053b324c06338dcce71bf8427255c6f2e686db41
Parents: 7df31f5
Author: Christian Amend <ch...@apache.org>
Authored: Tue Oct 28 10:39:07 2014 +0100
Committer: Christian Amend <ch...@apache.org>
Committed: Tue Oct 28 10:39:07 2014 +0100

----------------------------------------------------------------------
 .../core/batch/BatchRequestParserTest.java      | 80 +++++++++++++-------
 1 file changed, 53 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/053b324c/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
index 69ca695..1a21c0b 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchRequestParserTest.java
@@ -6,9 +6,9 @@
  * to you under the Apache License, Version 2.0 (the
  * "License"); you may not use this file except in compliance
  * with the License. You may obtain a copy of the License at
- *
+ * 
  * http://www.apache.org/licenses/LICENSE-2.0
- *
+ * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -118,7 +118,7 @@ public class BatchRequestParserTest {
       }
     }
   }
-  
+
   @Test
   public void testImageInContent() throws IOException, BatchException, URISyntaxException {
     String fileName = "/batchWithContent.batch";
@@ -270,7 +270,7 @@ public class BatchRequestParserTest {
     parseInvalidBatchBody(batch);
   }
 
-  @Test(expected=BatchException.class)
+  @Test(expected = BatchException.class)
   public void testMissingHttpVersion() throws BatchException {
     String batch = ""
         + "--batch_8194-cf13-1f56" + CRLF
@@ -281,11 +281,11 @@ public class BatchRequestParserTest {
         + "Host: localhost:8080" + CRLF
         + CRLF
         + "--batch_8194-cf13-1f56--";
-    
+
     parseInvalidBatchBody(batch);
   }
-  
-  @Test(expected=BatchException.class)
+
+  @Test(expected = BatchException.class)
   public void testMissingHttpVersion2() throws BatchException {
     String batch = ""
         + "--batch_8194-cf13-1f56" + CRLF
@@ -296,11 +296,11 @@ public class BatchRequestParserTest {
         + "Host: localhost:8080" + CRLF
         + CRLF
         + "--batch_8194-cf13-1f56--";
-    
+
     parseInvalidBatchBody(batch);
   }
-  
-  @Test(expected=BatchException.class)
+
+  @Test(expected = BatchException.class)
   public void testMissingHttpVersion3() throws BatchException {
     String batch = ""
         + "--batch_8194-cf13-1f56" + CRLF
@@ -311,10 +311,10 @@ public class BatchRequestParserTest {
         + "Host: localhost:8080" + CRLF
         + CRLF
         + "--batch_8194-cf13-1f56--";
-    
+
     parseInvalidBatchBody(batch);
   }
-  
+
   @Test(expected = BatchException.class)
   public void testBoundaryWithoutHyphen() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF
@@ -354,20 +354,46 @@ public class BatchRequestParserTest {
         + "--batch_8194-cf13-1f56--";
     parseInvalidBatchBody(batch);
   }
-  
+
   @Test
   public void testContentTypeCharset() throws BatchException {
     final String contentType = "multipart/mixed; charset=UTF-8;boundary=batch_14d1-b293-b99a";
     final String batch = ""
-                    + "--batch_14d1-b293-b99a" + CRLF
-                    + GET_REQUEST
-                    + "--batch_14d1-b293-b99a--";
+        + "--batch_14d1-b293-b99a" + CRLF
+        + GET_REQUEST
+        + "--batch_14d1-b293-b99a--";
     final BatchParser parser = new BatchParser(contentType, batchProperties, true);
     final List<BatchRequestPart> parts = parser.parseBatchRequest(new ByteArrayInputStream(batch.getBytes()));
-    
+
     assertEquals(1, parts.size());
   }
-  
+
+  @Test
+  public void testContentTypeCharsetWrongBoundaryAtEnd() throws BatchException {
+    final String contentType = "multipart/mixed; charset=UTF-8;boundary=batch_14d1-b293-b99a;boundary=wrong_boundary";
+    final String batch = ""
+        + "--batch_14d1-b293-b99a" + CRLF
+        + GET_REQUEST
+        + "--batch_14d1-b293-b99a--";
+    final BatchParser parser = new BatchParser(contentType, batchProperties, true);
+    final List<BatchRequestPart> parts = parser.parseBatchRequest(new ByteArrayInputStream(batch.getBytes()));
+
+    assertEquals(1, parts.size());
+  }
+
+  @Test(expected = BatchException.class)
+  public void testContentTypeCharsetWrongBoundaryAtBeginning() throws BatchException {
+    final String contentType = "multipart/mixed; charset=UTF-8;boundary=wrong_boundary;boundary=batch_14d1-b293-b99a";
+    final String batch = ""
+        + "--batch_14d1-b293-b99a" + CRLF
+        + GET_REQUEST
+        + "--batch_14d1-b293-b99a--";
+    final BatchParser parser = new BatchParser(contentType, batchProperties, true);
+    final List<BatchRequestPart> parts = parser.parseBatchRequest(new ByteArrayInputStream(batch.getBytes()));
+
+    assertEquals(1, parts.size());
+  }
+
   @Test(expected = BatchException.class)
   public void testNoContentType() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF
@@ -402,7 +428,7 @@ public class BatchRequestParserTest {
         + "--batch_8194-cf13-1f56--";
     parseInvalidBatchBody(batch);
   }
-  
+
   @Test(expected = BatchException.class)
   public void testGetRequestMissingCRLF() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF
@@ -410,13 +436,13 @@ public class BatchRequestParserTest {
         + "Content-ID: 1" + CRLF
         + CRLF
         + "GET Employees('1')/EmployeeName HTTP/1.1" + CRLF
-        //+ CRLF  // Belongs to the GET request
-        + CRLF    //Belongs to the 
+        // + CRLF // Belongs to the GET request
+        + CRLF // Belongs to the
         + "--batch_8194-cf13-1f56--";
-    
+
     parseInvalidBatchBody(batch);
   }
-  
+
   @Test(expected = BatchException.class)
   public void testInvalidMethodForBatch() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF
@@ -471,7 +497,7 @@ public class BatchRequestParserTest {
     parseInvalidBatchBody(batch);
   }
 
-  @Test(expected=BatchException.class)
+  @Test(expected = BatchException.class)
   public void testInvalidChangeSetBoundary() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF
         + "Content-Type: multipart/mixed;boundary=changeset_f980-1cb6-94dd" + CRLF
@@ -488,8 +514,8 @@ public class BatchRequestParserTest {
         + "--batch_8194-cf13-1f56--";
     parseInvalidBatchBody(batch);
   }
-  
-  @Test(expected=BatchException.class)
+
+  @Test(expected = BatchException.class)
   public void testNestedChangeset() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF
         + "Content-Type: multipart/mixed;boundary=changeset_f980-1cb6-94dd" + CRLF
@@ -513,7 +539,7 @@ public class BatchRequestParserTest {
         + "--batch_8194-cf13-1f56--";
     parse(batch);
   }
-  
+
   @Test(expected = BatchException.class)
   public void testMissingContentTransferEncoding() throws BatchException {
     String batch = "--batch_8194-cf13-1f56" + CRLF