Search in sources :

Example 11 with MinijaxApplication

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

the class RequestContextTest method testBasic.

@Test
void testBasic() throws Exception {
    final Minijax minijax = new Minijax();
    final MinijaxApplication app = minijax.getDefaultApplication();
    final Map<String, List<String>> headerMap = new HashMap<>();
    headerMap.computeIfAbsent(HttpHeaders.CONTENT_TYPE, k -> new ArrayList<>()).add("text/plain");
    final WebSocketHttpExchange exchange = mock(WebSocketHttpExchange.class);
    when(exchange.getRequestHeaders()).thenReturn(headerMap);
    try (final MinijaxUndertowWebSocketRequestContext ctx = new MinijaxUndertowWebSocketRequestContext(app, exchange)) {
        assertEquals(GET, ctx.getMethod());
        assertNotNull(ctx.getUriInfo());
        assertNotNull(ctx.getHttpHeaders());
        assertNull(ctx.getEntityStream());
    }
}
Also used : Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) List(java.util.List) Minijax(org.minijax.Minijax) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) Map(java.util.Map) MinijaxApplication(org.minijax.rs.MinijaxApplication) Assertions(org.junit.jupiter.api.Assertions) HashMap(java.util.HashMap) HttpMethod(jakarta.ws.rs.HttpMethod) ArrayList(java.util.ArrayList) WebSocketHttpExchange(io.undertow.websockets.spi.WebSocketHttpExchange) MinijaxApplication(org.minijax.rs.MinijaxApplication) HashMap(java.util.HashMap) Minijax(org.minijax.Minijax) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 12 with MinijaxApplication

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

the class ServerHandler method process.

private void process(final ChannelHandlerContext nettyCtx, final FullHttpRequest request) throws Exception {
    // NOSONAR
    final MinijaxApplication application = minijax.getDefaultApplication();
    try (final MinijaxRequestContext minijaxCtx = new MinijaxNettyRequestContext(application, request)) {
        final Response minijaxResponse = application.handle(minijaxCtx);
        final MediaType mediaType = minijaxResponse.getMediaType();
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        EntityUtils.writeEntity(minijaxResponse.getEntity(), mediaType, minijaxCtx.getProviders(), outputStream);
        final int contentLength = outputStream.size();
        final ByteBuf buf = Unpooled.wrappedBuffer(outputStream.toByteArray());
        final HttpResponseStatus status = HttpResponseStatus.valueOf(minijaxResponse.getStatus());
        final DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, buf, false);
        response.headers().set(SERVER, SERVER_NAME).set(DATE, date).set(CONTENT_LENGTH, contentLength);
        if (mediaType != null) {
            response.headers().set(CONTENT_TYPE, mediaType);
        }
        for (final Entry<String, List<Object>> entry : minijaxResponse.getHeaders().entrySet()) {
            final String name = entry.getKey();
            for (final Object value : entry.getValue()) {
                response.headers().set(name, value);
            }
        }
        nettyCtx.write(response).addListener(ChannelFutureListener.CLOSE);
    } catch (final Exception ex) {
        // NOSONAR
        LOG.error("Unhandled exception: {}", ex.getMessage(), ex);
        // NOSONAR
        throw ex;
    }
}
Also used : MinijaxApplication(org.minijax.rs.MinijaxApplication) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) Response(jakarta.ws.rs.core.Response) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) MediaType(jakarta.ws.rs.core.MediaType) List(java.util.List)

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