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 2015/12/17 15:33:03 UTC
olingo-odata2 git commit: [OLINGO-839] Prevent wrong status code for
invalid URL encoding
Repository: olingo-odata2
Updated Branches:
refs/heads/master c6631df2b -> 4facec58b
[OLINGO-839] Prevent wrong status code for invalid URL encoding
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/4facec58
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/4facec58
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/4facec58
Branch: refs/heads/master
Commit: 4facec58b7f536501d0078908877979113874568
Parents: c6631df
Author: Christian Amend <ch...@sap.com>
Authored: Thu Dec 17 15:28:00 2015 +0100
Committer: Christian Amend <ch...@sap.com>
Committed: Thu Dec 17 15:28:00 2015 +0100
----------------------------------------------------------------------
.../api/exception/ODataBadRequestException.java | 5 +++-
.../odata2/api/processor/ODataErrorContext.java | 2 +-
.../odata2/core/rest/ODataExceptionWrapper.java | 30 +++++++++++++++-----
.../core/servlet/ODataExceptionWrapper.java | 13 ++++++---
.../odata2/core/servlet/ODataServlet.java | 23 +++++++++------
.../src/main/resources/i18n.properties | 1 +
.../odata2/fit/basic/AbstractBasicTest.java | 2 +-
.../odata2/fit/basic/ErrorResponseTest.java | 2 +-
.../fit/basic/HttpExceptionResponseTest.java | 2 +-
9 files changed, 55 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataBadRequestException.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataBadRequestException.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataBadRequestException.java
index 9cb0503..9933b5c 100644
--- a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataBadRequestException.java
+++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/exception/ODataBadRequestException.java
@@ -45,7 +45,10 @@ public class ODataBadRequestException extends ODataHttpException {
"INVALID_HEADER");
/** INVALID_SYNTAX requires NO content values */
public static final MessageReference INVALID_SYNTAX = createMessageReference(ODataBadRequestException.class,
- "INVALID_SYNTAX");;
+ "INVALID_SYNTAX");
+ /** INVALID_REQUEST requires NO content values */
+ public static final MessageReference INVALID_REQUEST = createMessageReference(ODataBadRequestException.class,
+ "INVALID_REQUEST");
public ODataBadRequestException(final MessageReference messageReference) {
super(messageReference, HttpStatusCodes.BAD_REQUEST);
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/processor/ODataErrorContext.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/processor/ODataErrorContext.java b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/processor/ODataErrorContext.java
index 6ae011b..70527be 100644
--- a/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/processor/ODataErrorContext.java
+++ b/odata2-lib/odata-api/src/main/java/org/apache/olingo/odata2/api/processor/ODataErrorContext.java
@@ -185,7 +185,7 @@ public class ODataErrorContext {
}
/**
- * Get the request uri to be used in a error response.
+ * Get the request uri to be used in an error response. Might be null in case the URI was the cause of the exception.
* @return a uri object
*/
public URI getRequestUri() {
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/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 3eb8c98..30efb9c 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
@@ -62,12 +62,12 @@ public class ODataExceptionWrapper {
private static final String DOLLAR_FORMAT_JSON = "json";
private static final Locale DEFAULT_RESPONSE_LOCALE = Locale.ENGLISH;
- private final String contentType;
+ private String contentType;
+ private URI requestUri;
private final Locale messageLocale;
private final Map<String, List<String>> httpRequestHeaders;
private final ODataErrorCallback callback;
private final ODataErrorContext errorContext = new ODataErrorContext();
- private final URI requestUri;
public ODataExceptionWrapper(final ODataContext context, final Map<String, String> queryParameters,
final List<String> acceptHeaderContentTypes) {
@@ -85,10 +85,15 @@ public class ODataExceptionWrapper {
public ODataExceptionWrapper(final UriInfo uriInfo, final HttpHeaders httpHeaders,
final ODataErrorCallback errorCallback) {
- contentType = getContentType(uriInfo, httpHeaders).toContentTypeString();
+ try {
+ contentType = getContentType(uriInfo, httpHeaders).toContentTypeString();
+ requestUri = uriInfo.getRequestUri();
+ } catch (IllegalArgumentException e) {
+ contentType = null;
+ requestUri = null;
+ }
messageLocale = MessageService.getSupportedLocale(getLanguages(httpHeaders), DEFAULT_RESPONSE_LOCALE);
httpRequestHeaders = httpHeaders.getRequestHeaders();
- requestUri = uriInfo.getRequestUri();
callback = errorCallback;
}
@@ -168,13 +173,24 @@ public class ODataExceptionWrapper {
* @param exception exception with values to be set on error context
*/
private void fillErrorContext(final Exception exception) {
- errorContext.setContentType(contentType);
+ if (contentType != null || requestUri != null) {
+ errorContext.setContentType(contentType);
+ errorContext.setRequestUri(requestUri);
+ errorContext.setHttpStatus(HttpStatusCodes.INTERNAL_SERVER_ERROR);
+ } else {
+ /*
+ * We have to add this here in case CXF decides that the URL is invalid. In this case we have to give the correct
+ * response code nonetheless. Since we get called without context we have to try and guess here.
+ * This should be the case when either the content type or the request URI are null.
+ */
+ errorContext.setContentType(ContentType.APPLICATION_ATOM_XML.toContentTypeString());
+ errorContext.setRequestUri(null);
+ errorContext.setHttpStatus(HttpStatusCodes.BAD_REQUEST);
+ }
errorContext.setException(exception);
- errorContext.setHttpStatus(HttpStatusCodes.INTERNAL_SERVER_ERROR);
errorContext.setErrorCode(null);
errorContext.setMessage(exception.getMessage());
errorContext.setLocale(DEFAULT_RESPONSE_LOCALE);
- errorContext.setRequestUri(requestUri);
if (httpRequestHeaders != null) {
for (Entry<String, List<String>> entry : httpRequestHeaders.entrySet()) {
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/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 e2069d4..9a87505 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
@@ -21,6 +21,7 @@ package org.apache.olingo.odata2.core.servlet;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -45,7 +46,6 @@ 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;
/**
*
@@ -58,18 +58,23 @@ public class ODataExceptionWrapper {
private final ODataErrorContext errorContext = new ODataErrorContext();
private final String contentType;
private final Locale messageLocale;
- private final URI requestUri;
private final Map<String, List<String>> httpRequestHeaders;
private final ODataErrorCallback callback;
+ private URI requestUri;
public ODataExceptionWrapper(final HttpServletRequest req, ODataServiceFactory serviceFactory) {
try {
requestUri = new URI(req.getRequestURI());
} catch (URISyntaxException e) {
- throw new ODataRuntimeException(e);
+ requestUri = null;
}
httpRequestHeaders = RestUtil.extractHeaders(req);
- Map<String, String> queryParameters = RestUtil.extractQueryParameters(req.getQueryString());
+ Map<String, String> queryParameters;
+ try {
+ queryParameters = RestUtil.extractQueryParameters(req.getQueryString());
+ } catch (Exception e) {
+ queryParameters = new HashMap<String, String>();
+ }
List<Locale> acceptableLanguages = RestUtil.extractAcceptableLanguage(req.getHeader("Accept-Language"));
List<String> acceptHeaders = RestUtil.extractAcceptHeaders(req.getHeader("Accept"));
contentType = getContentType(queryParameters, acceptHeaders).toContentTypeString();
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/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 e83f46f..764380b 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
@@ -181,15 +181,20 @@ public class ODataServlet extends HttpServlet {
createNotAcceptableResponse(req, ODataNotAcceptableException.COMMON, resp, serviceFactory);
return;
}
- ODataRequest odataRequest = ODataRequest.method(method)
- .contentType(RestUtil.extractRequestContentType(req.getContentType()).toContentTypeString())
- .acceptHeaders(RestUtil.extractAcceptHeaders(req.getHeader(HttpHeaders.ACCEPT)))
- .acceptableLanguages(RestUtil.extractAcceptableLanguage(req.getHeader(HttpHeaders.ACCEPT_LANGUAGE)))
- .pathInfo(RestUtil.buildODataPathInfo(req, pathSplit))
- .allQueryParameters(RestUtil.extractAllQueryParameters(req.getQueryString()))
- .requestHeaders(RestUtil.extractHeaders(req))
- .body(req.getInputStream())
- .build();
+ ODataRequest odataRequest = null;
+ try {
+ odataRequest = ODataRequest.method(method)
+ .contentType(RestUtil.extractRequestContentType(req.getContentType()).toContentTypeString())
+ .acceptHeaders(RestUtil.extractAcceptHeaders(req.getHeader(HttpHeaders.ACCEPT)))
+ .acceptableLanguages(RestUtil.extractAcceptableLanguage(req.getHeader(HttpHeaders.ACCEPT_LANGUAGE)))
+ .pathInfo(RestUtil.buildODataPathInfo(req, pathSplit))
+ .allQueryParameters(RestUtil.extractAllQueryParameters(req.getQueryString()))
+ .requestHeaders(RestUtil.extractHeaders(req))
+ .body(req.getInputStream())
+ .build();
+ } catch (IllegalArgumentException e) {
+ throw new ODataBadRequestException(ODataBadRequestException.INVALID_REQUEST, e);
+ }
ODataContextImpl context = new ODataContextImpl(odataRequest, serviceFactory);
context.setParameter(ODataContext.HTTP_SERVLET_REQUEST_OBJECT, req);
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/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 2adda81..ff227a9 100644
--- a/odata2-lib/odata-core/src/main/resources/i18n.properties
+++ b/odata2-lib/odata-core/src/main/resources/i18n.properties
@@ -154,6 +154,7 @@ org.apache.olingo.odata2.api.exception.ODataBadRequestException.COMMON=Bad Reque
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'.
org.apache.olingo.odata2.api.exception.ODataBadRequestException.INVALID_SYNTAX=The Data Services Request could not be understood due to malformed syntax.
+org.apache.olingo.odata2.api.exception.ODataBadRequestException.INVALID_REQUEST=The Request can`t be parsed either because the URL HTTP syntax is incorrect or the body stream could not be read.
org.apache.olingo.odata2.api.exception.ODataBadRequestException.URLTOOSHORT=The URL is too short.
org.apache.olingo.odata2.api.exception.ODataBadRequestException.VERSIONERROR=The Data Services Request version '%1$s' is not supported for the request payload.
org.apache.olingo.odata2.api.exception.ODataBadRequestException.PARSEVERSIONERROR=The Data Services Request version '%1$s' cannot be parsed.
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/AbstractBasicTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/AbstractBasicTest.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/AbstractBasicTest.java
index 4e96b84..d4dac10 100644
--- a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/AbstractBasicTest.java
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/AbstractBasicTest.java
@@ -77,7 +77,7 @@ public abstract class AbstractBasicTest extends AbstractFitTest {
return new ODataSingleProcessorService(provider, processor) {};
}
- EdmProvider createEdmProvider() {
+ protected EdmProvider createEdmProvider() {
return mock(EdmProvider.class);
}
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/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 c11b961..0739de8 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
@@ -61,7 +61,7 @@ public class ErrorResponseTest extends AbstractBasicTest {
return processor;
}
-
+
@Test
public void test500RuntimeError() throws ClientProtocolException, IOException, ODataException {
disableLogging();
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/4facec58/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/HttpExceptionResponseTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/HttpExceptionResponseTest.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/HttpExceptionResponseTest.java
index 1bb8f69..780d142 100644
--- a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/HttpExceptionResponseTest.java
+++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/HttpExceptionResponseTest.java
@@ -74,7 +74,7 @@ public class HttpExceptionResponseTest extends AbstractBasicTest {
}
@Override
- EdmProvider createEdmProvider() {
+ protected EdmProvider createEdmProvider() {
final EdmProvider provider = new ScenarioEdmProvider();
return provider;
}