Search in sources :

Example 1 with RestApiHttpResponse

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());
}
Also used : UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) CONNECTION(io.netty.handler.codec.http.HttpHeaderNames.CONNECTION) CONTENT_LENGTH(io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH) HttpVersion(io.netty.handler.codec.http.HttpVersion) KEEP_ALIVE(io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) CompletableFuture(java.util.concurrent.CompletableFuture) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) IgniteLogger(org.apache.ignite.lang.IgniteLogger) Unpooled(io.netty.buffer.Unpooled) ChannelFuture(io.netty.channel.ChannelFuture) RestApiHttpResponse(org.apache.ignite.internal.rest.api.RestApiHttpResponse) EmptyHttpHeaders(io.netty.handler.codec.http.EmptyHttpHeaders) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) ChannelFutureListener(io.netty.channel.ChannelFutureListener) CLOSE(io.netty.handler.codec.http.HttpHeaderValues.CLOSE) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Router(org.apache.ignite.internal.rest.routes.Router) OK(io.netty.handler.codec.http.HttpResponseStatus.OK) HttpUtil(io.netty.handler.codec.http.HttpUtil) ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) RestApiHttpResponse(org.apache.ignite.internal.rest.api.RestApiHttpResponse) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf)

Example 2 with RestApiHttpResponse

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;
    });
}
Also used : IgniteException(org.apache.ignite.lang.IgniteException) RestHandlersRegister(org.apache.ignite.internal.rest.api.RestHandlersRegister) HoconPresentation(org.apache.ignite.internal.configuration.rest.presentation.hocon.HoconPresentation) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionException(java.util.concurrent.CompletionException) ConfigurationManager(org.apache.ignite.internal.configuration.ConfigurationManager) StandardCharsets(java.nio.charset.StandardCharsets) RestApiHttpRequest(org.apache.ignite.internal.rest.api.RestApiHttpRequest) RestApiHttpResponse(org.apache.ignite.internal.rest.api.RestApiHttpResponse) BAD_REQUEST(io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST) ConfigurationPresentation(org.apache.ignite.internal.configuration.rest.presentation.ConfigurationPresentation) ErrorResult(org.apache.ignite.internal.rest.api.ErrorResult) APPLICATION_JSON(io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON) Routes(org.apache.ignite.internal.rest.api.Routes) Map(java.util.Map) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) IgniteException(org.apache.ignite.lang.IgniteException) CompletionException(java.util.concurrent.CompletionException) ErrorResult(org.apache.ignite.internal.rest.api.ErrorResult)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)2 RestApiHttpResponse (org.apache.ignite.internal.rest.api.RestApiHttpResponse)2 ByteBuf (io.netty.buffer.ByteBuf)1 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 EmptyHttpHeaders (io.netty.handler.codec.http.EmptyHttpHeaders)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 CONNECTION (io.netty.handler.codec.http.HttpHeaderNames.CONNECTION)1 CONTENT_LENGTH (io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH)1 APPLICATION_JSON (io.netty.handler.codec.http.HttpHeaderValues.APPLICATION_JSON)1 CLOSE (io.netty.handler.codec.http.HttpHeaderValues.CLOSE)1 KEEP_ALIVE (io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1