@ Override public void filter ( ContainerRequestContext requestContext , ContainerResponseContext responseContext ) { Object response = responseContext . getEntity ( ) ; if ( response == null ) { if ( feature . getBoot ( ) . isNullDataResponseEnabled ( ) ) { Document document = new Document ( ) ; document . setData ( Nullable . nullValue ( ) ) ; responseContext . setEntity ( document ) ; responseContext . setStatus ( Response . Status . OK . getStatusCode ( ) ) ; responseContext . getHeaders ( ) . put ( "Content-Type" , Collections . singletonList ( JsonApiMediaType . APPLICATION_JSON_API ) ) ; } return ; } // only modify responses which contain a single or a list of Crnk resources Optional < RegistryEntry > registryEntry = getRegistryEntry ( response ) ; if ( registryEntry . isPresent ( ) ) { CrnkBoot boot = feature . getBoot ( ) ; DocumentMapper documentMapper = boot . getDocumentMapper ( ) ; HttpRequestContextProvider httpRequestContextProvider = boot . getModuleRegistry ( ) . getHttpRequestContextProvider ( ) ; try { HttpRequestContext context = new HttpRequestContextBaseAdapter ( new JaxrsRequestContext ( requestContext , feature ) ) ; httpRequestContextProvider . onRequestStarted ( context ) ; JsonApiResponse jsonApiResponse = new JsonApiResponse ( ) ; jsonApiResponse . setEntity ( response ) ; // use the Crnk document mapper to create a JSON API response DocumentMappingConfig mappingConfig = new DocumentMappingConfig ( ) ; ResourceInformation resourceInformation = registryEntry . get ( ) . getResourceInformation ( ) ; Map < String , Set < String > > jsonApiParameters = context . getRequestParameters ( ) . entrySet ( ) . stream ( ) . filter ( entry -> isJsonApiParameter ( entry . getKey ( ) ) ) . collect ( Collectors . toMap ( Map . Entry :: getKey , Map . Entry :: getValue ) ) ; QuerySpecUrlMapper urlMapper = feature . getBoot ( ) . getUrlMapper ( ) ; QuerySpec querySpec = urlMapper . deserialize ( resourceInformation , jsonApiParameters ) ; ResourceRegistry resourceRegistry = feature . getBoot ( ) . getResourceRegistry ( ) ; QueryAdapter queryAdapter = new QuerySpecAdapter ( querySpec , resourceRegistry , context . getQueryContext ( ) ) ; responseContext . setEntity ( documentMapper . toDocument ( jsonApiResponse , queryAdapter , mappingConfig ) . get ( ) ) ; responseContext . getHeaders ( ) . put ( "Content-Type" , Collections . singletonList ( JsonApiMediaType . APPLICATION_JSON_API ) ) ; } finally { httpRequestContextProvider . onRequestFinished ( ) ; } } else if ( isJsonApiResponse ( responseContext ) && ! doNotWrap ( response ) ) { Document document = new Document ( ) ; document . setData ( Nullable . of ( response ) ) ; responseContext . setEntity ( document ) ; } }
Creates JSON API responses for custom JAX - RS actions returning Crnk resources .