Repository: olingo-odata4 Updated Branches: refs/heads/master 9c61e2237 -> 49b859943
[OLINGO-903] Check for in uri parser error cases Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/49b85994 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/49b85994 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/49b85994 Branch: refs/heads/master Commit: 49b8599432a8ea48732283b914de60c9d4577d77 Parents: 9c61e22 Author: Michael Bolz <[email protected]> Authored: Thu Mar 17 21:59:48 2016 +0100 Committer: Michael Bolz <[email protected]> Committed: Thu Mar 17 22:02:49 2016 +0100 ---------------------------------------------------------------------- .../olingo/server/core/ODataHandlerImpl.java | 41 ++++++++++++- .../server/core/ODataHandlerImplTest.java | 61 ++++++++++++++++++++ 2 files changed, 99 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/49b85994/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java index 3c2a903..1d6a415 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java @@ -18,6 +18,7 @@ */ package org.apache.olingo.server.core; +import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -45,11 +46,17 @@ import org.apache.olingo.server.api.serializer.CustomContentTypeSupport; import org.apache.olingo.server.api.serializer.RepresentationType; import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.api.uri.UriInfo; +import org.apache.olingo.server.api.uri.queryoption.FormatOption; +import org.apache.olingo.server.api.uri.queryoption.QueryOption; +import org.apache.olingo.server.api.uri.queryoption.SystemQueryOption; +import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind; import org.apache.olingo.server.core.debug.ServerCoreDebugger; import org.apache.olingo.server.core.uri.parser.Parser; +import org.apache.olingo.server.core.uri.parser.UriDecoder; import org.apache.olingo.server.core.uri.parser.UriParserException; import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException; +import org.apache.olingo.server.core.uri.queryoption.FormatOptionImpl; import org.apache.olingo.server.core.uri.validator.UriValidationException; import org.apache.olingo.server.core.uri.validator.UriValidator; @@ -174,9 +181,9 @@ public class ODataHandlerImpl implements ODataHandler { } ContentType requestedContentType; try { - requestedContentType = ContentNegotiator.doContentNegotiation( - uriInfo == null ? null : uriInfo.getFormatOption(), request, getCustomContentTypeSupport(), - RepresentationType.ERROR); + final FormatOption formatOption = getFormatOption(request, uriInfo); + requestedContentType = ContentNegotiator.doContentNegotiation(formatOption, request, + getCustomContentTypeSupport(), RepresentationType.ERROR); } catch (final ContentNegotiatorException e) { requestedContentType = ContentType.JSON; } @@ -186,6 +193,34 @@ public class ODataHandlerImpl implements ODataHandler { debugger.stopRuntimeMeasurement(measurementHandle); } + /** + * Extract format option from either <code>uriInfo</code> (if not <code>NULL</code>) + * or query from <code>request</code> (if not <code>NULL</code>). + * If both options are <code>NULL</code>, <code>NULL</code> is returned. + * + * @param request request which is checked + * @param uriInfo uriInfo which is checked + * @return the evaluated format option or <code>NULL</code>. + */ + private FormatOption getFormatOption(final ODataRequest request, final UriInfo uriInfo) { + if(uriInfo == null) { + String query = request.getRawQueryPath(); + if(query == null) { + return null; + } + + final String formatOption = SystemQueryOptionKind.FORMAT.toString(); + int index = query.indexOf(formatOption); + int endIndex = query.indexOf("&", index); + if(endIndex == -1) { + endIndex = query.length(); + } + final String format = query.substring(index + formatOption.length(), endIndex); + return new FormatOptionImpl().setFormat(format); + } + return uriInfo.getFormatOption(); + } + private void validateODataVersion(final ODataRequest request) throws ODataHandlerException { final String maxVersion = request.getHeader(HttpHeader.ODATA_MAX_VERSION); if (maxVersion != null && ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion)) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/49b85994/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java index 7c26eb7..e1734c5 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerImplTest.java @@ -38,6 +38,7 @@ import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.edm.provider.CsdlAbstractEdmProvider; import org.apache.olingo.commons.api.edm.provider.CsdlEntitySet; import org.apache.olingo.commons.api.ex.ODataException; +import org.apache.olingo.commons.api.format.AcceptType; import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpMethod; @@ -207,6 +208,66 @@ public class ODataHandlerImplTest { assertEquals(HttpStatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode()); } + + @Test + public void uriParserExceptionWithFormatQueryJson() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json", "", "", null); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + + @Test + public void uriParserExceptionWithFormatQueryJsonAndMore() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json&$top=3", "", "", null); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + + + @Test + public void uriParserExceptionWithFormatJsonAcceptAtom() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=json", + HttpHeader.ACCEPT, ContentType.APPLICATION_ATOM_XML.toContentTypeString(), null); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + + @Test + public void uriParserExceptionWithFormatQueryAtom() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom", "", "", null); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + + @Test + public void uriParserExceptionWithFormatQueryAtomAndTop() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom&$top=19", "", "", null); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + + @Test + public void uriParserExceptionWithFormatAtomAcceptJson() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=atom", + HttpHeader.ACCEPT, ContentType.APPLICATION_JSON.toContentTypeString(), null); + assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + + @Test + public void uriParserExceptionWithFormatQueryInvali() throws Exception { + final ODataResponse response = dispatch(HttpMethod.GET, "ESAllPrims", "$format=somenotvalid", "", "", null); + assertEquals(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode(), response.getStatusCode()); + assertEquals("application/json;odata.metadata=minimal", + response.getHeader(HttpHeader.CONTENT_TYPE)); + } + @Test public void applicationExceptionInProcessor() throws Exception { MetadataProcessor processor = mock(MetadataProcessor.class);
