[OLINGO-663] Add new exceptiontext and right status code

This commit is contained in:
Christian Amend 2015-05-28 11:02:18 +02:00
parent 29b169def4
commit ea89f7213e
7 changed files with 25 additions and 15 deletions

View File

@ -19,7 +19,7 @@
package org.apache.olingo.server.api; package org.apache.olingo.server.api;
/** /**
* <p>Processors that would like to support concurrency control for certain entity sets can implement this * <p>Processors that would like to support etags for certain entity sets can implement this
* interface.</p> * interface.</p>
* <p>If implemented this interface can be registered at the ODataHttpHandler. This will result in change request to * <p>If implemented this interface can be registered at the ODataHttpHandler. This will result in change request to
* require an if-match/if-none-match or an if-modified-since/if-unmodified-since header. Otherwise the request will * require an if-match/if-none-match or an if-modified-since/if-unmodified-since header. Otherwise the request will

View File

@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the * to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance * "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at * with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@ -54,12 +54,12 @@ public class ODataExceptionHelper {
} }
public static ODataServerError public static ODataServerError
createServerErrorObject(final UriParserSyntaxException e, final Locale requestedLocale) { createServerErrorObject(final UriParserSyntaxException e, final Locale requestedLocale) {
ODataServerError serverError = basicTranslatedError(e, requestedLocale); ODataServerError serverError = basicTranslatedError(e, requestedLocale);
serverError.setStatusCode( serverError.setStatusCode(
UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT.equals(e.getMessageKey()) ? UriParserSyntaxException.MessageKeys.WRONG_VALUE_FOR_SYSTEM_QUERY_OPTION_FORMAT.equals(e.getMessageKey()) ?
HttpStatusCode.NOT_ACCEPTABLE.getStatusCode() : HttpStatusCode.NOT_ACCEPTABLE.getStatusCode() :
HttpStatusCode.BAD_REQUEST.getStatusCode()); HttpStatusCode.BAD_REQUEST.getStatusCode());
return serverError; return serverError;
} }
@ -103,8 +103,14 @@ public class ODataExceptionHelper {
.setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode()); .setStatusCode(HttpStatusCode.BAD_REQUEST.getStatusCode());
} }
public static ODataServerError createServerErrorObject(final PreconditionRequiredException e,
final Locale requestedLocale) {
return basicTranslatedError(e, requestedLocale)
.setStatusCode(HttpStatusCode.PRECONDITION_REQUIRED.getStatusCode());
}
public static ODataServerError public static ODataServerError
createServerErrorObject(final ODataTranslatedException e, final Locale requestedLocale) { createServerErrorObject(final ODataTranslatedException e, final Locale requestedLocale) {
return basicTranslatedError(e, requestedLocale); return basicTranslatedError(e, requestedLocale);
} }

View File

@ -93,7 +93,10 @@ public class ODataHandler {
} catch (DeserializerException e) { } catch (DeserializerException e) {
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
handleException(request, response, serverError); handleException(request, response, serverError);
} catch (ODataHandlerException e) { } catch (PreconditionRequiredException e) {
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
handleException(request, response, serverError);
}catch (ODataHandlerException e) {
ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null);
handleException(request, response, serverError); handleException(request, response, serverError);
} catch (ODataApplicationException e) { } catch (ODataApplicationException e) {

View File

@ -24,8 +24,7 @@ public class PreconditionRequiredException extends ODataTranslatedException {
private static final long serialVersionUID = -8112658467394158700L; private static final long serialVersionUID = -8112658467394158700L;
public static enum MessageKeys implements MessageKey { public static enum MessageKeys implements MessageKey {
/** parameter: required header */ MISSING_HEADER;
PRECONDITION_REQUIRED;
@Override @Override
public String getKey() { public String getKey() {

View File

@ -60,7 +60,7 @@ public class PreconditionsValidator {
private void checkETagHeaderPresent() throws PreconditionRequiredException { private void checkETagHeaderPresent() throws PreconditionRequiredException {
if (ifMatch == null && ifNoneMatch == null) { if (ifMatch == null && ifNoneMatch == null) {
throw new PreconditionRequiredException("Expected an if-match or if-none-match header", throw new PreconditionRequiredException("Expected an if-match or if-none-match header",
PreconditionRequiredException.MessageKeys.PRECONDITION_REQUIRED); PreconditionRequiredException.MessageKeys.MISSING_HEADER);
} }
} }

View File

@ -142,4 +142,6 @@ BatchDeserializerException.MISSING_CONTENT_TYPE=Missing content-type at line '%1
BatchDeserializerException.MISSING_MANDATORY_HEADER=Missing mandatory header at line '%1$s'. BatchDeserializerException.MISSING_MANDATORY_HEADER=Missing mandatory header at line '%1$s'.
BatchDeserializerException.INVALID_BASE_URI=The base URI do not match the service base URI at line '%1$s'. BatchDeserializerException.INVALID_BASE_URI=The base URI do not match the service base URI at line '%1$s'.
BatchSerializerExecption.MISSING_CONTENT_ID=Each request within a change set required exactly one content id. BatchSerializerExecption.MISSING_CONTENT_ID=Each request within a change set required exactly one content id.
PreconditionRequiredException.MISSING_HEADER=The Operation you requested on this Entity requires an if-match or if-none-match header.

View File

@ -69,7 +69,7 @@ public class PreconditionsValidatorTest {
new PreconditionsValidator(new ETagSupport(), uriInfo, null, null).validatePreconditions(false); new PreconditionsValidator(new ETagSupport(), uriInfo, null, null).validatePreconditions(false);
fail("Expected a PreconditionRequiredException but was not thrown"); fail("Expected a PreconditionRequiredException but was not thrown");
} catch (PreconditionRequiredException e) { } catch (PreconditionRequiredException e) {
assertEquals(PreconditionRequiredException.MessageKeys.PRECONDITION_REQUIRED, e.getMessageKey()); assertEquals(PreconditionRequiredException.MessageKeys.MISSING_HEADER, e.getMessageKey());
} }
} }
@ -81,7 +81,7 @@ public class PreconditionsValidatorTest {
new PreconditionsValidator(new ETagSupport("ESKeyNav"), uriInfo, null, null).validatePreconditions(false); new PreconditionsValidator(new ETagSupport("ESKeyNav"), uriInfo, null, null).validatePreconditions(false);
fail("Expected a PreconditionRequiredException but was not thrown"); fail("Expected a PreconditionRequiredException but was not thrown");
} catch (PreconditionRequiredException e) { } catch (PreconditionRequiredException e) {
assertEquals(PreconditionRequiredException.MessageKeys.PRECONDITION_REQUIRED, e.getMessageKey()); assertEquals(PreconditionRequiredException.MessageKeys.MISSING_HEADER, e.getMessageKey());
} }
} }
@ -92,7 +92,7 @@ public class PreconditionsValidatorTest {
new PreconditionsValidator(new ETagSupport(), uriInfo, null, null).validatePreconditions(true); new PreconditionsValidator(new ETagSupport(), uriInfo, null, null).validatePreconditions(true);
fail("Expected a PreconditionRequiredException but was not thrown"); fail("Expected a PreconditionRequiredException but was not thrown");
} catch (PreconditionRequiredException e) { } catch (PreconditionRequiredException e) {
assertEquals(PreconditionRequiredException.MessageKeys.PRECONDITION_REQUIRED, e.getMessageKey()); assertEquals(PreconditionRequiredException.MessageKeys.MISSING_HEADER, e.getMessageKey());
} }
} }
@ -103,7 +103,7 @@ public class PreconditionsValidatorTest {
new PreconditionsValidator(new ETagSupport(), uriInfo, null, null).validatePreconditions(false); new PreconditionsValidator(new ETagSupport(), uriInfo, null, null).validatePreconditions(false);
fail("Expected a PreconditionRequiredException but was not thrown"); fail("Expected a PreconditionRequiredException but was not thrown");
} catch (PreconditionRequiredException e) { } catch (PreconditionRequiredException e) {
assertEquals(PreconditionRequiredException.MessageKeys.PRECONDITION_REQUIRED, e.getMessageKey()); assertEquals(PreconditionRequiredException.MessageKeys.MISSING_HEADER, e.getMessageKey());
} }
} }