[OLINGO-337] refactor requestedContentType

This commit is contained in:
Stephan Klevenz 2014-07-03 09:09:22 +02:00
parent 098ed145db
commit d049864ea4
9 changed files with 38 additions and 27 deletions

View File

@ -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);
}

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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");
}

View File

@ -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());
}
}
}

View File

@ -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() {

View File

@ -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());
}
}