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