Search in sources :

Example 31 with MinijaxRequestContext

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

the class CsrfFilterTest method setUpCsrfFilterTest.

@BeforeAll
@SuppressWarnings("unchecked")
public static void setUpCsrfFilterTest() throws IOException {
    register(PersistenceFeature.class);
    register(new SecurityFeature(User.class, Dao.class));
    register(CsrfFilterTest.class);
    try (MinijaxRequestContext ctx = createRequestContext()) {
        user = new User();
        user.setName("Alice");
        user.setEmail("alice_csrf@example.com");
        user.setRoles("user");
        final Dao dao = ctx.getResource(Dao.class);
        user = dao.create(user);
        final Security<User> security = ctx.getResource(Security.class);
        cookie = security.loginAs(user);
    }
}
Also used : MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 32 with MinijaxRequestContext

use of org.minijax.rs.MinijaxRequestContext 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)

Example 33 with MinijaxRequestContext

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

the class ResponseTest method testWidgetEntity.

@Test
void testWidgetEntity() throws IOException {
    register(WidgetWriter.class);
    final Widget widget = new Widget();
    widget.setId("123");
    widget.setValue("Hello");
    try (final MinijaxRequestContext context = createRequestContext()) {
        final MinijaxResponse r = new MinijaxResponseBuilder(context).entity(widget).build();
        assertEquals(widget, r.getEntity());
        assertEquals(Widget.class, r.getEntityClass());
        assertEquals("(widget 123 Hello)", r.readEntity(String.class));
    }
}
Also used : MinijaxRequestContext(org.minijax.rs.MinijaxRequestContext) Widget(org.minijax.rs.Widget) Test(org.junit.jupiter.api.Test) MinijaxTest(org.minijax.rs.test.MinijaxTest)

Aggregations

MinijaxRequestContext (org.minijax.rs.MinijaxRequestContext)33 Test (org.junit.jupiter.api.Test)23 MinijaxTest (org.minijax.rs.test.MinijaxTest)21 Response (jakarta.ws.rs.core.Response)13 Form (jakarta.ws.rs.core.Form)8 MinijaxApplication (org.minijax.rs.MinijaxApplication)7 Cookie (jakarta.ws.rs.core.Cookie)6 MediaType (jakarta.ws.rs.core.MediaType)4 Minijax (org.minijax.Minijax)4 IOException (java.io.IOException)3 List (java.util.List)3 NotFoundException (jakarta.ws.rs.NotFoundException)2 InputStream (java.io.InputStream)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 MinijaxTestRequestContext (org.minijax.rs.test.MinijaxTestRequestContext)2 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 HttpString (io.undertow.util.HttpString)1