Search in sources :

Example 6 with HttpRequestContextBaseAdapter

use of io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter in project crnk-framework by crnk-project.

the class JsonApiResponseFilter method filter.

/**
 * Creates JSON API responses for custom JAX-RS actions returning Crnk resources.
 */
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    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((Object) JsonApiMediaType.APPLICATION_JSON_API));
        }
        return;
    }
    // only modify responses which contain a single or a list of Crnk resources
    if (isResourceResponse(response)) {
        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
            responseContext.setEntity(documentMapper.toDocument(jsonApiResponse, null));
            responseContext.getHeaders().put("Content-Type", Collections.singletonList((Object) 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);
    }
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider)

Aggregations

HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)6 CrnkBoot (io.crnk.core.boot.CrnkBoot)3 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)2 Document (io.crnk.core.engine.document.Document)2 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)2 ServletRequestContext (io.crnk.servlet.internal.ServletRequestContext)2 ServletContext (javax.servlet.ServletContext)2 Before (org.junit.Before)2 Test (org.junit.Test)2 HttpRequestContext (io.crnk.core.engine.http.HttpRequestContext)1 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)1 CollectionGet (io.crnk.core.engine.internal.dispatcher.controller.CollectionGet)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 DocumentMapper (io.crnk.core.engine.internal.document.mapper.DocumentMapper)1 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)1 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)1 JsonApiRequestProcessor (io.crnk.core.engine.internal.http.JsonApiRequestProcessor)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 SecurityProvider (io.crnk.core.engine.security.SecurityProvider)1 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)1