blob: 302959af486fc6bfd4a5d48d1b95a1cea6a72c34 [file] [log] [blame]
/*
* 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.api.processor;
import java.io.ByteArrayInputStream;
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.server.api.OData;
import org.apache.olingo.server.api.ODataApplicationException;
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.serializer.ODataSerializer;
import org.apache.olingo.server.api.serializer.SerializerException;
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 DefaultProcessor 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, SerializerException {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.serviceDocument(serviceMetadata.getEdm(), request.getRawBaseUri()));
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, SerializerException {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.metadataDocument(serviceMetadata));
response.setStatusCode(HttpStatusCode.OK.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, requestedContentType.toContentTypeString());
}
@Override
public void processError(ODataRequest request, ODataResponse response, ODataServerError serverError,
ContentType requestedContentType) {
try {
ODataSerializer serializer = odata.createSerializer(ODataFormat.fromContentType(requestedContentType));
response.setContent(serializer.error(serverError));
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 with message: " + e.getMessage() + "\"}}";
response.setContent(new ByteArrayInputStream(responseContent.getBytes()));
response.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
response.setHeader(HttpHeader.CONTENT_TYPE, ContentType.APPLICATION_JSON.toContentTypeString());
}
}
}