Repository: olingo-odata2 Updated Branches: refs/heads/master 973af5555 -> 070090ead
[OLINGO-752] Support for + sign as a separator Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/070090ea Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/070090ea Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/070090ea Branch: refs/heads/master Commit: 070090eada88ce9b4c886cc6d30043654995f869 Parents: 973af55 Author: Archana Rai <archana....@sap.com> Authored: Wed Aug 9 13:33:02 2017 +0530 Committer: Archana Rai <archana....@sap.com> Committed: Wed Aug 9 13:33:02 2017 +0530 ---------------------------------------------------------------------- .../olingo/odata2/core/uri/UriParserImpl.java | 16 +++++- .../olingo/odata2/core/uri/UriParserTest.java | 54 ++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/070090ea/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/uri/UriParserImpl.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/uri/UriParserImpl.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/uri/UriParserImpl.java index 0d917bb..cddbe96 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/uri/UriParserImpl.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/uri/UriParserImpl.java @@ -81,6 +81,7 @@ public class UriParserImpl extends UriParser { private static final Pattern NAMED_VALUE_PATTERN = Pattern.compile("(?:([^=]+)=)?([^=]+)"); private static final char COMMA = ','; private static final char SQUOTE = '\''; + private static final String ACCEPT_FORM_ENCODING = "odata-accept-forms-encoding"; private final Edm edm; private final EdmSimpleTypeFacade simpleTypeFacade; @@ -584,13 +585,19 @@ public class UriParserImpl extends UriParser { } private void distributeQueryParameters(final Map<String, List<String>> queryParameters) throws UriSyntaxException { + boolean formEncoding = false; + if(queryParameters.containsKey(ACCEPT_FORM_ENCODING)){ + formEncoding=Boolean.parseBoolean(queryParameters.get(ACCEPT_FORM_ENCODING).get(0));; + } for (final String queryOptionString : queryParameters.keySet()) { final String decodedString = percentDecode(queryOptionString); final List<String> valueList = queryParameters.get(queryOptionString); if (valueList.size() >= 1) { String value = valueList.get(0); - + if(formEncoding){ + value = getFormEncodedValue(value); + } if (decodedString.startsWith("$")) { SystemQueryOption queryOption; try { @@ -617,6 +624,13 @@ public class UriParserImpl extends UriParser { } } + private String getFormEncodedValue(String value) { + if(value.contains("+")){ + value = value.replaceAll("\\+", " "); + } + return value; + } + private void checkSystemQueryOptionsCompatibility() throws UriSyntaxException { final UriType uriType = uriResult.getUriType(); http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/070090ea/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/uri/UriParserTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/uri/UriParserTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/uri/UriParserTest.java index cc3004a..b41eab6 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/uri/UriParserTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/uri/UriParserTest.java @@ -63,6 +63,7 @@ import org.junit.Test; public class UriParserTest extends BaseTest { private Edm edm; + private static final String ACCEPT_FORM_ENCODING = "odata-accept-forms-encoding"; @Before public void getEdm() throws ODataException { @@ -1035,6 +1036,59 @@ public class UriParserTest extends BaseTest { } @Test + public void parseFilterWithSpaceFormEncoding() throws Exception { + UriInfoImpl result = parse("Employees?$filter=EmployeeId%20eq%20%271%27&odata-accept-forms-encoding=true"); + assertEquals("Employees", result.getTargetEntitySet().getName()); + assertEquals(UriType.URI1, result.getUriType()); + assertEquals("EmployeeId eq '1'", result.getFilter().getUriLiteral()); + assertEquals("true", result.getCustomQueryOptions().get(ACCEPT_FORM_ENCODING)); + } + + @Test + public void parseFilterWithSpaceNoFormEncoding() throws Exception { + UriInfoImpl result = parse("Employees?$filter=EmployeeId%20eq%20%271%27"); + assertEquals("Employees", result.getTargetEntitySet().getName()); + assertEquals(UriType.URI1, result.getUriType()); + assertEquals("EmployeeId eq '1'", result.getFilter().getUriLiteral()); + } + + @Test + public void parseSystemQueryOptionFilterFormEncoding() throws Exception { + UriInfoImpl result = parse("Employees?$filter=EmployeeId+eq+%271%27&odata-accept-forms-encoding=true"); + assertEquals("Employees", result.getTargetEntitySet().getName()); + assertEquals(UriType.URI1, result.getUriType()); + assertEquals("EmployeeId eq '1'", result.getFilter().getUriLiteral()); + assertEquals("true", result.getCustomQueryOptions().get(ACCEPT_FORM_ENCODING)); + } + + @Test + public void parseFilterFormEncoding() throws Exception { + UriInfoImpl result = parse("Employees?odata-accept-forms-encoding=true&$filter=EmployeeId+eq+%271%27"); + assertEquals("Employees", result.getTargetEntitySet().getName()); + assertEquals(UriType.URI1, result.getUriType()); + assertEquals("EmployeeId eq '1'", result.getFilter().getUriLiteral()); + assertEquals("true", result.getCustomQueryOptions().get(ACCEPT_FORM_ENCODING)); + } + + @Test + public void parseSystemQueryOptionFilterFalseFormEncoding() throws Exception { + parseWrongUri("Employees?$filter=EmployeeId+eq+%271%27&odata-accept-forms-encoding=false", + UriSyntaxException.INVALIDFILTEREXPRESSION); + } + + @Test + public void parseSystemQueryOptionFilterNoFormEncoding() throws Exception { + parseWrongUri("Employees?$filter=EmployeeId+eq+%271%27", + UriSyntaxException.INVALIDFILTEREXPRESSION); + } + + @Test + public void parseSystemQueryOptionFilterInvalidFormEncoding() throws Exception { + parseWrongUri("Employees?$filter=EmployeeId+eq+%271%27&odata-accept-forms-encoding=asdf", + UriSyntaxException.INVALIDFILTEREXPRESSION); + } + + @Test public void parseSystemQueryOptionExpand() throws Exception { UriInfoImpl result = parse("Managers('1')?$expand=nm_Employees"); assertEquals("Managers", result.getTargetEntitySet().getName());