added rest exception framework

This commit is contained in:
jomu
2018-01-08 23:48:55 +00:00
parent fedba989e0
commit fdd7b83468
15 changed files with 676 additions and 0 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.core.Response;
/**
*
* @author jomu
*/
public interface APIError {
Response.Status getStatus();
String getErrorCode();
String getMessageKey();
}

View File

@ -0,0 +1,83 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
/**
*
* @author jomu
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"status", "errorCode", "message"})
public class APIErrorResponse {
@XmlJavaTypeAdapter(ResponseStatusAdapter.class)
private Response.Status status;
private String errorCode;
private String message;
public APIErrorResponse() {
}
public APIErrorResponse(APIError apiError, Locale locale) {
this.status = apiError.getStatus();
this.errorCode = apiError.getErrorCode();
this.message = getLocalizedMessage(apiError, locale);
}
public APIErrorResponse(Exception exception, Locale locale) {
this.status = Response.Status.INTERNAL_SERVER_ERROR;
this.errorCode = "0";
this.message = exception.getLocalizedMessage();
}
public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) {
this.status = status;
this.errorCode = errorCode;
this.message = getLocalizedMessage(messageKey, locale);
}
public String getErrorCode() {
return this.errorCode;
}
public Response.Status getStatus() {
return status;
}
public String getMessage() {
return this.message;
}
private String getLocalizedMessage(APIError apiError, Locale locale) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale);
return resourceBundle.getString(apiError.getMessageKey());
}
private String getLocalizedMessage(String messageKey, Locale locale) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getName(), locale);
return resourceBundle.getString(messageKey);
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale;
import javax.ws.rs.core.Response;
/**
*
* @author jomu
*/
public class APIException extends RuntimeException {
public static final String HTTP_HEADER_X_ERROR = "X-Error";
public static final String HTTP_HEADER_X_ERROR_CODE = "X-Error-Code";
private final Response httpResponse;
public APIException(APIError apiError, Locale locale) {
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale));
}
public APIException(Exception exception, Locale locale) {
httpResponse = createHttpResponse(new APIErrorResponse(exception, locale));
}
public Response getHttpResponse() {
return httpResponse;
}
private static Response createHttpResponse(APIErrorResponse response) {
return Response.status(response.getStatus()).entity(response)
.header(HTTP_HEADER_X_ERROR, response.getMessage())
.header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode()).build();
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Response;
/**
*
* @author jomu
*/
public class APIExceptionInterceptor {
@Inject
Locale locale;
@AroundInvoke
public Object handleException(InvocationContext context) {
Object proceedResponse;
try {
// continue to execute context
// if an exception is thrown during processing, this is passed in to the catch block below
proceedResponse = context.proceed();
} catch (Exception ex) {
Response errorResponse;
if (ex instanceof APIException) {
errorResponse = ((APIException) ex).getHttpResponse();
} else if (ex.getCause() instanceof APIException) {
errorResponse = ((APIException) ex.getCause()).getHttpResponse();
// } else if (ex instanceof ConstraintViolationException) {
// this exception is handled via the ConstraintViolationMapper
// throw (ConstraintViolationException) ex;
} else if (ex.getCause() instanceof ConstraintViolationException) {
// this exception is handled via the ConstraintViolationMapper
throw (ConstraintViolationException) ex.getCause();
} else {
errorResponse = new APIException(ex, locale).getHttpResponse();
}
return errorResponse;
}
return proceedResponse;
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class BadRequestMapper implements ExceptionMapper<BadRequestException> {
@Override
public Response toResponse(BadRequestException ex) {
return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,64 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import java.util.Iterator;
import javax.validation.ConstraintViolation;
import javax.validation.Path;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author jomu
*/
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ConstraintViolationEntry {
private String fieldName;
private String wrongValue;
private String errorMessage;
public ConstraintViolationEntry() {
}
public ConstraintViolationEntry(ConstraintViolation violation) {
Iterator<Path.Node> iterator = violation.getPropertyPath().iterator();
Path.Node currentNode = iterator.next();
String invalidValue = "";
if (violation.getInvalidValue() != null) {
invalidValue = violation.getInvalidValue().toString();
}
this.fieldName = currentNode.getName();
this.wrongValue = invalidValue;
this.errorMessage = violation.getMessage();
}
public String getFieldName() {
return fieldName;
}
public String getWrongValue() {
return wrongValue;
}
public String getErrorMessage() {
return errorMessage;
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Variant;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author jomu
*/
@Provider
public class ConstraintViolationMapper implements ExceptionMapper<ConstraintViolationException> {
private static List<Variant> acceptableMediaTypes = Variant.mediaTypes(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).build();
@Context
protected Request request;
@Override
public Response toResponse(ConstraintViolationException ex) {
Set<ConstraintViolation<?>> constViolations = ex.getConstraintViolations();
List<ConstraintViolationEntry> errorList = new ArrayList<>();
for (ConstraintViolation<?> constraintViolation : constViolations) {
errorList.add(new ConstraintViolationEntry(constraintViolation));
}
GenericEntity<List<ConstraintViolationEntry>> entity = new GenericEntity<List<ConstraintViolationEntry>>(errorList) {};
return Response.status(Response.Status.BAD_REQUEST).entity(entity).type(getNegotiatedMediaType()).build();
}
protected MediaType getNegotiatedMediaType() {
final Variant selectedMediaType = request.selectVariant(acceptableMediaTypes);
if (selectedMediaType == null) {
return MediaType.APPLICATION_JSON_TYPE;
}
return selectedMediaType.getMediaType();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class ForbiddenMapper implements ExceptionMapper<ForbiddenException> {
@Override
public Response toResponse(ForbiddenException ex) {
return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class NotAcceptableMapper implements ExceptionMapper<NotAcceptableException> {
@Override
public Response toResponse(NotAcceptableException ex) {
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotAllowedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class NotAllowedMapper implements ExceptionMapper<NotAllowedException> {
@Override
public Response toResponse(NotAllowedException ex) {
return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class NotAuthorizedMapper implements ExceptionMapper<NotAuthorizedException> {
@Override
public Response toResponse(NotAuthorizedException ex) {
return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class NotFoundMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException ex) {
return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright 2018 joern (at) muehlencord.de
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotSupportedException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
*
* @author Joern Muehlencord (joern (at) muehlencord.de
*/
@Provider
public class NotSupportMapper implements ExceptionMapper<NotSupportedException> {
@Override
public Response toResponse(NotSupportedException ex) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(ex.getMessage()).build();
}
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.adapters.XmlAdapter;
/**
*
* @author jomu
*/
public class ResponseStatusAdapter extends XmlAdapter<String, Response.Status> {
@Override
public String marshal(Response.Status status) throws Exception {
return status.name();
}
@Override
public Response.Status unmarshal(String statusAsString) throws Exception {
return Response.Status.valueOf(statusAsString);
}
}

View File

@ -0,0 +1,40 @@
/*
* Copyright 2018 jomu.
*
* Licensed 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 de.muehlencord.shared.jeeutil.restexfw;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
/**
*
* @author jomu
*/
public class ValidationController {
public static <T> void processBeanValidation(T entity) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> errors = validator.validate(entity);
if (!errors.isEmpty()) {
throw new ConstraintViolationException(errors);
}
}
}