diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java index 33a2984e3..cea9c6c1b 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java @@ -215,13 +215,13 @@ public class ODataHandler { case entitySet: case navigationProperty: if (((UriResourcePartTyped) lastPathSegment).isCollection()) { - if (method.equals(HttpMethod.GET)) { + if (method == HttpMethod.GET) { final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, customContentTypeSupport, RepresentationType.COLLECTION_ENTITY); selectProcessor(EntityCollectionProcessor.class) .readEntityCollection(request, response, uriInfo, requestedContentType); - } else if (method.equals(HttpMethod.POST)) { + } else if (method == HttpMethod.POST) { if (isMedia(lastPathSegment)) { final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), @@ -236,28 +236,31 @@ public class ODataHandler { .createEntity(request, response, uriInfo, requestFormat, responseFormat); } } else { - throw new ODataHandlerException("HTTP method not allowed.", + throw new ODataHandlerException("HTTP method " + method + " is not allowed.", ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); } } else { - if (method.equals(HttpMethod.GET)) { + if (method == HttpMethod.GET) { final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, customContentTypeSupport, RepresentationType.ENTITY); selectProcessor(EntityProcessor.class) .readEntity(request, response, uriInfo, requestedContentType); - } else if (method.equals(HttpMethod.DELETE)) { + } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { + throw new ODataHandlerException("not implemented", + ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); + } else if (method == HttpMethod.DELETE) { selectProcessor(isMedia(lastPathSegment) ? MediaEntityProcessor.class : EntityProcessor.class) .deleteEntity(request, response, uriInfo); } else { - throw new ODataHandlerException("not implemented", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); + throw new ODataHandlerException("HTTP method " + method + " is not allowed.", + ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); } } break; case count: - if (method.equals(HttpMethod.GET)) { + if (method == HttpMethod.GET) { final UriResource resource = uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1); if (resource instanceof UriResourceEntitySet || resource instanceof UriResourceNavigation) { selectProcessor(CountEntityCollectionProcessor.class) @@ -274,10 +277,10 @@ public class ODataHandler { break; case primitiveProperty: - if (method.equals(HttpMethod.GET)) { - final UriResourceProperty propertyResource = (UriResourceProperty) lastPathSegment; - final RepresentationType representationType = propertyResource.isCollection() ? - RepresentationType.COLLECTION_PRIMITIVE : RepresentationType.PRIMITIVE; + final UriResourceProperty propertyResource = (UriResourceProperty) lastPathSegment; + final RepresentationType representationType = propertyResource.isCollection() ? + RepresentationType.COLLECTION_PRIMITIVE : RepresentationType.PRIMITIVE; + if (method == HttpMethod.GET) { final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, customContentTypeSupport, representationType); if (representationType == RepresentationType.PRIMITIVE) { @@ -287,67 +290,95 @@ public class ODataHandler { selectProcessor(PrimitiveCollectionProcessor.class) .readPrimitiveCollection(request, response, uriInfo, requestedContentType); } - } else { + } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { throw new ODataHandlerException("not implemented", ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); + } else if (method == HttpMethod.DELETE) { + if (representationType == RepresentationType.PRIMITIVE) { + selectProcessor(PrimitiveProcessor.class) + .deletePrimitive(request, response, uriInfo); + } else { + selectProcessor(PrimitiveCollectionProcessor.class) + .deletePrimitiveCollection(request, response, uriInfo); + } + } else { + throw new ODataHandlerException("HTTP method " + method + " is not allowed.", + ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); } break; case complexProperty: - if (method.equals(HttpMethod.GET)) { - final UriResourceProperty propertyResource = (UriResourceProperty) lastPathSegment; - final RepresentationType representationType = propertyResource.isCollection() ? - RepresentationType.COLLECTION_COMPLEX : RepresentationType.COMPLEX; + final UriResourceProperty complexPropertyResource = (UriResourceProperty) lastPathSegment; + final RepresentationType complexRepresentationType = complexPropertyResource.isCollection() ? + RepresentationType.COLLECTION_COMPLEX : RepresentationType.COMPLEX; + if (method == HttpMethod.GET) { final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, representationType); - if (representationType == RepresentationType.COMPLEX) { + request, customContentTypeSupport, complexRepresentationType); + if (complexRepresentationType == RepresentationType.COMPLEX) { selectProcessor(ComplexProcessor.class) .readComplex(request, response, uriInfo, requestedContentType); } else { selectProcessor(ComplexCollectionProcessor.class) .readComplexCollection(request, response, uriInfo, requestedContentType); } - } else { + } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { throw new ODataHandlerException("not implemented", ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); + } else if (method == HttpMethod.DELETE) { + if (complexRepresentationType == RepresentationType.COMPLEX) { + selectProcessor(ComplexProcessor.class) + .deleteComplex(request, response, uriInfo); + } else { + selectProcessor(ComplexCollectionProcessor.class) + .deleteComplexCollection(request, response, uriInfo); + } + } else { + throw new ODataHandlerException("HTTP method " + method + " is not allowed.", + ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); } break; case value: final UriResource resource = uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1); if (resource instanceof UriResourceProperty) { - if (method.equals(HttpMethod.GET)) { - final RepresentationType representationType = + if (method == HttpMethod.GET) { + final RepresentationType valueRepresentationType = (EdmPrimitiveType) ((UriResourceProperty) resource).getType() == EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Binary) ? RepresentationType.BINARY : RepresentationType.VALUE; final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, representationType); + request, customContentTypeSupport, valueRepresentationType); selectProcessor(PrimitiveValueProcessor.class) .readPrimitiveValue(request, response, uriInfo, requestedContentType); - } else { + } else if (method == HttpMethod.PUT) { throw new ODataHandlerException("not implemented", ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); + } else if (method == HttpMethod.DELETE) { + selectProcessor(PrimitiveValueProcessor.class) + .deletePrimitive(request, response, uriInfo); + } else { + throw new ODataHandlerException("HTTP method " + method + " is not allowed.", + ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); } } else { - if (method.equals(HttpMethod.GET)) { + if (method == HttpMethod.GET) { final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, customContentTypeSupport, RepresentationType.MEDIA); selectProcessor(MediaEntityProcessor.class) .readMediaEntity(request, response, uriInfo, requestedContentType); - } else if (method.equals(HttpMethod.PUT)) { + } else if (method == HttpMethod.PUT) { final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, customContentTypeSupport, RepresentationType.ENTITY); selectProcessor(MediaEntityProcessor.class) .updateMediaEntity(request, response, uriInfo, requestFormat, responseFormat); - } else if (method.equals(HttpMethod.DELETE)) { + } else if (method == HttpMethod.DELETE) { selectProcessor(MediaEntityProcessor.class) .deleteEntity(request, response, uriInfo); } else { - throw new ODataHandlerException("not implemented", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); + throw new ODataHandlerException("HTTP method " + method + " is not allowed.", + ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); } } break; diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java index ec9aa3db1..a611c846f 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -222,22 +221,31 @@ public class ODataHandlerTest { @Test public void dispatchEntitySet() throws Exception { + final String uri = "ESAllPrim"; final EntityCollectionProcessor processor = mock(EntityCollectionProcessor.class); - dispatch(HttpMethod.GET, "ESAllPrim", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readEntityCollection( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); + + dispatchMethodNotAllowed(HttpMethod.PATCH, uri, processor); + dispatchMethodNotAllowed(HttpMethod.PUT, uri, processor); + dispatchMethodNotAllowed(HttpMethod.DELETE, uri, processor); } @Test public void dispatchEntitySetCount() throws Exception { + final String uri = "ESAllPrim/$count"; final CountEntityCollectionProcessor processor = mock(CountEntityCollectionProcessor.class); - dispatch(HttpMethod.GET, "ESAllPrim/$count", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).countEntityCollection( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); - dispatchMethodNotAllowed(HttpMethod.POST, "ESAllPrim/$count", processor); + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); + dispatchMethodNotAllowed(HttpMethod.PATCH, uri, processor); + dispatchMethodNotAllowed(HttpMethod.PUT, uri, processor); + dispatchMethodNotAllowed(HttpMethod.DELETE, uri, processor); } @Test @@ -251,63 +259,45 @@ public class ODataHandlerTest { @Test public void dispatchEntity() throws Exception { + final String uri = "ESAllPrim(0)"; final EntityProcessor processor = mock(EntityProcessor.class); - dispatch(HttpMethod.GET, "ESAllPrim(0)", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readEntity( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); - } - - @Test - public void dispatchEntityDelete() throws Exception { - final EntityProcessor processor = mock(EntityProcessor.class); - dispatch(HttpMethod.DELETE, "ESAllPrim(0)", processor); + dispatch(HttpMethod.DELETE, uri, processor); verify(processor).deleteEntity(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); - } - @Test - public void dispatchEntityCreate() throws Exception { - final EntityProcessor processor = mock(EntityProcessor.class); dispatch(HttpMethod.POST, "ESAllPrim", processor); + verify(processor).createEntity(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), + any(ContentType.class), any(ContentType.class)); - verify(processor).createEntity(any(ODataRequest.class), any(ODataResponse.class), - any(UriInfo.class), any(ContentType.class), any(ContentType.class)); + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); } @Test public void dispatchMedia() throws Exception { + final String uri = "ESMedia(1)/$value"; final MediaEntityProcessor processor = mock(MediaEntityProcessor.class); - dispatch(HttpMethod.GET, "ESMedia(1)/$value", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readMediaEntity( - any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); - } + any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); - @Test - public void dispatchMediaCreate() throws Exception { - final MediaEntityProcessor processor = mock(MediaEntityProcessor.class); dispatch(HttpMethod.POST, "ESMedia", processor); + verify(processor).createMediaEntity(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), + any(ContentType.class), any(ContentType.class)); - verify(processor).createMediaEntity(any(ODataRequest.class), any(ODataResponse.class), - any(UriInfo.class), any(ContentType.class), any(ContentType.class)); - } - - @Test - public void dispatchMediaUpdate() throws Exception { - final MediaEntityProcessor processor = mock(MediaEntityProcessor.class); - dispatch(HttpMethod.PUT, "ESMedia(1)/$value", processor); - - verify(processor).updateMediaEntity(any(ODataRequest.class), any(ODataResponse.class), - any(UriInfo.class), any(ContentType.class), any(ContentType.class)); - } - - @Test - public void dispatchMediaDelete() throws Exception { - final MediaEntityProcessor processor = mock(MediaEntityProcessor.class); - dispatch(HttpMethod.DELETE, "ESMedia(1)/$value", processor); + dispatch(HttpMethod.PUT, uri, processor); + verify(processor).updateMediaEntity(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), + any(ContentType.class), any(ContentType.class)); + dispatch(HttpMethod.DELETE, uri, processor); verify(processor).deleteEntity(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); + + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); + dispatchMethodNotAllowed(HttpMethod.PATCH, uri, processor); } @Test @@ -320,47 +310,77 @@ public class ODataHandlerTest { @Test public void dispatchPrimitiveProperty() throws Exception { + final String uri = "ESAllPrim(0)/PropertyInt16"; final PrimitiveProcessor processor = mock(PrimitiveProcessor.class); - dispatch(HttpMethod.GET, "ESAllPrim(0)/PropertyInt16", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readPrimitive( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); + + dispatch(HttpMethod.DELETE, uri, processor); + verify(processor).deletePrimitive(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); + + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); } @Test public void dispatchPrimitivePropertyValue() throws Exception { + final String uri = "ESAllPrim(0)/PropertyInt16/$value"; final PrimitiveValueProcessor processor = mock(PrimitiveValueProcessor.class); - dispatch(HttpMethod.GET, "ESAllPrim(0)/PropertyInt16/$value", processor); - verify(processor).readPrimitiveValue(any(ODataRequest.class), any(ODataResponse.class), - any(UriInfo.class), any(ContentType.class)); + dispatch(HttpMethod.GET, uri, processor); + verify(processor).readPrimitiveValue(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), + any(ContentType.class)); + + dispatch(HttpMethod.DELETE, uri, processor); + verify(processor).deletePrimitive(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); + + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); } @Test public void dispatchPrimitiveCollectionProperty() throws Exception { + final String uri = "ESMixPrimCollComp(7)/CollPropertyString"; final PrimitiveCollectionProcessor processor = mock(PrimitiveCollectionProcessor.class); - dispatch(HttpMethod.GET, "ESMixPrimCollComp(7)/CollPropertyString", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readPrimitiveCollection( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); + + dispatch(HttpMethod.DELETE, uri, processor); + verify(processor).deletePrimitiveCollection(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); + + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); } @Test public void dispatchComplexProperty() throws Exception { + final String uri = "ESMixPrimCollComp(7)/PropertyComp"; final ComplexProcessor processor = mock(ComplexProcessor.class); - dispatch(HttpMethod.GET, "ESMixPrimCollComp(7)/PropertyComp", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readComplex( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); + + dispatch(HttpMethod.DELETE, uri, processor); + verify(processor).deleteComplex(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); + + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); } @Test public void dispatchComplexCollectionProperty() throws Exception { + final String uri = "ESMixPrimCollComp(7)/CollPropertyComp"; final ComplexCollectionProcessor processor = mock(ComplexCollectionProcessor.class); - dispatch(HttpMethod.GET, "ESMixPrimCollComp(7)/CollPropertyComp", processor); + dispatch(HttpMethod.GET, uri, processor); verify(processor).readComplexCollection( any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class)); + + dispatch(HttpMethod.DELETE, uri, processor); + verify(processor).deleteComplexCollection(any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class)); + + dispatchMethodNotAllowed(HttpMethod.POST, uri, processor); } private ODataResponse dispatch(final HttpMethod method, final String path, final String query,