Search in sources :

Example 1 with MinijaxRequestContext

use of org.minijax.MinijaxRequestContext in project minijax by minijax.

the class CsrfFilter method filter.

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
    if (!requestContext.getMethod().equals("POST")) {
        return;
    }
    final MinijaxRequestContext ctx = (MinijaxRequestContext) requestContext;
    final Security<?> security = ctx.get(Security.class);
    if (!security.isLoggedIn() || security.getAuthenticationScheme() != SecurityContext.FORM_AUTH) {
        return;
    }
    final MediaType contentType = ctx.getMediaType();
    if (contentType == null) {
        return;
    }
    if (!contentType.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE) && !contentType.isCompatible(MediaType.MULTIPART_FORM_DATA_TYPE)) {
        return;
    }
    security.validateSession(ctx.getForm().getString("csrf"));
}
Also used : MinijaxRequestContext(org.minijax.MinijaxRequestContext) MediaType(javax.ws.rs.core.MediaType)

Example 2 with MinijaxRequestContext

use of org.minijax.MinijaxRequestContext in project minijax by minijax.

the class RolesAllowedTest method testLogin.

@Test
public void testLogin() throws Exception {
    try (final MinijaxRequestContext ctx = createRequestContext()) {
        final NewCookie cookie = ctx.get(Security.class).login("alice@example.com", "alicepwd");
        assertNotNull(cookie);
        assertNotNull(cookie.getValue());
    }
}
Also used : MinijaxRequestContext(org.minijax.MinijaxRequestContext) NewCookie(javax.ws.rs.core.NewCookie) MinijaxTest(org.minijax.test.MinijaxTest) Test(org.junit.Test)

Example 3 with MinijaxRequestContext

use of org.minijax.MinijaxRequestContext in project minijax by minijax.

the class PersistenceContextTest method testPersistenceContextInject.

@Test
public void testPersistenceContextInject() throws IOException {
    final Minijax container = new Minijax().register(PersistenceFeature.class).register(PersistenceContextDao.class);
    final MinijaxApplication application = container.getDefaultApplication();
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/"));
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, request, null)) {
        final PersistenceContextDao dao = container.getResource(PersistenceContextDao.class);
        final Widget widget = new Widget();
        widget.setName("test");
        final Widget result = dao.create(widget);
        assertNotNull(result);
        assertNotNull(result.getId());
    }
    container.getInjector().close();
}
Also used : MinijaxApplication(org.minijax.MinijaxApplication) MinijaxRequestContext(org.minijax.MinijaxRequestContext) Minijax(org.minijax.Minijax) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) Widget(org.minijax.db.test.Widget) Test(org.junit.Test)

Example 4 with MinijaxRequestContext

use of org.minijax.MinijaxRequestContext in project minijax by minijax.

the class WebSocketTest method testRun.

@Test
public void testRun() throws Exception {
    final Minijax minijax = createMinijax();
    minijax.register(WebSocketResource.class);
    minijax.start();
    final MinijaxApplication application = minijax.getDefaultApplication();
    final DeploymentInfo deploymentInfo = new DeploymentInfo();
    MinijaxWebSocketUtils.init(deploymentInfo, application);
    final WebSocketDeploymentInfo webSocketDeploymentInfo = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
    final ServerEndpointConfig endpointConfig = webSocketDeploymentInfo.getProgramaticEndpoints().get(0);
    final MinijaxWebSocketConfigurator configurator = (MinijaxWebSocketConfigurator) endpointConfig.getConfigurator();
    final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/echo"));
    try (MinijaxRequestContext context = new MinijaxRequestContext(application, request, null)) {
        configurator.modifyHandshake(endpointConfig, null, null);
        final AnnotatedEndpoint endpoint = configurator.getEndpointInstance(AnnotatedEndpoint.class);
        assertNotNull(endpoint);
    }
}
Also used : MinijaxApplication(org.minijax.MinijaxApplication) MinijaxRequestContext(org.minijax.MinijaxRequestContext) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Minijax(org.minijax.Minijax) MockHttpServletRequest(org.minijax.test.MockHttpServletRequest) AnnotatedEndpoint(io.undertow.websockets.jsr.annotated.AnnotatedEndpoint) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) WebSocketDeploymentInfo(io.undertow.websockets.jsr.WebSocketDeploymentInfo) Test(org.junit.Test)

Example 5 with MinijaxRequestContext

use of org.minijax.MinijaxRequestContext in project minijax by minijax.

the class CookieParamProvider method get.

@Override
public T get() {
    final MinijaxRequestContext context = MinijaxRequestContext.getThreadLocal();
    final Cookie cookie = context.getCookies().get(key.getName());
    final String cookieValue = cookie == null ? null : cookie.getValue();
    return context.getApplication().convertParamToType(cookieValue, key.getType(), key.getAnnotations());
}
Also used : Cookie(javax.ws.rs.core.Cookie) MinijaxRequestContext(org.minijax.MinijaxRequestContext)

Aggregations

MinijaxRequestContext (org.minijax.MinijaxRequestContext)25 Test (org.junit.Test)16 MinijaxTest (org.minijax.test.MinijaxTest)13 Response (javax.ws.rs.core.Response)10 Form (javax.ws.rs.core.Form)8 Cookie (javax.ws.rs.core.Cookie)7 Minijax (org.minijax.Minijax)4 MinijaxApplication (org.minijax.MinijaxApplication)4 MockHttpServletRequest (org.minijax.test.MockHttpServletRequest)3 IOException (java.io.IOException)2 NotFoundException (javax.ws.rs.NotFoundException)2 BeforeClass (org.junit.BeforeClass)2 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)1 WebSocketDeploymentInfo (io.undertow.websockets.jsr.WebSocketDeploymentInfo)1 AnnotatedEndpoint (io.undertow.websockets.jsr.annotated.AnnotatedEndpoint)1 InputStream (java.io.InputStream)1 InjectionException (javax.enterprise.inject.InjectionException)1 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)1 DefaultValue (javax.ws.rs.DefaultValue)1 MediaType (javax.ws.rs.core.MediaType)1