Search in sources :

Example 1 with RequestDispatcher

use of io.crnk.core.engine.dispatcher.RequestDispatcher in project crnk-framework by crnk-project.

the class SpringCrnkFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    if (req instanceof HttpServletRequest && res instanceof HttpServletResponse && matchesPrefix((HttpServletRequest) req)) {
        ServletContext servletContext = filterConfig.getServletContext();
        RequestDispatcher requestDispatcher = boot.getRequestDispatcher();
        ServletRequestContext context = new ServletRequestContext(servletContext, (HttpServletRequest) req, (HttpServletResponse) res, boot.getWebPathPrefix(), defaultCharacterEncoding);
        requestDispatcher.process(context);
        if (!context.checkAbort()) {
            chain.doFilter(req, res);
        }
    } else {
        chain.doFilter(req, res);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher)

Example 2 with RequestDispatcher

use of io.crnk.core.engine.dispatcher.RequestDispatcher in project crnk-framework by crnk-project.

the class CrnkFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    try {
        JaxrsRequestContext context = new JaxrsRequestContext(requestContext, feature);
        RequestDispatcher requestDispatcher = feature.getBoot().getRequestDispatcher();
        requestDispatcher.process(context);
        context.checkAbort();
    } catch (WebApplicationException e) {
        LOGGER.error("failed to dispatch request", e);
        throw e;
    } catch (Exception e) {
        LOGGER.error("failed to dispatch request", e);
        throw new WebApplicationException(e);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException)

Example 3 with RequestDispatcher

use of io.crnk.core.engine.dispatcher.RequestDispatcher in project crnk-framework by crnk-project.

the class CrnkServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServletContext servletContext = getServletContext();
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, boot.getWebPathPrefix(), defaultCharacterEncoding);
    RequestDispatcher requestDispatcher = boot.getRequestDispatcher();
    requestDispatcher.process(context);
    if (!context.checkAbort()) {
        HttpRequestContextBaseAdapter requestContext = new HttpRequestContextBaseAdapter(context);
        boolean jsonApiRequest = JsonApiRequestProcessor.isJsonApiRequest(requestContext, acceptPlainJson);
        response.setStatus(jsonApiRequest ? HttpStatus.NOT_FOUND_404 : HttpStatus.UNSUPPORTED_MEDIA_TYPE_415);
    }
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) ServletContext(javax.servlet.ServletContext) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher)

Example 4 with RequestDispatcher

use of io.crnk.core.engine.dispatcher.RequestDispatcher in project crnk-framework by crnk-project.

the class HttpRequestProcessorImplTest method onGivenPathAndRequestTypeControllerShouldHandleRequest.

@Test
public void onGivenPathAndRequestTypeControllerShouldHandleRequest() throws Exception {
    // GIVEN
    String path = "/tasks/";
    String requestType = "GET";
    ControllerRegistry controllerRegistry = new ControllerRegistry(null);
    CollectionGet controller = mock(CollectionGet.class);
    controllerRegistry.addController(controller);
    QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
    RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
    // WHEN
    when(controller.isAcceptable(any(JsonPath.class), eq(requestType))).thenCallRealMethod();
    Map<String, Set<String>> parameters = new HashMap<>();
    sut.dispatchRequest(path, requestType, parameters, null, null);
    // THEN
    verify(controller, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
}
Also used : ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) QuerySpecAdapterBuilder(io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder) Set(java.util.Set) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) Matchers.anyString(org.mockito.Matchers.anyString) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) CollectionGet(io.crnk.core.engine.internal.dispatcher.controller.CollectionGet) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) ExceptionMapperRegistryTest(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest) Test(org.junit.Test)

Example 5 with RequestDispatcher

use of io.crnk.core.engine.dispatcher.RequestDispatcher in project crnk-framework by crnk-project.

the class HttpRequestProcessorImplTest method checkProcess.

@Test
public void checkProcess() throws IOException {
    HttpRequestContextBase requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    HttpRequestContextBaseAdapter requestContext = new HttpRequestContextBaseAdapter(requestContextBase);
    Mockito.when(requestContextBase.getMethod()).thenReturn("GET");
    Mockito.when(requestContextBase.getPath()).thenReturn("/tasks/");
    Mockito.when(requestContextBase.getRequestHeader("Accept")).thenReturn("*");
    ControllerRegistry controllerRegistry = new ControllerRegistry(null);
    CollectionGet controller = mock(CollectionGet.class);
    when(controller.isAcceptable(any(JsonPath.class), eq("GET"))).thenCallRealMethod();
    controllerRegistry.addController(controller);
    QuerySpecAdapterBuilder queryAdapterBuilder = new QuerySpecAdapterBuilder(new DefaultQuerySpecDeserializer(), moduleRegistry);
    RequestDispatcher sut = new HttpRequestProcessorImpl(moduleRegistry, controllerRegistry, null, queryAdapterBuilder);
    sut.process(requestContext);
    verify(controller, times(1)).handle(any(JsonPath.class), any(QueryAdapter.class), any(RepositoryMethodParameterProvider.class), any(Document.class));
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) ControllerRegistry(io.crnk.core.engine.internal.dispatcher.ControllerRegistry) CollectionGet(io.crnk.core.engine.internal.dispatcher.controller.CollectionGet) QuerySpecAdapterBuilder(io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) DefaultQuerySpecDeserializer(io.crnk.core.queryspec.DefaultQuerySpecDeserializer) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) Document(io.crnk.core.engine.document.Document) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) RequestDispatcher(io.crnk.core.engine.dispatcher.RequestDispatcher) HttpRequestProcessorImpl(io.crnk.core.engine.internal.http.HttpRequestProcessorImpl) ExceptionMapperRegistryTest(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest) Test(org.junit.Test)

Aggregations

RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)14 Test (org.junit.Test)7 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)6 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)6 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)6 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)6 DefaultQuerySpecDeserializer (io.crnk.core.queryspec.DefaultQuerySpecDeserializer)6 QuerySpecAdapterBuilder (io.crnk.core.queryspec.internal.QuerySpecAdapterBuilder)6 RepositoryMethodParameterProvider (io.crnk.legacy.internal.RepositoryMethodParameterProvider)6 Set (java.util.Set)6 Response (io.crnk.core.engine.dispatcher.Response)5 Document (io.crnk.core.engine.document.Document)5 HashMap (java.util.HashMap)4 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)3 ServletRequestContext (io.crnk.servlet.internal.ServletRequestContext)3 Matchers.anyString (org.mockito.Matchers.anyString)3 CollectionGet (io.crnk.core.engine.internal.dispatcher.controller.CollectionGet)2 ActionPath (io.crnk.core.engine.internal.dispatcher.path.ActionPath)2 HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)2 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)2