Search in sources :

Example 31 with HttpResponseStatus

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);
}
Also used : TDeserializer(org.apache.thrift.TDeserializer) Batch(io.jaegertracing.thriftjava.Batch) JaegerThriftUtils.processBatch(com.wavefront.agent.listeners.tracing.JaegerThriftUtils.processBatch) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 32 with HttpResponseStatus

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);
}
Also used : HealthCheckManager(com.wavefront.agent.channel.HealthCheckManager) FeatureCheckUtils.isFeatureDisabled(com.wavefront.agent.listeners.FeatureCheckUtils.isFeatureDisabled) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SPANLOGS_DISABLED(com.wavefront.agent.listeners.FeatureCheckUtils.SPANLOGS_DISABLED) SpanLogs(wavefront.report.SpanLogs) Supplier(java.util.function.Supplier) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DataFormat(com.wavefront.agent.formatter.DataFormat) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TokenAuthenticator(com.wavefront.agent.auth.TokenAuthenticator) ReportableEntityHandler(com.wavefront.agent.handlers.ReportableEntityHandler) ChannelUtils.errorMessageWithRootCause(com.wavefront.agent.channel.ChannelUtils.errorMessageWithRootCause) ChannelUtils.writeHttpResponse(com.wavefront.agent.channel.ChannelUtils.writeHttpResponse) Map(java.util.Map) SharedGraphiteHostAnnotator(com.wavefront.agent.channel.SharedGraphiteHostAnnotator) HandlerKey(com.wavefront.agent.handlers.HandlerKey) Constants(com.wavefront.api.agent.Constants) CharsetUtil(io.netty.util.CharsetUtil) MetricName(com.yammer.metrics.core.MetricName) ReportPoint(wavefront.report.ReportPoint) JsonNode(com.fasterxml.jackson.databind.JsonNode) Clock(com.wavefront.common.Clock) URI(java.net.URI) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) Counter(com.yammer.metrics.core.Counter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReportableEntityPreprocessor(com.wavefront.agent.preprocessor.ReportableEntityPreprocessor) SPAN_DISABLED(com.wavefront.agent.listeners.FeatureCheckUtils.SPAN_DISABLED) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Span(wavefront.report.Span) Logger(java.util.logging.Logger) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ReportableEntityType(com.wavefront.data.ReportableEntityType) List(java.util.List) ChannelUtils.formatErrorMessage(com.wavefront.agent.channel.ChannelUtils.formatErrorMessage) ReportableEntityHandlerFactory(com.wavefront.agent.handlers.ReportableEntityHandlerFactory) ReportableEntityDecoder(com.wavefront.ingester.ReportableEntityDecoder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Utils(com.wavefront.common.Utils) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) ChannelHandler(io.netty.channel.ChannelHandler) Metrics(com.yammer.metrics.Metrics) HISTO_DISABLED(com.wavefront.agent.listeners.FeatureCheckUtils.HISTO_DISABLED) NameValuePair(org.apache.http.NameValuePair) WavefrontPortUnificationHandler.preprocessAndHandlePoint(com.wavefront.agent.listeners.WavefrontPortUnificationHandler.preprocessAndHandlePoint) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ArrayList(java.util.ArrayList) SpanLogs(wavefront.report.SpanLogs) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Span(wavefront.report.Span) DataFormat(com.wavefront.agent.formatter.DataFormat) NameValuePair(org.apache.http.NameValuePair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ReportableEntityDecoder(com.wavefront.ingester.ReportableEntityDecoder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReportPoint(wavefront.report.ReportPoint)

Example 33 with HttpResponseStatus

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);
}
Also used : Matcher(java.util.regex.Matcher) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) URI(java.net.URI)

Example 34 with HttpResponseStatus

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);
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Example 35 with HttpResponseStatus

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();
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) Tuple2(reactor.util.function.Tuple2) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) AfterAll(org.junit.jupiter.api.AfterAll) BeforeAll(org.junit.jupiter.api.BeforeAll) ByteBufFlux(reactor.netty.ByteBufFlux) TomcatServer(reactor.netty.TomcatServer) Duration(java.time.Duration) Path(java.nio.file.Path) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Files(java.nio.file.Files) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Mono(reactor.core.publisher.Mono) Channel(io.netty.channel.Channel) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) Paths(java.nio.file.Paths) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) ConnectionProvider(reactor.netty.resources.ConnectionProvider) InputStream(java.io.InputStream) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Channel(io.netty.channel.Channel) ConnectionProvider(reactor.netty.resources.ConnectionProvider) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)73 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)16 ByteBuf (io.netty.buffer.ByteBuf)15 HttpMethod (io.netty.handler.codec.http.HttpMethod)11 IOException (java.io.IOException)11 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)10 HttpResponse (io.netty.handler.codec.http.HttpResponse)10 HttpVersion (io.netty.handler.codec.http.HttpVersion)9 Map (java.util.Map)8 URI (java.net.URI)7 Test (org.junit.Test)7 URISyntaxException (java.net.URISyntaxException)6 Test (org.junit.jupiter.api.Test)6 Channel (io.netty.channel.Channel)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)5 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4