You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2014/10/06 18:32:33 UTC

git commit: [OLINGO-339] ODataError for null service

Repository: olingo-odata2
Updated Branches:
  refs/heads/master 295165810 -> 2340c023d


[OLINGO-339] ODataError for null service


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

Branch: refs/heads/master
Commit: 2340c023da02512c574a17dd5954caf674f8dd4f
Parents: 2951658
Author: Christian Amend <ch...@apache.org>
Authored: Mon Oct 6 18:31:48 2014 +0200
Committer: Christian Amend <ch...@apache.org>
Committed: Mon Oct 6 18:31:48 2014 +0200

----------------------------------------------------------------------
 .../ODataInternalServerErrorException.java      | 46 +++++++++++++++++++
 .../odata2/core/rest/ODataSubLocator.java       | 18 +++++++-
 .../odata2/core/servlet/ODataServlet.java       | 11 +++++
 .../src/main/resources/i18n.properties          |  2 +
 .../src/main/resources/i18n_en.properties       |  2 +
 .../odata2/fit/basic/NullServiceTest.java       | 47 ++++++++++++++++++++
 .../testutil/fit/FitStaticServiceFactory.java   |  6 +--
 7 files changed, 127 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataInternalServerErrorException.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataInternalServerErrorException.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataInternalServerErrorException.java
new file mode 100644
index 0000000..49620f8
--- /dev/null
+++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataInternalServerErrorException.java
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * 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 org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+
+public class ODataInternalServerErrorException extends ODataHttpException {
+  private static final long serialVersionUID = 1L;
+
+  public static final MessageReference NOSERVICE = createMessageReference(ODataInternalServerErrorException.class,
+      "NOSERVICE");
+
+  public ODataInternalServerErrorException(final MessageReference messageReference) {
+    super(messageReference, HttpStatusCodes.INTERNAL_SERVER_ERROR);
+  }
+
+  public ODataInternalServerErrorException(final MessageReference messageReference, final String errorCode) {
+    super(messageReference, HttpStatusCodes.INTERNAL_SERVER_ERROR, errorCode);
+  }
+
+  public ODataInternalServerErrorException(final MessageReference messageReference, final Throwable cause) {
+    super(messageReference, cause, HttpStatusCodes.INTERNAL_SERVER_ERROR);
+  }
+
+  public ODataInternalServerErrorException(final MessageReference messageReference, final Throwable cause,
+      final String errorCode) {
+    super(messageReference, cause, HttpStatusCodes.INTERNAL_SERVER_ERROR, errorCode);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataSubLocator.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataSubLocator.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataSubLocator.java
index 7ab9ee1..5727b1d 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataSubLocator.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/rest/ODataSubLocator.java
@@ -34,6 +34,8 @@ import org.apache.olingo.odata2.api.ODataServiceFactory;
 import org.apache.olingo.odata2.api.commons.ODataHttpMethod;
 import org.apache.olingo.odata2.api.exception.MessageReference;
 import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.exception.ODataInternalServerErrorException;
+import org.apache.olingo.odata2.api.exception.ODataMessageException;
 import org.apache.olingo.odata2.api.exception.ODataNotImplementedException;
 import org.apache.olingo.odata2.api.processor.ODataContext;
 import org.apache.olingo.odata2.api.processor.ODataRequest;
@@ -110,6 +112,10 @@ public final class ODataSubLocator {
   private Response returnNotImplementedResponse(final MessageReference messageReference) {
     // 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."
+    return returnException(new ODataNotImplementedException(messageReference));
+  }
+
+  private Response returnException(final ODataMessageException messageException) {
     ODataContextImpl context = new ODataContextImpl(request, serviceFactory);
     context.setRequest(request);
     context.setAcceptableLanguages(request.getAcceptableLanguages());
@@ -119,9 +125,13 @@ public final class ODataSubLocator {
     ODataExceptionWrapper exceptionWrapper =
         new ODataExceptionWrapper(context, request.getQueryParameters(), request.getAcceptHeaders());
     ODataResponse response =
-        exceptionWrapper.wrapInExceptionResponse(new ODataNotImplementedException(messageReference));
+        exceptionWrapper.wrapInExceptionResponse(messageException);
     return RestUtil.convertResponse(response);
   }
+  
+  private Response returnNoServiceResponse(MessageReference messageReference) {
+    return returnException(new ODataInternalServerErrorException(messageReference));
+  }
 
   @OPTIONS
   public Response handleOptions() throws ODataException {
@@ -146,7 +156,9 @@ public final class ODataSubLocator {
     context.setParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT, httpRequest);
 
     ODataService service = serviceFactory.createService(context);
-
+    if(service == null){
+      return returnNoServiceResponse(ODataInternalServerErrorException.NOSERVICE);
+    }
     service.getProcessor().setContext(context);
     context.setService(service);
 
@@ -158,6 +170,8 @@ public final class ODataSubLocator {
     return response;
   }
 
+
+
   public static ODataSubLocator create(final SubLocatorParameter param) throws ODataException {
     ODataSubLocator subLocator = new ODataSubLocator();
 

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/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 1d4dfe3..2312170 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
@@ -35,6 +35,7 @@ import org.apache.olingo.odata2.api.exception.MessageReference;
 import org.apache.olingo.odata2.api.exception.ODataBadRequestException;
 import org.apache.olingo.odata2.api.exception.ODataException;
 import org.apache.olingo.odata2.api.exception.ODataHttpException;
+import org.apache.olingo.odata2.api.exception.ODataInternalServerErrorException;
 import org.apache.olingo.odata2.api.exception.ODataMethodNotAllowedException;
 import org.apache.olingo.odata2.api.exception.ODataNotAcceptableException;
 import org.apache.olingo.odata2.api.exception.ODataNotImplementedException;
@@ -174,6 +175,9 @@ public class ODataServlet extends HttpServlet {
       context.setParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT, req);
 
       ODataService service = serviceFactory.createService(context);
+      if(service == null){
+        createServiceUnavailableResponse(req, ODataInternalServerErrorException.NOSERVICE, resp);
+      }
       context.setService(service);
       service.getProcessor().setContext(context);
 
@@ -271,7 +275,14 @@ public class ODataServlet extends HttpServlet {
     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);
+    ODataResponse response =
+        exceptionWrapper.wrapInExceptionResponse(new ODataInternalServerErrorException(messageReference));
+    createResponse(resp, response);
   }
 
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/odata2-lib/odata-core/src/main/resources/i18n.properties
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/resources/i18n.properties b/odata2-lib/odata-core/src/main/resources/i18n.properties
index 90b7045..61b45d0 100644
--- a/odata2-lib/odata-core/src/main/resources/i18n.properties
+++ b/odata2-lib/odata-core/src/main/resources/i18n.properties
@@ -148,6 +148,8 @@ org.apache.olingo.odata2.api.batch.BatchException.TRUNCATED_BODY=Body is truncat
 ##################################
 org.apache.olingo.odata2.api.exception.ODataHttpException.COMMON=Common exception
 
+org.apache.olingo.odata2.api.exception.ODataInternalServerErrorException.NOSERVICE=Service unavailable.
+
 org.apache.olingo.odata2.api.exception.ODataBadRequestException.COMMON=Bad Request.
 org.apache.olingo.odata2.api.exception.ODataBadRequestException.NOTSUPPORTED=The request is not supported by the processor.
 org.apache.olingo.odata2.api.exception.ODataBadRequestException.INVALID_HEADER=The request contains an invalid header parameter '%1$s' with value '%2$s'.

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/odata2-lib/odata-core/src/main/resources/i18n_en.properties
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/resources/i18n_en.properties b/odata2-lib/odata-core/src/main/resources/i18n_en.properties
index 564dfef..7de9229 100644
--- a/odata2-lib/odata-core/src/main/resources/i18n_en.properties
+++ b/odata2-lib/odata-core/src/main/resources/i18n_en.properties
@@ -97,6 +97,8 @@ org.apache.olingo.odata2.api.ep.EntityProviderException.ILLEGAL_ARGUMENT=Illegal
 ##################################
 org.apache.olingo.odata2.api.exception.ODataHttpException.COMMON=Common exception
 
+org.apache.olingo.odata2.api.exception.ODataInternalServerErrorException.NOSERVICE=Service unavailable.
+
 org.apache.olingo.odata2.api.exception.ODataBadRequestException.COMMON=Bad Request.
 org.apache.olingo.odata2.api.exception.ODataBadRequestException.NOTSUPPORTED=The request is not supported by the processor.
 org.apache.olingo.odata2.api.exception.ODataBadRequestException.URLTOOSHORT=The URL is too short.

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/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
new file mode 100644
index 0000000..fddc9a9
--- /dev/null
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/NullServiceTest.java
@@ -0,0 +1,47 @@
+package org.apache.olingo.odata2.fit.basic;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.URI;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.olingo.odata2.api.ODataService;
+import org.apache.olingo.odata2.api.commons.HttpStatusCodes;
+import org.apache.olingo.odata2.api.ep.EntityProvider;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.processor.ODataErrorContext;
+import org.apache.olingo.odata2.testutil.fit.AbstractFitTest;
+import org.apache.olingo.odata2.testutil.server.ServletType;
+import org.junit.Test;
+
+public class NullServiceTest extends AbstractFitTest {
+
+  public NullServiceTest(final ServletType servletType) {
+    super(servletType);
+  }
+
+  @Override
+  protected ODataService createService() throws ODataException {
+    return null;
+  }
+
+  @Test
+  public void nullServiceMustResultInODataResponse() throws Exception {
+    System.out.println("The following internal Server Error is wanted if this test doesnt fail!");
+    final HttpResponse response = executeGetRequest("$metadata");
+    assertEquals(HttpStatusCodes.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatusLine().getStatusCode());
+
+    
+    ODataErrorContext error = EntityProvider.readErrorDocument(response.getEntity().getContent(), "application/xml");
+    assertEquals("Service unavailable.", error.getMessage());
+  }
+  
+  private HttpResponse executeGetRequest(final String request) throws ClientProtocolException, IOException {
+    final HttpGet get = new HttpGet(URI.create(getEndpoint().toString() + request));
+    return getHttpClient().execute(get);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/2340c023/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitStaticServiceFactory.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitStaticServiceFactory.java b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitStaticServiceFactory.java
index 8fcf979..0a86faf 100644
--- a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitStaticServiceFactory.java
+++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/FitStaticServiceFactory.java
@@ -83,9 +83,9 @@ public class FitStaticServiceFactory extends ODataServiceFactory {
     // access and validation in synchronized block
     synchronized (PORT_2_SERVICE) {
       final ODataService service = PORT_2_SERVICE.get(port);
-      if (service == null) {
-        throw new IllegalArgumentException("no static service set for JUnit test");
-      }
+//      if (service == null) {
+//        throw new IllegalArgumentException("no static service set for JUnit test");
+//      }
       return service;
     }
   }