You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2015/06/29 13:54:34 UTC
[30/34] olingo-odata4 git commit: [OLINGO-690] the server actually
supports JSON without metadata
[OLINGO-690] the server actually supports JSON without metadata
Change-Id: Ib9289c9e81c05ed404e71bdc1bc7eb7bbdde7a7c
Signed-off-by: Christian Amend <ch...@sap.com>
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/94b3917f
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/94b3917f
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/94b3917f
Branch: refs/heads/olingo712
Commit: 94b3917f4e820396a3c8cea0b3c2a72cb845017a
Parents: e32034b
Author: Klaus Straubinger <kl...@sap.com>
Authored: Thu Jun 25 10:22:37 2015 +0200
Committer: Christian Amend <ch...@sap.com>
Committed: Thu Jun 25 11:07:39 2015 +0200
----------------------------------------------------------------------
.../olingo/commons/api/format/ContentType.java | 48 ++---
.../apache/olingo/server/core/ODataImpl.java | 181 +++++++++----------
.../olingo/server/core/ODataImplTest.java | 55 +++---
3 files changed, 138 insertions(+), 146 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/94b3917f/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 bfdebe3..57f73df 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
@@ -49,21 +49,29 @@ public final class ContentType {
private static final String APPLICATION = "application";
private static final String TEXT = "text";
private static final String MULTIPART = "multipart";
-
+
+ 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";
+
public static final ContentType APPLICATION_JSON = new ContentType(APPLICATION, "json", null);
- public static final ContentType JSON = ContentType.create(ContentType.APPLICATION_JSON, "odata.metadata=minimal");
- public static final ContentType JSON_NO_METADATA = ContentType.create(ContentType.APPLICATION_JSON,
- "odata.metadata=none");
- public static final ContentType JSON_FULL_METADATA = ContentType.create(ContentType.APPLICATION_JSON,
- "odata.metadata=full");
-
+ public static final ContentType JSON = ContentType.create(ContentType.APPLICATION_JSON,
+ PARAMETER_ODATA_METADATA + '=' + VALUE_ODATA_METADATA_MINIMAL);
+ public static final ContentType JSON_NO_METADATA = ContentType.create(ContentType.APPLICATION_JSON,
+ PARAMETER_ODATA_METADATA + '=' + VALUE_ODATA_METADATA_NONE);
+ public static final ContentType JSON_FULL_METADATA = ContentType.create(ContentType.APPLICATION_JSON,
+ PARAMETER_ODATA_METADATA + '=' + VALUE_ODATA_METADATA_FULL);
+
public static final ContentType APPLICATION_XML = new ContentType(APPLICATION, "xml", null);
public static final ContentType APPLICATION_ATOM_XML = new ContentType(APPLICATION, "atom+xml", null);
public static final ContentType APPLICATION_ATOM_XML_ENTRY = create(APPLICATION_ATOM_XML, "type=entry");
public static final ContentType APPLICATION_ATOM_XML_FEED = create(APPLICATION_ATOM_XML, "type=feed");
public static final ContentType APPLICATION_ATOM_SVC = new ContentType(APPLICATION, "atomsvc+xml", null);
-
public static final ContentType APPLICATION_OCTET_STREAM = new ContentType(APPLICATION, "octet-stream", null);
public static final ContentType APPLICATION_XHTML_XML = new ContentType(APPLICATION, "xhtml+xml", null);
@@ -81,14 +89,6 @@ public final class ContentType {
public static final ContentType MULTIPART_MIXED = new ContentType(MULTIPART, "mixed", null);
public static final ContentType MULTIPART_FORM_DATA = new ContentType(MULTIPART, "form-data", null);
- 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;
@@ -255,7 +255,7 @@ 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
@@ -266,7 +266,7 @@ public final class ContentType {
public String getParameter(final String name) {
return parameters.get(name);
}
-
+
@Override
public int hashCode() {
return 1;
@@ -323,7 +323,7 @@ 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>
@@ -331,16 +331,16 @@ public final class ContentType {
* (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)) {
+ 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.
*
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/94b3917f/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 8d41005..b4c414b 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
@@ -51,99 +51,92 @@ import org.apache.olingo.server.core.uri.UriHelperImpl;
public class ODataImpl extends OData {
- @Override
- public ODataSerializer createSerializer(final ContentType contentType)
- throws SerializerException {
- ODataSerializer serializer;
-
- // odata.metadata=none, odata.metadata=minimal, odata.metadata=full
- if (contentType.isCompatible(ContentType.APPLICATION_JSON)
- && ContentType.VALUE_ODATA_METADATA_MINIMAL
- .equals(contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA))) {
- serializer = new ODataJsonSerializer(contentType);
- } else if (contentType.isCompatible(ContentType.APPLICATION_XML)) {
- serializer = new ODataXmlSerializerImpl();
- } else {
- throw new SerializerException("Unsupported format: "
- + contentType.toContentTypeString(),
- SerializerException.MessageKeys.UNSUPPORTED_FORMAT,
- contentType.toContentTypeString());
- }
-
- return serializer;
- }
-
- @Override
- public FixedFormatSerializer createFixedFormatSerializer() {
- return new FixedFormatSerializerImpl();
- }
-
- @Override
- public ODataHttpHandler createHandler(final ServiceMetadata edm) {
- return new ODataHttpHandlerImpl(this, edm);
- }
-
- @Override
- public ServiceMetadata createServiceMetadata(
- final CsdlEdmProvider edmProvider,
- final List<EdmxReference> references) {
- return createServiceMetadata(edmProvider, references, null);
- }
-
- @Override
- public ServiceMetadata createServiceMetadata(CsdlEdmProvider edmProvider,
- List<EdmxReference> references,
- ServiceMetadataETagSupport serviceMetadataETagSupport) {
- return new ServiceMetadataImpl(edmProvider, references,
- serviceMetadataETagSupport);
- }
-
- @Override
- public FixedFormatDeserializer createFixedFormatDeserializer() {
- return new FixedFormatDeserializerImpl();
- }
-
- @Override
- public UriHelper createUriHelper() {
- return new UriHelperImpl();
- }
-
- @Override
- public ODataDeserializer createDeserializer(final ContentType contentType)
- throws DeserializerException {
- ODataDeserializer deserializer;
-
- // odata.metadata=none, odata.metadata=minimal, odata.metadata=full
- if (contentType.isCompatible(ContentType.JSON)) {
- deserializer = new ODataJsonDeserializer(contentType);
- // } else if(contentType.isCompatible(ContentType.APPLICATION_XML))
- // We do not support XML deserialization right now so this must lead
- // to an error.
- // {
- } else {
- throw new DeserializerException("Unsupported format: "
- + contentType.toContentTypeString(),
- DeserializerException.MessageKeys.UNSUPPORTED_FORMAT,
- contentType.toContentTypeString());
- }
-
- return deserializer;
- }
-
- @Override
- public EdmPrimitiveType createPrimitiveTypeInstance(
- final EdmPrimitiveTypeKind kind) {
- return EdmPrimitiveTypeFactory.getInstance(kind);
- }
-
- @Override
- public ETagHelper createETagHelper() {
- return new ETagHelperImpl();
- }
-
- @Override
- public Preferences createPreferences(final Collection<String> preferHeaders) {
- return new PreferencesImpl(preferHeaders);
- }
+ @Override
+ public ODataSerializer createSerializer(final ContentType contentType) throws SerializerException {
+ ODataSerializer serializer = null;
+
+ if (contentType.isCompatible(ContentType.APPLICATION_JSON)) {
+ final String metadata = contentType.getParameter(ContentType.PARAMETER_ODATA_METADATA);
+ if (metadata == null
+ || ContentType.VALUE_ODATA_METADATA_MINIMAL.equals(metadata)
+ || ContentType.VALUE_ODATA_METADATA_NONE.equals(metadata)) {
+ serializer = new ODataJsonSerializer(contentType);
+ }
+ } else if (contentType.isCompatible(ContentType.APPLICATION_XML)) {
+ serializer = new ODataXmlSerializerImpl();
+ }
+
+ if (serializer == null) {
+ throw new SerializerException("Unsupported format: " + contentType.toContentTypeString(),
+ SerializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ } else {
+ return serializer;
+ }
+ }
+
+ @Override
+ public FixedFormatSerializer createFixedFormatSerializer() {
+ return new FixedFormatSerializerImpl();
+ }
+
+ @Override
+ public ODataHttpHandler createHandler(final ServiceMetadata edm) {
+ return new ODataHttpHandlerImpl(this, edm);
+ }
+
+ @Override
+ public ServiceMetadata createServiceMetadata(final CsdlEdmProvider edmProvider,
+ final List<EdmxReference> references) {
+ return createServiceMetadata(edmProvider, references, null);
+ }
+
+ @Override
+ public ServiceMetadata createServiceMetadata(final CsdlEdmProvider edmProvider,
+ final List<EdmxReference> references, final ServiceMetadataETagSupport serviceMetadataETagSupport) {
+ return new ServiceMetadataImpl(edmProvider, references, serviceMetadataETagSupport);
+ }
+
+ @Override
+ public FixedFormatDeserializer createFixedFormatDeserializer() {
+ return new FixedFormatDeserializerImpl();
+ }
+
+ @Override
+ public UriHelper createUriHelper() {
+ return new UriHelperImpl();
+ }
+
+ @Override
+ public ODataDeserializer createDeserializer(final ContentType contentType) throws DeserializerException {
+ ODataDeserializer deserializer;
+
+ if (contentType.isCompatible(ContentType.JSON)) {
+ deserializer = new ODataJsonDeserializer(contentType);
+ // } else if(contentType.isCompatible(ContentType.APPLICATION_XML))
+ // We do not support XML deserialization right now so this must lead
+ // to an error.
+ // {
+ } else {
+ throw new DeserializerException("Unsupported format: " + contentType.toContentTypeString(),
+ DeserializerException.MessageKeys.UNSUPPORTED_FORMAT, contentType.toContentTypeString());
+ }
+
+ return deserializer;
+ }
+
+ @Override
+ public EdmPrimitiveType createPrimitiveTypeInstance(final EdmPrimitiveTypeKind kind) {
+ return EdmPrimitiveTypeFactory.getInstance(kind);
+ }
+
+ @Override
+ public ETagHelper createETagHelper() {
+ return new ETagHelperImpl();
+ }
+
+ @Override
+ public Preferences createPreferences(final Collection<String> preferHeaders) {
+ return new PreferencesImpl(preferHeaders);
+ }
}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/94b3917f/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
index bc48de9..87a6554 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataImplTest.java
@@ -23,36 +23,35 @@ import static org.junit.Assert.assertNotNull;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.deserializer.DeserializerException;
-import org.apache.olingo.server.api.deserializer.ODataDeserializer;
-import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.SerializerException;
import org.junit.Test;
public class ODataImplTest {
-
- private final OData odata = OData.newInstance();
-
- @Test(expected=SerializerException.class)
- public void testJsonSerializerForOdataMetadataNone() throws SerializerException {
- odata.createSerializer(ContentType.JSON_NO_METADATA);
- }
-
- @Test(expected=SerializerException.class)
- public void testJsonSerializerForODataMetadataFull() throws SerializerException {
- odata.createSerializer(ContentType.JSON_FULL_METADATA);
- }
-
- @Test
- public void testCreateJsonSerializerForODataMetadataMinimal() throws SerializerException {
- final ODataSerializer serializer = odata.createSerializer(ContentType.JSON);
-
- assertNotNull(serializer);
- }
-
- @Test
- public void testCreateJsonDeserialierForODataMetadataMinimal() throws DeserializerException {
- final ODataDeserializer deserializer = odata.createDeserializer(ContentType.JSON);
-
- assertNotNull(deserializer);
- }
+
+ private final OData odata = OData.newInstance();
+
+ @Test
+ public void serializerSupportedFormats() throws SerializerException {
+ assertNotNull(odata.createSerializer(ContentType.JSON_NO_METADATA));
+ assertNotNull(odata.createSerializer(ContentType.JSON));
+ assertNotNull(odata.createSerializer(ContentType.APPLICATION_JSON));
+ }
+
+ @Test(expected = SerializerException.class)
+ public void jsonSerializerForODataMetadataFull() throws SerializerException {
+ odata.createSerializer(ContentType.JSON_FULL_METADATA);
+ }
+
+ @Test
+ public void deserializerSupportedFormats() throws DeserializerException {
+ assertNotNull(odata.createDeserializer(ContentType.JSON_NO_METADATA));
+ assertNotNull(odata.createDeserializer(ContentType.JSON));
+ assertNotNull(odata.createDeserializer(ContentType.JSON_FULL_METADATA));
+ assertNotNull(odata.createDeserializer(ContentType.APPLICATION_JSON));
+ }
+
+ @Test(expected = DeserializerException.class)
+ public void xmlDeserializer() throws DeserializerException {
+ odata.createDeserializer(ContentType.APPLICATION_XML);
+ }
}