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