Search in sources :

Example 16 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class MetaFilteringTest method checkReadOnlyAttribute.

@Test
public void checkReadOnlyAttribute() throws IOException {
    RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.FORBIDDEN);
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    Assert.assertTrue(taskMeta.hasAttribute("project"));
    MetaAttribute projectAttr = taskMeta.getAttribute("project");
    Assert.assertFalse(projectAttr.isInsertable());
    Assert.assertFalse(projectAttr.isUpdatable());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 17 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class ResourceMetaFilter method adjustFieldForRequest.

private MetaElement adjustFieldForRequest(MetaResourceField field) {
    MetaResource metaResource = (MetaResource) field.getParent();
    Module.ModuleContext moduleContext = context.getModuleContext();
    RegistryEntry entry = moduleContext.getResourceRegistry().getEntry(metaResource.getResourceType());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField fieldInformation = resourceInformation.findFieldByUnderlyingName(field.getName());
    ResourceFilterDirectory filterBehaviorProvider = moduleContext.getResourceFilterDirectory();
    boolean readable = metaResource.isReadable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.GET) == FilterBehavior.NONE;
    boolean insertable = metaResource.isInsertable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.POST) == FilterBehavior.NONE;
    boolean updatable = metaResource.isUpdatable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.PATCH) == FilterBehavior.NONE;
    // hide element if no permission
    if (!readable && !insertable && !updatable) {
        return null;
    }
    if (field.isUpdatable() != updatable || field.isInsertable() != insertable) {
        MetaResourceField clone = (MetaResourceField) field.duplicate();
        clone.setInsertable(insertable);
        clone.setUpdatable(updatable);
        return clone;
    }
    return field;
}
Also used : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) ResourceField(io.crnk.core.engine.information.resource.ResourceField) MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) Module(io.crnk.core.module.Module) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceFilterDirectory(io.crnk.core.engine.filter.ResourceFilterDirectory)

Example 18 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class BraveUtil method getSpanName.

public static String getSpanName(RepositoryRequestSpec request) {
    ResourceField relationshipField = request.getRelationshipField();
    StringBuilder pathBuilder = new StringBuilder();
    String method = request.getMethod().toString();
    pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME);
    pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME_SEPARATOR);
    pathBuilder.append(method);
    pathBuilder.append(BraveRepositoryFilter.COMPONENT_NAME_SEPARATOR);
    pathBuilder.append("/");
    if (relationshipField == null) {
        pathBuilder.append(request.getQueryAdapter().getResourceInformation().getResourceType());
    } else {
        pathBuilder.append(relationshipField.getParentResourceInformation().getResourceType());
    }
    pathBuilder.append("/");
    Iterable<Object> ids = request.getIds();
    if (ids != null) {
        pathBuilder.append(StringUtils.join(",", ids));
        pathBuilder.append("/");
    }
    if (relationshipField != null) {
        pathBuilder.append(relationshipField.getJsonName());
        pathBuilder.append("/");
    }
    return pathBuilder.toString();
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField)

Example 19 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class JpaPartialEntityExposureTest method testInformationBuilder.

@Test
public void testInformationBuilder() {
    EntityManager em = null;
    JpaResourceInformationProvider builder = new JpaResourceInformationProvider(new NullPropertiesProvider());
    ResourceInformation info = builder.build(TestEntity.class);
    List<ResourceField> relationshipFields = info.getRelationshipFields();
    Assert.assertEquals(0, relationshipFields.size());
}
Also used : JpaResourceInformationProvider(io.crnk.jpa.internal.JpaResourceInformationProvider) ResourceField(io.crnk.core.engine.information.resource.ResourceField) EntityManager(javax.persistence.EntityManager) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) NullPropertiesProvider(io.crnk.core.engine.properties.NullPropertiesProvider)

Example 20 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class DefaultRegistryEntryBuilder method buildRelationships.

private Map<ResourceField, ResponseRelationshipEntry> buildRelationships(ResourceInformation resourceInformation) {
    for (String relationshipName : relationshipRepositoryMap.keySet()) {
        if (resourceInformation.findFieldByUnderlyingName(relationshipName) == null) {
            throw new ResourceFieldNotFoundException("failed to find relationship field '" + relationshipName + "' to setup " + "registered relationship repository");
        }
    }
    Map<ResourceField, ResponseRelationshipEntry> map = new HashMap<>();
    for (ResourceField relationshipField : resourceInformation.getRelationshipFields()) {
        ResponseRelationshipEntry relationshipEntry = null;
        // check for local definition
        DefaultRelationshipRepository repository = relationshipRepositoryMap.get(relationshipField.getUnderlyingName());
        if (repository != null) {
            RelationshipRepositoryInformation relationshipInformation = repository.information.build();
            relationshipEntry = setupRelationship(relationshipField, relationshipInformation, repository.instance);
        }
        // check for match
        if (relationshipEntry == null) {
            relationshipEntry = findRelationshipMatch(relationshipField);
        }
        // check for implicit
        if (relationshipEntry == null) {
            relationshipEntry = setupImplicitRelationshipRepository(relationshipField);
        }
        if (relationshipEntry != null) {
            map.put(relationshipField, relationshipEntry);
        } else {
            LOGGER.warn("no relationship repository found for " + resourceInformation.getResourceType() + "." + relationshipField.getUnderlyingName());
        }
    }
    return map;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RelationshipRepositoryInformation(io.crnk.core.engine.information.repository.RelationshipRepositoryInformation) ResourceFieldNotFoundException(io.crnk.core.exception.ResourceFieldNotFoundException) ResponseRelationshipEntry(io.crnk.core.engine.registry.ResponseRelationshipEntry) DirectResponseRelationshipEntry(io.crnk.legacy.internal.DirectResponseRelationshipEntry) HashMap(java.util.HashMap)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)109 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)75 Test (org.junit.Test)41 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)36 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)13 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ArrayList (java.util.ArrayList)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)8 Response (io.crnk.core.engine.dispatcher.Response)8 Resource (io.crnk.core.engine.document.Resource)8 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 HashSet (java.util.HashSet)8 Document (io.crnk.core.engine.document.Document)7 Task (io.crnk.core.mock.models.Task)7 Serializable (java.io.Serializable)7 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6