-
Notifications
You must be signed in to change notification settings - Fork 5
Jax-RS 2.1 #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
trajano
wants to merge
19
commits into
master
Choose a base branch
from
jaxrs-2.1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Jax-RS 2.1 #51
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f95774b
(update) spring boot 2.0.0.RELEASE
trajano 7bea62e
(fix) use setWebApplicationType
trajano bd91f7c
Port to RestEasy 4.0.0.Beta1 and JAX-RS 2.1
trajano e4ed889
Excluded JBoss version of 2.1 spec
trajano 6bac886
Implemented register methods
trajano 4f1e965
Corrected docker configuration and test cases
trajano e54ffcd
Extracted messages to resource bundle
trajano 8f3b37f
Updated host for docker
trajano 477d32e
Expanded exception message for collator
trajano 395a493
(upgrade) RestEasy 4.0.0.Beta2
trajano 54cde13
(fix) dependencies added and updated
trajano a48e180
(chore) fix deprecations
trajano 99b3424
(new) Bean Validation and Exception Mapper logging
trajano 2b071e6
Skip validation on context unless Valid
trajano d00e7a9
(new) Bean Validation 2.0
trajano 40e3291
(upgrade) jose4j 0.6.3
trajano db5cd39
Added NotNull check
trajano 0b1fbdb
(upd) spring-boot 2.0.2.RELEASE
trajano 76b17f6
(chore) removed override relied on managed version
trajano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
ms-common-impl/src/main/java/net/trajano/ms/vertx/beans/BeanValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| package net.trajano.ms.vertx.beans; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.lang.reflect.Parameter; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import javax.validation.ConstraintViolation; | ||
| import javax.validation.ConstraintViolationException; | ||
| import javax.validation.Valid; | ||
| import javax.validation.Validator; | ||
| import javax.validation.constraints.NotNull; | ||
| import javax.ws.rs.BadRequestException; | ||
| import javax.ws.rs.FormParam; | ||
| import javax.ws.rs.HeaderParam; | ||
| import javax.ws.rs.InternalServerErrorException; | ||
| import javax.ws.rs.PathParam; | ||
| import javax.ws.rs.QueryParam; | ||
| import javax.ws.rs.core.Context; | ||
| import javax.ws.rs.core.Response; | ||
| import javax.ws.rs.core.Response.Status; | ||
|
|
||
| import org.jboss.resteasy.spi.HttpRequest; | ||
| import org.jboss.resteasy.spi.validation.GeneralValidator; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import net.trajano.ms.core.ConstraintViolationResponse; | ||
| import net.trajano.ms.core.ErrorResponses; | ||
|
|
||
| /** | ||
| * This performs validation checks on the request message. If the request | ||
| * message does not pass validation it will throw a bad request exception. | ||
| * | ||
| * @author Archimedes Trajano | ||
| */ | ||
| @Component | ||
| public class BeanValidator implements | ||
| GeneralValidator { | ||
|
|
||
| /** | ||
| * Logger. | ||
| */ | ||
| private static final Logger LOG = LoggerFactory.getLogger(BeanValidator.class); | ||
|
|
||
| @Autowired | ||
| private Validator validator; | ||
|
|
||
| @Override | ||
| public void checkViolations(final HttpRequest request) { | ||
|
|
||
| // does nothing | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Obtain the name from JAX-RS annotations. If the parameter is not annotated is | ||
| * uses "body" as the name. | ||
| * | ||
| * @param parameter | ||
| * parameter | ||
| * @return parameter name | ||
| */ | ||
| private String getParameterName(final Parameter parameter) { | ||
|
|
||
| final String name; | ||
| if (parameter.getAnnotation(FormParam.class) != null) { | ||
| name = "form." + parameter.getAnnotation(FormParam.class).value(); | ||
| } else if (parameter.getAnnotation(QueryParam.class) != null) { | ||
| name = "query." + parameter.getAnnotation(QueryParam.class).value(); | ||
| } else if (parameter.getAnnotation(PathParam.class) != null) { | ||
| name = "path." + parameter.getAnnotation(PathParam.class).value(); | ||
| } else if (parameter.getAnnotation(HeaderParam.class) != null) { | ||
| name = "header." + parameter.getAnnotation(HeaderParam.class).value(); | ||
| } else if (parameter.isAnnotationPresent(Context.class)) { | ||
| name = parameter.getType().getName(); | ||
| } else { | ||
| name = "body"; | ||
| } | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isMethodValidatable(final Method method) { | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isValidatable(final Class<?> clazz) { | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void validate(final HttpRequest request, | ||
| final Object object, | ||
| final Class<?>... groups) { | ||
|
|
||
| LOG.debug("validate {} {}", request, object); | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc}. {@link Context} annotated parameters are skipped unless the | ||
| * {@link Valid} annotation is present. | ||
| */ | ||
| @Override | ||
| public void validateAllParameters(final HttpRequest request, | ||
| final Object object, | ||
| final Method method, | ||
| final Object[] parameterValues, | ||
| final Class<?>... groups) { | ||
|
|
||
| LOG.debug("validateAllParameters {} {} {}", request, object, parameterValues); | ||
|
|
||
| final Map<String, Set<ConstraintViolation<Object>>> violationMap = new LinkedHashMap<>(); | ||
| boolean hasViolation = false; | ||
| for (int i = 0; i < method.getParameterCount(); ++i) { | ||
|
|
||
| final Parameter parameter = method.getParameters()[i]; | ||
| if (parameter.isAnnotationPresent(Context.class) && !parameter.isAnnotationPresent(Valid.class)) { | ||
| continue; | ||
| } | ||
|
|
||
| final Object value = parameterValues[i]; | ||
|
|
||
| final String name = getParameterName(parameter); | ||
|
|
||
| final Set<ConstraintViolation<Object>> violations; | ||
| if (value == null) { | ||
| if (parameter.isAnnotationPresent(NotNull.class)) { | ||
| throw ErrorResponses.invalidRequest("missing value for " + name); | ||
| } else { | ||
| continue; | ||
| } | ||
| } else { | ||
| violations = validator.validate(value); | ||
| } | ||
| if (violations.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| violationMap.put(name, violations); | ||
| hasViolation = true; | ||
| } | ||
| if (hasViolation) { | ||
| throw new BadRequestException(Response.status(Status.BAD_REQUEST).entity(new ConstraintViolationResponse(violationMap)).build()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void validateReturnValue(final HttpRequest request, | ||
| final Object object, | ||
| final Method method, | ||
| final Object returnValue, | ||
| final Class<?>... groups) { | ||
|
|
||
| LOG.debug("validateReturnValue {} {} {}", request, object, returnValue); | ||
|
|
||
| if (returnValue == null) { | ||
| if (method.isAnnotationPresent(NotNull.class)) { | ||
| throw ErrorResponses.internalServerError("unexpected null result"); | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| final Set<ConstraintViolation<Object>> errors = validator.validate(returnValue); | ||
|
|
||
| if (!errors.isEmpty()) { | ||
| throw new InternalServerErrorException("result object validation failed", new ConstraintViolationException(errors)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
17 changes: 17 additions & 0 deletions
17
ms-common-impl/src/main/java/net/trajano/ms/vertx/beans/ValidationProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package net.trajano.ms.vertx.beans; | ||
|
|
||
| import javax.validation.Validation; | ||
| import javax.validation.Validator; | ||
|
|
||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| public class ValidationProvider { | ||
|
|
||
| @Bean | ||
| public Validator beanValidationValidator() { | ||
|
|
||
| return Validation.buildDefaultValidatorFactory().getValidator(); | ||
| } | ||
| } |
91 changes: 91 additions & 0 deletions
91
ms-common-impl/src/main/java/net/trajano/ms/vertx/jaxrs/AbstractJsonExceptionMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package net.trajano.ms.vertx.jaxrs; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import javax.ws.rs.core.Context; | ||
| import javax.ws.rs.core.HttpHeaders; | ||
| import javax.ws.rs.core.MediaType; | ||
| import javax.ws.rs.core.UriInfo; | ||
| import javax.ws.rs.ext.ExceptionMapper; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
|
|
||
| import net.trajano.ms.Microservice; | ||
|
|
||
| public abstract class AbstractJsonExceptionMapper<T extends Throwable> implements | ||
| ExceptionMapper<T> { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(Microservice.class); | ||
|
|
||
| /** | ||
| * Supported media types. | ||
| */ | ||
| private static final Set<MediaType> SUPPORTED_MEDIA_TYPES = new HashSet<>(Arrays.asList( | ||
| MediaType.APPLICATION_JSON_TYPE, | ||
| MediaType.APPLICATION_XML_TYPE, | ||
| MediaType.TEXT_XML_TYPE, | ||
| MediaType.TEXT_PLAIN_TYPE, | ||
| MediaType.TEXT_HTML_TYPE)); | ||
|
|
||
| @Context | ||
| private HttpHeaders headers; | ||
|
|
||
| @Value("${microservice.show_stack_trace:#{null}}") | ||
| protected Boolean showStackTrace; | ||
|
|
||
| @Context | ||
| protected UriInfo uriInfo; | ||
|
|
||
| /** | ||
| * Determines the appropriate media type based on what is requested. If wildcard | ||
| * use JSON. | ||
| * | ||
| * @return media type appropriate for request | ||
| */ | ||
| protected MediaType getAppropriateMediaType() { | ||
|
|
||
| final List<MediaType> acceptableMediaTypes = headers.getAcceptableMediaTypes(); | ||
| for (final MediaType mediaType : acceptableMediaTypes) { | ||
| if (mediaType.equals(MediaType.WILDCARD_TYPE)) { | ||
| return MediaType.APPLICATION_JSON_TYPE; | ||
| } else if (SUPPORTED_MEDIA_TYPES.contains(mediaType)) { | ||
| return mediaType; | ||
| } | ||
| } | ||
| return MediaType.APPLICATION_JSON_TYPE; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * This sets the context data so the mapper can be unit tested. | ||
| */ | ||
| public void setContextData(final HttpHeaders headers, | ||
| final UriInfo uriInfo, | ||
| final boolean showStackTrace) { | ||
|
|
||
| this.headers = headers; | ||
| this.uriInfo = uriInfo; | ||
| this.showStackTrace = showStackTrace; | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * If the show request URI or show stack trace are not defined, it will default | ||
| * to whether the current logger is on debug mode or not. | ||
| */ | ||
| @PostConstruct | ||
| public void setDebugFlags() { | ||
|
|
||
| if (showStackTrace == null) { | ||
| showStackTrace = LOG.isDebugEnabled(); | ||
| LOG.debug("stack trace enabled if this is shown"); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.