From d049864ea4657fc95114e1e6bd72fd85f4f4f671 Mon Sep 17 00:00:00 2001 From: Stephan Klevenz Date: Thu, 3 Jul 2014 09:09:22 +0200 Subject: [PATCH] [OLINGO-337] refactor requestedContentType --- .../server/api/processor/CollectionProcessor.java | 3 ++- .../server/api/processor/DefaultProcessor.java | 10 +++++----- .../olingo/server/api/processor/EntityProcessor.java | 4 +++- .../server/api/processor/MetadataProcessor.java | 3 ++- .../api/processor/ServiceDocumentProcessor.java | 4 +++- .../org/apache/olingo/server/core/ODataHandler.java | 12 ++++++------ .../olingo/server/core/ContentNegotiatorTest.java | 12 ++++++------ .../server/tecsvc/processor/SampleJsonProcessor.java | 9 +++++---- .../server/tecsvc/processor/TechnicalProcessor.java | 8 ++++++-- 9 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CollectionProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CollectionProcessor.java index bf45c3ff4..2d6f1f50d 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CollectionProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/CollectionProcessor.java @@ -18,12 +18,13 @@ */ package org.apache.olingo.server.api.processor; +import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.uri.UriInfo; public interface CollectionProcessor extends Processor { - void readCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format); + void readCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType requestedContentType); } 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 9251c73c4..97821f1e7 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 @@ -21,8 +21,8 @@ package org.apache.olingo.server.api.processor; import java.io.InputStream; import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.format.ODataFormat; -import org.apache.olingo.commons.api.http.HttpContentType; import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataRequest; @@ -43,7 +43,7 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce @Override public void readServiceDocument(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { + final ContentType requestedContentType) { ODataSerializer serializer; InputStream responseEntity; @@ -51,22 +51,22 @@ public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProce responseEntity = serializer.serviceDocument(edm, request.getRawBaseUri()); response.setStatusCode(200); - response.setHeader(HttpHeader.CONTENT_TYPE, HttpContentType.APPLICATION_JSON); response.setContent(responseEntity); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } @Override public void readMetadata(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { + final ContentType requestedContentType) { ODataSerializer serializer; InputStream responseEntity; serializer = odata.createSerializer(ODataFormat.XML); responseEntity = serializer.metadataDocument(edm); response.setStatusCode(200); - response.setHeader(HttpHeader.CONTENT_TYPE, HttpContentType.APPLICATION_XML); response.setContent(responseEntity); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } } diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/EntityProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/EntityProcessor.java index b0b9476e9..a6ec6c200 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/EntityProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/EntityProcessor.java @@ -18,11 +18,13 @@ */ package org.apache.olingo.server.api.processor; +import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.uri.UriInfo; public interface EntityProcessor extends Processor { - void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format); + void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType format); + } diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java index 89f8e5303..ed187ea1a 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java @@ -18,11 +18,12 @@ */ package org.apache.olingo.server.api.processor; +import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.uri.UriInfo; public interface MetadataProcessor extends Processor { - void readMetadata(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format); + void readMetadata(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType requestedContentType); } diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java index a1ef6685d..dd90b38de 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java @@ -18,12 +18,14 @@ */ package org.apache.olingo.server.api.processor; +import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; import org.apache.olingo.server.api.uri.UriInfo; public interface ServiceDocumentProcessor extends Processor { - void readServiceDocument(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format); + void readServiceDocument(ODataRequest request, ODataResponse response, UriInfo uriInfo, + ContentType requestedContentType); } 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 36a52a882..b8f7bbd52 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 @@ -84,7 +84,7 @@ public class ODataHandler { requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, mp, MetadataProcessor.class); - mp.readMetadata(request, response, uriInfo, requestedContentType.toContentTypeString()); + mp.readMetadata(request, response, uriInfo, requestedContentType); break; case service: if ("".equals(request.getRawODataPath())) { @@ -97,7 +97,7 @@ public class ODataHandler { ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, sdp, ServiceDocumentProcessor.class); - sdp.readServiceDocument(request, response, uriInfo, requestedContentType.toContentTypeString()); + sdp.readServiceDocument(request, response, uriInfo, requestedContentType); } break; case resource: @@ -129,7 +129,7 @@ public class ODataHandler { requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, cp, CollectionProcessor.class); - cp.readCollection(request, response, uriInfo, requestedContentType.toContentTypeString()); + cp.readCollection(request, response, uriInfo, requestedContentType); } else { throw new ODataRuntimeException("not implemented"); } @@ -140,7 +140,7 @@ public class ODataHandler { requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, ep, EntityProcessor.class); - ep.readEntity(request, response, uriInfo, requestedContentType.toContentTypeString()); + ep.readEntity(request, response, uriInfo, requestedContentType); } else { throw new ODataRuntimeException("not implemented"); } @@ -154,7 +154,7 @@ public class ODataHandler { requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, cp, CollectionProcessor.class); - cp.readCollection(request, response, uriInfo, requestedContentType.toContentTypeString()); + cp.readCollection(request, response, uriInfo, requestedContentType); } else { throw new ODataRuntimeException("not implemented"); } @@ -165,7 +165,7 @@ public class ODataHandler { requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), request, ep, EntityProcessor.class); - ep.readEntity(request, response, uriInfo, requestedContentType.toContentTypeString()); + ep.readEntity(request, response, uriInfo, requestedContentType); } else { throw new ODataRuntimeException("not implemented"); } diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java index 10df086d7..cc766f48a 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ContentNegotiatorTest.java @@ -214,20 +214,20 @@ public class ContentNegotiatorTest { @Override public void readServiceDocument(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { - response.setHeader(HttpHeader.CONTENT_TYPE, format); + final ContentType format) { + response.setHeader(HttpHeader.CONTENT_TYPE, format.toContentTypeString()); } @Override public void readCollection(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { - response.setHeader(HttpHeader.CONTENT_TYPE, format); + final ContentType requestedContentType) { + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } @Override public void readMetadata(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { - response.setHeader(HttpHeader.CONTENT_TYPE, format); + final ContentType requestedContentType) { + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } } } diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/SampleJsonProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/SampleJsonProcessor.java index e7d67173c..b3dc02de1 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/SampleJsonProcessor.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/processor/SampleJsonProcessor.java @@ -30,6 +30,7 @@ import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.format.ContentType; import org.apache.olingo.commons.api.format.ODataFormat; +import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.commons.api.http.HttpStatusCode; import org.apache.olingo.commons.core.data.EntityImpl; import org.apache.olingo.commons.core.data.EntitySetImpl; @@ -58,7 +59,7 @@ public class SampleJsonProcessor implements CollectionProcessor, EntityProcessor @Override public void readCollection(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { + final ContentType requestedContentType) { long time = System.nanoTime(); EntitySet entitySet = createEntitySet(); @@ -74,12 +75,12 @@ public class SampleJsonProcessor implements CollectionProcessor, EntityProcessor LOG.info("Finished in " + (System.nanoTime() - time) / 1000 + " microseconds"); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); - response.setHeader("Content-Type", ContentType.APPLICATION_JSON.toContentTypeString()); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } @Override public void readEntity(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { + final ContentType requestedContentType) { long time = System.nanoTime(); Entity entity = createEntity(); @@ -94,7 +95,7 @@ public class SampleJsonProcessor implements CollectionProcessor, EntityProcessor LOG.info("Finished in " + (System.nanoTime() - time) / 1000 + " microseconds"); response.setStatusCode(HttpStatusCode.OK.getStatusCode()); - response.setHeader("Content-Type", ContentType.APPLICATION_JSON.toContentTypeString()); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } protected Entity createEntity() { 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 b3c671c12..7c5a267c1 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 @@ -21,6 +21,8 @@ package org.apache.olingo.server.tecsvc.processor; import java.io.ByteArrayInputStream; import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.format.ContentType; +import org.apache.olingo.commons.api.http.HttpHeader; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataRequest; import org.apache.olingo.server.api.ODataResponse; @@ -38,15 +40,17 @@ public class TechnicalProcessor implements CollectionProcessor, EntityProcessor @Override public void readEntity(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { + final ContentType requestedContentType) { response.setContent(new ByteArrayInputStream("Entity".getBytes())); response.setStatusCode(200); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } @Override public void readCollection(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo, - final String format) { + final ContentType requestedContentType) { response.setContent(new ByteArrayInputStream("EntitySet".getBytes())); response.setStatusCode(200); + response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString()); } }