Repository: olingo-odata4 Updated Branches: refs/heads/master 1f286fd42 -> 1dae99912
[OLINGO-663] Custom etag handling in default processor Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/1dae9991 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/1dae9991 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/1dae9991 Branch: refs/heads/master Commit: 1dae999126a059f0ae7f6f780341bbf3201a8bd2 Parents: 1f286fd Author: Christian Amend <[email protected]> Authored: Wed Jun 3 13:06:49 2015 +0200 Committer: Christian Amend <[email protected]> Committed: Wed Jun 3 13:06:49 2015 +0200 ---------------------------------------------------------------------- .../org/apache/olingo/server/api/OData.java | 17 +++++- .../olingo/server/api/ServiceMetadata.java | 4 ++ .../server/api/etag/CustomETagSupport.java | 23 --------- .../olingo/server/api/etag/ETagHelper.java | 4 +- .../server/api/etag/PreconditionException.java | 54 ++++++++++++++++++++ .../api/etag/PreconditionRequiredException.java | 54 -------------------- .../api/etag/ServiceMetadataETagSupport.java | 45 ++++++++++++++++ .../server/api/processor/DefaultProcessor.java | 54 ++++++++++++++++---- .../olingo/server/core/ODataDispatcher.java | 4 +- .../server/core/ODataExceptionHelper.java | 13 +++-- .../apache/olingo/server/core/ODataHandler.java | 4 +- .../apache/olingo/server/core/ODataImpl.java | 14 +++-- .../olingo/server/core/ServiceMetadataImpl.java | 15 ++++-- .../olingo/server/core/etag/ETagHelperImpl.java | 14 ++--- .../core/etag/PreconditionsValidator.java | 16 +++--- .../server-core-exceptions-i18n.properties | 4 +- .../xml/MetadataDocumentXmlSerializerTest.java | 2 +- .../olingo/server/tecsvc/ETagSupport.java | 10 ---- .../server/core/PreconditionsValidatorTest.java | 18 ++----- .../serializer/xml/MetadataDocumentTest.java | 5 +- 20 files changed, 225 insertions(+), 149 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/OData.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/OData.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/OData.java index c8914ef..3a9c1cb 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/OData.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/OData.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -30,6 +30,7 @@ import org.apache.olingo.server.api.deserializer.FixedFormatDeserializer; import org.apache.olingo.server.api.deserializer.ODataDeserializer; import org.apache.olingo.server.api.edmx.EdmxReference; import org.apache.olingo.server.api.etag.ETagHelper; +import org.apache.olingo.server.api.etag.ServiceMetadataETagSupport; import org.apache.olingo.server.api.serializer.FixedFormatSerializer; import org.apache.olingo.server.api.serializer.ODataSerializer; import org.apache.olingo.server.api.serializer.SerializerException; @@ -93,10 +94,22 @@ public abstract class OData { * * @param edmProvider a custom or default implementation for creating metadata * @param references list of edmx references + * @return a service metadata implementation */ public abstract ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider, List<EdmxReference> references); /** + * Creates a metadata object for this service. + * + * @param edmProvider a custom or default implementation for creating metadata + * @param references list of edmx references + * @param serviceMetadataETagSupport + * @return a service metadata implementation + */ + public abstract ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider, List<EdmxReference> references, + ServiceMetadataETagSupport serviceMetadataETagSupport); + + /** * Creates a new URI helper object for performing URI-related tasks. * It can be used in Processor implementations. */ http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/ServiceMetadata.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/ServiceMetadata.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/ServiceMetadata.java index e850dbd..3cecd8b 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/ServiceMetadata.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/ServiceMetadata.java @@ -23,6 +23,7 @@ import java.util.List; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.server.api.edmx.EdmxReference; +import org.apache.olingo.server.api.etag.ServiceMetadataETagSupport; /** * Metadata of an OData service like the Entity Data Model. @@ -45,4 +46,7 @@ public interface ServiceMetadata { * @return list of defined emdx references of this service */ List<EdmxReference> getReferences(); + + ServiceMetadataETagSupport getServiceMetadataETagSupport(); + } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/CustomETagSupport.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/CustomETagSupport.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/CustomETagSupport.java index c0dba78..8194b96 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/CustomETagSupport.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/CustomETagSupport.java @@ -48,27 +48,4 @@ public interface CustomETagSupport { * @return true if the entity set specified needs an if-match/if-none-match header */ boolean hasMediaETag(EdmBindingTarget entitySetOrSingleton); - - /** - * Since the Olingo library cannot generate a metadata document etag in a generic way we call this method to retrieve - * an application specific etag for the metadata document. If this interface is registered applications can return an - * etag or null here to provide caching support for clients. If a client sends a GET request to the metadata document - * and this method delivers an etag we will match it to the request. If there has been no modification we will return - * a 304 NOT MODIFIED status code. If this interface is not registered or delivers null we just send back the usual - * metadata response. - * @return the application generated etag for the metadata document - */ - String getMetadataETag(); - - /** - * Since the Olingo library cannot generate a service document etag in a generic way we call this method to retrieve - * an application specific etag for the service document. If this interface is registered applications can return an - * etag or null here to provide caching support for clients. If a client sends a GET request to the service document - * and this method delivers an etag we will match it to the request. If there has been no modification we will return - * a 304 NOT MODIFIED status code. If this interface is not registered or delivers null we just send back the usual - * service document response. - * @return the application generated etag for the service document - */ - String getServiceDocumentETag(); - } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ETagHelper.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ETagHelper.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ETagHelper.java index ae8b0e2..b47c6aa 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ETagHelper.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ETagHelper.java @@ -44,7 +44,7 @@ public interface ETagHelper { */ public boolean checkReadPreconditions(String eTag, Collection<String> ifMatchHeaders, Collection<String> ifNoneMatchHeaders) - throws PreconditionRequiredException; + throws PreconditionException; /** * <p>Checks the preconditions of a change request (with HTTP methods PUT, PATCH, or DELETE) @@ -62,5 +62,5 @@ public interface ETagHelper { */ public void checkChangePreconditions(String eTag, Collection<String> ifMatchHeaders, Collection<String> ifNoneMatchHeaders) - throws PreconditionRequiredException; + throws PreconditionException; } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionException.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionException.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionException.java new file mode 100644 index 0000000..8e9c84f --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionException.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.api.etag; + +import org.apache.olingo.server.api.ODataLibraryException; + +public class PreconditionException extends ODataLibraryException { + private static final long serialVersionUID = -8112658467394158700L; + + public static enum MessageKeys implements MessageKey { + /** no parameter */ + MISSING_HEADER, + /** no parameter */ + FAILED, + /** no parameter */ + INVALID_URI; + + @Override + public String getKey() { + return name(); + } + } + + public PreconditionException(final String developmentMessage, final MessageKey messageKey, + final String... parameters) { + super(developmentMessage, messageKey, parameters); + } + + public PreconditionException(final String developmentMessage, final Throwable cause, + final MessageKey messageKey, final String... parameters) { + super(developmentMessage, cause, messageKey, parameters); + } + + @Override + protected String getBundleName() { + return DEFAULT_SERVER_BUNDLE_NAME; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionRequiredException.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionRequiredException.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionRequiredException.java deleted file mode 100644 index b49298f..0000000 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/PreconditionRequiredException.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.olingo.server.api.etag; - -import org.apache.olingo.server.api.ODataLibraryException; - -public class PreconditionRequiredException extends ODataLibraryException { - private static final long serialVersionUID = -8112658467394158700L; - - public static enum MessageKeys implements MessageKey { - /** no parameter */ - MISSING_HEADER, - /** no parameter */ - FAILED, - /** no parameter */ - INVALID_URI; - - @Override - public String getKey() { - return name(); - } - } - - public PreconditionRequiredException(final String developmentMessage, final MessageKey messageKey, - final String... parameters) { - super(developmentMessage, messageKey, parameters); - } - - public PreconditionRequiredException(final String developmentMessage, final Throwable cause, - final MessageKey messageKey, final String... parameters) { - super(developmentMessage, cause, messageKey, parameters); - } - - @Override - protected String getBundleName() { - return DEFAULT_SERVER_BUNDLE_NAME; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ServiceMetadataETagSupport.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ServiceMetadataETagSupport.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ServiceMetadataETagSupport.java new file mode 100644 index 0000000..76e60f2 --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/etag/ServiceMetadataETagSupport.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.server.api.etag; + +public interface ServiceMetadataETagSupport { + + /** + * Since the Olingo library cannot generate a metadata document etag in a generic way we call this method to retrieve + * an application specific etag for the metadata document. If this interface is registered applications can return an + * etag or null here to provide caching support for clients. If a client sends a GET request to the metadata document + * and this method delivers an etag we will match it to the request. If there has been no modification we will return + * a 304 NOT MODIFIED status code. If this interface is not registered or delivers null we just send back the usual + * metadata response. + * @return the application generated etag for the metadata document + */ + String getMetadataETag(); + + /** + * Since the Olingo library cannot generate a service document etag in a generic way we call this method to retrieve + * an application specific etag for the service document. If this interface is registered applications can return an + * etag or null here to provide caching support for clients. If a client sends a GET request to the service document + * and this method delivers an etag we will match it to the request. If there has been no modification we will return + * a 304 NOT MODIFIED status code. If this interface is not registered or delivers null we just send back the usual + * service document response. + * @return the application generated etag for the service document + */ + String getServiceDocumentETag(); + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java index d7da6fd..f73ed27 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DefaultProcessor.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -31,6 +31,8 @@ import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.ODataServerError; import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.api.etag.ETagHelper; +import org.apache.olingo.server.api.etag.ServiceMetadataETagSupport; import org.apache.olingo.server.api.serializer.ODataSerializer; import org.apache.olingo.server.api.uri.UriInfo; @@ -55,19 +57,51 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce @Override public void readServiceDocument(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, final ContentType requestedContentType) throws ODataApplicationException, ODataLibraryException { - ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType)); - response.setContent(serializer.serviceDocument(serviceMetadata.getEdm(), null).getContent()); - response.setStatusCode(HttpStatusCode.OK.getStatusCode()); - response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); + boolean isNotModified = false; + ServiceMetadataETagSupport eTagSupport = serviceMetadata.getServiceMetadataETagSupport(); + if (eTagSupport != null && eTagSupport.getServiceDocumentETag() != null) { + // Set application etag at response + response.setHeader(HttpHeader.ETAG, eTagSupport.getServiceDocumentETag()); + // Check if service document has been modified + ETagHelper eTagHelper = odata.createETagHelper(); + isNotModified = eTagHelper.checkReadPreconditions(eTagSupport.getServiceDocumentETag(), request + .getHeaders(HttpHeader.IF_MATCH), request.getHeaders(HttpHeader.IF_NONE_MATCH)); + } + + // Send the correct response + if (isNotModified) { + response.setStatusCode(HttpStatusCode.NOT_MODIFIED.getStatusCode()); + } else { + ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType)); + response.setContent(serializer.serviceDocument(serviceMetadata.getEdm(), null).getContent()); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); + } } @Override public void readMetadata(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, final ContentType requestedContentType) throws ODataApplicationException, ODataLibraryException { - ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType)); - response.setContent(serializer.metadataDocument(serviceMetadata).getContent()); - response.setStatusCode(HttpStatusCode.OK.getStatusCode()); - response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); + boolean isNotModified = false; + ServiceMetadataETagSupport eTagSupport = serviceMetadata.getServiceMetadataETagSupport(); + if (eTagSupport != null && eTagSupport.getMetadataETag() != null) { + // Set application etag at response + response.setHeader(HttpHeader.ETAG, eTagSupport.getMetadataETag()); + // Check if metadata document has been modified + ETagHelper eTagHelper = odata.createETagHelper(); + isNotModified = eTagHelper.checkReadPreconditions(eTagSupport.getMetadataETag(), request + .getHeaders(HttpHeader.IF_MATCH), request.getHeaders(HttpHeader.IF_NONE_MATCH)); + } + + // Send the correct response + if (isNotModified) { + response.setStatusCode(HttpStatusCode.NOT_MODIFIED.getStatusCode()); + } else { + ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType)); + response.setContent(serializer.metadataDocument(serviceMetadata).getContent()); + response.setStatusCode(HttpStatusCode.OK.getStatusCode()); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); + } } @Override http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java index d05a98b..45b3eb8 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataDispatcher.java @@ -33,7 +33,7 @@ import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.ODataLibraryException; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.etag.PreconditionRequiredException; +import org.apache.olingo.server.api.etag.PreconditionException; import org.apache.olingo.server.api.processor.ActionComplexCollectionProcessor; import org.apache.olingo.server.api.processor.ActionComplexProcessor; import org.apache.olingo.server.api.processor.ActionEntityCollectionProcessor; @@ -515,7 +515,7 @@ public class ODataDispatcher { } } - private void validatePreconditions(ODataRequest request, boolean isMediaValue) throws PreconditionRequiredException { + private void validatePreconditions(ODataRequest request, boolean isMediaValue) throws PreconditionException { // If needed perform preconditions validation if (handler.getCustomETagSupport() != null) { new PreconditionsValidator(handler.getCustomETagSupport(), uriInfo, http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java index 664b50a..417dde2 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java @@ -26,7 +26,7 @@ import org.apache.olingo.server.api.ODataApplicationException; import org.apache.olingo.server.api.ODataLibraryException; import org.apache.olingo.server.api.ODataLibraryException.ODataErrorMessage; import org.apache.olingo.server.api.deserializer.DeserializerException; -import org.apache.olingo.server.api.etag.PreconditionRequiredException; +import org.apache.olingo.server.api.etag.PreconditionException; import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.core.uri.parser.UriParserException; import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; @@ -104,10 +104,15 @@ public class ODataExceptionHelper { .setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); } - public static ODataServerError createServerErrorObject(final PreconditionRequiredException e, + public static ODataServerError createServerErrorObject(final PreconditionException e, final Locale requestedLocale) { - return basicTranslatedError(e, requestedLocale) - .setStatusCode(HttpStatusCode.PRECONDITION_REQUIRED.getStatusCode()); + ODataServerError serverError = basicTranslatedError(e, requestedLocale); + if (PreconditionException.MessageKeys.MISSING_HEADER == e.getMessageKey()) { + serverError.setStatusCode(HttpStatusCode.PRECONDITION_REQUIRED.getStatusCode()); + } else if (PreconditionException.MessageKeys.FAILED == e.getMessageKey()) { + serverError.setStatusCode(HttpStatusCode.PRECONDITION_FAILED.getStatusCode()); + } + return serverError; } public static ODataServerError http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java ---------------------------------------------------------------------- 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 82313f5..b9989f0 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 @@ -35,7 +35,7 @@ import org.apache.olingo.server.api.ODataServerError; import org.apache.olingo.server.api.ServiceMetadata; import org.apache.olingo.server.api.deserializer.DeserializerException; import org.apache.olingo.server.api.etag.CustomETagSupport; -import org.apache.olingo.server.api.etag.PreconditionRequiredException; +import org.apache.olingo.server.api.etag.PreconditionException; import org.apache.olingo.server.api.processor.DefaultProcessor; import org.apache.olingo.server.api.processor.ErrorProcessor; import org.apache.olingo.server.api.processor.Processor; @@ -95,7 +95,7 @@ public class ODataHandler { } catch (DeserializerException e) { ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); handleException(request, response, serverError); - } catch (PreconditionRequiredException e) { + } catch (PreconditionException e) { ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); handleException(request, response, serverError); } catch (ODataHandlerException e) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java index 5c5229f..57cceaf 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -33,6 +33,7 @@ import org.apache.olingo.server.api.deserializer.FixedFormatDeserializer; import org.apache.olingo.server.api.deserializer.ODataDeserializer; import org.apache.olingo.server.api.edmx.EdmxReference; import org.apache.olingo.server.api.etag.ETagHelper; +import org.apache.olingo.server.api.etag.ServiceMetadataETagSupport; import org.apache.olingo.server.api.serializer.FixedFormatSerializer; import org.apache.olingo.server.api.serializer.ODataSerializer; import org.apache.olingo.server.api.serializer.SerializerException; @@ -80,7 +81,13 @@ public class ODataImpl extends OData { @Override public ServiceMetadata createServiceMetadata(final CsdlEdmProvider edmProvider, final List<EdmxReference> references) { - return new ServiceMetadataImpl(edmProvider, references); + return createServiceMetadata(edmProvider, references, null); + } + + @Override + public ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider, List<EdmxReference> references, + ServiceMetadataETagSupport serviceMetadataETagSupport) { + return new ServiceMetadataImpl(edmProvider, references, serviceMetadataETagSupport); } @Override @@ -120,4 +127,5 @@ public class ODataImpl extends OData { public ETagHelper createETagHelper() { return new ETagHelperImpl(); } + } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java index e788600..4b24156 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -28,6 +28,7 @@ import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; import org.apache.olingo.commons.core.edm.EdmProviderImpl; import org.apache.olingo.server.api.ServiceMetadata; import org.apache.olingo.server.api.edmx.EdmxReference; +import org.apache.olingo.server.api.etag.ServiceMetadataETagSupport; /** */ @@ -35,10 +36,13 @@ public class ServiceMetadataImpl implements ServiceMetadata { private final EdmProviderImpl edm; private final List<EdmxReference> references = new ArrayList<EdmxReference>(); + private final ServiceMetadataETagSupport serviceMetadataETagSupport; - public ServiceMetadataImpl(final CsdlEdmProvider edmProvider, final List<EdmxReference> references) { + public ServiceMetadataImpl(CsdlEdmProvider edmProvider, List<EdmxReference> references, + ServiceMetadataETagSupport serviceMetadataETagSupport) { edm = new EdmProviderImpl(edmProvider); this.references.addAll(references); + this.serviceMetadataETagSupport = serviceMetadataETagSupport; } @Override @@ -55,4 +59,9 @@ public class ServiceMetadataImpl implements ServiceMetadata { public List<EdmxReference> getReferences() { return Collections.unmodifiableList(references); } + + @Override + public ServiceMetadataETagSupport getServiceMetadataETagSupport() { + return serviceMetadataETagSupport; + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/ETagHelperImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/ETagHelperImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/ETagHelperImpl.java index 1d8c6d6..81cefe3 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/ETagHelperImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/ETagHelperImpl.java @@ -22,19 +22,19 @@ import java.util.Collection; import java.util.Collections; import org.apache.olingo.server.api.etag.ETagHelper; -import org.apache.olingo.server.api.etag.PreconditionRequiredException; +import org.apache.olingo.server.api.etag.PreconditionException; public class ETagHelperImpl implements ETagHelper { @Override public boolean checkReadPreconditions(final String eTag, final Collection<String> ifMatchHeaders, final Collection<String> ifNoneMatchHeaders) - throws PreconditionRequiredException { + throws PreconditionException { if (eTag != null) { final ETagInformation ifMatch = createETagInformation(ifMatchHeaders); if (!ifMatch.isMatchedBy(eTag) && !ifMatch.getETags().isEmpty()) { - throw new PreconditionRequiredException("The If-Match precondition is not fulfilled.", - PreconditionRequiredException.MessageKeys.FAILED); + throw new PreconditionException("The If-Match precondition is not fulfilled.", + PreconditionException.MessageKeys.FAILED); } return createETagInformation(ifNoneMatchHeaders).isMatchedBy(eTag); } @@ -44,14 +44,14 @@ public class ETagHelperImpl implements ETagHelper { @Override public void checkChangePreconditions(final String eTag, final Collection<String> ifMatchHeaders, final Collection<String> ifNoneMatchHeaders) - throws PreconditionRequiredException { + throws PreconditionException { if (eTag != null) { final ETagInformation ifMatch = createETagInformation(ifMatchHeaders); final ETagInformation ifNoneMatch = createETagInformation(ifNoneMatchHeaders); if (!ifMatch.isMatchedBy(eTag) && !ifMatch.getETags().isEmpty() || ifNoneMatch.isMatchedBy(eTag)) { - throw new PreconditionRequiredException("The preconditions are not fulfilled.", - PreconditionRequiredException.MessageKeys.FAILED); + throw new PreconditionException("The preconditions are not fulfilled.", + PreconditionException.MessageKeys.FAILED); } } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/PreconditionsValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/PreconditionsValidator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/PreconditionsValidator.java index 3adf8af..32c055b 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/PreconditionsValidator.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/etag/PreconditionsValidator.java @@ -22,7 +22,7 @@ import org.apache.olingo.commons.api.edm.EdmBindingTarget; import org.apache.olingo.commons.api.edm.EdmFunctionImport; import org.apache.olingo.commons.api.edm.EdmNavigationProperty; import org.apache.olingo.server.api.etag.CustomETagSupport; -import org.apache.olingo.server.api.etag.PreconditionRequiredException; +import org.apache.olingo.server.api.etag.PreconditionException; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.UriResource; import org.apache.olingo.server.api.uri.UriResourceEntitySet; @@ -45,7 +45,7 @@ public class PreconditionsValidator { this.ifNoneMatch = ifNoneMatch; } - public void validatePreconditions(boolean isMediaValue) throws PreconditionRequiredException { + public void validatePreconditions(boolean isMediaValue) throws PreconditionException { EdmBindingTarget affectedEntitySetOrSingleton = extractInformation(); if (affectedEntitySetOrSingleton != null) { if ((isMediaValue && customETagSupport.hasMediaETag(affectedEntitySetOrSingleton)) || @@ -55,14 +55,14 @@ public class PreconditionsValidator { } } - private void checkETagHeaderPresent() throws PreconditionRequiredException { + private void checkETagHeaderPresent() throws PreconditionException { if (ifMatch == null && ifNoneMatch == null) { - throw new PreconditionRequiredException("Expected an if-match or if-none-match header", - PreconditionRequiredException.MessageKeys.MISSING_HEADER); + throw new PreconditionException("Expected an if-match or if-none-match header", + PreconditionException.MessageKeys.MISSING_HEADER); } } - private EdmBindingTarget extractInformation() throws PreconditionRequiredException { + private EdmBindingTarget extractInformation() throws PreconditionException { EdmBindingTarget lastFoundEntitySetOrSingleton = null; int counter = 0; for (UriResource uriResourcePart : uriInfo.getUriResourceParts()) { @@ -84,8 +84,8 @@ public class PreconditionsValidator { case action: // This should not be possible since the URI Parser validates this but to be sure we throw an exception. if (counter != uriInfo.getUriResourceParts().size() - 1) { - throw new PreconditionRequiredException("$Value or Action must be the last segment in the URI.", - PreconditionRequiredException.MessageKeys.INVALID_URI); + throw new PreconditionException("$Value or Action must be the last segment in the URI.", + PreconditionException.MessageKeys.INVALID_URI); } break; default: http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties index 9455c05..797aae2 100644 --- a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties +++ b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties @@ -144,4 +144,6 @@ BatchDeserializerException.INVALID_BASE_URI=The base URI do not match the servic BatchSerializerExecption.MISSING_CONTENT_ID=Each request within a change set required exactly one content id. -PreconditionRequiredException.MISSING_HEADER=The Operation you requested on this Entity requires an if-match or if-none-match header. \ No newline at end of file +PreconditionException.MISSING_HEADER=The Operation you requested on this Entity requires an if-match or if-none-match header. +PreconditionException.FAILED=The If-Match precondition is not fulfilled. +PreconditionException.INVALID_URI=Cannot evaluate preconditions for the given URI. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java index 7eeeb35..d78b50e 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentXmlSerializerTest.java @@ -213,7 +213,7 @@ public class MetadataDocumentXmlSerializerTest { @Test public void aliasTest() throws Exception { CsdlEdmProvider provider = new LocalProvider(); - ServiceMetadata serviceMetadata = new ServiceMetadataImpl(provider, Collections.<EdmxReference> emptyList()); + ServiceMetadata serviceMetadata = new ServiceMetadataImpl(provider, Collections.<EdmxReference> emptyList(), null); InputStream metadataStream = serializer.metadataDocument(serviceMetadata).getContent(); String metadata = IOUtils.toString(metadataStream); assertNotNull(metadata); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/ETagSupport.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/ETagSupport.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/ETagSupport.java index 09d4910..b098b24 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/ETagSupport.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/ETagSupport.java @@ -32,14 +32,4 @@ public class ETagSupport implements CustomETagSupport { public boolean hasMediaETag(final EdmBindingTarget entitySetOrSingleton) { return entitySetOrSingleton.getName().equals("ESMedia"); } - - @Override - public String getMetadataETag() { - return null; - } - - @Override - public String getServiceDocumentETag() { - return null; - } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-test/src/test/java/org/apache/olingo/server/core/PreconditionsValidatorTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/PreconditionsValidatorTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/PreconditionsValidatorTest.java index 7cab567..acd578c 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/PreconditionsValidatorTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/PreconditionsValidatorTest.java @@ -26,7 +26,7 @@ import org.apache.olingo.commons.api.edm.EdmBindingTarget; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.commons.core.edm.EdmProviderImpl; import org.apache.olingo.server.api.etag.CustomETagSupport; -import org.apache.olingo.server.api.etag.PreconditionRequiredException; +import org.apache.olingo.server.api.etag.PreconditionException; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.core.etag.PreconditionsValidator; import org.apache.olingo.server.core.uri.parser.Parser; @@ -198,7 +198,7 @@ public class PreconditionsValidatorTest { @Ignore @Test public void resourceSegmentAfterActionMustLeadToUriParserException() throws Exception { - //TODO: Check with URI Parser + // TODO: Check with URI Parser UriInfo uriInfo = new Parser().parseUri("ESKeyNav(1)/Namespace1_Alias.BAETTwoKeyNavRTETTwoKeyNav/PropertyInt16", null, null, getEdm()); @@ -219,8 +219,8 @@ public class PreconditionsValidatorTest { boolean isMedia = uri.endsWith("$value"); new PreconditionsValidator(etagSupport, uriInfo, null, null).validatePreconditions(isMedia); fail("Expected a PreconditionRequiredException but was not thrown"); - } catch (PreconditionRequiredException e) { - assertEquals(PreconditionRequiredException.MessageKeys.MISSING_HEADER, e.getMessageKey()); + } catch (PreconditionException e) { + assertEquals(PreconditionException.MessageKeys.MISSING_HEADER, e.getMessageKey()); } } @@ -260,15 +260,5 @@ public class PreconditionsValidatorTest { } return mediaETag; } - - @Override - public String getMetadataETag() { - return null; - } - - @Override - public String getServiceDocumentETag() { - return null; - } } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1dae9991/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java index d3bdbac..a8619b3 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/xml/MetadataDocumentTest.java @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -50,7 +50,6 @@ public class MetadataDocumentTest { final String metadata = IOUtils.toString( odata.createSerializer(ODataFormat.XML).metadataDocument(serviceMetadata).getContent()); assertNotNull(metadata); - assertThat(metadata, containsString("<edmx:Reference Uri=\"" + CORE_VOCABULARY + "\">" + "<edmx:Include Namespace=\"Org.OData.Core.V1\" Alias=\"Core\"/>"
