Search in sources :

Example 46 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class BraveResponseFilterTest method statusCodeOkWhenEmptyErrors.

@Test
public void statusCodeOkWhenEmptyErrors() {
    JsonApiResponse response = new JsonApiResponse();
    response.setErrors(new ArrayList<ErrorData>());
    Mockito.when(filterChain.doFilter(Mockito.any(RepositoryFilterContext.class))).thenReturn(response);
    filter.filterRequest(filterContext, filterChain);
    Mockito.verify(span, Mockito.times(1)).tag(BraveRepositoryFilter.STATUS_CODE_ANNOTATION, BraveRepositoryFilter.STRING_OK);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ErrorData(io.crnk.core.engine.document.ErrorData) Test(org.junit.Test)

Example 47 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class OperationsCall method toResource.

protected Resource toResource(Object object) {
    JsonApiResponse response = new JsonApiResponse();
    response.setEntity(object);
    QuerySpec querySpec = new QuerySpec(object.getClass());
    QueryAdapter queryAdapter = new QuerySpecAdapter(querySpec, client.getCrnk().getRegistry());
    CrnkClient crnk = client.getCrnk();
    DocumentMapper documentMapper = crnk.getDocumentMapper();
    Document document = documentMapper.toDocument(response, queryAdapter);
    return document.getSingleData().get();
}
Also used : CrnkClient(io.crnk.client.CrnkClient) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ClientDocumentMapper(io.crnk.client.internal.ClientDocumentMapper) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document)

Example 48 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class BraveRepositoryFilter method filterRequest.

@Override
public JsonApiResponse filterRequest(RepositoryFilterContext context, RepositoryRequestFilterChain chain) {
    Tracer tracer = tracing.tracer();
    RepositoryRequestSpec request = context.getRequest();
    String query = BraveUtil.getQuery(request, moduleContext.getResourceRegistry());
    Span span = tracer.nextSpan();
    span.name(BraveUtil.getSpanName(request));
    span.tag(Constants.LOCAL_COMPONENT, COMPONENT_NAME);
    span.start();
    JsonApiResponse result = null;
    Exception exception = null;
    try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
        result = chain.doFilter(context);
        return result;
    } catch (RuntimeException e) {
        exception = e;
        throw e;
    } finally {
        boolean resultError = result != null && result.getErrors() != null && result.getErrors().iterator().hasNext();
        boolean failed = exception != null || resultError;
        String status = failed ? STRING_EXCEPTION : STRING_OK;
        span.tag(STATUS_CODE_ANNOTATION, status);
        writeQuery(span, query);
        writeResults(span, result);
        span.finish();
    }
}
Also used : RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) Tracer(brave.Tracer) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Span(brave.Span)

Example 49 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse 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

JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)49 Document (io.crnk.core.engine.document.Document)16 Test (org.junit.Test)16 ResourceField (io.crnk.core.engine.information.resource.ResourceField)15 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)14 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)12 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)10 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10 Serializable (java.io.Serializable)10 Response (io.crnk.core.engine.dispatcher.Response)9 Resource (io.crnk.core.engine.document.Resource)9 Task (io.crnk.core.mock.models.Task)7 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)6 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)6 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)6 HashSet (java.util.HashSet)6