use of org.apache.ignite.internal.rest.api.RestApiHttpResponse in project ignite-3 by apache.
the class RestApiHandler method channelRead0.
/**
* {@inheritDoc}
*/
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
CompletableFuture<DefaultFullHttpResponse> responseFuture = router.route(request).map(route -> {
var response = new RestApiHttpResponse(new DefaultHttpResponse(HttpVersion.HTTP_1_1, OK));
return route.handle(request, response).thenApply(resp -> {
ByteBuf content = resp.content() != null ? Unpooled.wrappedBuffer(resp.content()) : new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT);
return new DefaultFullHttpResponse(resp.protocolVersion(), resp.status(), content, resp.headers(), EmptyHttpHeaders.INSTANCE);
});
}).orElseGet(() -> CompletableFuture.completedFuture(new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.NOT_FOUND)));
responseFuture.whenCompleteAsync((response, e) -> {
if (e != null) {
exceptionCaught(ctx, e);
return;
}
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
boolean keepAlive = HttpUtil.isKeepAlive(request);
if (keepAlive) {
if (!request.protocolVersion().isKeepAliveDefault()) {
response.headers().set(CONNECTION, KEEP_ALIVE);
}
} else {
response.headers().set(CONNECTION, CLOSE);
}
ChannelFuture f = ctx.writeAndFlush(response);
if (!keepAlive) {
f.addListener(ChannelFutureListener.CLOSE);
}
}, ctx.executor());
}
use of org.apache.ignite.internal.rest.api.RestApiHttpResponse in project ignite-3 by apache.
the class ConfigurationHttpHandlers method handleUpdate.
/**
* Handle a configuration update request as json.
*
* @param req Rest request.
* @param res Rest response.
* @param presentation Configuration presentation.
*/
private static CompletableFuture<RestApiHttpResponse> handleUpdate(RestApiHttpRequest req, RestApiHttpResponse res, ConfigurationPresentation<String> presentation) {
String updateReq = req.request().content().toString(StandardCharsets.UTF_8);
return presentation.update(updateReq).thenApply(v -> res).exceptionally(e -> {
if (e instanceof CompletionException) {
e = e.getCause();
}
ErrorResult errRes;
if (e instanceof IllegalArgumentException) {
errRes = new ErrorResult("INVALID_CONFIG_FORMAT", e.getMessage());
} else if (e instanceof ConfigurationValidationException) {
errRes = new ErrorResult("VALIDATION_EXCEPTION", e.getMessage());
} else if (e instanceof IgniteException) {
errRes = new ErrorResult("APPLICATION_EXCEPTION", e.getMessage());
} else {
throw new CompletionException(e);
}
res.status(BAD_REQUEST);
res.json(Map.of("error", errRes));
return res;
});
}
Aggregations