Skip to content

Commit

Permalink
use absulute url
Browse files Browse the repository at this point in the history
  • Loading branch information
lelmarir committed Nov 14, 2017
1 parent 03ab69c commit 8a947d3
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected void service(final HttpServletRequest req, final HttpServletResponse r
OData odata = OData.newInstance();
ServiceMetadata edm = odata.createServiceMetadata(new CarsEdmProvider(), new ArrayList<EdmxReference>());
ODataHttpHandler handler = odata.createHandler(edm);
handler.register(new CustomDefaultProcessor());
handler.register(new CarsProcessor(dataProvider));
handler.process(req, resp);
} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package org.apache.olingo.server.sample;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;

import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpMethod;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.ODataLibraryException;
import org.apache.olingo.server.api.ODataRequest;
import org.apache.olingo.server.api.ODataResponse;
import org.apache.olingo.server.api.ODataServerError;
import org.apache.olingo.server.api.ServiceMetadata;
import org.apache.olingo.server.api.etag.ETagHelper;
import org.apache.olingo.server.api.etag.ServiceMetadataETagSupport;
import org.apache.olingo.server.api.processor.ErrorProcessor;
import org.apache.olingo.server.api.processor.MetadataProcessor;
import org.apache.olingo.server.api.processor.ServiceDocumentProcessor;
import org.apache.olingo.server.api.serializer.ODataSerializer;
import org.apache.olingo.server.api.uri.UriInfo;

/**
* <p>Processor implementation for handling default cases:
* <ul><li>request for the metadata document</li>
* <li>request for the service document</li>
* <li>error handling</li></ul></p>
* <p>This implementation is registered in the ODataHandler by default.
* The default can be replaced by re-registering a custom implementation.</p>
*/
public class CustomDefaultProcessor implements MetadataProcessor, ServiceDocumentProcessor, ErrorProcessor {
private OData odata;
private ServiceMetadata serviceMetadata;

@Override
public void init(final OData odata, final ServiceMetadata serviceMetadata) {
this.odata = odata;
this.serviceMetadata = serviceMetadata;
}

@Override
public void readServiceDocument(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo,
final ContentType requestedContentType) throws ODataApplicationException, ODataLibraryException {
boolean isNotModified = false;
ServiceMetadataETagSupport eTagSupport = serviceMetadata.getServiceMetadataETagSupport();
if (eTagSupport != null && eTagSupport.getServiceDocumentETag() != null) {
// Set application etag at response
response.setHeader(HttpHeader.ETAG, eTagSupport.getServiceDocumentETag());
// Check if service document has been modified
ETagHelper eTagHelper = odata.createETagHelper();
isNotModified = eTagHelper.checkReadPreconditions(eTagSupport.getServiceDocumentETag(), request
.getHeaders(HttpHeader.IF_MATCH), request.getHeaders(HttpHeader.IF_NONE_MATCH));
}

// Send the correct response
if (isNotModified) {
response.setStatusCode(HttpStatusCode.NOT_MODIFIED.getStatusCode());
} else {
// HTTP HEAD requires no payload but a 200 OK response
if (HttpMethod.HEAD == request.getMethod()) {
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
} else {
ODataSerializer serializer = odata.createSerializer(requestedContentType);
//response.setContent(serializer.serviceDocument(serviceMetadata, null);
response.setContent(serializer.serviceDocument(serviceMetadata, request.getRawBaseUri()).getContent());
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
}
}
}

@Override
public void readMetadata(final ODataRequest request, final ODataResponse response, final UriInfo uriInfo,
final ContentType requestedContentType) throws ODataApplicationException, ODataLibraryException {
boolean isNotModified = false;
ServiceMetadataETagSupport eTagSupport = serviceMetadata.getServiceMetadataETagSupport();
if (eTagSupport != null && eTagSupport.getMetadataETag() != null) {
// Set application etag at response
response.setHeader(HttpHeader.ETAG, eTagSupport.getMetadataETag());
// Check if metadata document has been modified
ETagHelper eTagHelper = odata.createETagHelper();
isNotModified = eTagHelper.checkReadPreconditions(eTagSupport.getMetadataETag(), request
.getHeaders(HttpHeader.IF_MATCH), request.getHeaders(HttpHeader.IF_NONE_MATCH));
}

// Send the correct response
if (isNotModified) {
response.setStatusCode(HttpStatusCode.NOT_MODIFIED.getStatusCode());
} else {
// HTTP HEAD requires no payload but a 200 OK response
if (HttpMethod.HEAD == request.getMethod()) {
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
} else {
ODataSerializer serializer = odata.createSerializer(requestedContentType);
response.setContent(serializer.metadataDocument(serviceMetadata).getContent());
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
}
}
}

@Override
public void processError(final ODataRequest request, final ODataResponse response,
final ODataServerError serverError,
final ContentType requestedContentType) {
try {
ODataSerializer serializer = odata.createSerializer(requestedContentType);
response.setContent(serializer.error(serverError).getContent());
response.setStatusCode(serverError.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
} catch (Exception e) {
// This should never happen but to be sure we have this catch here to prevent sending a stacktrace to a client.
String responseContent =
"{\"error\":{\"code\":null,\"message\":\"An unexpected exception occurred during error processing\"}}";
response.setContent(new ByteArrayInputStream(responseContent.getBytes(Charset.forName("utf-8"))));
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* 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
Expand All @@ -20,6 +20,8 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -110,7 +112,7 @@ public void readEntityCollection(final ODataRequest request, ODataResponse respo
EntityCollectionSerializerOptions.with()
.id(id)
.contextURL(isODataMetadataNone(requestedContentType) ? null :
getContextUrl(edmEntitySet, false, expand, select, null))
getContextUrl(edmEntitySet, false, expand, select, null, request.getRawBaseUri()))
.count(uriInfo.getCountOption())
.expand(expand).select(select)
.build()).getContent();
Expand Down Expand Up @@ -147,7 +149,7 @@ public void readEntity(final ODataRequest request, ODataResponse response, final
InputStream serializedContent = serializer.entity(edm, edmEntitySet.getEntityType(), entity,
EntitySerializerOptions.with()
.contextURL(isODataMetadataNone(requestedContentType) ? null :
getContextUrl(edmEntitySet, true, expand, select, null))
getContextUrl(edmEntitySet, true, expand, select, null, request.getRawBaseUri()))
.expand(expand).select(select)
.build()).getContent();
response.setContent(serializedContent);
Expand All @@ -174,13 +176,13 @@ public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo u
@Override
public void readPrimitive(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType format)
throws ODataApplicationException, SerializerException {
readProperty(response, uriInfo, format, false);
readProperty(request, response, uriInfo, format, false);
}

@Override
public void readComplex(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType format)
throws ODataApplicationException, SerializerException {
readProperty(response, uriInfo, format, true);
readProperty(request, response, uriInfo, format, true);
}

@Override
Expand Down Expand Up @@ -223,7 +225,7 @@ public void readPrimitiveValue(ODataRequest request, ODataResponse response, Uri
}
}

private void readProperty(ODataResponse response, UriInfo uriInfo, ContentType contentType,
private void readProperty(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType contentType,
boolean complex) throws ODataApplicationException, SerializerException {
// To read a property we have to first get the entity out of the entity set
final EdmEntitySet edmEntitySet = getEdmEntitySet(uriInfo.asUriInfoResource());
Expand Down Expand Up @@ -254,7 +256,7 @@ private void readProperty(ODataResponse response, UriInfo uriInfo, ContentType c
} else {
ODataSerializer serializer = odata.createSerializer(contentType);
final ContextURL contextURL = isODataMetadataNone(contentType) ? null :
getContextUrl(edmEntitySet, true, null, null, edmProperty.getName());
getContextUrl(edmEntitySet, true, null, null, edmProperty.getName(), request.getRawBaseUri());
InputStream serializerContent = complex ?
serializer.complex(edm, (EdmComplexType) edmProperty.getType(), property,
ComplexSerializerOptions.with().contextURL(contextURL).build()).getContent() :
Expand Down Expand Up @@ -300,14 +302,19 @@ private EdmEntitySet getEdmEntitySet(final UriInfoResource uriInfo) throws OData
}

private ContextURL getContextUrl(final EdmEntitySet entitySet, final boolean isSingleEntity,
final ExpandOption expand, final SelectOption select, final String navOrPropertyPath)
final ExpandOption expand, final SelectOption select, final String navOrPropertyPath, String serviceRoot)
throws SerializerException {

return ContextURL.with().entitySet(entitySet)
try {
return ContextURL.with().entitySet(entitySet)
.selectList(odata.createUriHelper().buildContextURLSelectList(entitySet.getEntityType(), expand, select))
.suffix(isSingleEntity ? Suffix.ENTITY : null)
.navOrPropertyPath(navOrPropertyPath)
.serviceRoot(new URI(serviceRoot))
.build();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down

0 comments on commit 8a947d3

Please sign in to comment.