use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project flink by apache.
the class HandlerUtils method sendResponse.
/**
* Sends the given response and status code to the given channel.
*
* @param channelHandlerContext identifying the open channel
* @param keepAlive If the connection should be kept alive.
* @param message which should be sent
* @param statusCode of the message to send
* @param headers additional header values
*/
public static CompletableFuture<Void> sendResponse(@Nonnull ChannelHandlerContext channelHandlerContext, boolean keepAlive, @Nonnull String message, @Nonnull HttpResponseStatus statusCode, @Nonnull Map<String, String> headers) {
HttpResponse response = new DefaultHttpResponse(HTTP_1_1, statusCode);
response.headers().set(CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
for (Map.Entry<String, String> headerEntry : headers.entrySet()) {
response.headers().set(headerEntry.getKey(), headerEntry.getValue());
}
if (keepAlive) {
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
byte[] buf = message.getBytes(ConfigConstants.DEFAULT_CHARSET);
ByteBuf b = Unpooled.copiedBuffer(buf);
HttpHeaders.setContentLength(response, buf.length);
// write the initial line and the header.
channelHandlerContext.write(response);
channelHandlerContext.write(b);
ChannelFuture lastContentFuture = channelHandlerContext.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
// close the connection, if no keep-alive is needed
if (!keepAlive) {
lastContentFuture.addListener(ChannelFutureListener.CLOSE);
}
return toCompletableFuture(lastContentFuture);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project crate by crate.
the class HttpBlobHandler method writeToFile.
private void writeToFile(HttpRequest request, ByteBuf input, boolean last, final boolean continueExpected) throws IOException {
if (digestBlob == null) {
throw new IllegalStateException("digestBlob is null in writeToFile");
}
RemoteDigestBlob.Status status = digestBlob.addContent(input, last);
HttpResponseStatus exitStatus = null;
switch(status) {
case FULL:
exitStatus = HttpResponseStatus.CREATED;
break;
case PARTIAL:
// tell the client to continue
if (continueExpected) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.CONTINUE));
}
return;
case MISMATCH:
exitStatus = HttpResponseStatus.BAD_REQUEST;
break;
case EXISTS:
exitStatus = HttpResponseStatus.CONFLICT;
break;
case FAILED:
exitStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
break;
default:
throw new IllegalArgumentException("Unknown status: " + status);
}
assert exitStatus != null : "exitStatus should not be null";
LOGGER.trace("writeToFile exit status http:{} blob: {}", exitStatus, status);
simpleResponse(request, exitStatus);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.
the class ZipkinPortUnificationHandler method handleHttpMessage.
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) throws URISyntaxException {
URI uri = new URI(request.uri());
String path = uri.getPath().endsWith("/") ? uri.getPath() : uri.getPath() + "/";
// Validate Uri Path and HTTP method of incoming Zipkin spans.
if (!ZIPKIN_VALID_PATHS.contains(path)) {
writeHttpResponse(ctx, HttpResponseStatus.BAD_REQUEST, "Unsupported URL path.", request);
logWarning("Requested URI path '" + path + "' is not supported.", null, ctx);
return;
}
if (!request.method().toString().equalsIgnoreCase(ZIPKIN_VALID_HTTP_METHOD)) {
writeHttpResponse(ctx, HttpResponseStatus.BAD_REQUEST, "Unsupported Http method.", request);
logWarning("Requested http method '" + request.method().toString() + "' is not supported.", null, ctx);
return;
}
HttpResponseStatus status;
StringBuilder output = new StringBuilder();
try {
byte[] bytesArray = new byte[request.content().nioBuffer().remaining()];
request.content().nioBuffer().get(bytesArray, 0, bytesArray.length);
BytesDecoder<zipkin2.Span> decoder = SpanBytesDecoderDetector.decoderForListMessage(bytesArray);
List<zipkin2.Span> zipkinSpanSink = new ArrayList<>();
decoder.decodeList(bytesArray, zipkinSpanSink);
// Handle case when tracing is disabled, ignore reported spans.
if (isFeatureDisabled(traceDisabled, SPAN_DISABLED, discardedBatches, output)) {
status = HttpResponseStatus.ACCEPTED;
writeHttpResponse(ctx, status, output, request);
discardedTraces.inc(zipkinSpanSink.size());
receivedSpansTotal.inc(zipkinSpanSink.size());
processedBatches.inc();
return;
}
receivedSpansTotal.inc(zipkinSpanSink.size());
processZipkinSpans(zipkinSpanSink);
status = HttpResponseStatus.ACCEPTED;
processedBatches.inc();
} catch (Exception e) {
failedBatches.inc();
output.append(errorMessageWithRootCause(e));
status = HttpResponseStatus.BAD_REQUEST;
logger.log(Level.WARNING, "Zipkin batch processing failed", Throwables.getRootCause(e));
}
writeHttpResponse(ctx, status, output, request);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.
the class WriteHttpJsonPortUnificationHandler method handleHttpMessage.
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) {
HttpResponseStatus status = HttpResponseStatus.OK;
String requestBody = request.content().toString(CharsetUtil.UTF_8);
try {
JsonNode metrics = jsonParser.readTree(requestBody);
if (!metrics.isArray()) {
logger.warning("metrics is not an array!");
pointHandler.reject((ReportPoint) null, "[metrics] is not an array!");
status = HttpResponseStatus.BAD_REQUEST;
writeHttpResponse(ctx, status, "", request);
return;
}
reportMetrics(metrics);
writeHttpResponse(ctx, status, "", request);
} catch (Exception e) {
status = HttpResponseStatus.BAD_REQUEST;
logWarning("WF-300: Failed to handle incoming write_http request", e, ctx);
writeHttpResponse(ctx, status, errorMessageWithRootCause(e), request);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.
the class AbstractLineDelimitedHandler method handleHttpMessage.
/**
* Handles an incoming HTTP message. Accepts HTTP POST on all paths
*/
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) {
StringBuilder output = new StringBuilder();
HttpResponseStatus status;
try {
DataFormat format = getFormat(request);
Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(line -> processLine(ctx, line, format));
status = HttpResponseStatus.ACCEPTED;
} catch (Exception e) {
status = HttpResponseStatus.BAD_REQUEST;
output.append(errorMessageWithRootCause(e));
logWarning("WF-300: Failed to handle HTTP POST", e, ctx);
}
writeHttpResponse(ctx, status, output, request);
}
Aggregations