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"));
}
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());
}
}
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();
}
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);
}
}
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());
}
Aggregations