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