http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiatorException.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiatorException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiatorException.java deleted file mode 100644 index e2ee866..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiatorException.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.core; - -import org.apache.olingo.server.api.ODataTranslatedException; - -public class ContentNegotiatorException extends ODataTranslatedException { - private static final long serialVersionUID = -8112658467394158700L; - - public static enum MessageKeys implements MessageKey { - /** parameters: HTTP header name, HTTP header value */ - WRONG_CHARSET_IN_HEADER, - /** parameter: list of content-type ranges */ - UNSUPPORTED_CONTENT_TYPES, - /** parameter: content type */ - UNSUPPORTED_CONTENT_TYPE, - /** no parameter */ - NO_CONTENT_TYPE_SUPPORTED, - /** parameter: format string */ - UNSUPPORTED_FORMAT_OPTION; - - @Override - public String getKey() { - return name(); - } - } - - public ContentNegotiatorException(String developmentMessage, MessageKey messageKey, String... parameters) { - super(developmentMessage, messageKey, parameters); - } - - public ContentNegotiatorException(String developmentMessage, Throwable cause, MessageKey messageKey, - String... parameters) { - super(developmentMessage, cause, messageKey, parameters); - } - - @Override - protected String getBundleName() { - return DEFAULT_SERVER_BUNDLE_NAME; - } -}
http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultRedirectProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultRedirectProcessor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultRedirectProcessor.java deleted file mode 100644 index 23e539b..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultRedirectProcessor.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.core; - -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.ODataRequest; -import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.ServiceMetadata; - -public class DefaultRedirectProcessor implements RedirectProcessor { - - @Override - public void init(final OData odata, final ServiceMetadata serviceMetadata) {} - - @Override - public void redirect(final ODataRequest request, final ODataResponse response) { - response.setStatusCode(HttpStatusCode.TEMPORARY_REDIRECT.getStatusCode()); - - String location; - - String rawUri = request.getRawRequestUri(); - String rawQueryPath = request.getRawQueryPath(); - if (rawQueryPath == null) { - location = request.getRawRequestUri() + "/"; - } else { - location = rawUri.substring(0, rawUri.indexOf(rawQueryPath) - 1) + "/?" + rawQueryPath; - } - - response.setHeader(HttpHeader.LOCATION, location); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java deleted file mode 100644 index be3e7ec..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataExceptionHelper.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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.core; - -import java.util.Locale; - -import org.apache.olingo.commons.api.http.HttpStatusCode; -import org.apache.olingo.server.api.ODataApplicationException; -import org.apache.olingo.server.api.ODataServerError; -import org.apache.olingo.server.api.ODataTranslatedException; -import org.apache.olingo.server.api.ODataTranslatedException.ODataErrorMessage; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.core.uri.parser.UriParserException; -import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; -import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException; -import org.apache.olingo.server.core.uri.validator.UriValidationException; - -public class ODataExceptionHelper { - - public static ODataServerError createServerErrorObject(UriValidationException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(UriParserSemanticException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - if (UriParserSemanticException.MessageKeys.RESOURCE_NOT_FOUND.equals(e.getMessageKey()) - || UriParserSemanticException.MessageKeys.FUNCTION_NOT_FOUND.equals(e.getMessageKey()) - || UriParserSemanticException.MessageKeys.PROPERTY_NOT_IN_TYPE.equals(e.getMessageKey())) { - serverError.setStatusCode(HttpStatusCode.NOT_FOUND.getStatusCode()); - } else { - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - } - return serverError; - } - - public static ODataServerError createServerErrorObject(UriParserSyntaxException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - serverError.setStatusCode( - UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT.equals(e.getMessageKey()) ? - HttpStatusCode.NOT_ACCEPTABLE.getStatusCode() : - HttpStatusCode.BAD_REQUEST.getStatusCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(UriParserException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(ContentNegotiatorException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - serverError.setStatusCode(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(ODataHandlerException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - if (ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED.equals(e.getMessageKey()) - || ODataHandlerException.MessageKeys.PROCESSOR_NOT_IMPLEMENTED.equals(e.getMessageKey())) { - serverError.setStatusCode(HttpStatusCode.NOT_IMPLEMENTED.getStatusCode()); - } else if (ODataHandlerException.MessageKeys.ODATA_VERSION_NOT_SUPPORTED.equals(e.getMessageKey()) - || ODataHandlerException.MessageKeys.INVALID_HTTP_METHOD.equals(e.getMessageKey()) - || ODataHandlerException.MessageKeys.AMBIGUOUS_XHTTP_METHOD.equals(e.getMessageKey())) { - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - } else if (ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED.equals(e.getMessageKey())) { - serverError.setStatusCode(HttpStatusCode.METHOD_NOT_ALLOWED.getStatusCode()); - } - - return serverError; - } - - public static ODataServerError createServerErrorObject(SerializerException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(BatchDeserializerException e, Locale requestedLocale) { - ODataServerError serverError = basicTranslatedError(e, requestedLocale); - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(ODataTranslatedException e, Locale requestedLocale) { - return basicTranslatedError(e, requestedLocale); - } - - public static ODataServerError createServerErrorObject(ODataApplicationException e) { - ODataServerError serverError = basicServerError(e); - serverError.setStatusCode(e.getStatusCode()); - serverError.setLocale(e.getLocale()); - serverError.setCode(e.getODataErrorCode()); - return serverError; - } - - public static ODataServerError createServerErrorObject(Exception e) { - ODataServerError serverError = basicServerError(e); - serverError.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); - serverError.setLocale(Locale.ENGLISH); - return serverError; - } - - private static ODataServerError basicServerError(Exception e) { - ODataServerError serverError = new ODataServerError().setException(e).setMessage(e.getMessage()); - return serverError; - } - - private static ODataServerError basicTranslatedError(ODataTranslatedException e, Locale requestedLocale) { - ODataServerError serverError = basicServerError(e); - ODataErrorMessage translatedMessage = e.getTranslatedMessage(requestedLocale); - serverError.setMessage(translatedMessage.getMessage()); - serverError.setLocale(translatedMessage.getLocale()); - serverError.setStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); - return serverError; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java ---------------------------------------------------------------------- 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 deleted file mode 100644 index 4528d3a..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java +++ /dev/null @@ -1,699 +0,0 @@ -/* - * 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.core; - -import java.util.LinkedList; -import java.util.List; - -import org.apache.olingo.commons.api.edm.EdmAction; -import org.apache.olingo.commons.api.edm.EdmActionImport; -import org.apache.olingo.commons.api.edm.EdmEntityType; -import org.apache.olingo.commons.api.edm.EdmFunction; -import org.apache.olingo.commons.api.edm.EdmFunctionImport; -import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; -import org.apache.olingo.commons.api.edm.EdmReturnType; -import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; -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.HttpMethod; -import org.apache.olingo.commons.core.edm.primitivetype.EdmPrimitiveTypeFactory; -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.batch.exception.BatchDeserializerException; -import org.apache.olingo.server.api.deserializer.DeserializerException; -import org.apache.olingo.server.api.processor.ActionComplexCollectionProcessor; -import org.apache.olingo.server.api.processor.ActionComplexProcessor; -import org.apache.olingo.server.api.processor.ActionEntityCollectionProcessor; -import org.apache.olingo.server.api.processor.ActionEntityProcessor; -import org.apache.olingo.server.api.processor.ActionPrimitiveCollectionProcessor; -import org.apache.olingo.server.api.processor.ActionPrimitiveProcessor; -import org.apache.olingo.server.api.processor.BatchProcessor; -import org.apache.olingo.server.api.processor.ComplexCollectionProcessor; -import org.apache.olingo.server.api.processor.ComplexProcessor; -import org.apache.olingo.server.api.processor.CountComplexCollectionProcessor; -import org.apache.olingo.server.api.processor.CountEntityCollectionProcessor; -import org.apache.olingo.server.api.processor.CountPrimitiveCollectionProcessor; -import org.apache.olingo.server.api.processor.DefaultProcessor; -import org.apache.olingo.server.api.processor.EntityCollectionProcessor; -import org.apache.olingo.server.api.processor.EntityProcessor; -import org.apache.olingo.server.api.processor.ErrorProcessor; -import org.apache.olingo.server.api.processor.MediaEntityProcessor; -import org.apache.olingo.server.api.processor.MetadataProcessor; -import org.apache.olingo.server.api.processor.PrimitiveCollectionProcessor; -import org.apache.olingo.server.api.processor.PrimitiveProcessor; -import org.apache.olingo.server.api.processor.PrimitiveValueProcessor; -import org.apache.olingo.server.api.processor.Processor; -import org.apache.olingo.server.api.processor.ReferenceCollectionProcessor; -import org.apache.olingo.server.api.processor.ReferenceProcessor; -import org.apache.olingo.server.api.processor.ServiceDocumentProcessor; -import org.apache.olingo.server.api.serializer.CustomContentTypeSupport; -import org.apache.olingo.server.api.serializer.RepresentationType; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.api.uri.UriInfo; -import org.apache.olingo.server.api.uri.UriResource; -import org.apache.olingo.server.api.uri.UriResourceAction; -import org.apache.olingo.server.api.uri.UriResourceEntitySet; -import org.apache.olingo.server.api.uri.UriResourceFunction; -import org.apache.olingo.server.api.uri.UriResourceNavigation; -import org.apache.olingo.server.api.uri.UriResourcePartTyped; -import org.apache.olingo.server.api.uri.UriResourcePrimitiveProperty; -import org.apache.olingo.server.api.uri.UriResourceProperty; -import org.apache.olingo.server.core.batchhandler.BatchHandler; -import org.apache.olingo.server.core.uri.parser.Parser; -import org.apache.olingo.server.core.uri.parser.UriParserException; -import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; -import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException; -import org.apache.olingo.server.core.uri.validator.UriValidationException; -import org.apache.olingo.server.core.uri.validator.UriValidator; - -public class ODataHandler { - - private final OData odata; - private final ServiceMetadata serviceMetadata; - private List<Processor> processors = new LinkedList<Processor>(); - private CustomContentTypeSupport customContentTypeSupport = null; - - private UriInfo uriInfo; - - public ODataHandler(final OData server, final ServiceMetadata serviceMetadata) { - odata = server; - this.serviceMetadata = serviceMetadata; - - register(new DefaultProcessor()); - register(new DefaultRedirectProcessor()); - } - - public ODataResponse process(final ODataRequest request) { - ODataResponse response = new ODataResponse(); - try { - - processInternal(request, response); - - } catch (final UriValidationException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (final UriParserSemanticException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (final UriParserSyntaxException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (final UriParserException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (ContentNegotiatorException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (SerializerException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (BatchDeserializerException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (DeserializerException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (ODataHandlerException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); - handleException(request, response, serverError); - } catch (ODataApplicationException e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e); - handleException(request, response, serverError); - } catch (Exception e) { - ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e); - handleException(request, response, serverError); - } - return response; - } - - private void processInternal(final ODataRequest request, final ODataResponse response) - throws ODataHandlerException, UriParserException, UriValidationException, ContentNegotiatorException, - ODataApplicationException, SerializerException, DeserializerException { - validateODataVersion(request, response); - - uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null, - serviceMetadata.getEdm()); - - final HttpMethod method = request.getMethod(); - new UriValidator().validate(uriInfo, method); - - switch (uriInfo.getKind()) { - case metadata: - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.METADATA); - selectProcessor(MetadataProcessor.class) - .readMetadata(request, response, uriInfo, requestedContentType); - } else { - throw new ODataHandlerException("HttpMethod " + method + " not allowed for metadata document", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - break; - - case service: - if (method == HttpMethod.GET) { - if ("".equals(request.getRawODataPath())) { - selectProcessor(RedirectProcessor.class).redirect(request, response); - } else { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.SERVICE); - - selectProcessor(ServiceDocumentProcessor.class) - .readServiceDocument(request, response, uriInfo, requestedContentType); - } - } else { - throw new ODataHandlerException("HttpMethod " + method + " not allowed for service document", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - break; - - case resource: - handleResourceDispatching(request, response); - break; - - case batch: - if (method == HttpMethod.POST) { - final BatchProcessor bp = selectProcessor(BatchProcessor.class); - final BatchHandler handler = new BatchHandler(this, bp); - handler.process(request, response, true); - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - break; - - default: - throw new ODataHandlerException("not implemented", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - } - - public void handleException(final ODataRequest request, final ODataResponse response, - final ODataServerError serverError) { - - ErrorProcessor exceptionProcessor; - try { - exceptionProcessor = selectProcessor(ErrorProcessor.class); - } catch (ODataHandlerException e) { - // This cannot happen since there is always an ExceptionProcessor registered. - exceptionProcessor = new DefaultProcessor(); - } - ContentType requestedContentType; - try { - requestedContentType = ContentNegotiator.doContentNegotiation( - uriInfo == null ? null : uriInfo.getFormatOption(), request, customContentTypeSupport, - RepresentationType.ERROR); - } catch (final ContentNegotiatorException e) { - requestedContentType = ODataFormat.JSON.getContentType(ODataServiceVersion.V40); - } - exceptionProcessor.processError(request, response, serverError, requestedContentType); - } - - private void handleResourceDispatching(final ODataRequest request, final ODataResponse response) - throws ODataHandlerException, ContentNegotiatorException, ODataApplicationException, - SerializerException, DeserializerException { - - final int lastPathSegmentIndex = uriInfo.getUriResourceParts().size() - 1; - final UriResource lastPathSegment = uriInfo.getUriResourceParts().get(lastPathSegmentIndex); - - switch (lastPathSegment.getKind()) { - case action: - handleActionDispatching(request, response, (UriResourceAction) lastPathSegment); - break; - - case function: - handleFunctionDispatching(request, response, (UriResourceFunction) lastPathSegment); - break; - - case entitySet: - case navigationProperty: - handleEntityDispatching(request, response, (UriResourcePartTyped) lastPathSegment); - break; - - case count: - handleCountDispatching(request, response, lastPathSegmentIndex); - break; - - case primitiveProperty: - handlePrimitivePropertyDispatching(request, response, false, - ((UriResourceProperty) lastPathSegment).isCollection()); - break; - - case complexProperty: - handleComplexPropertyDispatching(request, response, false, - ((UriResourceProperty) lastPathSegment).isCollection()); - break; - - case value: - handleValueDispatching(request, response, lastPathSegmentIndex); - break; - - case ref: - handleReferenceDispatching(request, response, lastPathSegmentIndex); - break; - - default: - throw new ODataHandlerException("not implemented", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - } - - private void handleFunctionDispatching(final ODataRequest request, final ODataResponse response, - final UriResourceFunction uriResourceFunction) - throws ODataHandlerException, SerializerException, ContentNegotiatorException, - ODataApplicationException, DeserializerException { - final HttpMethod method = request.getMethod(); - if(method != HttpMethod.GET) { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - - EdmFunctionImport functionImport = uriResourceFunction.getFunctionImport(); - // could be null for bound functions - if(functionImport == null) { - throw new ODataHandlerException("Bound functions are not implemented yet", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - - List<EdmFunction> unboundFunctions = functionImport.getUnboundFunctions(); - if(unboundFunctions == null || unboundFunctions.isEmpty()) { - throw new ODataHandlerException("No unbound function defined for function import", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - EdmReturnType returnType = unboundFunctions.get(0).getReturnType(); - handleOperationDispatching(request, response, false, returnType); - } - - private void handleActionDispatching(final ODataRequest request, final ODataResponse response, - final UriResourceAction uriResourceAction) - throws ODataHandlerException, SerializerException, ContentNegotiatorException, - ODataApplicationException, DeserializerException { - - final HttpMethod method = request.getMethod(); - if(request.getMethod() != HttpMethod.POST) { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - - EdmActionImport actionImport = uriResourceAction.getActionImport(); - // could be null for bound actions - if(actionImport == null) { - throw new ODataHandlerException("Bound actions are not implemented yet", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - - EdmAction unboundActions = actionImport.getUnboundAction(); - if(unboundActions == null) { - throw new ODataHandlerException("No unbound function defined for function import", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - EdmReturnType returnType = unboundActions.getReturnType(); - handleOperationDispatching(request, response, true, returnType); - } - - - private void handleOperationDispatching(final ODataRequest request, final ODataResponse response, - final boolean isAction, final EdmReturnType edmReturnTypeKind) - throws ODataHandlerException, SerializerException, ContentNegotiatorException, - ODataApplicationException, DeserializerException { - - switch (edmReturnTypeKind.getType().getKind()) { - case ENTITY: - handleEntityDispatching(request, response, edmReturnTypeKind.isCollection(), false, isAction); - break; - case PRIMITIVE: - handlePrimitivePropertyDispatching(request, response, isAction, edmReturnTypeKind.isCollection()); - break; - case COMPLEX: - handleComplexPropertyDispatching(request, response, isAction, edmReturnTypeKind.isCollection()); - break; - default: - throw new ODataHandlerException("not implemented", - ODataHandlerException.MessageKeys.FUNCTIONALITY_NOT_IMPLEMENTED); - } - } - - - private void handleReferenceDispatching(final ODataRequest request, final ODataResponse response, - final int lastPathSegmentIndex) - throws ContentNegotiatorException, ODataApplicationException, SerializerException, ODataHandlerException, - DeserializerException { - final HttpMethod method = request.getMethod(); - if (((UriResourcePartTyped) uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1)).isCollection()) { - if (method == HttpMethod.GET) { - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.COLLECTION_REFERENCE); - selectProcessor(ReferenceCollectionProcessor.class) - .readReferenceCollection(request, response, uriInfo, responseFormat); - } else if (method == HttpMethod.POST) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, RepresentationType.REFERENCE); - selectProcessor(ReferenceProcessor.class) - .createReference(request, response, uriInfo, requestFormat); - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } else { - if (method == HttpMethod.GET) { - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.REFERENCE); - selectProcessor(ReferenceProcessor.class).readReference(request, response, uriInfo, responseFormat); - } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, RepresentationType.REFERENCE); - selectProcessor(ReferenceProcessor.class) - .updateReference(request, response, uriInfo, requestFormat); - } else if (method == HttpMethod.DELETE) { - selectProcessor(ReferenceProcessor.class) - .deleteReference(request, response, uriInfo); - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } - } - - private void handleValueDispatching(final ODataRequest request, final ODataResponse response, - final int lastPathSegmentIndex) - throws ContentNegotiatorException, ODataApplicationException, SerializerException, ODataHandlerException, - DeserializerException { - final HttpMethod method = request.getMethod(); - final UriResource resource = uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1); - if (resource instanceof UriResourceProperty) { - final RepresentationType valueRepresentationType = - ((UriResourceProperty) resource).getType() == - EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.Binary) ? - RepresentationType.BINARY : RepresentationType.VALUE; - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, valueRepresentationType); - - selectProcessor(PrimitiveValueProcessor.class) - .readPrimitiveValue(request, response, uriInfo, requestedContentType); - } else if (method == HttpMethod.PUT) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, valueRepresentationType); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, valueRepresentationType); - selectProcessor(PrimitiveValueProcessor.class) - .updatePrimitive(request, response, uriInfo, requestFormat, responseFormat); - } else if (method == HttpMethod.DELETE) { - selectProcessor(PrimitiveValueProcessor.class).deletePrimitive(request, response, uriInfo); - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } else { - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.MEDIA); - selectProcessor(MediaEntityProcessor.class) - .readMediaEntity(request, response, uriInfo, requestedContentType); - } else if (method == HttpMethod.PUT) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - selectProcessor(MediaEntityProcessor.class) - .updateMediaEntity(request, response, uriInfo, requestFormat, responseFormat); - } else if (method == HttpMethod.DELETE) { - selectProcessor(MediaEntityProcessor.class).deleteEntity(request, response, uriInfo); - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } - } - - private void handleComplexPropertyDispatching(final ODataRequest request, final ODataResponse response, - final boolean isAction, final boolean isCollection) - throws ContentNegotiatorException, ODataApplicationException, SerializerException, ODataHandlerException, - DeserializerException { - - final HttpMethod method = request.getMethod(); - final RepresentationType complexRepresentationType = isCollection ? - RepresentationType.COLLECTION_COMPLEX : RepresentationType.COMPLEX; - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, complexRepresentationType); - if (complexRepresentationType == RepresentationType.COMPLEX) { - selectProcessor(ComplexProcessor.class) - .readComplex(request, response, uriInfo, requestedContentType); - } else { - selectProcessor(ComplexCollectionProcessor.class) - .readComplexCollection(request, response, uriInfo, requestedContentType); - } - } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, complexRepresentationType); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, complexRepresentationType); - if (complexRepresentationType == RepresentationType.COMPLEX) { - selectProcessor(ComplexProcessor.class) - .updateComplex(request, response, uriInfo, requestFormat, responseFormat); - } else { - selectProcessor(ComplexCollectionProcessor.class) - .updateComplexCollection(request, response, uriInfo, requestFormat, responseFormat); - } - } else if (method == HttpMethod.POST && isAction) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, complexRepresentationType); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, complexRepresentationType); - if (complexRepresentationType == RepresentationType.COMPLEX) { - selectProcessor(ActionComplexProcessor.class) - .processActionComplex(request, response, uriInfo, requestFormat, responseFormat); - } else { - selectProcessor(ActionComplexCollectionProcessor.class) - .processActionComplexCollection(request, response, uriInfo, requestFormat, responseFormat); - } - } else if (method == HttpMethod.DELETE) { - if (complexRepresentationType == RepresentationType.COMPLEX) { - selectProcessor(ComplexProcessor.class).deleteComplex(request, response, uriInfo); - } else { - selectProcessor(ComplexCollectionProcessor.class).deleteComplexCollection(request, response, uriInfo); - } - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } - - private void handlePrimitivePropertyDispatching(final ODataRequest request, final ODataResponse response, - boolean isAction, final boolean isCollection) - throws ContentNegotiatorException, ODataApplicationException, SerializerException, ODataHandlerException, - DeserializerException { - - final HttpMethod method = request.getMethod(); - final RepresentationType representationType = isCollection ? - RepresentationType.COLLECTION_PRIMITIVE : RepresentationType.PRIMITIVE; - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, representationType); - if (representationType == RepresentationType.PRIMITIVE) { - selectProcessor(PrimitiveProcessor.class).readPrimitive(request, response, uriInfo, requestedContentType); - } else { - selectProcessor(PrimitiveCollectionProcessor.class) - .readPrimitiveCollection(request, response, uriInfo, requestedContentType); - } - } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, representationType); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, representationType); - if (representationType == RepresentationType.PRIMITIVE) { - selectProcessor(PrimitiveProcessor.class) - .updatePrimitive(request, response, uriInfo, requestFormat, responseFormat); - } else { - selectProcessor(PrimitiveCollectionProcessor.class) - .updatePrimitiveCollection(request, response, uriInfo, requestFormat, responseFormat); - } - } else if (method == HttpMethod.DELETE) { - if (representationType == RepresentationType.PRIMITIVE) { - selectProcessor(PrimitiveProcessor.class).deletePrimitive(request, response, uriInfo); - } else { - selectProcessor(PrimitiveCollectionProcessor.class).deletePrimitiveCollection(request, response, uriInfo); - } - } else if (method == HttpMethod.POST && isAction) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, representationType); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, representationType); - if (representationType == RepresentationType.PRIMITIVE) { - selectProcessor(ActionPrimitiveProcessor.class) - .processActionPrimitive(request, response, uriInfo, requestFormat, responseFormat); - } else { - selectProcessor(ActionPrimitiveCollectionProcessor.class) - .processActionPrimitiveCollection(request, response, uriInfo, requestFormat, responseFormat); - } - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } - - private void handleCountDispatching(final ODataRequest request, final ODataResponse response, - final int lastPathSegmentIndex) - throws ODataApplicationException, SerializerException, ODataHandlerException { - - final HttpMethod method = request.getMethod(); - if (method == HttpMethod.GET) { - final UriResource resource = uriInfo.getUriResourceParts().get(lastPathSegmentIndex - 1); - if (resource instanceof UriResourceEntitySet || resource instanceof UriResourceNavigation) { - selectProcessor(CountEntityCollectionProcessor.class) - .countEntityCollection(request, response, uriInfo); - } else if (resource instanceof UriResourcePrimitiveProperty) { - selectProcessor(CountPrimitiveCollectionProcessor.class) - .countPrimitiveCollection(request, response, uriInfo); - } else { - selectProcessor(CountComplexCollectionProcessor.class) - .countComplexCollection(request, response, uriInfo); - } - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed for count.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } - - private void handleEntityDispatching(final ODataRequest request, final ODataResponse response, - final UriResourcePartTyped uriResourcePart) - throws ContentNegotiatorException, ODataApplicationException, SerializerException, ODataHandlerException, - DeserializerException { - handleEntityDispatching(request, response, uriResourcePart.isCollection(), isMedia(uriResourcePart), false); - } - - private void handleEntityDispatching(final ODataRequest request, final ODataResponse response, - final boolean isCollection, final boolean isMedia, boolean isAction) - throws ContentNegotiatorException, ODataApplicationException, SerializerException, ODataHandlerException, - DeserializerException { - - final HttpMethod method = request.getMethod(); - if (isCollection) { - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.COLLECTION_ENTITY); - - selectProcessor(EntityCollectionProcessor.class) - .readEntityCollection(request, response, uriInfo, requestedContentType); - } else if (method == HttpMethod.POST) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - if (isMedia) { - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - selectProcessor(MediaEntityProcessor.class) - .createMediaEntity(request, response, uriInfo, requestFormat, responseFormat); - } else if(isAction) { - checkContentTypeSupport(requestFormat, RepresentationType.ENTITY); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - selectProcessor(ActionEntityCollectionProcessor.class) - .processActionEntityCollection(request, response, uriInfo, requestFormat, responseFormat); - } else { - checkContentTypeSupport(requestFormat, RepresentationType.ENTITY); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - selectProcessor(EntityProcessor.class) - .createEntity(request, response, uriInfo, requestFormat, responseFormat); - } - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } else { - if (method == HttpMethod.GET) { - final ContentType requestedContentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - - selectProcessor(EntityProcessor.class).readEntity(request, response, uriInfo, requestedContentType); - } else if (method == HttpMethod.PUT || method == HttpMethod.PATCH) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, RepresentationType.ENTITY); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - selectProcessor(EntityProcessor.class).updateEntity(request, response, uriInfo, requestFormat, responseFormat); - } else if (method == HttpMethod.POST && isAction) { - final ContentType requestFormat = ContentType.parse(request.getHeader(HttpHeader.CONTENT_TYPE)); - checkContentTypeSupport(requestFormat, RepresentationType.ENTITY); - final ContentType responseFormat = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(), - request, customContentTypeSupport, RepresentationType.ENTITY); - selectProcessor(ActionEntityProcessor.class).processActionEntity( - request, response, uriInfo, requestFormat, responseFormat); - } else if (method == HttpMethod.DELETE) { - selectProcessor(isMedia ? MediaEntityProcessor.class : EntityProcessor.class) - .deleteEntity(request, response, uriInfo); - } else { - throw new ODataHandlerException("HTTP method " + method + " is not allowed.", - ODataHandlerException.MessageKeys.HTTP_METHOD_NOT_ALLOWED, method.toString()); - } - } - } - - private void checkContentTypeSupport(ContentType requestFormat, RepresentationType representationType) - throws ODataHandlerException, ContentNegotiatorException { - if (!ContentNegotiator.isSupported(requestFormat, customContentTypeSupport, representationType)) { - final String contentTypeString = requestFormat.toContentTypeString(); - throw new ODataHandlerException("ContentType " + contentTypeString + " is not supported.", - ODataHandlerException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, contentTypeString); - } - } - - private void validateODataVersion(final ODataRequest request, final ODataResponse response) - throws ODataHandlerException { - final String maxVersion = request.getHeader(HttpHeader.ODATA_MAX_VERSION); - response.setHeader(HttpHeader.ODATA_VERSION, ODataServiceVersion.V40.toString()); - - if (maxVersion != null) { - if (ODataServiceVersion.isBiggerThan(ODataServiceVersion.V40.toString(), maxVersion)) { - throw new ODataHandlerException("ODataVersion not supported: " + maxVersion, - ODataHandlerException.MessageKeys.ODATA_VERSION_NOT_SUPPORTED, maxVersion); - } - } - } - - private boolean isMedia(final UriResource pathSegment) { - return pathSegment instanceof UriResourceEntitySet - && ((UriResourceEntitySet) pathSegment).getEntityType().hasStream() - || pathSegment instanceof UriResourceNavigation - && ((EdmEntityType) ((UriResourceNavigation) pathSegment).getType()).hasStream(); - } - - private <T extends Processor> T selectProcessor(final Class<T> cls) throws ODataHandlerException { - for (final Processor processor : processors) { - if (cls.isAssignableFrom(processor.getClass())) { - processor.init(odata, serviceMetadata); - return cls.cast(processor); - } - } - throw new ODataHandlerException("Processor: " + cls.getSimpleName() + " not registered.", - ODataHandlerException.MessageKeys.PROCESSOR_NOT_IMPLEMENTED, cls.getSimpleName()); - } - - public void register(final Processor processor) { - processors.add(0, processor); - } - - public void register(final CustomContentTypeSupport customContentTypeSupport) { - this.customContentTypeSupport = customContentTypeSupport; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java deleted file mode 100644 index 8ebbc26..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerException.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.core; - -import org.apache.olingo.server.api.ODataTranslatedException; - -/** Exception thrown during basic request handling. */ -public class ODataHandlerException extends ODataTranslatedException { - private static final long serialVersionUID = -907752788975531134L; - - public static enum MessageKeys implements MessageKey { - /** parameters: HTTP method, HTTP method */ AMBIGUOUS_XHTTP_METHOD, - /** parameter: HTTP method */ INVALID_HTTP_METHOD, - /** parameter: HTTP method */ HTTP_METHOD_NOT_ALLOWED, - /** parameter: processor interface */ PROCESSOR_NOT_IMPLEMENTED, - /** no parameter */ FUNCTIONALITY_NOT_IMPLEMENTED, - /** parameter: content type */ UNSUPPORTED_CONTENT_TYPE, - /** parameter: version */ ODATA_VERSION_NOT_SUPPORTED; - - @Override - public String getKey() { - return name(); - } - } - - public ODataHandlerException(final String developmentMessage, final MessageKey messageKey, - final String... parameters) { - super(developmentMessage, messageKey, parameters); - } - - @Override - protected String getBundleName() { - return DEFAULT_SERVER_BUNDLE_NAME; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java deleted file mode 100644 index 36cbd18..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * 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.core; - -import java.io.Closeable; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.Map.Entry; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.olingo.commons.api.ODataRuntimeException; -import org.apache.olingo.commons.api.http.HttpHeader; -import org.apache.olingo.commons.api.http.HttpMethod; -import org.apache.olingo.server.api.OData; -import org.apache.olingo.server.api.ODataHttpHandler; -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.ODataTranslatedException; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.processor.Processor; -import org.apache.olingo.server.api.serializer.CustomContentTypeSupport; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ODataHttpHandlerImpl implements ODataHttpHandler { - - private static final Logger LOG = LoggerFactory.getLogger(ODataHttpHandlerImpl.class); - - private ODataHandler handler; - private int split = 0; - - public ODataHttpHandlerImpl(final OData odata, final ServiceMetadata serviceMetadata) { - handler = new ODataHandler(odata, serviceMetadata); - } - - @Override - public void process(final HttpServletRequest request, final HttpServletResponse response) { - ODataRequest odRequest = null; - ODataResponse odResponse; - try { - odRequest = createODataRequest(request, split); - odResponse = handler.process(odRequest); - // ALL future methods after process must not throw exceptions! - } catch (Exception e) { - odResponse = handleException(odRequest, e); - } - - convertToHttp(response, odResponse); - } - - @Override - public void setSplit(int split) { - this.split = split; - } - - private ODataResponse handleException(ODataRequest odRequest, Exception e) { - ODataResponse resp = new ODataResponse(); - ODataServerError serverError; - if (e instanceof ODataHandlerException) { - serverError = ODataExceptionHelper.createServerErrorObject((ODataHandlerException) e, null); - } else if (e instanceof ODataTranslatedException) { - serverError = ODataExceptionHelper.createServerErrorObject((ODataTranslatedException) e, null); - } else { - serverError = ODataExceptionHelper.createServerErrorObject(e); - } - handler.handleException(odRequest, resp, serverError); - return resp; - } - - static void convertToHttp(final HttpServletResponse response, final ODataResponse odResponse) { - response.setStatus(odResponse.getStatusCode()); - - for (Entry<String, String> entry : odResponse.getHeaders().entrySet()) { - response.setHeader(entry.getKey(), entry.getValue()); - } - - InputStream input = odResponse.getContent(); - if (input != null) { - OutputStream output = null; - try { - output = response.getOutputStream(); - byte[] buffer = new byte[1024]; - int n; - while (-1 != (n = input.read(buffer))) { - output.write(buffer, 0, n); - } - } catch (IOException e) { - LOG.error(e.getMessage(), e); - throw new ODataRuntimeException(e); - } finally { - closeStream(output); - closeStream(input); - } - } - } - - private static void closeStream(Closeable closeable) { - if(closeable != null) { - try { - closeable.close(); - } catch (IOException e) { - LOG.error(e.getMessage(), e); - } - } - } - - private ODataRequest createODataRequest(final HttpServletRequest httpRequest, final int split) - throws ODataTranslatedException { - try { - ODataRequest odRequest = new ODataRequest(); - - odRequest.setBody(httpRequest.getInputStream()); - extractHeaders(odRequest, httpRequest); - extractMethod(odRequest, httpRequest); - extractUri(odRequest, httpRequest, split); - - return odRequest; - } catch (final IOException e) { - throw new SerializerException("An I/O exception occurred.", e, - SerializerException.MessageKeys.IO_EXCEPTION); - } - } - - static void extractMethod(final ODataRequest odRequest, final HttpServletRequest httpRequest) - throws ODataTranslatedException { - try { - HttpMethod httpRequestMethod = HttpMethod.valueOf(httpRequest.getMethod()); - - if (httpRequestMethod == HttpMethod.POST) { - String xHttpMethod = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD); - String xHttpMethodOverride = httpRequest.getHeader(HttpHeader.X_HTTP_METHOD_OVERRIDE); - - if (xHttpMethod == null && xHttpMethodOverride == null) { - odRequest.setMethod(httpRequestMethod); - } else if (xHttpMethod == null) { - odRequest.setMethod(HttpMethod.valueOf(xHttpMethodOverride)); - } else if (xHttpMethodOverride == null) { - odRequest.setMethod(HttpMethod.valueOf(xHttpMethod)); - } else { - if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) { - throw new ODataHandlerException("Ambiguous X-HTTP-Methods", - ODataHandlerException.MessageKeys.AMBIGUOUS_XHTTP_METHOD, xHttpMethod, xHttpMethodOverride); - } - odRequest.setMethod(HttpMethod.valueOf(xHttpMethod)); - } - } else { - odRequest.setMethod(httpRequestMethod); - } - } catch (IllegalArgumentException e) { - throw new ODataHandlerException("Invalid HTTP method" + httpRequest.getMethod(), - ODataHandlerException.MessageKeys.INVALID_HTTP_METHOD, httpRequest.getMethod()); - } - } - - static void extractUri(final ODataRequest odRequest, final HttpServletRequest httpRequest, final int split) { - String rawRequestUri = httpRequest.getRequestURL().toString(); - - String rawODataPath; - if (!"".equals(httpRequest.getServletPath())) { - int beginIndex; - beginIndex = rawRequestUri.indexOf(httpRequest.getServletPath()); - beginIndex += httpRequest.getServletPath().length(); - rawODataPath = rawRequestUri.substring(beginIndex); - } else if (!"".equals(httpRequest.getContextPath())) { - int beginIndex; - beginIndex = rawRequestUri.indexOf(httpRequest.getContextPath()); - beginIndex += httpRequest.getContextPath().length(); - rawODataPath = rawRequestUri.substring(beginIndex); - } else { - rawODataPath = httpRequest.getRequestURI(); - } - - String rawServiceResolutionUri; - if (split > 0) { - rawServiceResolutionUri = rawODataPath; - for (int i = 0; i < split; i++) { - int e = rawODataPath.indexOf("/", 1); - if (-1 == e) { - rawODataPath = ""; - } else { - rawODataPath = rawODataPath.substring(e); - } - } - int end = rawServiceResolutionUri.length() - rawODataPath.length(); - rawServiceResolutionUri = rawServiceResolutionUri.substring(0, end); - } else { - rawServiceResolutionUri = null; - } - - String rawBaseUri = rawRequestUri.substring(0, rawRequestUri.length() - rawODataPath.length()); - - odRequest.setRawQueryPath(httpRequest.getQueryString()); - odRequest.setRawRequestUri(rawRequestUri - + (httpRequest.getQueryString() == null ? "" : "?" + httpRequest.getQueryString())); - odRequest.setRawODataPath(rawODataPath); - odRequest.setRawBaseUri(rawBaseUri); - odRequest.setRawServiceResolutionUri(rawServiceResolutionUri); - } - - static void extractHeaders(final ODataRequest odRequest, final HttpServletRequest req) { - for (Enumeration<?> headerNames = req.getHeaderNames(); headerNames.hasMoreElements();) { - String headerName = (String) headerNames.nextElement(); - List<String> headerValues = new ArrayList<String>(); - for (Enumeration<?> headers = req.getHeaders(headerName); headers.hasMoreElements();) { - String value = (String) headers.nextElement(); - headerValues.add(value); - } - odRequest.addHeader(headerName, headerValues); - } - } - - @Override - public void register(final Processor processor) { - handler.register(processor); - } - - @Override - public void register(final CustomContentTypeSupport customContentTypeSupport) { - handler.register(customContentTypeSupport); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/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 deleted file mode 100644 index 1551461..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataImpl.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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.core; - -import java.util.List; - -import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; -import org.apache.olingo.commons.api.format.ODataFormat; -import org.apache.olingo.server.api.OData; -import org.apache.olingo.server.api.ODataHttpHandler; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.deserializer.DeserializerException; -import org.apache.olingo.server.api.deserializer.FixedFormatDeserializer; -import org.apache.olingo.server.api.deserializer.ODataDeserializer; -import org.apache.olingo.server.api.edm.provider.EdmProvider; -import org.apache.olingo.server.api.edmx.EdmxReference; -import org.apache.olingo.server.api.serializer.FixedFormatSerializer; -import org.apache.olingo.server.api.serializer.ODataSerializer; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.api.uri.UriHelper; -import org.apache.olingo.server.core.deserializer.FixedFormatDeserializerImpl; -import org.apache.olingo.server.core.deserializer.json.ODataJsonDeserializer; -import org.apache.olingo.server.core.serializer.FixedFormatSerializerImpl; -import org.apache.olingo.server.core.serializer.json.ODataJsonSerializer; -import org.apache.olingo.server.core.serializer.xml.ODataXmlSerializerImpl; -import org.apache.olingo.server.core.uri.UriHelperImpl; - -public class ODataImpl extends OData { - - @Override - public ODataSerializer createSerializer(final ODataFormat format) throws SerializerException { - ODataSerializer serializer; - switch (format) { - case JSON: - case JSON_NO_METADATA: - case JSON_FULL_METADATA: - serializer = new ODataJsonSerializer(format); - break; - case XML: - serializer = new ODataXmlSerializerImpl(); - break; - default: - throw new SerializerException("Unsupported format: " + format, - SerializerException.MessageKeys.UNSUPPORTED_FORMAT, format.toString()); - } - - 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(EdmProvider edmProvider, List<EdmxReference> references) { - return new ServiceMetadataImpl(ODataServiceVersion.V40, edmProvider, references); - } - - @Override - public FixedFormatDeserializer createFixedFormatDeserializer() { - return new FixedFormatDeserializerImpl(); - } - - @Override - public UriHelper createUriHelper() { - return new UriHelperImpl(); - } - - @Override - public ODataDeserializer createDeserializer(ODataFormat format) throws DeserializerException{ - ODataDeserializer serializer; - switch (format) { - case JSON: - case JSON_NO_METADATA: - case JSON_FULL_METADATA: - serializer = new ODataJsonDeserializer(); - break; - case XML: - //We do not support xml deserialization right now so this mus lead to an error - default: - throw new DeserializerException("Unsupported format: " + format, - SerializerException.MessageKeys.UNSUPPORTED_FORMAT, format.toString()); - } - - return serializer; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java deleted file mode 100644 index 4e99d31..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/RedirectProcessor.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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.core; - -import org.apache.olingo.server.api.ODataRequest; -import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.processor.Processor; - -public interface RedirectProcessor extends Processor { - - void redirect(ODataRequest request, ODataResponse response); - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java deleted file mode 100644 index 5008092..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ServiceMetadataImpl.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.core; - -import org.apache.olingo.commons.api.edm.Edm; -import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; -import org.apache.olingo.server.api.edmx.EdmxReference; -import org.apache.olingo.server.api.ServiceMetadata; -import org.apache.olingo.server.api.edm.provider.EdmProvider; -import org.apache.olingo.server.core.edm.provider.EdmProviderImpl; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - */ -public class ServiceMetadataImpl implements ServiceMetadata { - - private final EdmProviderImpl edm; - private final ODataServiceVersion version; - private final List<EdmxReference> references = new ArrayList<EdmxReference>(); - - public ServiceMetadataImpl(ODataServiceVersion version, EdmProvider edmProvider, List<EdmxReference> references) { - this.edm = new EdmProviderImpl(edmProvider); - this.version = version; - this.references.addAll(references); - } - - @Override - public Edm getEdm() { - return edm; - } - - @Override - public ODataServiceVersion getDataServiceVersion() { - return version; - } - - @Override - public List<EdmxReference> getReferences() { - return Collections.unmodifiableList(references); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFascadeImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFascadeImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFascadeImpl.java deleted file mode 100644 index 6b2ee39..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFascadeImpl.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.core.batchhandler; - -import org.apache.olingo.server.api.ODataRequest; -import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.batch.BatchFacade; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException; -import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart; -import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart; -import org.apache.olingo.server.api.processor.BatchProcessor; -import org.apache.olingo.server.core.ODataHandler; -import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon; - -public class BatchFascadeImpl implements BatchFacade { - private final BatchPartHandler partHandler; - - public BatchFascadeImpl(ODataHandler oDataHandler, ODataRequest request, BatchProcessor batchProcessor, - final boolean isStrict) { - partHandler = new BatchPartHandler(oDataHandler, batchProcessor, this); - } - - @Override - public ODataResponse handleODataRequest(ODataRequest request) throws BatchDeserializerException { - return partHandler.handleODataRequest(request); - } - - @Override - public ODataResponsePart handleBatchRequest(BatchRequestPart request) throws BatchDeserializerException { - return partHandler.handleBatchRequest(request); - } - - @Override - public String extractBoundaryFromContentType(String contentType) throws BatchDeserializerException { - return BatchParserCommon.getBoundary(contentType, 0); - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchHandler.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchHandler.java deleted file mode 100644 index e8fe1db..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchHandler.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.core.batchhandler; - -import org.apache.olingo.commons.api.http.HttpHeader; -import org.apache.olingo.commons.api.http.HttpMethod; -import org.apache.olingo.server.api.ODataRequest; -import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.batch.BatchFacade; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException.MessageKeys; -import org.apache.olingo.server.api.deserializer.DeserializerException; -import org.apache.olingo.server.api.processor.BatchProcessor; -import org.apache.olingo.server.api.serializer.SerializerException; -import org.apache.olingo.server.core.ODataHandler; -import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon; - -public class BatchHandler { - private final BatchProcessor batchProcessor; - private final ODataHandler oDataHandler; - - public BatchHandler(final ODataHandler oDataHandler, final BatchProcessor batchProcessor) { - - this.batchProcessor = batchProcessor; - this.oDataHandler = oDataHandler; - } - - public void process(final ODataRequest request, final ODataResponse response, final boolean isStrict) - throws DeserializerException, SerializerException { - validateRequest(request); - - final BatchFacade operation = new BatchFascadeImpl(oDataHandler, request, batchProcessor, isStrict); - batchProcessor.processBatch(operation, request, response); - } - - private void validateRequest(final ODataRequest request) throws BatchDeserializerException { - validateHttpMethod(request); - validateContentType(request); - } - - private void validateContentType(final ODataRequest request) throws BatchDeserializerException { - final String contentType = request.getHeader(HttpHeader.CONTENT_TYPE); - - if (contentType == null || !BatchParserCommon.PATTERN_MULTIPART_BOUNDARY.matcher(contentType).matches()) { - throw new BatchDeserializerException("Invalid content type", MessageKeys.INVALID_CONTENT_TYPE, 0); - } - } - - private void validateHttpMethod(final ODataRequest request) throws BatchDeserializerException { - if (request.getMethod() != HttpMethod.POST) { - throw new BatchDeserializerException("Invalid HTTP method", MessageKeys.INVALID_METHOD, 0); - } - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java deleted file mode 100644 index b140b0b..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.core.batchhandler; - -import org.apache.olingo.server.api.ODataRequest; -import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.batch.BatchFacade; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException; -import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart; -import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart; -import org.apache.olingo.server.api.processor.BatchProcessor; -import org.apache.olingo.server.core.ODataHandler; -import org.apache.olingo.server.core.batchhandler.referenceRewriting.BatchReferenceRewriter; -import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon; - -public class BatchPartHandler { - private final ODataHandler oDataHandler; - private final BatchProcessor batchProcessor; - private final BatchFacade batchFascade; - private final BatchReferenceRewriter rewriter; - - public BatchPartHandler(final ODataHandler oDataHandler, final BatchProcessor processor, - final BatchFacade batchFascade) { - this.oDataHandler = oDataHandler; - this.batchProcessor = processor; - this.batchFascade = batchFascade; - this.rewriter = new BatchReferenceRewriter(); - } - - public ODataResponse handleODataRequest(ODataRequest request) throws BatchDeserializerException { - return handle(request, true); - } - - public ODataResponsePart handleBatchRequest(BatchRequestPart request) throws BatchDeserializerException { - if (request.isChangeSet()) { - return handleChangeSet(request); - } else { - final ODataResponse response = handle(request.getRequests().get(0), false); - - return new ODataResponsePart(response, false); - } - } - - public ODataResponse handle(ODataRequest request, boolean isChangeSet) - throws BatchDeserializerException { - final ODataResponse response; - - if (isChangeSet) { - rewriter.replaceReference(request); - - response = oDataHandler.process(request); - - rewriter.addMapping(request, response); - } else { - response = oDataHandler.process(request); - } - - // Add content id to response - final String contentId = request.getHeader(BatchParserCommon.HTTP_CONTENT_ID); - if (contentId != null) { - response.setHeader(BatchParserCommon.HTTP_CONTENT_ID, contentId); - } - - return response; - } - - private ODataResponsePart handleChangeSet(BatchRequestPart request) throws BatchDeserializerException { - return batchProcessor.processChangeSet(batchFascade, request.getRequests()); - } - -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/f7a7b484/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/referenceRewriting/BatchReferenceRewriter.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/referenceRewriting/BatchReferenceRewriter.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/referenceRewriting/BatchReferenceRewriter.java deleted file mode 100644 index 772fd9d..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/referenceRewriting/BatchReferenceRewriter.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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.core.batchhandler.referenceRewriting; - -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.olingo.commons.api.ODataRuntimeException; -import org.apache.olingo.commons.api.http.HttpHeader; -import org.apache.olingo.commons.api.http.HttpMethod; -import org.apache.olingo.server.api.ODataRequest; -import org.apache.olingo.server.api.ODataResponse; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException; -import org.apache.olingo.server.api.batch.exception.BatchDeserializerException.MessageKeys; -import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon; - -public class BatchReferenceRewriter { - private static final String REG_EX_REFERENCE = "\\$(.*)(/.*)?"; - private static final Pattern REFERENCE_PATTERN = Pattern.compile(REG_EX_REFERENCE); - - private Map<String, String> contentIdMapping = new HashMap<String, String>(); - - public String getReferenceInURI(ODataRequest request) { - Matcher matcher = REFERENCE_PATTERN.matcher(removeSlash(removeSlash(request.getRawODataPath(), true), false)); - - return (matcher.matches()) ? matcher.group(1) : null; - } - - public void replaceReference(ODataRequest request) { - final String reference = getReferenceInURI(request); - - if (reference != null) { - final String replacement = contentIdMapping.get(reference); - - if (replacement != null) { - replaceContentIdReference(request, reference, replacement); - } else { - throw new ODataRuntimeException("Required Content-Id for reference \"" + reference + "\" not found."); - } - } - } - - private void replaceContentIdReference(ODataRequest request, String contentId, String resourceUri) { - final String newUri = request.getRawODataPath().replace("/$" + contentId, resourceUri); - request.setRawODataPath(newUri); - request.setRawRequestUri(request.getRawBaseUri() + "/" + newUri); - } - - public void addMapping(ODataRequest request, ODataResponse response) - throws BatchDeserializerException { - final String resourceUri = getODataPath(request, response); - final String contentId = request.getHeader(BatchParserCommon.HTTP_CONTENT_ID); - - contentIdMapping.put(contentId, resourceUri); - } - - private String getODataPath(ODataRequest request, ODataResponse response) throws BatchDeserializerException { - String resourceUri = null; - - if (request.getMethod() == HttpMethod.POST) { - // Create entity - // The URI of the new resource will be generated by the server and published in the location header - resourceUri = parseODataPath(response.getHeaders().get(HttpHeader.LOCATION), request.getRawBaseUri()); - } else { - // Update, Upsert (PUT, PATCH, Delete) - // These methods still addresses a given resource, so we use the URI given by the request - resourceUri = request.getRawODataPath(); - } - - return resourceUri; - } - - private String parseODataPath(String uri, String rawBaseUri) throws BatchDeserializerException { - int index = uri.indexOf(rawBaseUri); - - if(index == 0) { - return uri.substring(rawBaseUri.length()); - } else { - throw new BatchDeserializerException("Invalid base uri or uri", MessageKeys.INVALID_URI, 0); - } - } - - private String removeSlash(String rawODataPath, boolean first) { - final int indexOfSlash = rawODataPath.indexOf("/"); - if (first) { - return (indexOfSlash == 0) ? rawODataPath.substring(1) : rawODataPath; - } else { - return (indexOfSlash != -1) ? rawODataPath.substring(0, indexOfSlash) : rawODataPath; - } - } -}
