Repository: olingo-odata2 Updated Branches: refs/heads/master d56247a11 -> d69117779
[OLINGO-808] Implement ODataApplicationRuntimeException Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/d6911777 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/d6911777 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/d6911777 Branch: refs/heads/master Commit: d691177793431c50fb3a2e67fffecfa75b206c72 Parents: d56247a Author: Christian Amend <[email protected]> Authored: Thu Oct 29 10:13:29 2015 +0100 Committer: Christian Amend <[email protected]> Committed: Thu Oct 29 10:13:29 2015 +0100 ---------------------------------------------------------------------- .../ODataRuntimeApplicationException.java | 163 +++++++++++++++++++ .../odata2/core/rest/ODataExceptionWrapper.java | 8 + .../odata2/fit/basic/ErrorResponseTest.java | 20 ++- 3 files changed, 188 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d6911777/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataRuntimeApplicationException.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataRuntimeApplicationException.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataRuntimeApplicationException.java new file mode 100644 index 0000000..a453b9a --- /dev/null +++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataRuntimeApplicationException.java @@ -0,0 +1,163 @@ +/******************************************************************************* + * 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.odata2.api.exception; + +import java.util.Locale; + +import org.apache.olingo.odata2.api.commons.HttpStatusCodes; + +/** + * This class represents a translated application runtime exception. Use this exception class to display custom + * exception + * messages. + * <br>If a HTTP status is given this exception will result in the set status code like an HTTP exception. + * <br>A set status code can be used to show a substatus to a HTTP status as described in the OData protocol + * specification. + * + */ +public class ODataRuntimeApplicationException extends RuntimeException { + + private static final long serialVersionUID = -7869148184447528782L; + + private String errorCode; + private HttpStatusCodes httpStatus = HttpStatusCodes.INTERNAL_SERVER_ERROR; + private final Locale locale; + + /** + * Since this is a translated application exception locale must not be null. + * @param message + * @param locale + */ + public ODataRuntimeApplicationException(final String message, final Locale locale) { + super(message); + this.locale = locale; + } + + /** + * Since this is a translated application exception locale must not be null. + * <br>The status code given will be displayed at the client. + * @param message + * @param locale + * @param status + */ + public ODataRuntimeApplicationException(final String message, final Locale locale, final HttpStatusCodes status) { + this(message, locale); + httpStatus = status; + } + + /** + * Since this is a translated application exception locale must not be null. + * <br>The status code given will be displayed at the client. + * <br>The error code may be used as a substatus for the HTTP status code as described in the OData protocol + * specification. + * @param message + * @param locale + * @param status + * @param errorCode + */ + public ODataRuntimeApplicationException(final String message, final Locale locale, final HttpStatusCodes status, + final String errorCode) { + this(message, locale, status); + this.errorCode = errorCode; + } + + /** + * Since this is a translated application exception locale must not be null. + * <br>The status code given will be displayed at the client. + * <br>The error code may be used as a substatus for the HTTP status code as described in the OData protocol + * specification. + * @param message + * @param locale + * @param status + * @param errorCode + * @param e + */ + public ODataRuntimeApplicationException(final String message, final Locale locale, final HttpStatusCodes status, + final String errorCode, final Throwable e) { + super(message, e); + this.errorCode = errorCode; + httpStatus = status; + this.locale = locale; + } + + /** + * Since this is a translated application exception locale must not be null. + * @param message + * @param locale + * @param e + */ + public ODataRuntimeApplicationException(final String message, final Locale locale, final Throwable e) { + super(message, e); + this.locale = locale; + } + + /** + * Since this is a translated application exception locale must not be null. + * <br>The status code given will be displayed at the client. + * @param message + * @param locale + * @param status + * @param e + */ + public ODataRuntimeApplicationException(final String message, final Locale locale, final HttpStatusCodes status, + final Throwable e) { + this(message, locale, e); + httpStatus = status; + } + + /** + * Since this is a translated application exception locale must not be null. + * <br>The error code may be used as a substatus for the HTTP status code as described in the OData protocol + * specification. + * @param message + * @param locale + * @param errorCode + * @param e + */ + public ODataRuntimeApplicationException(final String message, final Locale locale, final String errorCode, + final Throwable e) { + this(message, locale, e); + this.errorCode = errorCode; + + } + + /** + * @return {@link Locale} the locale + */ + public Locale getLocale() { + return locale; + } + + /** + * Default HttpStatusCodes.INTERNAL_SERVER_ERROR + * @return {@link HttpStatusCodes} the status code + */ + public HttpStatusCodes getHttpStatus() { + return httpStatus; + } + + /** + * Default code is null + * @return <b>String</b>The error code displayed in the error message. + */ + public String getCode() { + return errorCode; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d6911777/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java index e7969a5..3eb8c98 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataExceptionWrapper.java @@ -42,6 +42,7 @@ import org.apache.olingo.odata2.api.exception.ODataApplicationException; import org.apache.olingo.odata2.api.exception.ODataException; import org.apache.olingo.odata2.api.exception.ODataHttpException; import org.apache.olingo.odata2.api.exception.ODataMessageException; +import org.apache.olingo.odata2.api.exception.ODataRuntimeApplicationException; import org.apache.olingo.odata2.api.processor.ODataContext; import org.apache.olingo.odata2.api.processor.ODataErrorCallback; import org.apache.olingo.odata2.api.processor.ODataErrorContext; @@ -97,6 +98,8 @@ public class ODataExceptionWrapper { fillErrorContext(toHandleException); if (toHandleException instanceof ODataApplicationException) { enhanceContextWithApplicationException((ODataApplicationException) toHandleException); + } else if (toHandleException instanceof ODataRuntimeApplicationException) { + enhanceContextWithRuntimeApplicationException((ODataRuntimeApplicationException) toHandleException); } else if (toHandleException instanceof ODataMessageException) { enhanceContextWithMessageException((ODataMessageException) toHandleException); } @@ -119,6 +122,11 @@ public class ODataExceptionWrapper { } } + private void enhanceContextWithRuntimeApplicationException(ODataRuntimeApplicationException toHandleException) { + errorContext.setHttpStatus(toHandleException.getHttpStatus()); + errorContext.setErrorCode(toHandleException.getCode()); + } + private ODataResponse handleErrorCallback(final ODataErrorCallback callback) throws EntityProviderException { ODataResponse oDataResponse; try { http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d6911777/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ErrorResponseTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ErrorResponseTest.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ErrorResponseTest.java index 28087d3..c11b961 100644 --- a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ErrorResponseTest.java +++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/ErrorResponseTest.java @@ -24,13 +24,17 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; +import java.util.Locale; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.olingo.odata2.api.commons.HttpStatusCodes; import org.apache.olingo.odata2.api.exception.ODataException; +import org.apache.olingo.odata2.api.exception.ODataRuntimeApplicationException; import org.apache.olingo.odata2.api.processor.ODataSingleProcessor; +import org.apache.olingo.odata2.api.processor.part.MetadataProcessor; import org.apache.olingo.odata2.api.processor.part.ServiceDocumentProcessor; +import org.apache.olingo.odata2.api.uri.info.GetMetadataUriInfo; import org.apache.olingo.odata2.api.uri.info.GetServiceDocumentUriInfo; import org.apache.olingo.odata2.core.exception.ODataRuntimeException; import org.apache.olingo.odata2.testutil.server.ServletType; @@ -49,9 +53,11 @@ public class ErrorResponseTest extends AbstractBasicTest { protected ODataSingleProcessor createProcessor() throws ODataException { final ODataSingleProcessor processor = mock(ODataSingleProcessor.class); - when( - ((ServiceDocumentProcessor) processor).readServiceDocument(any(GetServiceDocumentUriInfo.class), - any(String.class))).thenThrow(new ODataRuntimeException("unit testing")); + when(((ServiceDocumentProcessor) processor).readServiceDocument(any(GetServiceDocumentUriInfo.class), + any(String.class))).thenThrow(new ODataRuntimeException("unit testing")); + when(((MetadataProcessor) processor).readMetadata(any(GetMetadataUriInfo.class), + any(String.class))).thenThrow( + new ODataRuntimeApplicationException("unit testing", Locale.ROOT, HttpStatusCodes.FORBIDDEN)); return processor; } @@ -63,4 +69,12 @@ public class ErrorResponseTest extends AbstractBasicTest { final HttpResponse response = executeGetRequest(""); assertEquals(HttpStatusCodes.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusLine().getStatusCode()); } + + @Test + public void testODataRuntimeApplicationException() throws ClientProtocolException, IOException, ODataException { + disableLogging(); + + final HttpResponse response = executeGetRequest("$metadata"); + assertEquals(HttpStatusCodes.FORBIDDEN.getStatusCode(), response.getStatusLine().getStatusCode()); + } }
