Search in sources :

Example 91 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project crate by crate.

the class MainAndStaticFileHandler method clusterStateRespToHttpResponse.

private static FullHttpResponse clusterStateRespToHttpResponse(HttpMethod method, ClusterStateResponse response, ByteBufAllocator alloc, @Nullable String nodeName) {
    var httpStatus = response.getState().blocks().hasGlobalBlockWithStatus(RestStatus.SERVICE_UNAVAILABLE) ? HttpResponseStatus.SERVICE_UNAVAILABLE : HttpResponseStatus.OK;
    try {
        DefaultFullHttpResponse resp;
        if (method == HttpMethod.HEAD) {
            resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpStatus);
            HttpUtil.setContentLength(resp, 0);
        } else {
            var buffer = alloc.buffer();
            try (var outputStream = new ByteBufOutputStream(buffer)) {
                writeJSON(outputStream, response, httpStatus, nodeName);
            }
            resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpStatus, buffer);
            resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
            HttpUtil.setContentLength(resp, buffer.readableBytes());
        }
        return resp;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) IOException(java.io.IOException)

Example 92 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project crate by crate.

the class HttpBlobHandler method prepareResponse.

private HttpResponse prepareResponse(HttpResponseStatus status) {
    HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status);
    HttpUtil.setContentLength(response, 0);
    maybeSetConnectionCloseHeader(response);
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 93 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project crate by crate.

the class HttpBlobHandler method put.

private void put(HttpRequest request, HttpContent content, String index, String digest) throws IOException {
    if (digestBlob == null) {
        digestBlob = blobService.newBlob(index, digest);
    }
    boolean continueExpected = HttpUtil.is100ContinueExpected(currentMessage);
    if (content == null) {
        if (continueExpected) {
            ctx.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        return;
    }
    boolean isLast = content instanceof LastHttpContent;
    ByteBuf byteBuf = content.content();
    try {
        writeToFile(request, byteBuf, isLast, continueExpected);
    } finally {
        byteBuf.release();
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ByteBuf(io.netty.buffer.ByteBuf)

Example 94 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project crate by crate.

the class HttpBlobHandler method head.

private void head(HttpRequest request, String index, String digest) throws IOException {
    // this method only supports local mode, which is ok, since there
    // should be a redirect upfront if data is not local
    BlobShard blobShard = localBlobShard(index, digest);
    long length = blobShard.blobContainer().getFile(digest).length();
    if (length < 1) {
        simpleResponse(request, HttpResponseStatus.NOT_FOUND);
        return;
    }
    HttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
    HttpUtil.setContentLength(response, length);
    setDefaultGetHeaders(response);
    sendResponse(request, response);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) BlobShard(io.crate.blob.v2.BlobShard) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 95 with DefaultFullHttpResponse

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpResponse in project crate by crate.

the class SqlHttpHandler method sendResponse.

private void sendResponse(Session session, ChannelHandlerContext ctx, FullHttpRequest request, Map<String, List<String>> parameters, XContentBuilder result, @Nullable Throwable t) {
    final HttpVersion httpVersion = request.protocolVersion();
    final DefaultFullHttpResponse resp;
    final ByteBuf content;
    if (t == null) {
        content = Netty4Utils.toByteBuf(BytesReference.bytes(result));
        resp = new DefaultFullHttpResponse(httpVersion, HttpResponseStatus.OK, content);
        resp.headers().add(HttpHeaderNames.CONTENT_TYPE, result.contentType().mediaType());
    } else {
        var throwable = SQLExceptions.prepareForClientTransmission(getAccessControl.apply(session.sessionContext()), t);
        HttpError httpError = HttpError.fromThrowable(throwable);
        String mediaType;
        boolean includeErrorTrace = paramContainFlag(parameters, "error_trace");
        try (XContentBuilder contentBuilder = httpError.toXContent(includeErrorTrace)) {
            content = Netty4Utils.toByteBuf(BytesReference.bytes(contentBuilder));
            mediaType = contentBuilder.contentType().mediaType();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        resp = new DefaultFullHttpResponse(httpVersion, httpError.httpResponseStatus(), content);
        resp.headers().add(HttpHeaderNames.CONTENT_TYPE, mediaType);
    }
    Netty4CorsHandler.setCorsResponseHeaders(request, resp, corsConfig);
    resp.headers().add(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(content.readableBytes()));
    boolean closeConnection = isCloseConnection(request);
    ChannelPromise promise = ctx.newPromise();
    if (closeConnection) {
        promise.addListener(ChannelFutureListener.CLOSE);
    } else {
        Headers.setKeepAlive(httpVersion, resp);
    }
    ctx.writeAndFlush(resp, promise);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ChannelPromise(io.netty.channel.ChannelPromise) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) HttpVersion(io.netty.handler.codec.http.HttpVersion) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)223 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)145 ByteBuf (io.netty.buffer.ByteBuf)68 HttpResponse (io.netty.handler.codec.http.HttpResponse)39 Test (org.junit.Test)25 HttpRequest (io.netty.handler.codec.http.HttpRequest)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)23 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)22 ChannelFuture (io.netty.channel.ChannelFuture)19 IOException (java.io.IOException)19 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)16 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)15 HttpObject (io.netty.handler.codec.http.HttpObject)15 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 HttpVersion (io.netty.handler.codec.http.HttpVersion)12 InetSocketAddress (java.net.InetSocketAddress)12 SSLException (javax.net.ssl.SSLException)12 Test (org.junit.jupiter.api.Test)12 Map (java.util.Map)11