From 788036db25637f9872c55346df208d4a0d4b93d4 Mon Sep 17 00:00:00 2001 From: Klaus Straubinger Date: Thu, 25 Sep 2014 13:47:38 +0200 Subject: [PATCH] better server error messages in case of $format errors Change-Id: If4495f5c91086618ee7d3824339aa5e7b99ff76c Signed-off-by: Christian Amend --- .../olingo/server/core/ODataExceptionHelper.java | 9 ++++----- .../apache/olingo/server/core/uri/parser/Parser.java | 3 +-- .../core/uri/parser/UriParserSyntaxException.java | 2 +- .../resources/server-core-exceptions-i18n.properties | 2 +- .../server/core/uri/antlr/TestFullResourcePath.java | 10 +++++----- 5 files changed, 12 insertions(+), 14 deletions(-) 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 index d0f248fc3..9b0ddfd9e 100644 --- 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 @@ -52,11 +52,10 @@ public class ODataExceptionHelper { public static ODataServerError createServerErrorObject(UriParserSyntaxException e, Locale requestedLocale) { ODataServerError serverError = basicTranslatedError(e, requestedLocale); - if(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE.equals(e.getMessageKey())){ - serverError.setStatusCode(HttpStatusCode.NOT_ACCEPTABLE.getStatusCode()); - }else{ - serverError.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); - } + serverError.setStatusCode( + UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT.equals(e.getMessageKey()) ? + HttpStatusCode.NOT_ACCEPTABLE.getStatusCode() : + HttpStatusCode.BAD_REQUEST.getStatusCode()); return serverError; } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java index 016aeb98d..c2be4388f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/Parser.java @@ -188,8 +188,7 @@ public class Parser { formatOption.setFormat(option.value); } else { throw new UriParserSyntaxException("Illegal value of $format option!", - UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE, - option.name, option.value); + UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT, option.value); } context.contextUriInfo.setSystemQueryOption(formatOption); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java index e1506ee53..454ed3bfb 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParserSyntaxException.java @@ -28,7 +28,7 @@ public class UriParserSyntaxException extends UriParserException { /** parameters: query-option name, query-option value */ WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION, SYNTAX, SYSTEM_QUERY_OPTION_LEVELS_NOT_ALLOWED_HERE, - /** parameter: query-option value */ WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE; + /** parameter: $format option value */ WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT; @Override public String getKey() { diff --git a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties index 4695c73b2..57e805cc8 100644 --- a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties +++ b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties @@ -26,7 +26,7 @@ ODataHandlerException.ODATA_VERSION_NOT_SUPPORTED=OData version '%1$s' is not su UriParserSyntaxException.UNKNOWN_SYSTEM_QUERY_OPTION=The system query option '%1$s' is not defined. UriParserSyntaxException.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION=The system query option '%1$s' has the not-allowed value '%2$s'. -UriParserSyntaxException.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE=The system query option $value must be either json, xml, atom or a valid content-type. The value '%1$s' is neither. +UriParserSyntaxException.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT=The system query option '$format' must be either 'json', 'xml', 'atom', or a valid content type; the value '%1$s' is neither. UriParserSyntaxException.SYNTAX=The URI is malformed. UriParserSyntaxException.SYSTEM_QUERY_OPTION_LEVELS_NOT_ALLOWED_HERE=The system query option '$levels' is not allowed here. diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java index 5d7325b79..856be6ba5 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java @@ -2553,15 +2553,15 @@ public class TestFullResourcePath { .isKind(UriInfoKind.resource).goPath() .isFormatText(HttpContentType.APPLICATION_ATOM_XML_ENTRY_UTF8); testUri.runEx("ESKeyNav(1)?$format=noSlash") - .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE); + .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT); testUri.runEx("ESKeyNav(1)?$format=slashAtEnd/") - .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE); + .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT); testUri.runEx("ESKeyNav(1)?$format=/startsWithSlash") - .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE); + .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT); testUri.runEx("ESKeyNav(1)?$format=two/Slashes/tooMuch") - .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE); + .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT); testUri.runEx("ESKeyNav(1)?$format=") - .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_VALUE); + .isExSyntax(UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT); } @Test