use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class RolesAllowedTest method testLoginIncorrectPassword.
@Test
void testLoginIncorrectPassword() throws Exception {
try (final MinijaxRequestContext ctx = createRequestContext()) {
final LoginResult result = ctx.getResource(Security.class).login("alice@example.com", "wrong_password");
assertEquals(LoginResult.Status.INCORRECT, result.getStatus());
}
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class PersistenceContextTest method testPersistenceContextInject.
@Test
void testPersistenceContextInject() throws IOException {
final Minijax container = new Minijax().register(PersistenceFeature.class).register(PersistenceContextDao.class);
final MinijaxApplication application = container.getDefaultApplication();
try (MinijaxRequestContext context = new MinijaxTestRequestContext(application, GET, "/")) {
final PersistenceContextDao dao = context.getResource(PersistenceContextDao.class);
final Widget widget = new Widget();
widget.setId(123);
widget.setName("test");
final Widget result = dao.create(widget);
assertNotNull(result);
assertEquals(123, result.getId());
}
container.getInjector().close();
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class ListenerTest method testHappyPath.
@Test
void testHappyPath() throws IOException {
final Minijax minijax = new Minijax().register(TestSocket.class);
final MinijaxApplication application = minijax.getDefaultApplication();
try (final MinijaxRequestContext ctx = createRequestContext()) {
final MinijaxUndertowWebSocketAdapter adapter = new MinijaxUndertowWebSocketAdapter(ctx, TestSocket.class);
final WebSocketHttpExchange exchange = mock(WebSocketHttpExchange.class);
final MinijaxUndertowWebSocketListener listener = new MinijaxUndertowWebSocketListener(application, adapter, exchange);
final WebSocketChannel channel = mock(WebSocketChannel.class);
final BufferedTextMessage message = mock(BufferedTextMessage.class);
listener.onFullTextMessage(channel, message);
}
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class MinijaxServlet method service.
@Override
protected void service(final HttpServletRequest servletRequest, final HttpServletResponse servletResponse) {
final MinijaxApplication application = minijax.getDefaultApplication();
try (final MinijaxRequestContext ctx = new MinijaxServletRequestContext(application, servletRequest)) {
final Response response = application.handle(ctx);
servletResponse.setStatus(response.getStatus());
for (final Entry<String, List<Object>> entry : response.getHeaders().entrySet()) {
final String name = entry.getKey();
for (final Object value : entry.getValue()) {
servletResponse.addHeader(name, value.toString());
}
}
final MediaType mediaType = response.getMediaType();
if (mediaType != null) {
servletResponse.setContentType(mediaType.toString());
}
EntityUtils.writeEntity(response.getEntity(), mediaType, ctx.getProviders(), servletResponse.getOutputStream());
} catch (final IOException ex) {
LOG.error("Unhandled exception: {}", ex.getMessage(), ex);
}
}
use of org.minijax.rs.MinijaxRequestContext in project minijax by minijax.
the class MinijaxUndertowServer method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
final MinijaxApplication application = minijax.getDefaultApplication();
try (final MinijaxRequestContext ctx = new MinijaxUndertowRequestContext(application, exchange)) {
final Response response = application.handle(ctx);
exchange.setStatusCode(response.getStatus());
for (final Entry<String, List<Object>> entry : response.getHeaders().entrySet()) {
final String name = entry.getKey();
for (final Object value : entry.getValue()) {
HttpString headerString = Headers.fromCache(name);
if (headerString == null) {
headerString = HttpString.tryFromString(name);
}
exchange.getResponseHeaders().add(headerString, value.toString());
}
}
final MediaType mediaType = response.getMediaType();
if (mediaType != null) {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, mediaType.toString());
}
EntityUtils.writeEntity(response.getEntity(), mediaType, ctx.getProviders(), exchange.getOutputStream());
} catch (final Exception ex) {
// NOSONAR
LOG.error("Unhandled exception: {}", ex.getMessage(), ex);
// NOSONAR
throw ex;
}
}
Aggregations