Search in sources :

Example 1 with MinijaxApplication

use of org.minijax.rs.MinijaxApplication 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();
}
Also used : MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) Minijax(org.minijax.Minijax) MinijaxTestRequestContext(org.minijax.rs.test.MinijaxTestRequestContext) Test(org.junit.jupiter.api.Test)

Example 2 with MinijaxApplication

use of org.minijax.rs.MinijaxApplication 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);
    }
}
Also used : WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) Minijax(org.minijax.Minijax) WebSocketChannel(io.undertow.websockets.core.WebSocketChannel) BufferedTextMessage(io.undertow.websockets.core.BufferedTextMessage) Test(org.junit.jupiter.api.Test) MinijaxTest(org.minijax.rs.test.MinijaxTest)

Example 3 with MinijaxApplication

use of org.minijax.rs.MinijaxApplication 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);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MediaType(jakarta.ws.rs.core.MediaType) List(java.util.List) IOException(java.io.IOException)

Example 4 with MinijaxApplication

use of org.minijax.rs.MinijaxApplication 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;
    }
}
Also used : Response(jakarta.ws.rs.core.Response) MinijaxApplication(org.minijax.rs.MinijaxApplication) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MediaType(jakarta.ws.rs.core.MediaType) List(java.util.List) HttpString(io.undertow.util.HttpString) HttpString(io.undertow.util.HttpString)

Example 5 with MinijaxApplication

use of org.minijax.rs.MinijaxApplication in project minijax by minijax.

the class MustacheFeature method configure.

@Override
public boolean configure(final FeatureContext context) {
    ((MinijaxApplication) context).bind(new DefaultMustacheFactory(), MustacheFactory.class);
    context.register(MinijaxMustacheWriter.class);
    context.register(MinijaxMustacheExceptionMapper.class);
    return true;
}
Also used : MinijaxApplication(org.minijax.rs.MinijaxApplication) DefaultMustacheFactory(com.github.mustachejava.DefaultMustacheFactory)

Aggregations

MinijaxApplication (org.minijax.rs.MinijaxApplication)12 MinijaxRequestContext (org.minijax.rs.MinijaxRequestContext)7 Minijax (org.minijax.Minijax)5 List (java.util.List)4 Test (org.junit.jupiter.api.Test)4 MediaType (jakarta.ws.rs.core.MediaType)3 Response (jakarta.ws.rs.core.Response)3 WebSocketHttpExchange (io.undertow.websockets.spi.WebSocketHttpExchange)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 MinijaxUriInfo (org.minijax.rs.MinijaxUriInfo)2 MinijaxTestRequestContext (org.minijax.rs.test.MinijaxTestRequestContext)2 DefaultMustacheFactory (com.github.mustachejava.DefaultMustacheFactory)1 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 AsciiString (io.netty.util.AsciiString)1 BlockingHandler (io.undertow.server.handlers.BlockingHandler)1 PathHandler (io.undertow.server.handlers.PathHandler)1 HttpString (io.undertow.util.HttpString)1