Search in sources :

Example 1 with HttpRequestContextBaseAdapter

use of io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter 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 2 with HttpRequestContextBaseAdapter

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

Example 3 with HttpRequestContextBaseAdapter

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

the class JsonApiRequestProcessorTest method setup.

@Before
public void setup() {
    TaskRepository.clear();
    boot = new CrnkBoot();
    boot.addModule(new Module() {

        @Override
        public String getModuleName() {
            return "test";
        }

        @Override
        public void setupModule(ModuleContext context) {
            moduleContext = context;
        }
    });
    boot.setServiceUrlProvider(new ConstantServiceUrlProvider("http://localhost:8080"));
    boot.setServiceDiscovery(new ReflectionsServiceDiscovery(MockConstants.TEST_MODELS_PACKAGE));
    boot.boot();
    Task task = new Task();
    task.setId(1L);
    task.setName("SomeTask");
    task.setLinksInformation(new TaskLinks());
    TaskRepository tasks = new TaskRepository();
    tasks.save(task);
    processor = new JsonApiRequestProcessor(moduleContext);
    requestContextBase = Mockito.mock(HttpRequestContextBase.class);
    requestContext = new HttpRequestContextBaseAdapter(requestContextBase);
    HttpRequestContextProvider requestContextProvider = boot.getModuleRegistry().getHttpRequestContextProvider();
    requestContextProvider.onRequestStarted(requestContext);
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) Task(io.crnk.core.mock.models.Task) TaskRepository(io.crnk.core.mock.repository.TaskRepository) JsonApiRequestProcessor(io.crnk.core.engine.internal.http.JsonApiRequestProcessor) CrnkBoot(io.crnk.core.boot.CrnkBoot) ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) ReflectionsServiceDiscovery(io.crnk.core.module.discovery.ReflectionsServiceDiscovery) Module(io.crnk.core.module.Module) TaskLinks(io.crnk.core.mock.models.TaskLinks) Before(org.junit.Before)

Example 4 with HttpRequestContextBaseAdapter

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

the class ServletModuleTest method testSecurityProviderInstalled.

@Test
public void testSecurityProviderInstalled() {
    HttpRequestContextProvider provider = new HttpRequestContextProvider();
    ServletModule module = new ServletModule(provider);
    CrnkBoot boot = new CrnkBoot();
    boot.addModule(module);
    boot.boot();
    SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.addUserRole("guest");
    request.addUserRole("admin");
    provider.onRequestStarted(new HttpRequestContextBaseAdapter(new ServletRequestContext(servletContext, request, response, "api", HttpHeaders.DEFAULT_CHARSET)));
    Assert.assertFalse(securityProvider.isUserInRole("doesNotExist"));
    Assert.assertTrue(securityProvider.isUserInRole("guest"));
    Assert.assertTrue(securityProvider.isUserInRole("admin"));
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) CrnkBoot(io.crnk.core.boot.CrnkBoot) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityProvider(io.crnk.core.engine.security.SecurityProvider) ServletContext(javax.servlet.ServletContext) ServletRequestContext(io.crnk.servlet.internal.ServletRequestContext) ServletModule(io.crnk.servlet.internal.ServletModule) HttpRequestContextProvider(io.crnk.core.engine.http.HttpRequestContextProvider) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 5 with HttpRequestContextBaseAdapter

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

the class HttpRequestContextBaseAdapterTest method setup.

@Before
public void setup() {
    base = Mockito.mock(HttpRequestContextBase.class);
    adapter = new HttpRequestContextBaseAdapter(base);
}
Also used : HttpRequestContextBaseAdapter(io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter) Before(org.junit.Before)

Aggregations

HttpRequestContextBaseAdapter (io.crnk.core.engine.internal.http.HttpRequestContextBaseAdapter)6 CrnkBoot (io.crnk.core.boot.CrnkBoot)3 RequestDispatcher (io.crnk.core.engine.dispatcher.RequestDispatcher)2 Document (io.crnk.core.engine.document.Document)2 HttpRequestContextProvider (io.crnk.core.engine.http.HttpRequestContextProvider)2 ServletRequestContext (io.crnk.servlet.internal.ServletRequestContext)2 ServletContext (javax.servlet.ServletContext)2 Before (org.junit.Before)2 Test (org.junit.Test)2 HttpRequestContext (io.crnk.core.engine.http.HttpRequestContext)1 ControllerRegistry (io.crnk.core.engine.internal.dispatcher.ControllerRegistry)1 CollectionGet (io.crnk.core.engine.internal.dispatcher.controller.CollectionGet)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 DocumentMapper (io.crnk.core.engine.internal.document.mapper.DocumentMapper)1 ExceptionMapperRegistryTest (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest)1 HttpRequestProcessorImpl (io.crnk.core.engine.internal.http.HttpRequestProcessorImpl)1 JsonApiRequestProcessor (io.crnk.core.engine.internal.http.JsonApiRequestProcessor)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 SecurityProvider (io.crnk.core.engine.security.SecurityProvider)1 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)1