Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.katharsis.rs;
  
  import  io.katharsis.dispatcher.RequestDispatcher;
  import  io.katharsis.errorhandling.exception.KatharsisException;
  import  io.katharsis.errorhandling.mapper.def.KatharsisExceptionMapper;
  import  io.katharsis.queryParams.RequestParams;
  import  io.katharsis.queryParams.RequestParamsBuilder;
  import  io.katharsis.request.dto.RequestBody;
 import  io.katharsis.request.path.JsonPath;
 import  io.katharsis.request.path.PathBuilder;
 import  io.katharsis.resource.registry.ResourceRegistry;
 import  io.katharsis.response.BaseResponse;
 
 import java.util.Map;
 
 import static io.katharsis.rs.type.JsonApiMediaType.APPLICATION_JSON_API_TYPE;

Handles JSON API requests.

Consumes: null | JsonApiMediaType Produces: JsonApiMediaType

Currently the response is sent using ContainerRequestContext.abortWith(Response) which might cause problems with Jackson, co the serialization is happening in this filter.

To be able to send a request to Katharsis it is necessary to provide full media type alongside the request. Wildcards are not accepted.

 
 public class KatharsisFilter implements ContainerRequestFilter {
 
     private ObjectMapper objectMapper;
     private ResourceRegistry resourceRegistry;
     private RequestDispatcher requestDispatcher;
 
     public KatharsisFilter(ObjectMapper objectMapper, ResourceRegistry resourceRegistry, RequestDispatcher
             requestDispatcher) {
         this. = objectMapper;
         this. = resourceRegistry;
         this. = requestDispatcher;
     }
 
     @Override
     public void filter(ContainerRequestContext requestContextthrows IOException {
         if (isAcceptableMediaType(requestContext) && isAcceptableContentType(requestContext)) {
             try {
                 dispatchRequest(requestContext);
             } catch (Exception e) {
                 throw new WebApplicationException(e);
             }
         }
     }
 
     private void dispatchRequest(ContainerRequestContext requestContextthrows Exception {
         UriInfo uriInfo = requestContext.getUriInfo();
         BaseResponse<?> katharsisResponse = null;
         //TODO: Refactor
         try {
             JsonPath jsonPath = new PathBuilder().buildPath(uriInfo.getPath());
 
             RequestParams requestParams = createRequestParams(uriInfo);
 
             String method = requestContext.getMethod();
             RequestBody requestBody = inputStreamToBody(requestContext.getEntityStream());
 
             katharsisResponse = 
                     .dispatchRequest(jsonPathmethodrequestParamsrequestBody);
         } catch (KatharsisException e) {
             katharsisResponse = new KatharsisExceptionMapper().toErrorResponse(e);
         } finally {
             Response response;
             if (katharsisResponse != null) {
                 ByteArrayOutputStream os = new ByteArrayOutputStream();
                 .writeValue(oskatharsisResponse);
                 response = Response
                         .status(katharsisResponse.getHttpStatus())
                         .entity(new ByteArrayInputStream(os.toByteArray()))
                         .type()
                         .build();
            } else {
                response = Response.noContent().build();
            }
            requestContext.abortWith(response);
        }
    }
    private boolean isAcceptableMediaType(ContainerRequestContext requestContext) {
        boolean result = false;
        for (MediaType acceptableType : requestContext.getAcceptableMediaTypes()) {
            if (.getType().equalsIgnoreCase(acceptableType.getType()) &&
                    .getSubtype().equalsIgnoreCase(acceptableType.getSubtype())) {
                result = true;
            }
        }
        return result;
    }
    private boolean isAcceptableContentType(ContainerRequestContext requestContext) {
        MediaType contentType = requestContext.getMediaType();
        return contentType == null || .isCompatible(contentType);
    }
    private RequestParams createRequestParams(UriInfo uriInfo) {
        RequestParamsBuilder requestParamsBuilder = new RequestParamsBuilder();
        MultivaluedMap<StringStringqueryParametersMultiMap = uriInfo.getQueryParameters();
        Map<StringStringqueryParameters = new HashMap<>();
        for (String queryName : queryParametersMultiMap.keySet()) {
            queryParameters.put(queryNamequeryParametersMultiMap.getFirst(queryName));
        }
        return requestParamsBuilder.buildRequestParams(queryParameters);
    }
    public RequestBody inputStreamToBody(InputStream isthrows IOException {
        if (is == null) {
            return null;
        }
        Scanner s = new Scanner(is).useDelimiter("\\A");
        String requestBody = s.hasNext() ? s.next() : "";
        if (requestBody == null || requestBody.isEmpty()) {
            return null;
        }
        return .readValue(requestBody, RequestBody.class);
    }
New to GrepCode? Check out our FAQ X