Search in sources :

Example 46 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class HomeModule method isHomeRequest.

private boolean isHomeRequest(HttpRequestContext requestContext) {
    String path = requestContext.getPath();
    if (!path.endsWith("/") || !requestContext.getMethod().equalsIgnoreCase(HttpMethod.GET.toString())) {
        return false;
    }
    boolean acceptsHome = requestContext.accepts(JSON_HOME_CONTENT_TYPE);
    boolean acceptsJsonApi = requestContext.accepts(HttpHeaders.JSONAPI_CONTENT_TYPE);
    boolean acceptsJson = requestContext.accepts(HttpHeaders.JSON_CONTENT_TYPE);
    boolean acceptsAny = requestContext.acceptsAny();
    if (!(acceptsHome || acceptsAny || acceptsJson || acceptsJsonApi)) {
        return false;
    }
    ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
    JsonPath jsonPath = new PathBuilder(resourceRegistry).build(path);
    // no repository with that path
    return jsonPath == null;
}
Also used : PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath)

Example 47 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class HomeModule method setupModule.

@Override
public void setupModule(final ModuleContext context) {
    this.moduleContext = context;
    context.addHttpRequestProcessor(new HttpRequestProcessor() {

        @Override
        public void process(HttpRequestContext requestContext) throws IOException {
            if (isHomeRequest(requestContext)) {
                Set<String> pathSet = new HashSet<>();
                ResourceRegistry resourceRegistry = context.getResourceRegistry();
                for (RegistryEntry resourceEntry : resourceRegistry.getResources()) {
                    RepositoryInformation repositoryInformation = resourceEntry.getRepositoryInformation();
                    if (repositoryInformation != null && context.getResourceFilterDirectory().get(resourceEntry.getResourceInformation(), HttpMethod.GET) == FilterBehavior.NONE) {
                        ResourceInformation resourceInformation = resourceEntry.getResourceInformation();
                        String resourceType = resourceInformation.getResourceType();
                        pathSet.add("/" + resourceType);
                    }
                }
                if (extensions != null) {
                    for (HomeModuleExtension extension : extensions) {
                        pathSet.addAll(extension.getPaths());
                    }
                }
                String requestPath = requestContext.getPath();
                pathSet = pathSet.stream().map(it -> getChildName(requestPath, it)).filter(it -> it != null).collect(Collectors.toSet());
                List<String> pathList = new ArrayList<>(pathSet);
                Collections.sort(pathList);
                if (defaultFormat == HomeFormat.JSON_HOME || requestContext.accepts(JSON_HOME_CONTENT_TYPE)) {
                    boolean acceptsHome = requestContext.accepts(JSON_HOME_CONTENT_TYPE);
                    if (acceptsHome) {
                        requestContext.setContentType(JSON_HOME_CONTENT_TYPE);
                    } else {
                        requestContext.setContentType(JSON_CONTENT_TYPE);
                    }
                    writeJsonHome(requestContext, pathList);
                } else {
                    boolean jsonapi = requestContext.accepts(HttpHeaders.JSONAPI_CONTENT_TYPE);
                    if (jsonapi) {
                        requestContext.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
                    } else {
                        requestContext.setContentType(JSON_CONTENT_TYPE);
                    }
                    writeJsonApi(requestContext, pathList);
                }
            }
        }

        private String getChildName(String requestPath, String it) {
            if (it.startsWith(requestPath)) {
                String subPath = UrlUtils.removeTrailingSlash(it.substring(requestPath.length()));
                int sep = subPath.indexOf('/');
                return sep == -1 ? subPath : subPath.substring(0, sep) + "/";
            }
            return null;
        }
    });
}
Also used : HttpRequestProcessor(io.crnk.core.engine.http.HttpRequestProcessor) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) RepositoryInformation(io.crnk.core.engine.information.repository.RepositoryInformation) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) IOException(java.io.IOException) HttpMethod(io.crnk.core.engine.http.HttpMethod) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PathBuilder(io.crnk.core.engine.internal.dispatcher.path.PathBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) HttpHeaders(io.crnk.core.engine.http.HttpHeaders) UrlUtils(io.crnk.core.engine.internal.utils.UrlUtils) Module(io.crnk.core.module.Module) FilterBehavior(io.crnk.core.engine.filter.FilterBehavior) ModuleExtensionAware(io.crnk.core.module.ModuleExtensionAware) Collections(java.util.Collections) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) HashSet(java.util.HashSet) HttpRequestProcessor(io.crnk.core.engine.http.HttpRequestProcessor) IOException(java.io.IOException) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RepositoryInformation(io.crnk.core.engine.information.repository.RepositoryInformation) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) ArrayList(java.util.ArrayList) List(java.util.List)

Example 48 with ResourceRegistry

use of io.crnk.core.engine.registry.ResourceRegistry in project crnk-framework by crnk-project.

the class MetaModuleTest method checkRegistryUpdateTriggerMetaUpdate.

@Test
public void checkRegistryUpdateTriggerMetaUpdate() {
    MetaLookup lookup = metaModule.getLookup();
    List<MetaResource> prevResources = lookup.findElements(MetaResource.class);
    ResourceRegistry resourceRegistry = boot.getResourceRegistry();
    resourceRegistry.addEntry(createRegistryEntry());
    // check request local caching against concurrent modifications
    Assert.assertEquals(prevResources.size(), metaModule.getLookup().findElements(MetaResource.class).size());
    // check new meta available for next request
    metaModule.getLookupRequestLocal().remove();
    Assert.assertEquals(prevResources.size() + 1, metaModule.getLookup().findElements(MetaResource.class).size());
}
Also used : MetaResource(io.crnk.meta.model.resource.MetaResource) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) Test(org.junit.Test)

Aggregations

ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)48 Test (org.junit.Test)25 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)21 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)19 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)17 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)15 ResourcePath (io.crnk.core.engine.internal.dispatcher.path.ResourcePath)10 ResourceField (io.crnk.core.engine.information.resource.ResourceField)5 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)4 ModuleRegistry (io.crnk.core.module.ModuleRegistry)4 QueryParamsAdapter (io.crnk.legacy.internal.QueryParamsAdapter)4 Before (org.junit.Before)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FieldResourceGet (io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet)3 RelationshipsResourceGet (io.crnk.core.engine.internal.dispatcher.controller.RelationshipsResourceGet)3 ResourceRegistryImpl (io.crnk.core.engine.internal.registry.ResourceRegistryImpl)3 DefaultResourceRegistryPart (io.crnk.core.engine.registry.DefaultResourceRegistryPart)3 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)3 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)2 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)2