olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [33/38] git commit: [OLINGO-256] Minor code clean up
Date Sun, 18 May 2014 05:07:01 GMT
[OLINGO-256] Minor code clean up


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

Branch: refs/heads/Olingo-129_PocJpaDataStore
Commit: 1ac8e7d33152fc5a03bbb6e4cd42537cf130743d
Parents: a938080
Author: Michael Bolz <mibo@apache.org>
Authored: Tue Apr 29 11:09:44 2014 +0200
Committer: Michael Bolz <mibo@apache.org>
Committed: Tue Apr 29 11:09:44 2014 +0200

----------------------------------------------------------------------
 .../odata2/core/batch/BatchRequestParser.java   | 84 +++++++++++---------
 .../odata2/core/batch/BatchRequestWriter.java   | 13 +--
 2 files changed, 55 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1ac8e7d3/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
index 1211cf8..6584ee9 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestParser.java
@@ -155,8 +155,7 @@ public class BatchRequestParser {
 
   private BatchRequestPart parseMultipart(final Scanner scanner, final String boundary, final
boolean isChangeSet)
       throws BatchException {
-    List<ODataRequest> requests = new ArrayList<ODataRequest>();
-    BatchRequestPart multipart;
+
     if (scanner.hasNext("--" + boundary + REG_EX_ZERO_OR_MORE_WHITESPACES)) {
       scanner.next();
       currentLineNumber++;
@@ -168,41 +167,9 @@ public class BatchRequestParser {
         throw new BatchException(BatchException.MISSING_CONTENT_TYPE);
       }
       if (isChangeSet) {
-        if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
-          validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
-          parseNewLine(scanner);// mandatory
-
-          requests.add(parseRequest(scanner, true, boundary));
-          multipart = new BatchRequestPartImpl(false, requests);
-        } else {
-          throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.APPLICATION_HTTP));
-        }
+        return parseBatchRequestPartInChangeset(scanner, boundary, mimeHeaders, contentType);
       } else {
-        if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
-          validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
-          parseNewLine(scanner);// mandatory
-          requests.add(parseRequest(scanner, false, boundary));
-          multipart = new BatchRequestPartImpl(false, requests);
-        } else if (contentType.matches(REG_EX_OPTIONAL_WHITESPACE + HttpContentType.MULTIPART_MIXED
+ ANY_CHARACTERS)) {
-          String changeSetBoundary = getBoundary(contentType);
-          if (boundary.equals(changeSetBoundary)) {
-            throw new BatchException(BatchException.INVALID_CHANGESET_BOUNDARY.addContent(currentLineNumber));
-          }
-          List<ODataRequest> changeSetRequests = new LinkedList<ODataRequest>();
-          parseNewLine(scanner);// mandatory
-          Pattern changeSetCloseDelimiter =
-              Pattern.compile("--" + changeSetBoundary + "--" + REG_EX_ZERO_OR_MORE_WHITESPACES);
-          while (!scanner.hasNext(changeSetCloseDelimiter)) {
-            BatchRequestPart part = parseMultipart(scanner, changeSetBoundary, true);
-            changeSetRequests.addAll(part.getRequests());
-          }
-          scanner.next(changeSetCloseDelimiter);
-          currentLineNumber++;
-          multipart = new BatchRequestPartImpl(true, changeSetRequests);
-        } else {
-          throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED
-              + " or " + HttpContentType.APPLICATION_HTTP));
-        }
+        return parseBatchRequestPart(scanner, boundary, mimeHeaders, contentType);
       }
     } else if (scanner.hasNext(boundary + REG_EX_ZERO_OR_MORE_WHITESPACES)) {
       currentLineNumber++;
@@ -215,8 +182,51 @@ public class BatchRequestParser {
       currentLineNumber++;
       throw new BatchException(BatchException.MISSING_BOUNDARY_DELIMITER.addContent(currentLineNumber));
     }
-    return multipart;
+  }
+
+  private BatchRequestPart parseBatchRequestPart(final Scanner scanner, final String boundary,
+                                                 final Map<String, String> mimeHeaders,
+                                                 final String contentType) throws BatchException
{
+    if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
+      validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
+      parseNewLine(scanner);// mandatory
+      List<ODataRequest> requests = new ArrayList<ODataRequest>(1);
+      requests.add(parseRequest(scanner, false, boundary));
+      return new BatchRequestPartImpl(false, requests);
+    } else if (contentType.matches(REG_EX_OPTIONAL_WHITESPACE + HttpContentType.MULTIPART_MIXED
+ ANY_CHARACTERS)) {
+      String changeSetBoundary = getBoundary(contentType);
+      if (boundary.equals(changeSetBoundary)) {
+        throw new BatchException(BatchException.INVALID_CHANGESET_BOUNDARY.addContent(currentLineNumber));
+      }
+      List<ODataRequest> changeSetRequests = new LinkedList<ODataRequest>();
+      parseNewLine(scanner);// mandatory
+      Pattern changeSetCloseDelimiter =
+          Pattern.compile("--" + changeSetBoundary + "--" + REG_EX_ZERO_OR_MORE_WHITESPACES);
+      while (!scanner.hasNext(changeSetCloseDelimiter)) {
+        BatchRequestPart part = parseMultipart(scanner, changeSetBoundary, true);
+        changeSetRequests.addAll(part.getRequests());
+      }
+      scanner.next(changeSetCloseDelimiter);
+      currentLineNumber++;
+      return new BatchRequestPartImpl(true, changeSetRequests);
+    } else {
+      throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.MULTIPART_MIXED
+          + " or " + HttpContentType.APPLICATION_HTTP));
+    }
+  }
 
+  private BatchRequestPart parseBatchRequestPartInChangeset(final Scanner scanner, final
String boundary,
+                                                            final Map<String, String>
mimeHeaders,
+                                                            final String contentType) throws
BatchException {
+    if (HttpContentType.APPLICATION_HTTP.equalsIgnoreCase(contentType)) {
+      validateEncoding(mimeHeaders.get(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING.toLowerCase(Locale.ENGLISH)));
+      parseNewLine(scanner);// mandatory
+      List<ODataRequest> requests = new ArrayList<ODataRequest>(1);
+      requests.add(parseRequest(scanner, true, boundary));
+      return new BatchRequestPartImpl(false, requests);
+    } else {
+      throw new BatchException(BatchException.INVALID_CONTENT_TYPE.addContent(HttpContentType.APPLICATION_HTTP));
+    }
   }
 
   private ODataRequest parseRequest(final Scanner scanner, final boolean isChangeSet, final
String boundary)

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1ac8e7d3/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
index 40cf0e0..63453c2 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/batch/BatchRequestWriter.java
@@ -38,6 +38,8 @@ public class BatchRequestWriter {
   private static final String COLON = ":";
   private static final String SP = " ";
   private static final String LF = "\r\n";
+  public static final String BOUNDARY_PREAMBLE = "changeset";
+  public static final String HTTP_1_1 = "HTTP/1.1";
   private String batchBoundary;
   private StringBuilder writer = new StringBuilder();
 
@@ -48,7 +50,7 @@ public class BatchRequestWriter {
       throw new IllegalArgumentException();
     }
     for (BatchPart batchPart : batchParts) {
-      writer.append("--" + boundary).append(LF);
+      writer.append("--").append(boundary).append(LF);
       if (batchPart instanceof BatchChangeSet) {
         appendChangeSet((BatchChangeSet) batchPart);
       } else if (batchPart instanceof BatchQueryPart) {
@@ -64,9 +66,9 @@ public class BatchRequestWriter {
   }
 
   private void appendChangeSet(final BatchChangeSet batchChangeSet) {
-    String boundary = BatchHelper.generateBoundary("changeset");
+    String boundary = BatchHelper.generateBoundary(BOUNDARY_PREAMBLE);
     while (boundary.equals(batchBoundary) || !boundary.matches(REG_EX_BOUNDARY)) {
-      boundary = BatchHelper.generateBoundary("changeset");
+      boundary = BatchHelper.generateBoundary(BOUNDARY_PREAMBLE);
     }
     writer.append(HttpHeaders.CONTENT_TYPE).append(COLON).append(SP).append(
         HttpContentType.MULTIPART_MIXED + "; boundary=" + boundary).append(LF);
@@ -83,7 +85,8 @@ public class BatchRequestWriter {
     boolean isContentLengthPresent = false;
     writer.append(HttpHeaders.CONTENT_TYPE).append(COLON).append(SP).append(HttpContentType.APPLICATION_HTTP)
         .append(LF);
-    writer.append(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING).append(COLON).append(SP).append("binary").append(LF);
+    writer.append(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING).append(COLON).append(SP)
+            .append(BatchHelper.BINARY_ENCODING).append(LF);
     if (contentId != null) {
       writer.append(BatchHelper.HTTP_CONTENT_ID).append(COLON).append(SP).append(contentId).append(LF);
     }
@@ -92,7 +95,7 @@ public class BatchRequestWriter {
       isContentLengthPresent = true;
     }
     writer.append(LF);
-    writer.append(method).append(SP).append(uri).append(SP).append("HTTP/1.1");
+    writer.append(method).append(SP).append(uri).append(SP).append(HTTP_1_1);
     writer.append(LF);
 
     if (!isContentLengthPresent && body != null && !body.isEmpty()) {


Mime
View raw message