olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chri...@apache.org
Subject olingo-odata2 git commit: [OLINGO-492] Call ODataCallback when using the servlet
Date Wed, 17 Dec 2014 16:46:46 GMT
Repository: olingo-odata2
Updated Branches:
  refs/heads/master fbfbff088 -> f40ea94c8


[OLINGO-492] Call ODataCallback when using the servlet


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/f40ea94c
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/f40ea94c
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/f40ea94c

Branch: refs/heads/master
Commit: f40ea94c83f5e94fba8cdca98241a64735929222
Parents: fbfbff0
Author: Christian Amend <chrisam@apache.org>
Authored: Wed Dec 17 17:46:10 2014 +0100
Committer: Christian Amend <chrisam@apache.org>
Committed: Wed Dec 17 17:46:10 2014 +0100

----------------------------------------------------------------------
 .../core/servlet/ODataExceptionWrapper.java     | 29 +++++--
 .../odata2/core/servlet/ODataServlet.java       | 88 +++++++++++---------
 .../odata2/core/servlet/ODataServletTest.java   |  1 -
 .../odata2/fit/basic/NullServiceTest.java       |  2 +-
 .../odata2/testutil/fit/FitErrorCallback.java   |  1 -
 5 files changed, 71 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/f40ea94c/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataExceptionWrapper.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataExceptionWrapper.java
b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataExceptionWrapper.java
index 7edd411..e2069d4 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataExceptionWrapper.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataExceptionWrapper.java
@@ -28,6 +28,7 @@ import java.util.Map.Entry;
 
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.olingo.odata2.api.ODataServiceFactory;
 import org.apache.olingo.odata2.api.batch.BatchException;
 import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
 import org.apache.olingo.odata2.api.ep.EntityProvider;
@@ -37,9 +38,11 @@ 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.processor.ODataErrorCallback;
 import org.apache.olingo.odata2.api.processor.ODataErrorContext;
 import org.apache.olingo.odata2.api.processor.ODataResponse;
 import org.apache.olingo.odata2.core.commons.ContentType;
+import org.apache.olingo.odata2.core.ep.ProviderFacadeImpl;
 import org.apache.olingo.odata2.core.exception.MessageService;
 import org.apache.olingo.odata2.core.exception.MessageService.Message;
 import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
@@ -57,8 +60,9 @@ public class ODataExceptionWrapper {
   private final Locale messageLocale;
   private final URI requestUri;
   private final Map<String, List<String>> httpRequestHeaders;
+  private final ODataErrorCallback callback;
 
-  public ODataExceptionWrapper(final HttpServletRequest req) {
+  public ODataExceptionWrapper(final HttpServletRequest req, ODataServiceFactory serviceFactory)
{
     try {
       requestUri = new URI(req.getRequestURI());
     } catch (URISyntaxException e) {
@@ -70,6 +74,7 @@ public class ODataExceptionWrapper {
     List<String> acceptHeaders = RestUtil.extractAcceptHeaders(req.getHeader("Accept"));
     contentType = getContentType(queryParameters, acceptHeaders).toContentTypeString();
     messageLocale = MessageService.getSupportedLocale(getLanguages(acceptableLanguages),
DEFAULT_RESPONSE_LOCALE);
+    callback = serviceFactory.getCallback(ODataErrorCallback.class);
   }
 
   public ODataResponse wrapInExceptionResponse(final Exception exception) {
@@ -83,11 +88,11 @@ public class ODataExceptionWrapper {
       }
 
       ODataResponse oDataResponse;
-      // if (callback != null) {
-      // oDataResponse = handleErrorCallback(callback);
-      // } else {
-      oDataResponse = EntityProvider.writeErrorDocument(errorContext);
-      // }
+      if (callback != null) {
+        oDataResponse = handleErrorCallback(callback);
+      } else {
+        oDataResponse = EntityProvider.writeErrorDocument(errorContext);
+      }
       if (!oDataResponse.containsHeader(org.apache.olingo.odata2.api.commons.HttpHeaders.CONTENT_TYPE))
{
         oDataResponse = ODataResponse.fromResponse(oDataResponse).contentHeader(contentType).build();
       }
@@ -100,6 +105,18 @@ public class ODataExceptionWrapper {
     }
   }
 
+  private ODataResponse handleErrorCallback(final ODataErrorCallback callback) throws EntityProviderException
{
+    ODataResponse oDataResponse;
+    try {
+      oDataResponse = callback.handleError(errorContext);
+    } catch (ODataApplicationException e) {
+      fillErrorContext(e);
+      enhanceContextWithApplicationException(e);
+      oDataResponse = new ProviderFacadeImpl().writeErrorDocument(errorContext);
+    }
+    return oDataResponse;
+  }
+
   private Exception extractException(final Exception exception) {
     if (exception instanceof ODataException) {
       ODataException odataException = (ODataException) exception;

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/f40ea94c/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
index 465d52e..0973f33 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/servlet/ODataServlet.java
@@ -62,84 +62,89 @@ public class ODataServlet extends HttpServlet {
     if (factoryClassName == null) {
       throw new ODataRuntimeException("config missing: org.apache.olingo.odata2.processor.factory");
     }
+
+    // We have to create the Service Factory here because otherwise we do not have access
to the error callback
+    ODataServiceFactory serviceFactory = createServiceFactory(req);
+
     String xHttpMethod = req.getHeader("X-HTTP-Method");
     String xHttpMethodOverride = req.getHeader("X-HTTP-Method-Override");
     if (xHttpMethod != null && xHttpMethodOverride != null) {
       if (!xHttpMethod.equalsIgnoreCase(xHttpMethodOverride)) {
-        ODataExceptionWrapper wrapper = new ODataExceptionWrapper(req);
+        ODataExceptionWrapper wrapper = new ODataExceptionWrapper(req, serviceFactory);
         createResponse(resp, wrapper.wrapInExceptionResponse(
             new ODataBadRequestException(ODataBadRequestException.AMBIGUOUS_XMETHOD)));
       }
     }
 
     if (req.getPathInfo() != null) {
-      handle(req, resp, xHttpMethod, xHttpMethodOverride);
+      handle(req, resp, xHttpMethod, xHttpMethodOverride, serviceFactory);
     } else {
-      handleRedirect(req, resp);
+      handleRedirect(req, resp, serviceFactory);
     }
   }
 
   private void handle(final HttpServletRequest req, final HttpServletResponse resp, final
String xHttpMethod,
-      final String xHttpMethodOverride) throws IOException {
+      final String xHttpMethodOverride, ODataServiceFactory serviceFactory) throws IOException
{
     String method = req.getMethod();
     if (ODataHttpMethod.GET.name().equals(method)) {
-      handleRequest(req, ODataHttpMethod.GET, resp);
+      handleRequest(req, ODataHttpMethod.GET, resp, serviceFactory);
     } else if (ODataHttpMethod.POST.name().equals(method)) {
       if (xHttpMethod == null && xHttpMethodOverride == null) {
-        handleRequest(req, ODataHttpMethod.POST, resp);
+        handleRequest(req, ODataHttpMethod.POST, resp, serviceFactory);
       } else if (xHttpMethod == null) {
         /* tunneling */
-        boolean methodHandled = handleHttpTunneling(req, resp, xHttpMethodOverride);
+        boolean methodHandled = handleHttpTunneling(req, resp, xHttpMethodOverride, serviceFactory);
         if (!methodHandled) {
-          createMethodNotAllowedResponse(req, ODataHttpException.COMMON, resp);
+          createMethodNotAllowedResponse(req, ODataHttpException.COMMON, resp, serviceFactory);
         }
       } else {
         /* tunneling */
-        boolean methodHandled = handleHttpTunneling(req, resp, xHttpMethod);
+        boolean methodHandled = handleHttpTunneling(req, resp, xHttpMethod, serviceFactory);
         if (!methodHandled) {
-          createNotImplementedResponse(req, ODataNotImplementedException.TUNNELING, resp);
+          createNotImplementedResponse(req, ODataNotImplementedException.TUNNELING, resp,
serviceFactory);
         }
       }
 
     } else if (ODataHttpMethod.PUT.name().equals(method)) {
-      handleRequest(req, ODataHttpMethod.PUT, resp);
+      handleRequest(req, ODataHttpMethod.PUT, resp, serviceFactory);
     } else if (ODataHttpMethod.DELETE.name().equals(method)) {
-      handleRequest(req, ODataHttpMethod.DELETE, resp);
+      handleRequest(req, ODataHttpMethod.DELETE, resp, serviceFactory);
     } else if (ODataHttpMethod.PATCH.name().equals(method)) {
-      handleRequest(req, ODataHttpMethod.PATCH, resp);
+      handleRequest(req, ODataHttpMethod.PATCH, resp, serviceFactory);
     } else if (ODataHttpMethod.MERGE.name().equals(method)) {
-      handleRequest(req, ODataHttpMethod.MERGE, resp);
+      handleRequest(req, ODataHttpMethod.MERGE, resp, serviceFactory);
     } else if (HTTP_METHOD_HEAD.equals(method) || HTTP_METHOD_OPTIONS.equals(method)) {
-      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
+      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp, serviceFactory);
     } else {
-      createNotImplementedResponse(req, ODataHttpException.COMMON, resp);
+      createNotImplementedResponse(req, ODataHttpException.COMMON, resp, serviceFactory);
     }
   }
 
   private boolean handleHttpTunneling(final HttpServletRequest req, final HttpServletResponse
resp,
-      final String xHttpMethod) throws IOException {
+      final String xHttpMethod, ODataServiceFactory serviceFactory) throws IOException {
     if (ODataHttpMethod.MERGE.name().equals(xHttpMethod)) {
-      handleRequest(req, ODataHttpMethod.MERGE, resp);
+      handleRequest(req, ODataHttpMethod.MERGE, resp, serviceFactory);
     } else if (ODataHttpMethod.PATCH.name().equals(xHttpMethod)) {
-      handleRequest(req, ODataHttpMethod.PATCH, resp);
+      handleRequest(req, ODataHttpMethod.PATCH, resp, serviceFactory);
     } else if (ODataHttpMethod.DELETE.name().equals(xHttpMethod)) {
-      handleRequest(req, ODataHttpMethod.DELETE, resp);
+      handleRequest(req, ODataHttpMethod.DELETE, resp, serviceFactory);
     } else if (ODataHttpMethod.PUT.name().equals(xHttpMethod)) {
-      handleRequest(req, ODataHttpMethod.PUT, resp);
+      handleRequest(req, ODataHttpMethod.PUT, resp, serviceFactory);
     } else if (ODataHttpMethod.GET.name().equals(xHttpMethod)) {
-      handleRequest(req, ODataHttpMethod.GET, resp);
+      handleRequest(req, ODataHttpMethod.GET, resp, serviceFactory);
     } else if (ODataHttpMethod.POST.name().equals(xHttpMethod)) {
-      handleRequest(req, ODataHttpMethod.POST, resp);
+      handleRequest(req, ODataHttpMethod.POST, resp, serviceFactory);
     } else if (HTTP_METHOD_HEAD.equals(xHttpMethod) || HTTP_METHOD_OPTIONS.equals(xHttpMethod))
{
-      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
+      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp, serviceFactory);
     } else {
-      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp);
+      createNotImplementedResponse(req, ODataNotImplementedException.COMMON, resp, serviceFactory);
     }
     return true;
   }
 
   private void
-      handleRequest(final HttpServletRequest req, final ODataHttpMethod method, final HttpServletResponse
resp)
+      handleRequest(final HttpServletRequest req, final ODataHttpMethod method, final HttpServletResponse
resp,
+          ODataServiceFactory serviceFactory)
           throws IOException {
     try {
       final String pathSplitAsString = getInitParameter(ODataServiceFactory.PATH_SPLIT_LABEL);
@@ -148,7 +153,7 @@ public class ODataServlet extends HttpServlet {
         pathSplit = Integer.parseInt(pathSplitAsString);
       }
       if (req.getHeader(HttpHeaders.ACCEPT) != null && req.getHeader(HttpHeaders.ACCEPT).isEmpty())
{
-        createNotAcceptableResponse(req, ODataNotAcceptableException.COMMON, resp);
+        createNotAcceptableResponse(req, ODataNotAcceptableException.COMMON, resp, serviceFactory);
       }
       ODataRequest odataRequest = ODataRequest.method(method)
           .contentType(RestUtil.extractRequestContentType(req.getContentType()).toContentTypeString())
@@ -159,13 +164,13 @@ public class ODataServlet extends HttpServlet {
           .requestHeaders(RestUtil.extractHeaders(req))
           .body(req.getInputStream())
           .build();
-      ODataServiceFactory serviceFactory = createServiceFactory(req);
+
       ODataContextImpl context = new ODataContextImpl(odataRequest, serviceFactory);
       context.setParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT, req);
 
       ODataService service = serviceFactory.createService(context);
-      if(service == null){
-        createServiceUnavailableResponse(req, ODataInternalServerErrorException.NOSERVICE,
resp);
+      if (service == null) {
+        createServiceUnavailableResponse(req, ODataInternalServerErrorException.NOSERVICE,
resp, serviceFactory);
       } else {
         context.setService(service);
         service.getProcessor().setContext(context);
@@ -175,7 +180,7 @@ public class ODataServlet extends HttpServlet {
         createResponse(resp, odataResponse);
       }
     } catch (ODataException e) {
-      ODataExceptionWrapper wrapper = new ODataExceptionWrapper(req);
+      ODataExceptionWrapper wrapper = new ODataExceptionWrapper(req, serviceFactory);
       createResponse(resp, wrapper.wrapInExceptionResponse(e));
     }
   }
@@ -194,7 +199,8 @@ public class ODataServlet extends HttpServlet {
     }
   }
 
-  private void handleRedirect(final HttpServletRequest req, final HttpServletResponse resp)
throws IOException {
+  private void handleRedirect(final HttpServletRequest req, final HttpServletResponse resp,
+      ODataServiceFactory serviceFactory) throws IOException {
     String method = req.getMethod();
     if (ODataHttpMethod.GET.name().equals(method) ||
         ODataHttpMethod.POST.name().equals(method) ||
@@ -209,7 +215,7 @@ public class ODataServlet extends HttpServlet {
           .build();
       createResponse(resp, odataResponse);
     } else {
-      createNotImplementedResponse(req, ODataHttpException.COMMON, resp);
+      createNotImplementedResponse(req, ODataHttpException.COMMON, resp, serviceFactory);
     }
 
   }
@@ -255,10 +261,10 @@ public class ODataServlet extends HttpServlet {
   }
 
   private void createNotImplementedResponse(final HttpServletRequest req, final MessageReference
messageReference,
-      final HttpServletResponse resp) throws IOException {
+      final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException
{
     // RFC 2616, 5.1.1: "An origin server SHOULD return the status code [...]
     // 501 (Not Implemented) if the method is unrecognized [...] by the origin server."
-    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req);
+    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
     ODataResponse response =
         exceptionWrapper.wrapInExceptionResponse(new ODataNotImplementedException(messageReference));
 //    resp.setStatus(HttpStatusCodes.NOT_IMPLEMENTED.getStatusCode());
@@ -266,24 +272,24 @@ public class ODataServlet extends HttpServlet {
   }
 
   private void createMethodNotAllowedResponse(final HttpServletRequest req, final MessageReference
messageReference,
-      final HttpServletResponse resp) throws IOException {
-    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req);
+      final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException
{
+    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
     ODataResponse response =
         exceptionWrapper.wrapInExceptionResponse(new ODataMethodNotAllowedException(messageReference));
     createResponse(resp, response);
   }
 
   private void createNotAcceptableResponse(final HttpServletRequest req, final MessageReference
messageReference,
-      final HttpServletResponse resp) throws IOException {
-    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req);
+      final HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException
{
+    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
     ODataResponse response =
         exceptionWrapper.wrapInExceptionResponse(new ODataNotAcceptableException(messageReference));
     createResponse(resp, response);
   }
 
   private void createServiceUnavailableResponse(HttpServletRequest req, MessageReference
messageReference,
-      HttpServletResponse resp) throws IOException {
-    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req);
+      HttpServletResponse resp, ODataServiceFactory serviceFactory) throws IOException {
+    ODataExceptionWrapper exceptionWrapper = new ODataExceptionWrapper(req, serviceFactory);
     ODataResponse response =
         exceptionWrapper.wrapInExceptionResponse(new ODataInternalServerErrorException(messageReference));
     createResponse(resp, response);

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/f40ea94c/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
index 1c74fc0..2f877d2 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/servlet/ODataServletTest.java
@@ -110,5 +110,4 @@ public class ODataServletTest {
     String factoryClassName = ODataServiceFactoryImpl.class.getName();
     Mockito.when(configMock.getInitParameter(ODataServiceFactory.FACTORY_LABEL)).thenReturn(factoryClassName);
   }
-
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/f40ea94c/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/NullServiceTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/NullServiceTest.java
b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/NullServiceTest.java
index 58b9aba..9f7ac08 100644
--- a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/NullServiceTest.java
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/NullServiceTest.java
@@ -48,7 +48,7 @@ public class NullServiceTest extends AbstractFitTest {
 
   @Test
   public void nullServiceMustResultInODataResponse() throws Exception {
-    System.out.println("The following internal Server Error is wanted if this test doesnt
fail!");
+    disableLogging();
     final HttpResponse response = executeGetRequest("$metadata");
     assertEquals(HttpStatusCodes.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusLine().getStatusCode());
 

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/f40ea94c/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitErrorCallback.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitErrorCallback.java
b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitErrorCallback.java
index fb8ff25..d63ff44 100644
--- a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitErrorCallback.java
+++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitErrorCallback.java
@@ -39,7 +39,6 @@ public class FitErrorCallback implements ODataErrorCallback {
     if (context.getHttpStatus() == HttpStatusCodes.INTERNAL_SERVER_ERROR) {
       LOG.error("Internal Server Error", context.getException());
     }
-
     return EntityProvider.writeErrorDocument(context);
   }
 


Mime
View raw message