use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.
the class JaegerPortUnificationHandler 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 Jaeger spans.
if (!path.equals(JAEGER_VALID_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(JAEGER_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);
Batch batch = new Batch();
new TDeserializer().deserialize(batch, bytesArray);
processBatch(batch, output, DEFAULT_SOURCE, proxyLevelApplicationName, spanHandler, spanLogsHandler, wfInternalReporter, traceDisabled, spanLogsDisabled, preprocessorSupplier, sampler, traceDerivedCustomTagKeys, discardedTraces, discardedBatches, discardedSpansBySampler, discoveredHeartbeatMetrics, receivedSpansTotal);
status = HttpResponseStatus.ACCEPTED;
processedBatches.inc();
} catch (Exception e) {
failedBatches.inc();
output.append(errorMessageWithRootCause(e));
status = HttpResponseStatus.BAD_REQUEST;
logger.log(Level.WARNING, "Jaeger HTTP 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 RelayPortUnificationHandler method handleHttpMessage.
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) {
URI uri = URI.create(request.uri());
StringBuilder output = new StringBuilder();
String path = uri.getPath();
final boolean isDirectIngestion = path.startsWith("/report");
if (path.endsWith("/checkin") && (path.startsWith("/api/daemon") || path.contains("wfproxy"))) {
// simulate checkin response for proxy chaining
ObjectNode jsonResponse = JsonNodeFactory.instance.objectNode();
jsonResponse.put("currentTime", Clock.now());
jsonResponse.put("allowAnyHostKeys", true);
writeHttpResponse(ctx, HttpResponseStatus.OK, jsonResponse, request);
return;
}
String format = URLEncodedUtils.parse(uri, CharsetUtil.UTF_8).stream().filter(x -> x.getName().equals("format") || x.getName().equals("f")).map(NameValuePair::getValue).findFirst().orElse(Constants.PUSH_FORMAT_WAVEFRONT);
// Return HTTP 200 (OK) for payloads received on the proxy endpoint
// Return HTTP 202 (ACCEPTED) for payloads received on the DDI endpoint
// Return HTTP 204 (NO_CONTENT) for payloads received on all other endpoints
HttpResponseStatus okStatus;
if (isDirectIngestion) {
okStatus = HttpResponseStatus.ACCEPTED;
} else if (path.contains("/pushdata/") || path.contains("wfproxy/report")) {
okStatus = HttpResponseStatus.OK;
} else {
okStatus = HttpResponseStatus.NO_CONTENT;
}
HttpResponseStatus status;
switch(format) {
case Constants.PUSH_FORMAT_HISTOGRAM:
if (isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get(), output, request)) {
status = HttpResponseStatus.FORBIDDEN;
break;
}
case Constants.PUSH_FORMAT_WAVEFRONT:
case Constants.PUSH_FORMAT_GRAPHITE_V2:
AtomicBoolean hasSuccessfulPoints = new AtomicBoolean(false);
try {
// noinspection unchecked
ReportableEntityDecoder<String, ReportPoint> histogramDecoder = (ReportableEntityDecoder<String, ReportPoint>) decoders.get(ReportableEntityType.HISTOGRAM);
Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(message -> {
DataFormat dataFormat = DataFormat.autodetect(message);
switch(dataFormat) {
case EVENT:
wavefrontHandler.reject(message, "Relay port does not support " + "event-formatted data!");
break;
case SOURCE_TAG:
wavefrontHandler.reject(message, "Relay port does not support " + "sourceTag-formatted data!");
break;
case HISTOGRAM:
if (isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get(), output)) {
break;
}
preprocessAndHandlePoint(message, histogramDecoder, histogramHandlerSupplier.get(), preprocessorSupplier, ctx, "histogram");
hasSuccessfulPoints.set(true);
break;
default:
// only apply annotator if point received on the DDI endpoint
message = annotator != null && isDirectIngestion ? annotator.apply(ctx, message) : message;
preprocessAndHandlePoint(message, wavefrontDecoder, wavefrontHandler, preprocessorSupplier, ctx, "metric");
hasSuccessfulPoints.set(true);
break;
}
});
status = hasSuccessfulPoints.get() ? okStatus : HttpResponseStatus.BAD_REQUEST;
} catch (Exception e) {
status = HttpResponseStatus.BAD_REQUEST;
output.append(errorMessageWithRootCause(e));
logWarning("WF-300: Failed to handle HTTP POST", e, ctx);
}
break;
case Constants.PUSH_FORMAT_TRACING:
if (isFeatureDisabled(traceDisabled, SPAN_DISABLED, discardedSpans.get(), output, request)) {
receivedSpansTotal.get().inc(discardedSpans.get().count());
status = HttpResponseStatus.FORBIDDEN;
break;
}
List<Span> spans = new ArrayList<>();
// noinspection unchecked
ReportableEntityDecoder<String, Span> spanDecoder = (ReportableEntityDecoder<String, Span>) decoders.get(ReportableEntityType.TRACE);
ReportableEntityHandler<Span, String> spanHandler = spanHandlerSupplier.get();
Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(line -> {
try {
receivedSpansTotal.get().inc();
spanDecoder.decode(line, spans, "dummy");
} catch (Exception e) {
spanHandler.reject(line, formatErrorMessage(line, e, ctx));
}
});
spans.forEach(spanHandler::report);
status = okStatus;
break;
case Constants.PUSH_FORMAT_TRACING_SPAN_LOGS:
if (isFeatureDisabled(spanLogsDisabled, SPANLOGS_DISABLED, discardedSpanLogs.get(), output, request)) {
status = HttpResponseStatus.FORBIDDEN;
break;
}
List<SpanLogs> spanLogs = new ArrayList<>();
// noinspection unchecked
ReportableEntityDecoder<JsonNode, SpanLogs> spanLogDecoder = (ReportableEntityDecoder<JsonNode, SpanLogs>) decoders.get(ReportableEntityType.TRACE_SPAN_LOGS);
ReportableEntityHandler<SpanLogs, String> spanLogsHandler = spanLogsHandlerSupplier.get();
Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(line -> {
try {
spanLogDecoder.decode(JSON_PARSER.readTree(line), spanLogs, "dummy");
} catch (Exception e) {
spanLogsHandler.reject(line, formatErrorMessage(line, e, ctx));
}
});
spanLogs.forEach(spanLogsHandler::report);
status = okStatus;
break;
default:
status = HttpResponseStatus.BAD_REQUEST;
logger.warning("Unexpected format for incoming HTTP request: " + format);
}
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 AdminPortUnificationHandler method handleHttpMessage.
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) throws URISyntaxException {
StringBuilder output = new StringBuilder();
String remoteIp = ((InetSocketAddress) ctx.channel().remoteAddress()).getAddress().getHostAddress();
if (remoteIpAllowRegex != null && !Pattern.compile(remoteIpAllowRegex).matcher(remoteIp).matches()) {
logger.warning("Incoming request from non-allowed remote address " + remoteIp + " rejected!");
writeHttpResponse(ctx, HttpResponseStatus.UNAUTHORIZED, output, request);
return;
}
URI uri = new URI(request.uri());
HttpResponseStatus status;
Matcher path = PATH.matcher(uri.getPath());
if (path.matches()) {
String strPort = path.group(2);
Integer port = NumberUtils.isNumber(strPort) ? Integer.parseInt(strPort) : null;
if (StringUtils.isBlank(strPort) || port != null) {
switch(path.group(1)) {
case "status":
if (request.method().equals(HttpMethod.GET)) {
if (port == null) {
output.append("Status check requires a specific port");
status = HttpResponseStatus.BAD_REQUEST;
} else {
// return 200 if status check ok, 503 if not
status = healthCheck.isHealthy(port) ? HttpResponseStatus.OK : HttpResponseStatus.SERVICE_UNAVAILABLE;
output.append(status.reasonPhrase());
}
} else {
status = HttpResponseStatus.METHOD_NOT_ALLOWED;
}
break;
case "enable":
if (request.method().equals(HttpMethod.POST)) {
if (port == null) {
logger.info("Request to mark all HTTP ports as healthy from remote: " + remoteIp);
healthCheck.setAllHealthy();
} else {
logger.info("Marking HTTP port " + port + " as healthy, remote: " + remoteIp);
healthCheck.setHealthy(port);
}
status = HttpResponseStatus.OK;
} else {
status = HttpResponseStatus.METHOD_NOT_ALLOWED;
}
break;
case "disable":
if (request.method().equals(HttpMethod.POST)) {
if (port == null) {
logger.info("Request to mark all HTTP ports as unhealthy from remote: " + remoteIp);
healthCheck.setAllUnhealthy();
} else {
logger.info("Marking HTTP port " + port + " as unhealthy, remote: " + remoteIp);
healthCheck.setUnhealthy(port);
}
status = HttpResponseStatus.OK;
} else {
status = HttpResponseStatus.METHOD_NOT_ALLOWED;
}
break;
default:
status = HttpResponseStatus.BAD_REQUEST;
}
} else {
status = HttpResponseStatus.BAD_REQUEST;
}
} else {
status = HttpResponseStatus.NOT_FOUND;
}
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 HttpHealthCheckEndpointHandler method handleHttpMessage.
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) {
StringBuilder output = new StringBuilder();
HttpResponseStatus status = HttpResponseStatus.NOT_FOUND;
writeHttpResponse(ctx, status, output, request);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project reactor-netty by reactor.
the class HttpClientWithTomcatTest method disableChunkImplicitDefault.
@Test
void disableChunkImplicitDefault() {
ConnectionProvider p = ConnectionProvider.create("disableChunkImplicitDefault", 1);
HttpClient client = HttpClient.create(p).host("localhost").port(getPort()).wiretap(true);
Tuple2<HttpResponseStatus, Channel> r = client.get().uri("/status/404").responseConnection((res, conn) -> Mono.just(res.status()).delayUntil(s -> conn.inbound().receive()).zipWith(Mono.just(conn.channel()))).blockLast(Duration.ofSeconds(30));
assertThat(r).isNotNull();
Channel r2 = client.get().uri("/status/404").responseConnection((res, conn) -> Mono.just(conn.channel()).delayUntil(s -> conn.inbound().receive())).blockLast(Duration.ofSeconds(30));
assertThat(r2).isNotNull();
assertThat(r.getT2()).isSameAs(r2);
assertThat(r.getT1()).isEqualTo(HttpResponseStatus.NOT_FOUND);
p.dispose();
}
Aggregations