Search in sources :

Example 1 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext 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 ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext 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 3 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletRequestContextTest method testResponseHeaders.

@Test
public void testResponseHeaders() {
    MockHttpServletResponse response = new MockHttpServletResponse();
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, "/api");
    context.setResponseHeader("test", "13");
    Assert.assertEquals("13", context.getResponseHeader("test"));
}
Also used : ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletRequestContextTest method testGetters.

@Test
public void testGetters() {
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, "/api");
    Assert.assertEquals(request, context.getRequest());
    Assert.assertEquals(response, context.getResponse());
    Assert.assertEquals(servletContext, context.getServletContext());
}
Also used : ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) Test(org.junit.Test)

Example 5 with ServletRequestContext

use of io.crnk.servlet.internal.ServletRequestContext in project crnk-framework by crnk-project.

the class ServletRequestContextTest method testRootPath.

@Test
public void testRootPath() {
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    request.setMethod("GET");
    request.setContextPath("");
    request.setServletPath("");
    request.setPathInfo("");
    request.setRequestURI("");
    request.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
    request.addHeader("Accept", "*/*");
    request.setServerName("test");
    request.setServerPort(1234);
    ServletRequestContext context = new ServletRequestContext(servletContext, request, response, null);
    Assert.assertEquals("/", context.getPath());
    Assert.assertEquals("http://test:1234", context.getBaseUrl());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) Test(org.junit.Test)

Aggregations

ServletRequestContext (io.crnk.servlet.internal.ServletRequestContext)10 Test (org.junit.Test)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)3 ServletContext (javax.servlet.ServletContext)3 HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)1 SecurityProvider (io.crnk.core.engine.security.SecurityProvider)1 ServletModule (io.crnk.servlet.internal.ServletModule)1 Set (java.util.Set)1