Search in sources :

Example 1 with DocumentMapper

use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includeByDefaultSerializeNLevels.

@Test
public void includeByDefaultSerializeNLevels() throws Exception {
    Project project = new Project();
    project.setId(1L);
    Task task = new Task().setId(2L);
    project.setTask(task);
    Project projectDefault = new Project().setId(3L);
    task.setProject(projectDefault);
    mapper = new DocumentMapper(resourceRegistry, objectMapper, new EmptyPropertiesProvider(), resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Project.class);
    querySpec.includeRelation(Collections.singletonList("task"));
    Document document = mapper.toDocument(toResponse(project), toAdapter(querySpec));
    Resource projectResource = document.getSingleData().get();
    Relationship relationship = projectResource.getRelationships().get("task");
    assertNotNull(relationship);
    assertEquals("2", relationship.getSingleData().get().getId());
    assertNotNull(document.getIncluded());
    assertEquals(2, document.getIncluded().size());
    List<Resource> resources = document.getIncluded();
    assertEquals("projects", resources.get(0).getType());
    assertEquals("3", resources.get(0).getId());
    assertEquals("tasks", resources.get(1).getType());
    assertEquals("2", resources.get(1).getId());
}
Also used : Project(io.crnk.core.mock.models.Project) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 2 with DocumentMapper

use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includePropertiesProviderNonOverwriteRelationshipLookup.

@Test
public void includePropertiesProviderNonOverwriteRelationshipLookup() throws Exception {
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
                return "false";
            }
            return null;
        }
    };
    mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    Project project = new Project();
    project.setId(12L);
    Task task = new Task();
    task.setId(1L);
    task.setProject(project);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    Relationship relationship = taskResource.getRelationships().get("project");
    assertNotNull(relationship);
    assertEquals(relationship.getSingleData().get().getId(), "12");
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) Project(io.crnk.core.mock.models.Project) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) Relationship(io.crnk.core.engine.document.Relationship) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 3 with DocumentMapper

use of io.crnk.core.engine.internal.document.mapper.DocumentMapper in project crnk-framework by crnk-project.

the class IncludeLookupSetterBaseTest method includePropertiesProviderAllTrueRelationshipLookup.

@Test
public void includePropertiesProviderAllTrueRelationshipLookup() throws Exception {
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (key.equalsIgnoreCase(CrnkProperties.INCLUDE_AUTOMATICALLY_OVERWRITE)) {
                return "true";
            }
            return null;
        }
    };
    mapper = new DocumentMapper(resourceRegistry, objectMapper, propertiesProvider, resourceFilterDirectory);
    QuerySpec querySpec = new QuerySpec(Task.class);
    querySpec.includeRelation(Arrays.asList("project"));
    Task task = new Task();
    task.setId(1L);
    Document document = mapper.toDocument(toResponse(task), toAdapter(querySpec));
    Resource taskResource = document.getSingleData().get();
    assertNotNull(taskResource.getRelationships().get("project"));
    assertNotNull(taskResource.getRelationships().get("project").getData());
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) EmptyPropertiesProvider(io.crnk.core.engine.properties.EmptyPropertiesProvider) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) DocumentMapper(io.crnk.core.engine.internal.document.mapper.DocumentMapper) Resource(io.crnk.core.engine.document.Resource) QuerySpec(io.crnk.core.queryspec.QuerySpec) Document(io.crnk.core.engine.document.Document) AbstractDocumentMapperTest(io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest) Test(org.junit.Test)

Example 4 with DocumentMapper

use of io.crnk.core.engine.internal.document.mapper.DocumentMapper 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 5 with DocumentMapper

use of io.crnk.core.engine.internal.document.mapper.DocumentMapper 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

Document (io.crnk.core.engine.document.Document)5 DocumentMapper (io.crnk.core.engine.internal.document.mapper.DocumentMapper)5 QuerySpec (io.crnk.core.queryspec.QuerySpec)4 Resource (io.crnk.core.engine.document.Resource)3 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)3 EmptyPropertiesProvider (io.crnk.core.engine.properties.EmptyPropertiesProvider)3 HierarchicalTask (io.crnk.core.mock.models.HierarchicalTask)3 Task (io.crnk.core.mock.models.Task)3 Test (org.junit.Test)3 Relationship (io.crnk.core.engine.document.Relationship)2 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)2 Project (io.crnk.core.mock.models.Project)2 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)2 CrnkClient (io.crnk.client.CrnkClient)1 ClientDocumentMapper (io.crnk.client.internal.ClientDocumentMapper)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 HttpRequestContext (io.crnk.core.engine.http.HttpRequestContext)1 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)1 HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1