[OLINGO-704] Refactoring ContentType
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/91d04271 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/91d04271 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/91d04271 Branch: refs/heads/olingo712 Commit: 91d04271bacc85ff6eb0461454783b7100e64dc0 Parents: 628e2e7 Author: Christian Holzer <[email protected]> Authored: Fri Jun 19 16:05:05 2015 +0200 Committer: Christian Holzer <[email protected]> Committed: Fri Jun 19 16:27:48 2015 +0200 ---------------------------------------------------------------------- .../olingo/fit/v4/EntityRetrieveTestITCase.java | 7 +- .../api/domain/ClientEntitySetIterator.java | 7 +- .../olingo/client/core/ODataClientImpl.java | 8 +- .../cud/ODataValueUpdateRequestImpl.java | 3 +- .../invoke/AbstractODataInvokeRequest.java | 4 +- .../request/retrieve/ODataValueRequestImpl.java | 3 +- .../ClientODataDeserializerImpl.java | 8 +- .../apache/olingo/client/core/AbstractTest.java | 5 +- .../olingo/client/core/v4/EntityTest.java | 3 +- .../olingo/client/core/v4/PropertyTest.java | 5 +- .../client/core/v4/ServiceDocumentTest.java | 3 +- .../olingo/commons/api/format/ContentType.java | 82 +++++++++----------- .../olingo/commons/api/format/Format.java | 41 ---------- .../serialization/JsonEntitySerializer.java | 7 +- .../core/serialization/JsonSerializer.java | 10 ++- .../olingo/server/core/ServiceRequest.java | 13 ++-- .../olingo/server/core/ContentNegotiator.java | 11 ++- .../apache/olingo/server/core/ODataImpl.java | 39 ++++------ .../json/ODataJsonDeserializer.java | 4 +- .../serializer/json/ODataJsonSerializer.java | 12 +-- .../json/ServiceDocumentJsonSerializer.java | 4 +- .../serializer/utils/ContentTypeHelper.java | 28 +++++++ .../olingo/server/core/uri/parser/Parser.java | 10 ++- .../processor/TechnicalActionProcessor.java | 5 +- .../processor/TechnicalEntityProcessor.java | 5 +- .../TechnicalPrimitiveComplexProcessor.java | 3 +- .../tecsvc/processor/TechnicalProcessor.java | 5 ++ .../server/sample/processor/CarsProcessor.java | 12 ++- 28 files changed, 163 insertions(+), 184 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/fit/src/test/java/org/apache/olingo/fit/v4/EntityRetrieveTestITCase.java ---------------------------------------------------------------------- diff --git a/fit/src/test/java/org/apache/olingo/fit/v4/EntityRetrieveTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/v4/EntityRetrieveTestITCase.java index 903684d..58fc160 100644 --- a/fit/src/test/java/org/apache/olingo/fit/v4/EntityRetrieveTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/v4/EntityRetrieveTestITCase.java @@ -46,7 +46,6 @@ import org.apache.olingo.client.api.uri.URIBuilder; import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.junit.Test; /** @@ -71,8 +70,10 @@ public class EntityRetrieveTestITCase extends AbstractTestITCase { assertEquals("Edm.GeographyPoint", entity.getProperty("Home").getPrimitiveValue().getTypeName()); // In JSON with minimal metadata, links are not provided - if (contentType.getODataFormat() == Format.ATOM - || contentType.getODataFormat() == Format.JSON_FULL_METADATA) { + if(contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML) + || (contentType.isCompatible(ContentType.JSON) + && ContentType.VALUE_ODATA_METADATA_FULL + .equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)))) { assertEquals(testStaticServiceRootURL + "/Customers(1)", entity.getEditLink().toASCIIString()); assertEquals(3, entity.getNavigationLinks().size()); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientEntitySetIterator.java ---------------------------------------------------------------------- diff --git a/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientEntitySetIterator.java b/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientEntitySetIterator.java index cbcd7f8..30a1caf 100644 --- a/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientEntitySetIterator.java +++ b/lib/client-api/src/main/java/org/apache/olingo/client/api/domain/ClientEntitySetIterator.java @@ -34,7 +34,6 @@ import org.apache.olingo.commons.api.Constants; import org.apache.olingo.commons.api.data.Entity; import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.serialization.ODataDeserializerException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -85,8 +84,8 @@ public class ClientEntitySetIterator<ES extends ClientEntitySet, E extends Clien this.stream = stream; this.contentType = contentType; this.osEntitySet = new ByteArrayOutputStream(); - - if (contentType.getODataFormat() == Format.ATOM) { + + if(contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML)) { namespaces = getAllElementAttributes(stream, "feed", osEntitySet); } else { namespaces = null; @@ -111,7 +110,7 @@ public class ClientEntitySetIterator<ES extends ClientEntitySet, E extends Clien @SuppressWarnings("unchecked") public boolean hasNext() { if (available && cached == null) { - if (contentType.getODataFormat() == Format.ATOM) { + if (contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML)) { cached = nextAtomEntityFromEntitySet(stream, osEntitySet, namespaces); } else { cached = nextJSONEntityFromEntitySet(stream, osEntitySet); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/main/java/org/apache/olingo/client/core/ODataClientImpl.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/ODataClientImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/ODataClientImpl.java index 6018c87..dd9f68e 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/ODataClientImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/ODataClientImpl.java @@ -51,7 +51,6 @@ import org.apache.olingo.client.core.uri.FilterFactoryImpl; import org.apache.olingo.client.core.uri.URIBuilderImpl; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.serialization.ODataSerializer; import org.apache.olingo.commons.core.serialization.AtomSerializer; import org.apache.olingo.commons.core.serialization.JsonSerializer; @@ -132,9 +131,10 @@ public class ODataClientImpl implements ODataClient { @Override public ODataSerializer getSerializer(final ContentType contentType) { - return contentType.getODataFormat() == Format.ATOM || contentType.getODataFormat() == Format.XML ? - new AtomSerializer() : - new JsonSerializer(false, contentType); + return contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML, + ContentType.APPLICATION_XML) ? + new AtomSerializer() : new JsonSerializer(false, contentType); + } @Override http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/ODataValueUpdateRequestImpl.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/ODataValueUpdateRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/ODataValueUpdateRequestImpl.java index 52e27fa..407a199 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/ODataValueUpdateRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/cud/ODataValueUpdateRequestImpl.java @@ -35,7 +35,6 @@ import org.apache.olingo.client.core.communication.response.AbstractODataRespons import org.apache.olingo.client.core.uri.URIUtils; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpMethod; /** @@ -110,7 +109,7 @@ public class ODataValueUpdateRequestImpl extends AbstractODataBasicRequest<OData try { resValue = odataClient.getObjectFactory().newPrimitiveValueBuilder(). - setType(contentType.getODataFormat() == Format.TEXT_PLAIN + setType(contentType.isCompatible(ContentType.TEXT_PLAIN) ? EdmPrimitiveTypeKind.String : EdmPrimitiveTypeKind.Stream). setValue(getRawResponse()). build(); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java index 88fd2e2..c721ed8 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/invoke/AbstractODataInvokeRequest.java @@ -44,7 +44,6 @@ import org.apache.olingo.client.core.communication.request.AbstractODataBasicReq import org.apache.olingo.client.core.communication.response.AbstractODataResponse; import org.apache.olingo.client.core.uri.URIUtils; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.commons.api.serialization.ODataDeserializerException; import org.apache.olingo.commons.api.serialization.ODataSerializerException; @@ -97,7 +96,8 @@ public abstract class AbstractODataInvokeRequest<T extends ClientInvokeResult> } private String getActualFormat(final ContentType contentType) { - return ((ClientProperty.class.isAssignableFrom(reference) && contentType.getODataFormat() == Format.ATOM) + return ((ClientProperty.class.isAssignableFrom(reference) + && contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML)) ? ContentType.APPLICATION_XML : contentType).toContentTypeString(); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataValueRequestImpl.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataValueRequestImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataValueRequestImpl.java index 229ab1a..e2161d4 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataValueRequestImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/communication/request/retrieve/ODataValueRequestImpl.java @@ -30,7 +30,6 @@ import org.apache.olingo.client.api.domain.ClientPrimitiveValue; import org.apache.olingo.client.api.http.HttpClientException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; /** * This class implements an OData entity property value query request. @@ -79,7 +78,7 @@ public class ODataValueRequestImpl extends AbstractODataRetrieveRequest<ClientPr try { value = odataClient.getObjectFactory().newPrimitiveValueBuilder(). - setType(contentType.getODataFormat() == Format.TEXT_PLAIN + setType(contentType.isCompatible(ContentType.TEXT_PLAIN) ? EdmPrimitiveTypeKind.String : EdmPrimitiveTypeKind.Stream). setValue(IOUtils.toString(getRawResponse())).build(); } catch (Exception e) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java index 5c44372..9bd4c6c 100644 --- a/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java +++ b/lib/client-core/src/main/java/org/apache/olingo/client/core/serialization/ClientODataDeserializerImpl.java @@ -38,7 +38,6 @@ import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.serialization.ODataDeserializer; import org.apache.olingo.commons.api.serialization.ODataDeserializerException; import org.apache.olingo.commons.core.serialization.AtomDeserializer; @@ -64,7 +63,8 @@ public class ClientODataDeserializerImpl implements ClientODataDeserializer { public ClientODataDeserializerImpl(final boolean serverMode, final ContentType contentType) { this.contentType = contentType; - if (contentType.getODataFormat() == Format.XML || contentType.getODataFormat() == Format.ATOM) { + if(contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML, + ContentType.APPLICATION_XML )) { deserializer = new AtomDeserializer(); } else { deserializer = new JsonDeserializer(serverMode); @@ -123,7 +123,7 @@ public class ClientODataDeserializerImpl implements ClientODataDeserializer { @Override public ResWrap<ServiceDocument> toServiceDocument(final InputStream input) throws ODataDeserializerException { - return contentType.getODataFormat() == Format.XML ? + return contentType.isCompatible(ContentType.APPLICATION_XML) ? new XMLServiceDocumentDeserializer(false).toServiceDocument(input) : new JSONServiceDocumentDeserializer(false).toServiceDocument(input); } @@ -131,7 +131,7 @@ public class ClientODataDeserializerImpl implements ClientODataDeserializer { @Override public ResWrap<Delta> toDelta(final InputStream input) throws ODataDeserializerException { try { - return contentType.getODataFormat() == Format.ATOM ? + return contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML) ? new AtomDeserializer().delta(input) : new JsonDeltaDeserializer(false).toDelta(input); } catch (XMLStreamException e) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/test/java/org/apache/olingo/client/core/AbstractTest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/AbstractTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/AbstractTest.java index ed21e24..beeda7e 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/AbstractTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/AbstractTest.java @@ -20,7 +20,6 @@ package org.apache.olingo.client.core; import org.apache.olingo.client.api.ODataClient; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.custommonkey.xmlunit.XMLUnit; import org.junit.BeforeClass; @@ -45,7 +44,7 @@ public abstract class AbstractTest { } protected String getSuffix(final ContentType contentType) { - return contentType.getODataFormat() == Format.ATOM - || contentType.getODataFormat() == Format.XML ? "xml" : "json"; + return contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML, + ContentType.APPLICATION_XML) ? "xml": "json"; } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/EntityTest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/EntityTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/EntityTest.java index 98ba7f9..a21cc15 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/EntityTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/EntityTest.java @@ -45,7 +45,6 @@ import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.core.edm.primitivetype.EdmDateTimeOffset; import org.apache.olingo.commons.core.edm.primitivetype.EdmDuration; import org.junit.Ignore; @@ -103,7 +102,7 @@ public class EntityTest extends AbstractTest { } if ("Orders".equals(link.getName())) { checked++; - if (contentType.getODataFormat() == Format.ATOM) { + if(contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC, ContentType.APPLICATION_ATOM_XML)) { assertEquals(ClientLinkType.ENTITY_SET_NAVIGATION, link.getType()); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/PropertyTest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/PropertyTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/PropertyTest.java index ccce804..73ae7f1 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/PropertyTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/PropertyTest.java @@ -32,7 +32,6 @@ import org.apache.olingo.client.api.domain.ClientProperty; import org.apache.olingo.client.api.domain.ClientValue; import org.apache.olingo.client.core.AbstractTest; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.serialization.ODataDeserializerException; import org.apache.olingo.commons.api.serialization.ODataSerializerException; import org.junit.Test; @@ -53,7 +52,7 @@ public class PropertyTest extends AbstractTest { final ClientProperty written = getClient().getReader().readProperty( getClient().getWriter().writeProperty(property, contentType), contentType); // This is needed because type information gets lost with serialization - if (contentType.getODataFormat() == Format.APPLICATION_XML) { + if (contentType.isCompatible(ContentType.APPLICATION_XML)) { final ClientProperty comparable = getClient().getObjectFactory().newEnumProperty(property.getName(), getClient().getObjectFactory(). newEnumValue(property.getEnumValue().getTypeName(), written.getEnumValue().getValue())); @@ -114,7 +113,7 @@ public class PropertyTest extends AbstractTest { final ClientProperty written = getClient().getReader().readProperty( getClient().getWriter().writeProperty(property, contentType), contentType); // This is needed because type information gets lost with JSON serialization - if (contentType.getODataFormat() == Format.APPLICATION_XML) { + if(contentType.isCompatible(ContentType.APPLICATION_XML)) { final ClientCollectionValue<ClientValue> typedValue = getClient().getObjectFactory(). newCollectionValue(property.getCollectionValue().getTypeName()); for (final Iterator<ClientValue> itor = written.getCollectionValue().iterator(); itor.hasNext();) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/ServiceDocumentTest.java ---------------------------------------------------------------------- diff --git a/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/ServiceDocumentTest.java b/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/ServiceDocumentTest.java index 65bd697..97f6d81 100644 --- a/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/ServiceDocumentTest.java +++ b/lib/client-core/src/test/java/org/apache/olingo/client/core/v4/ServiceDocumentTest.java @@ -30,7 +30,6 @@ import org.apache.olingo.client.api.domain.ClientServiceDocument; import org.apache.olingo.client.core.AbstractTest; import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.serialization.ODataDeserializerException; import org.junit.Test; @@ -42,7 +41,7 @@ public class ServiceDocumentTest extends AbstractTest { } private String getFileExtension(final ContentType contentType) { - return contentType.getODataFormat() == Format.XML ? "xml" : "json"; + return contentType.isCompatible(ContentType.APPLICATION_XML) ? "xml" : "json"; } private ClientServiceDocument parse(final ContentType contentType) throws ODataDeserializerException { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java ---------------------------------------------------------------------- diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java index 90572a4..2aea46f 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/ContentType.java @@ -82,7 +82,12 @@ public final class ContentType { public static final String PARAMETER_CHARSET = "charset"; public static final String PARAMETER_IEEE754_COMPATIBLE = "IEEE754Compatible"; - + public static final String PARAMETER_ODATA_METADATA = "odata.metadata"; + + public static final String VALUE_ODATA_METADATA_NONE = "none"; + public static final String VALUE_ODATA_METADATA_MINIMAL = "minimal"; + public static final String VALUE_ODATA_METADATA_FULL = "full"; + private final String type; private final String subtype; private final Map<String, String> parameters; @@ -249,7 +254,18 @@ public final class ContentType { public Map<String, String> getParameters() { return Collections.unmodifiableMap(parameters); } - + + /** + * Returns the value of a given parameter. + * If the parameter does not exists the method returns null + * + * @param name The parameter to get + * @return the value of the parameter or null if the parameter is not present + */ + public String getParameter(final String name) { + return parameters.get(name); + } + @Override public int hashCode() { return 1; @@ -306,7 +322,24 @@ public final class ContentType { public boolean isCompatible(final ContentType other) { return type.equalsIgnoreCase(other.type) && subtype.equalsIgnoreCase(other.subtype); } - + + /** + * <p>{@link ContentType}s are <b>compatible</b> + * if <code>type</code> and <code>subtype</code> have the same value.</p> + * <p>The set <code>parameters</code> are <b>always</b> ignored + * (for compare with parameters see {@link #equals(Object)}).</p> + * @return <code>true</code> if both instances are compatible (see definition above), otherwise <code>false</code>. + */ + public boolean isCompatible(final ContentType...otherTypes) { + for(final ContentType otherType : otherTypes) { + if(isCompatible(otherType)) { + return true; + } + } + + return false; + } + /** * Checks whether both strings are equal ignoring the case of the strings. * @@ -337,49 +370,6 @@ public final class ContentType { return sb.toString(); } - /** - * Returns the {@link Format} of the current Content-Type - * - * @return {@link Format} - */ - public Format getODataFormat() { - if (isCompatible(ContentType.APPLICATION_ATOM_XML) - || isCompatible(ContentType.APPLICATION_ATOM_SVC)) { - return Format.ATOM; - } else if (isCompatible(ContentType.APPLICATION_XML)) { - return Format.XML; - } else if (isCompatible(ContentType.APPLICATION_JSON)) { - String jsonVariant = getParameters().get("odata.metadata"); - if (jsonVariant != null) { - if ("none".equals(jsonVariant)) { - return Format.JSON_NO_METADATA; - } else if ("minimal".equals(jsonVariant)) { - return Format.JSON; - } else if ("full".equals(jsonVariant)) { - return Format.JSON_FULL_METADATA; - } - } - return Format.JSON; - } else if (isCompatible(ContentType.APPLICATION_OCTET_STREAM)) { - return Format.APPLICATION_OCTET_STREAM; - } else if (isCompatible(ContentType.TEXT_PLAIN)) { - return Format.TEXT_PLAIN; - } else if (isCompatible(ContentType.APPLICATION_XHTML_XML)) { - return Format.APPLICATION_XHTML_XML; - } else if (isCompatible(ContentType.APPLICATION_SVG_XML)) { - return Format.APPLICATION_SVG_XML; - } else if (isCompatible(ContentType.APPLICATION_FORM_URLENCODED)) { - return Format.APPLICATION_FORM_URLENCODED; - } else if (isCompatible(ContentType.MULTIPART_FORM_DATA)) { - return Format.MULTIPART_FORM_DATA; - } else if (isCompatible(ContentType.TEXT_XML)) { - return Format.TEXT_XML; - } else if (isCompatible(ContentType.TEXT_HTML)) { - return Format.TEXT_HTML; - } - return null; - } - @Override public String toString() { return toContentTypeString(); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/Format.java ---------------------------------------------------------------------- diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/Format.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/Format.java deleted file mode 100644 index 09e6a01..0000000 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/Format.java +++ /dev/null @@ -1,41 +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.commons.api.format; - -/** - * Available formats to be used in various contexts. - */ -public enum Format { - JSON_NO_METADATA, - JSON, - JSON_FULL_METADATA, - ATOM, - XML, - APPLICATION_XML, - APPLICATION_XHTML_XML, - APPLICATION_SVG_XML, - APPLICATION_JSON, - APPLICATION_FORM_URLENCODED, - MULTIPART_FORM_DATA, - APPLICATION_OCTET_STREAM, - TEXT_PLAIN, - TEXT_XML, - TEXT_HTML -} - http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java index 6cfd78d..35f9ec8 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonEntitySerializer.java @@ -30,7 +30,6 @@ import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.ResWrap; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.core.edm.EdmTypeInfo; import com.fasterxml.jackson.core.JsonGenerator; @@ -70,12 +69,12 @@ public class JsonEntitySerializer extends JsonSerializer { } } - if (StringUtils.isNotBlank(entity.getType()) && contentType.getODataFormat() != Format.JSON_NO_METADATA) { + if (StringUtils.isNotBlank(entity.getType()) && !isODataMetadataNone(contentType)) { jgen.writeStringField(Constants.JSON_TYPE, new EdmTypeInfo.Builder().setTypeExpression(entity.getType()).build().external()); } - if (entity.getId() != null && contentType.getODataFormat() != Format.JSON_NO_METADATA) { + if (entity.getId() != null && !isODataMetadataNone(contentType)) { jgen.writeStringField(Constants.JSON_ID, entity.getId().toASCIIString()); } @@ -97,7 +96,7 @@ public class JsonEntitySerializer extends JsonSerializer { } } - if (contentType.getODataFormat() != Format.JSON_NO_METADATA) { + if (!isODataMetadataNone(contentType)) { links(entity, jgen); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java ---------------------------------------------------------------------- diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java index 7fb1b06..bfbfc8d 100755 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/JsonSerializer.java @@ -44,7 +44,6 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.geo.Geospatial; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.serialization.ODataSerializer; import org.apache.olingo.commons.api.serialization.ODataSerializerException; import org.apache.olingo.commons.core.edm.EdmTypeInfo; @@ -314,7 +313,7 @@ public class JsonSerializer implements ODataSerializer { throws IOException, EdmPrimitiveTypeException { jgen.writeStartObject(); - if (typeInfo != null && contentType.getODataFormat() != Format.JSON_NO_METADATA) { + if (typeInfo != null && !isODataMetadataNone(contentType)) { jgen.writeStringField(Constants.JSON_TYPE, typeInfo.external()); } @@ -361,7 +360,7 @@ public class JsonSerializer implements ODataSerializer { valuable.isPrimitive()) || valuable.isNull()) { type = EdmPrimitiveTypeKind.String.getFullQualifiedName().toString(); } - if (StringUtils.isNotBlank(type) && contentType.getODataFormat() != Format.JSON_NO_METADATA) { + if (StringUtils.isNotBlank(type) && !isODataMetadataNone(contentType)) { jgen.writeFieldName( name + StringUtils.prependIfMissing(Constants.JSON_TYPE, "@")); jgen.writeString(new EdmTypeInfo.Builder().setTypeExpression(type).build().external()); @@ -380,4 +379,9 @@ public class JsonSerializer implements ODataSerializer { final String parameter = contentType.getParameters().get(ContentType.PARAMETER_IEEE754_COMPATIBLE); return parameter == null ? false : "true".equals(parameter.toLowerCase()); } + + protected boolean isODataMetadataNone(final ContentType contentType) { + return contentType.isCompatible(ContentType.APPLICATION_JSON) + && ContentType.VALUE_ODATA_METADATA_NONE.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)); + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceRequest.java ---------------------------------------------------------------------- diff --git a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceRequest.java b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceRequest.java index 26895de..76e8d5f 100644 --- a/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceRequest.java +++ b/lib/server-core-ext/src/main/java/org/apache/olingo/server/core/ServiceRequest.java @@ -28,7 +28,6 @@ import java.util.StringTokenizer; import org.apache.olingo.commons.api.data.ContextURL; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.OData; @@ -143,16 +142,15 @@ public abstract class ServiceRequest { @SuppressWarnings("unchecked") public <T> T getSerializerOptions(Class<T> serilizerOptions, ContextURL contextUrl, boolean references) throws ContentNegotiatorException { - final Format format = getResponseContentType().getODataFormat(); - + if (serilizerOptions.isAssignableFrom(EntitySerializerOptions.class)) { return (T) EntitySerializerOptions.with() - .contextURL(format == Format.JSON_NO_METADATA ? null : contextUrl) + .contextURL(isODataMetadataNone(getResponseContentType()) ? null : contextUrl) .expand(uriInfo.getExpandOption()).select(this.uriInfo.getSelectOption()) .setWriteOnlyReferences(references).build(); } else if (serilizerOptions.isAssignableFrom(EntityCollectionSerializerOptions.class)) { return (T) EntityCollectionSerializerOptions.with() - .contextURL(format == Format.JSON_NO_METADATA ? null : contextUrl) + .contextURL(isODataMetadataNone(getResponseContentType()) ? null : contextUrl) .count(uriInfo.getCountOption()).expand(uriInfo.getExpandOption()) .select(uriInfo.getSelectOption()).setWriteOnlyReferences(references).build(); } else if (serilizerOptions.isAssignableFrom(ComplexSerializerOptions.class)) { @@ -260,4 +258,9 @@ public abstract class ServiceRequest { dispatcher.visit(uriInfo); return (DataRequest)dispatcher.request; } + + private boolean isODataMetadataNone(final ContentType contentType) { + return contentType.isCompatible(ContentType.JSON) + && ContentType.VALUE_ODATA_METADATA_NONE.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)); + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java index f7675d6..a262b69 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java @@ -23,7 +23,6 @@ import java.util.List; import org.apache.olingo.commons.api.format.AcceptType; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.serializer.CustomContentTypeSupport; @@ -32,6 +31,10 @@ import org.apache.olingo.server.api.uri.queryoption.FormatOption; public class ContentNegotiator { + private static final String ATOM = "atom"; + private static final String JSON = "json"; + private static final String XML = "xml"; + private ContentNegotiator() {} private static List<ContentType> getDefaultSupportedContentTypes(final RepresentationType type) { @@ -79,9 +82,9 @@ public class ContentNegotiator { if (formatOption != null && formatOption.getFormat() != null) { final String formatString = formatOption.getFormat().trim(); final ContentType contentType = - Format.JSON.name().equalsIgnoreCase(formatString) ? ContentType.JSON : - Format.XML.name().equalsIgnoreCase(formatString) ? ContentType.APPLICATION_XML : - Format.ATOM.name().equalsIgnoreCase(formatString) ? ContentType.APPLICATION_ATOM_XML : null; + JSON.equalsIgnoreCase(formatString) ? ContentType.JSON : + XML.equalsIgnoreCase(formatString) ? ContentType.APPLICATION_XML : + ATOM.equalsIgnoreCase(formatString) ? ContentType.APPLICATION_ATOM_XML : null; try { result = getAcceptedType( AcceptType.fromContentType(contentType == null ? http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/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 55c6b71..1ad95a1 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 @@ -24,7 +24,6 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveType; import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; import org.apache.olingo.commons.api.edm.provider.CsdlEdmProvider; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataHttpHandler; @@ -51,21 +50,16 @@ public class ODataImpl extends OData { @Override public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException { - final Format format = contentType.getODataFormat(); ODataSerializer serializer; - - switch (format) { - case JSON: - case JSON_NO_METADATA: - case JSON_FULL_METADATA: + + // odata.metadata=none, odata.metadata=minimal, odata.metadata=full + if(contentType.isCompatible(ContentType.APPLICATION_JSON)) { serializer = new ODataJsonSerializer(contentType); - break; - case XML: + } else if(contentType.isCompatible(ContentType.APPLICATION_XML)) { serializer = new ODataXmlSerializerImpl(); - break; - default: - throw new SerializerException("Unsupported format: " + format, - SerializerException.MessageKeys.UNSUPPORTED_FORMAT, format.toString()); + } else { + throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(), + SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString()); } return serializer; @@ -105,21 +99,18 @@ public class ODataImpl extends OData { @Override public ODataDeserializer createDeserializer(final ContentType contentType) throws DeserializerException { - final Format format = contentType.getODataFormat(); ODataDeserializer deserializer; - - switch (format) { - case JSON: - case JSON_NO_METADATA: - case JSON_FULL_METADATA: + + // odata.metadata=none, odata.metadata=minimal, odata.metadata=full + if(contentType.isCompatible(ContentType.JSON)) { deserializer = new ODataJsonDeserializer(contentType); - break; - case XML: + //} else if(contentType.isCompatible(ContentType.APPLICATION_XML)) { // We do not support XML deserialization right now so this must lead to an error. - default: - throw new DeserializerException("Unsupported format: " + format, - DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, format.toString()); + } else { + throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(), + DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString()); } + return deserializer; } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java index 9c3c763..c67efbc 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java @@ -882,8 +882,8 @@ public class ODataJsonDeserializer implements ODataDeserializer { } private boolean isODataIEEE754Compatible(final ContentType contentType) { - return contentType.getParameters().containsKey("ieee754compatible") + return contentType.getParameters().containsKey(ContentType.PARAMETER_IEEE754_COMPATIBLE) && Boolean.TRUE.toString().toLowerCase().equals( - contentType.getParameters().get("ieee754compatible").toLowerCase()); + contentType.getParameter(ContentType.PARAMETER_IEEE754_COMPATIBLE).toLowerCase()); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java index 671a069..f50fcad 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java @@ -42,7 +42,6 @@ import org.apache.olingo.commons.api.edm.EdmProperty; import org.apache.olingo.commons.api.edm.EdmStructuredType; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; import org.apache.olingo.server.api.ODataServerError; import org.apache.olingo.server.api.ServiceMetadata; @@ -61,6 +60,7 @@ import org.apache.olingo.server.api.uri.queryoption.ExpandOption; import org.apache.olingo.server.api.uri.queryoption.SelectOption; import org.apache.olingo.server.core.serializer.SerializerResultImpl; import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer; +import org.apache.olingo.server.core.serializer.utils.ContentTypeHelper; import org.apache.olingo.server.core.serializer.utils.ContextURLBuilder; import org.apache.olingo.server.core.serializer.utils.ExpandSelectHelper; import org.apache.olingo.server.core.uri.UriHelperImpl; @@ -196,7 +196,7 @@ public class ODataJsonSerializer implements ODataSerializer { } private ContextURL checkContextURL(final ContextURL contextURL) throws SerializerException { - if (contentType.getODataFormat() == Format.JSON_NO_METADATA) { + if(ContentTypeHelper.isODataMetadataNone(contentType)) { return null; } else if (contextURL == null) { throw new SerializerException("ContextURL null!", SerializerException.MessageKeys.NO_CONTEXT_URL); @@ -205,7 +205,7 @@ public class ODataJsonSerializer implements ODataSerializer { } private void writeMetadataETag(final ServiceMetadata metadata, JsonGenerator json) throws IOException { - if (contentType.getODataFormat() != Format.JSON_NO_METADATA + if (!ContentTypeHelper.isODataMetadataNone(contentType) && metadata != null && metadata.getServiceMetadataETagSupport() != null && metadata.getServiceMetadataETagSupport().getMetadataETag() != null) { @@ -236,7 +236,7 @@ public class ODataJsonSerializer implements ODataSerializer { final SelectOption select, final boolean onlyReference, final JsonGenerator json) throws IOException, SerializerException { json.writeStartObject(); - if (contentType.getODataFormat() != Format.JSON_NO_METADATA) { + if(!ContentTypeHelper.isODataMetadataNone(contentType)) { if (contextURL != null) { // top-level entity json.writeStringField(Constants.JSON_CONTEXT, ContextURLBuilder.create(contextURL).toASCIIString()); writeMetadataETag(metadata, json); @@ -754,8 +754,8 @@ public class ODataJsonSerializer implements ODataSerializer { } private boolean isODataIEEE754Compatible(final ContentType contentType) { - return contentType.getParameters().containsKey("ieee754compatible") + return contentType.getParameters().containsKey(ContentType.PARAMETER_IEEE754_COMPATIBLE) && Boolean.TRUE.toString().toLowerCase().equals( - contentType.getParameters().get("ieee754compatible").toLowerCase()); + contentType.getParameter(ContentType.PARAMETER_IEEE754_COMPATIBLE).toLowerCase()); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ServiceDocumentJsonSerializer.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ServiceDocumentJsonSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ServiceDocumentJsonSerializer.java index ad98aa7..5b34184 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ServiceDocumentJsonSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ServiceDocumentJsonSerializer.java @@ -27,8 +27,8 @@ import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmFunctionImport; import org.apache.olingo.commons.api.edm.EdmSingleton; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.server.api.ServiceMetadata; +import org.apache.olingo.server.core.serializer.utils.ContentTypeHelper; import com.fasterxml.jackson.core.JsonGenerator; @@ -59,7 +59,7 @@ public class ServiceDocumentJsonSerializer { + Constants.METADATA; gen.writeObjectField(Constants.JSON_CONTEXT, metadataUri); - if (contentType.getODataFormat() != Format.JSON_NO_METADATA + if (!ContentTypeHelper.isODataMetadataNone(contentType) && metadata != null && metadata.getServiceMetadataETagSupport() != null && metadata.getServiceMetadataETagSupport().getMetadataETag() != null) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContentTypeHelper.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContentTypeHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContentTypeHelper.java new file mode 100644 index 0000000..1e80666 --- /dev/null +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContentTypeHelper.java @@ -0,0 +1,28 @@ +/* + * 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.core.serializer.utils; + +import org.apache.olingo.commons.api.format.ContentType; + +public class ContentTypeHelper { + public static boolean isODataMetadataNone(final ContentType contentType) { + return contentType.isCompatible(ContentType.APPLICATION_JSON) + && ContentType.VALUE_ODATA_METADATA_NONE.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java index 69651b3..f764c53 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java @@ -32,7 +32,6 @@ import org.antlr.v4.runtime.atn.PredictionMode; import org.antlr.v4.runtime.misc.ParseCancellationException; import org.apache.olingo.commons.api.ODataRuntimeException; import org.apache.olingo.commons.api.edm.Edm; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.UriInfoKind; import org.apache.olingo.server.api.uri.UriResource; @@ -68,6 +67,9 @@ import org.apache.olingo.server.core.uri.queryoption.SkipTokenOptionImpl; import org.apache.olingo.server.core.uri.queryoption.TopOptionImpl; public class Parser { + private static final String ATOM = "atom"; + private static final String JSON = "json"; + private static final String XML = "xml"; int logLevel = 0; private enum ParserEntryRules { @@ -177,9 +179,9 @@ public class Parser { FormatOptionImpl formatOption = new FormatOptionImpl(); formatOption.setName(option.name); formatOption.setText(option.value); - if (option.value.equalsIgnoreCase(Format.JSON.name()) - || option.value.equalsIgnoreCase(Format.XML.name()) - || option.value.equalsIgnoreCase(Format.ATOM.name()) + if (option.value.equalsIgnoreCase(JSON) + || option.value.equalsIgnoreCase(XML) + || option.value.equalsIgnoreCase(ATOM) || isFormatSyntaxValid(option.value)) { formatOption.setFormat(option.value); } else { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalActionProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalActionProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalActionProcessor.java index b6159ff..1e038b2 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalActionProcessor.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalActionProcessor.java @@ -31,7 +31,6 @@ import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.EdmPrimitiveType; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.server.api.ODataApplicationException; @@ -94,7 +93,7 @@ public class TechnicalActionProcessor extends TechnicalProcessor final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource()); final EdmEntityType type = (EdmEntityType) action.getReturnType().getType(); EntityCollectionSerializerOptions options = EntityCollectionSerializerOptions.with() - .contextURL(responseFormat.getODataFormat() == Format.JSON_NO_METADATA ? null : + .contextURL(isODataMetadataNone(responseFormat) ? null : getContextUrl(edmEntitySet, type, false)).build(); response.setContent(odata.createSerializer(requestFormat) .entityCollection(serviceMetadata, type, collection, options).getContent()); @@ -131,7 +130,7 @@ public class TechnicalActionProcessor extends TechnicalProcessor type, entityResult.getEntity(), EntitySerializerOptions.with() - .contextURL(responseFormat.getODataFormat() == Format.JSON_NO_METADATA ? null : + .contextURL(isODataMetadataNone(responseFormat) ? null : getContextUrl(edmEntitySet, type, true)).build()) .getContent()); response.setStatusCode((entityResult.isCreated() ? HttpStatusCode.CREATED : HttpStatusCode.OK) http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java index 32f76b1..f86646d 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalEntityProcessor.java @@ -28,7 +28,6 @@ import org.apache.olingo.commons.api.data.EntityCollection; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpContentType; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpMethod; @@ -451,7 +450,7 @@ public class TechnicalEntityProcessor extends TechnicalProcessor edmEntityType, entityCollection, EntityCollectionSerializerOptions.with() - .contextURL(requestedFormat.getODataFormat() == Format.JSON_NO_METADATA ? null : + .contextURL(isODataMetadataNone(requestedFormat) ? null : getContextUrl(edmEntitySet, edmEntityType, false, expand, select)) .count(countOption) .expand(expand).select(select) @@ -484,7 +483,7 @@ public class TechnicalEntityProcessor extends TechnicalProcessor edmEntityType, entity, EntitySerializerOptions.with() - .contextURL(requestedFormat.getODataFormat() == Format.JSON_NO_METADATA ? null : + .contextURL(isODataMetadataNone(requestedFormat) ? null : getContextUrl(edmEntitySet, edmEntityType, true, expand, select)) .expand(expand).select(select) .build()); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java index 50e1c1b..7a31f84 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalPrimitiveComplexProcessor.java @@ -38,7 +38,6 @@ import org.apache.olingo.commons.api.edm.EdmStructuredType; import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.commons.api.http.HttpStatusCode; @@ -396,7 +395,7 @@ public class TechnicalPrimitiveComplexProcessor extends TechnicalProcessor final RepresentationType representationType, final ContentType responseFormat, final ExpandOption expand, final SelectOption select) throws ODataLibraryException { ODataSerializer serializer = odata.createSerializer(responseFormat); - final ContextURL contextURL = responseFormat.getODataFormat() == Format.JSON_NO_METADATA ? null : + final ContextURL contextURL = isODataMetadataNone(responseFormat) ? null : getContextUrl(edmEntitySet, entity, path, type, representationType, expand, select); SerializerResult result = null; switch (representationType) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java index f3012d9..64327f3 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/TechnicalProcessor.java @@ -255,4 +255,9 @@ public abstract class TechnicalProcessor implements Processor { HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT); } } + + protected boolean isODataMetadataNone(final ContentType contentType) { + return contentType.isCompatible(ContentType.APPLICATION_JSON) + && ContentType.VALUE_ODATA_METADATA_NONE.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)); + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/91d04271/samples/server/src/main/java/org/apache/olingo/server/sample/processor/CarsProcessor.java ---------------------------------------------------------------------- diff --git a/samples/server/src/main/java/org/apache/olingo/server/sample/processor/CarsProcessor.java b/samples/server/src/main/java/org/apache/olingo/server/sample/processor/CarsProcessor.java index 013462c..0b1917f 100644 --- a/samples/server/src/main/java/org/apache/olingo/server/sample/processor/CarsProcessor.java +++ b/samples/server/src/main/java/org/apache/olingo/server/sample/processor/CarsProcessor.java @@ -34,7 +34,6 @@ import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmPrimitiveType; import org.apache.olingo.commons.api.edm.EdmProperty; import org.apache.olingo.commons.api.format.ContentType; -import org.apache.olingo.commons.api.format.Format; import org.apache.olingo.commons.api.http.HttpContentType; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; @@ -109,7 +108,7 @@ public class CarsProcessor implements EntityCollectionProcessor, EntityProcessor final SelectOption select = uriInfo.getSelectOption(); InputStream serializedContent = serializer.entityCollection(edm, edmEntitySet.getEntityType(), entitySet, EntityCollectionSerializerOptions.with() - .contextURL(requestedContentType.getODataFormat() == Format.JSON_NO_METADATA ? null : + .contextURL(isODataMetadataNone(requestedContentType) ? null : getContextUrl(edmEntitySet, false, expand, select, null)) .count(uriInfo.getCountOption()) .expand(expand).select(select) @@ -146,7 +145,7 @@ public class CarsProcessor implements EntityCollectionProcessor, EntityProcessor final SelectOption select = uriInfo.getSelectOption(); InputStream serializedContent = serializer.entity(edm, edmEntitySet.getEntityType(), entity, EntitySerializerOptions.with() - .contextURL(requestedContentType.getODataFormat() == Format.JSON_NO_METADATA ? null : + .contextURL(isODataMetadataNone(requestedContentType) ? null : getContextUrl(edmEntitySet, true, expand, select, null)) .expand(expand).select(select) .build()).getContent(); @@ -253,7 +252,7 @@ public class CarsProcessor implements EntityCollectionProcessor, EntityProcessor response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); } else { ODataSerializer serializer = odata.createSerializer(contentType); - final ContextURL contextURL = contentType.getODataFormat() == Format.JSON_NO_METADATA ? null : + final ContextURL contextURL = isODataMetadataNone(contentType) ? null : getContextUrl(edmEntitySet, true, null, null, edmProperty.getName()); InputStream serializerContent = complex ? serializer.complex(edm, (EdmComplexType) edmProperty.getType(), property, @@ -365,4 +364,9 @@ public class CarsProcessor implements EntityCollectionProcessor, EntityProcessor throw new ODataApplicationException("Entity update is not supported yet.", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH); } + + public static boolean isODataMetadataNone(final ContentType contentType) { + return contentType.isCompatible(ContentType.APPLICATION_JSON) + && ContentType.VALUE_ODATA_METADATA_NONE.equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA)); + } } \ No newline at end of file
