You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/09/30 12:36:15 UTC

git commit: Minor Bugfix: UriParserSyntaxException in case of wrong percentence

Repository: olingo-odata4
Updated Branches:
  refs/heads/master 493879173 -> 0eb95ed72


Minor Bugfix: UriParserSyntaxException in case of wrong percentence


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/0eb95ed7
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/0eb95ed7
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/0eb95ed7

Branch: refs/heads/master
Commit: 0eb95ed72b167dada3e3c80a59bde24a297c3aa2
Parents: 4938791
Author: Michael Bolz <mi...@sap.com>
Authored: Tue Sep 30 12:35:22 2014 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Tue Sep 30 12:35:22 2014 +0200

----------------------------------------------------------------------
 .../server/core/uri/parser/UriDecoder.java      | 44 ++++++++++----------
 .../olingo/server/core/uri/RawUriTest.java      | 23 +++++-----
 2 files changed, 36 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0eb95ed7/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriDecoder.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriDecoder.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriDecoder.java
index dc704fc..37ce102 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriDecoder.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriDecoder.java
@@ -30,9 +30,10 @@ import java.util.regex.Pattern;
 
 public class UriDecoder {
 
-  static Pattern uriPattern = Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
+  private static final Pattern uriPattern =
+      Pattern.compile("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?");
 
-  public static RawUri decodeUri(final String uri, final int scipSegments) {
+  public static RawUri decodeUri(final String uri, final int skipSegments) throws UriParserSyntaxException {
     RawUri rawUri = new RawUri();
 
     Matcher m = uriPattern.matcher(uri);
@@ -44,14 +45,14 @@ public class UriDecoder {
       rawUri.fragment = m.group(9);
     }
 
-    splittPath(rawUri, scipSegments);
-    splittOptions(rawUri);
+    splitPath(rawUri, skipSegments);
+    splitOptions(rawUri);
     decode(rawUri);
 
     return rawUri;
   }
 
-  private static void decode(final RawUri rawUri) {
+  private static void decode(final RawUri rawUri) throws UriParserSyntaxException {
     rawUri.pathSegmentListDecoded = new ArrayList<String>();
     for (String segment : rawUri.pathSegmentList) {
       rawUri.pathSegmentListDecoded.add(decode(segment));
@@ -65,25 +66,25 @@ public class UriDecoder {
     }
   }
 
-  private static void splittOptions(final RawUri rawUri) {
+  private static void splitOptions(final RawUri rawUri) {
     rawUri.queryOptionList = new ArrayList<RawUri.QueryOption>();
 
     if (rawUri.queryOptionString == null) {
       return;
     }
 
-    List<String> options = splitt(rawUri.queryOptionString, '&');
+    List<String> options = split(rawUri.queryOptionString, '&');
 
     for (String option : options) {
       if (option.length() != 0) {
-        List<String> pair = splittFirst(option, '=');
+        List<String> pair = splitFirst(option, '=');
         rawUri.queryOptionList.add(
             new RawUri.QueryOption(pair.get(0), pair.get(1)));
       }
     }
   }
 
-  private static List<String> splittFirst(final String input, final char c) {
+  private static List<String> splitFirst(final String input, final char c) {
     int pos = input.indexOf(c, 0);
     if (pos >= 0) {
       return Arrays.asList(input.substring(0, pos), input.substring(pos + 1));
@@ -92,21 +93,19 @@ public class UriDecoder {
     }
   }
 
-  public static void splittPath(final RawUri rawUri, int scipSegments) {
-    List<String> list = splitt(rawUri.path, '/');
+  public static void splitPath(final RawUri rawUri, int skipSegments) {
+    List<String> list = split(rawUri.path, '/');
 
     if (list.get(0).length() == 0) {
-      scipSegments++;
+      skipSegments++;
     }
 
-    if (scipSegments > 0) {
-      rawUri.pathSegmentList = list.subList(scipSegments, list.size());
-    } else {
-      rawUri.pathSegmentList = list;
-    }
+    rawUri.pathSegmentList = skipSegments > 0 ?
+        list.subList(skipSegments, list.size()) :
+        list;
   }
 
-  public static List<String> splitt(final String input, final char c) {
+  public static List<String> split(final String input, final char c) {
 
     List<String> list = new LinkedList<String>();
 
@@ -123,8 +122,11 @@ public class UriDecoder {
     return list;
   }
 
-  public static String decode(final String encoded) {
-    return Decoder.decode(encoded);
+  public static String decode(final String encoded) throws UriParserSyntaxException {
+    try {
+      return Decoder.decode(encoded);
+    } catch (final IllegalArgumentException e) {
+      throw new UriParserSyntaxException("Wrong percent encoding!", e, UriParserSyntaxException.MessageKeys.SYNTAX);
+    }
   }
-
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/0eb95ed7/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/RawUriTest.java
----------------------------------------------------------------------
diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/RawUriTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/RawUriTest.java
index bb3e891..4dc06c0 100644
--- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/RawUriTest.java
+++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/RawUriTest.java
@@ -31,8 +31,8 @@ import static org.junit.Assert.assertEquals;
 
 public class RawUriTest {
 
-  private RawUri runRawParser(final String uri, final int scipSegments) throws UriParserSyntaxException {
-    return UriDecoder.decodeUri(uri, scipSegments);
+  private RawUri runRawParser(final String uri, final int skipSegments) throws UriParserSyntaxException {
+    return UriDecoder.decodeUri(uri, skipSegments);
   }
 
   @Test
@@ -79,7 +79,6 @@ public class RawUriTest {
 
     assertEquals(name, option.name);
     assertEquals(value, option.value);
-
   }
 
   private void checkOptionCount(final RawUri rawUri, final int count) {
@@ -127,13 +126,13 @@ public class RawUriTest {
   }
 
   @Test
-  public void testSplitt() {
-    UriDecoder.splitt("", '/');
-    UriDecoder.splitt("/", '/');
-    UriDecoder.splitt("a", '/');
-    UriDecoder.splitt("a/", '/');
-    UriDecoder.splitt("/a", '/');
-    UriDecoder.splitt("a/a", '/');
+  public void testSplit() {
+    UriDecoder.split("", '/');
+    UriDecoder.split("/", '/');
+    UriDecoder.split("a", '/');
+    UriDecoder.split("a/", '/');
+    UriDecoder.split("/a", '/');
+    UriDecoder.split("a/a", '/');
   }
 
   private void checkPath(final RawUri rawUri, final String path, final List<String> list) {
@@ -148,4 +147,8 @@ public class RawUriTest {
     }
   }
 
+  @Test(expected = UriParserSyntaxException.class)
+  public void wrongPercentEncoding() throws Exception {
+    runRawParser("%wrong", 0);
+  }
 }