Search in sources :

Example 6 with HttpRequestContext

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

the class UIModuleTest method processorReturnsIndexHtmlForRootPage.

@Test
public void processorReturnsIndexHtmlForRootPage() throws IOException {
    UIHttpRequestProcessor processor = new UIHttpRequestProcessor(new UIModuleConfig());
    HttpRequestContext context = Mockito.mock(HttpRequestContext.class);
    Mockito.when(context.getMethod()).thenReturn("GET");
    Mockito.when(context.getPath()).thenReturn("browse/");
    processor.process(context);
    Mockito.verify(context, Mockito.times(1)).setResponse(Mockito.eq(200), Mockito.any(byte[].class));
}
Also used : UIHttpRequestProcessor(io.crnk.ui.internal.UIHttpRequestProcessor) HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext) Test(org.junit.Test)

Example 7 with HttpRequestContext

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

the class QuerySpecAdapterBuilder method build.

@Override
public QueryAdapter build(ResourceInformation resourceInformation, Map<String, Set<String>> parameters) {
    QuerySpecAdapter adapter = new QuerySpecAdapter(querySpecDeserializer.deserialize(resourceInformation, parameters), resourceRegistry);
    HttpRequestContext requestContext = requestContextProvider.getRequestContext();
    if (requestContext != null) {
        adapter.setCompactMode(Boolean.parseBoolean(requestContext.getRequestHeader(HttpHeaders.HTTP_HEADER_CRNK_COMPACT)));
    }
    return adapter;
}
Also used : HttpRequestContext(io.crnk.core.engine.http.HttpRequestContext)

Example 8 with HttpRequestContext

use of io.crnk.core.engine.http.HttpRequestContext 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 9 with HttpRequestContext

use of io.crnk.core.engine.http.HttpRequestContext 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

HttpRequestContext (io.crnk.core.engine.http.HttpRequestContext)9 Test (org.junit.Test)5 UIHttpRequestProcessor (io.crnk.ui.internal.UIHttpRequestProcessor)4 Document (io.crnk.core.engine.document.Document)2 FilterBehavior (io.crnk.core.engine.filter.FilterBehavior)2 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)2 HashMap (java.util.HashMap)2 Set (java.util.Set)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 Response (io.crnk.core.engine.dispatcher.Response)1 Relationship (io.crnk.core.engine.document.Relationship)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 HttpHeaders (io.crnk.core.engine.http.HttpHeaders)1 HttpMethod (io.crnk.core.engine.http.HttpMethod)1 HttpRequestProcessor (io.crnk.core.engine.http.HttpRequestProcessor)1 RepositoryInformation (io.crnk.core.engine.information.repository.RepositoryInformation)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1