Search in sources :

Example 71 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.

the class DataDogPortUnificationHandler method reportMetrics.

/**
 * Parse the metrics JSON and report the metrics found.
 * There are 2 formats supported: array of points and single point
 *
 * @param metrics a DataDog-format payload
 * @param pointCounter counter to track the number of points processed in one request
 *
 * @return final HTTP status code to return to the client
 * @see #reportMetric(JsonNode, AtomicInteger, Consumer)
 */
private HttpResponseStatus reportMetrics(final JsonNode metrics, @Nullable final AtomicInteger pointCounter, Consumer<String> outputConsumer) {
    if (metrics == null || !metrics.isObject()) {
        error("Empty or malformed /api/v1/series payload - ignoring", outputConsumer);
        return HttpResponseStatus.BAD_REQUEST;
    }
    if (!metrics.has("series")) {
        error("/api/v1/series payload missing 'series' field", outputConsumer);
        return HttpResponseStatus.BAD_REQUEST;
    }
    JsonNode series = metrics.get("series");
    if (!series.isArray()) {
        error("'series' field must be an array", outputConsumer);
        return HttpResponseStatus.BAD_REQUEST;
    }
    HttpResponseStatus worstStatus = HttpResponseStatus.ACCEPTED;
    for (final JsonNode metric : series) {
        HttpResponseStatus latestStatus = reportMetric(metric, pointCounter, outputConsumer);
        if (latestStatus.compareTo(worstStatus) > 0) {
            worstStatus = latestStatus;
        }
    }
    return worstStatus;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 72 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.

the class DataDogPortUnificationHandler method handleHttpMessage.

@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) throws URISyntaxException {
    StringBuilder output = new StringBuilder();
    AtomicInteger pointsPerRequest = new AtomicInteger();
    URI uri = new URI(request.uri());
    HttpResponseStatus status = HttpResponseStatus.ACCEPTED;
    String requestBody = request.content().toString(CharsetUtil.UTF_8);
    if (requestRelayClient != null && requestRelayTarget != null && request.method() == POST) {
        Histogram requestRelayDuration = Metrics.newHistogram(new TaggedMetricName("listeners", "http-relay.duration-nanos", "port", handle));
        long startNanos = System.nanoTime();
        try {
            String outgoingUrl = requestRelayTarget.replaceFirst("/*$", "") + request.uri();
            HttpPost outgoingRequest = new HttpPost(outgoingUrl);
            if (request.headers().contains("Content-Type")) {
                outgoingRequest.addHeader("Content-Type", request.headers().get("Content-Type"));
            }
            outgoingRequest.setEntity(new StringEntity(requestBody));
            if (synchronousMode) {
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Relaying incoming HTTP request to " + outgoingUrl);
                }
                HttpResponse response = requestRelayClient.execute(outgoingRequest);
                int httpStatusCode = response.getStatusLine().getStatusCode();
                httpStatusCounterCache.get(httpStatusCode).inc();
                if (httpStatusCode < 200 || httpStatusCode >= 300) {
                    // anything that is not 2xx is relayed as is to the client, don't process the payload
                    writeHttpResponse(ctx, HttpResponseStatus.valueOf(httpStatusCode), EntityUtils.toString(response.getEntity(), "UTF-8"), request);
                    return;
                }
            } else {
                threadpool.submit(() -> {
                    try {
                        if (logger.isLoggable(Level.FINE)) {
                            logger.fine("Relaying incoming HTTP request (async) to " + outgoingUrl);
                        }
                        HttpResponse response = requestRelayClient.execute(outgoingRequest);
                        int httpStatusCode = response.getStatusLine().getStatusCode();
                        httpStatusCounterCache.get(httpStatusCode).inc();
                        EntityUtils.consumeQuietly(response.getEntity());
                    } catch (IOException e) {
                        logger.warning("Unable to relay request to " + requestRelayTarget + ": " + e.getMessage());
                        Metrics.newCounter(new TaggedMetricName("listeners", "http-relay.failed", "port", handle)).inc();
                    }
                });
            }
        } catch (IOException e) {
            logger.warning("Unable to relay request to " + requestRelayTarget + ": " + e.getMessage());
            Metrics.newCounter(new TaggedMetricName("listeners", "http-relay.failed", "port", handle)).inc();
            writeHttpResponse(ctx, HttpResponseStatus.BAD_GATEWAY, "Unable to relay request: " + e.getMessage(), request);
            return;
        } finally {
            requestRelayDuration.update(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos));
        }
    }
    String path = uri.getPath().endsWith("/") ? uri.getPath() : uri.getPath() + "/";
    switch(path) {
        case "/api/v1/series/":
            try {
                status = reportMetrics(jsonParser.readTree(requestBody), pointsPerRequest, output::append);
            } catch (Exception e) {
                status = HttpResponseStatus.BAD_REQUEST;
                output.append(errorMessageWithRootCause(e));
                logWarning("WF-300: Failed to handle /api/v1/series request", e, ctx);
            }
            httpRequestSize.update(pointsPerRequest.intValue());
            writeHttpResponse(ctx, status, output, request);
            break;
        case "/api/v1/check_run/":
            if (!processServiceChecks) {
                Metrics.newCounter(new TaggedMetricName("listeners", "http-requests.ignored", "port", handle)).inc();
                writeHttpResponse(ctx, HttpResponseStatus.ACCEPTED, output, request);
                return;
            }
            try {
                reportChecks(jsonParser.readTree(requestBody), pointsPerRequest, output::append);
            } catch (Exception e) {
                status = HttpResponseStatus.BAD_REQUEST;
                output.append(errorMessageWithRootCause(e));
                logWarning("WF-300: Failed to handle /api/v1/check_run request", e, ctx);
            }
            writeHttpResponse(ctx, status, output, request);
            break;
        case "/api/v1/validate/":
            writeHttpResponse(ctx, HttpResponseStatus.OK, output, request);
            break;
        case "/intake/":
            try {
                status = processMetadataAndSystemMetrics(jsonParser.readTree(requestBody), processSystemMetrics, pointsPerRequest, output::append);
            } catch (Exception e) {
                status = HttpResponseStatus.BAD_REQUEST;
                output.append(errorMessageWithRootCause(e));
                logWarning("WF-300: Failed to handle /intake request", e, ctx);
            }
            httpRequestSize.update(pointsPerRequest.intValue());
            writeHttpResponse(ctx, status, output, request);
            break;
        default:
            writeHttpResponse(ctx, HttpResponseStatus.NO_CONTENT, output, request);
            logWarning("WF-300: Unexpected path '" + request.uri() + "', returning HTTP 204", null, ctx);
            break;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Histogram(com.yammer.metrics.core.Histogram) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ChannelUtils.writeHttpResponse(com.wavefront.agent.channel.ChannelUtils.writeHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) URI(java.net.URI) TaggedMetricName(com.wavefront.common.TaggedMetricName) ReportPoint(wavefront.report.ReportPoint) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 73 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project java by wavefrontHQ.

the class OpenTSDBPortUnificationHandler method handleHttpMessage.

@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) throws URISyntaxException {
    StringBuilder output = new StringBuilder();
    URI uri = new URI(request.uri());
    switch(uri.getPath()) {
        case "/api/put":
            final ObjectMapper jsonTree = new ObjectMapper();
            HttpResponseStatus status;
            // had an error, the API will return a 400.
            try {
                JsonNode metrics = jsonTree.readTree(request.content().toString(CharsetUtil.UTF_8));
                if (reportMetrics(metrics, ctx)) {
                    status = HttpResponseStatus.NO_CONTENT;
                } else {
                    // TODO: improve error message
                    // http://opentsdb.net/docs/build/html/api_http/put.html#response
                    // User should understand that successful points are processed and the reason
                    // for BAD_REQUEST is due to at least one failure point.
                    status = HttpResponseStatus.BAD_REQUEST;
                    output.append("At least one data point had error.");
                }
            } catch (Exception e) {
                status = HttpResponseStatus.BAD_REQUEST;
                output.append(errorMessageWithRootCause(e));
                logWarning("WF-300: Failed to handle /api/put request", e, ctx);
            }
            writeHttpResponse(ctx, status, output, request);
            break;
        case "/api/version":
            // http://opentsdb.net/docs/build/html/api_http/version.html
            ObjectNode node = JsonNodeFactory.instance.objectNode();
            node.put("version", ResourceBundle.getBundle("build").getString("build.version"));
            writeHttpResponse(ctx, HttpResponseStatus.OK, node, request);
            break;
        default:
            writeHttpResponse(ctx, HttpResponseStatus.BAD_REQUEST, "Unsupported path", request);
            logWarning("WF-300: Unexpected path '" + request.uri() + "'", null, ctx);
            break;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URISyntaxException(java.net.URISyntaxException)

Example 74 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project turbo-rpc by hank-whu.

the class RestHttResponseEncoder method doResponse.

private void doResponse(ChannelHandlerContext ctx, ChannelPromise promise, RestHttpResponse restHttpResponse) {
    ByteBuf buffer = ctx.alloc().ioBuffer();
    Object msg = restHttpResponse.getResult();
    HttpResponseStatus status = restHttpResponse.getStatus();
    boolean keepAlive = restHttpResponse.isKeepAlive();
    Throwable throwable = null;
    if (msg == null) {
        buffer.writeBytes(UnsafeStringUtils.getUTF8Bytes(""));
    } else if (msg instanceof Throwable) {
        throwable = (Throwable) msg;
        buffer.writeBytes(UnsafeStringUtils.getUTF8Bytes(throwable.getMessage()));
    } else {
        try {
            jsonMapper.write(buffer, msg);
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, buffer, false);
            response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
        } catch (Throwable e) {
            if (logger.isWarnEnabled()) {
                logger.warn("error ", e);
            }
            status = INTERNAL_SERVER_ERROR;
            buffer.clear();
            buffer.writeBytes(UnsafeStringUtils.getUTF8Bytes(e.getMessage()));
        }
    }
    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status, buffer, false);
    response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
    doResponseFilter(ctx, restHttpResponse.getRequest(), response, restHttpResponse.getInvoker(), throwable);
    if (keepAlive) {
        response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        ctx.write(response, promise);
    } else {
        ctx.write(response, promise).addListener(CLOSE);
    }
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 75 with HttpResponseStatus

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project reactor-netty by reactor.

the class HttpServerTests method testStatus.

@Test
void testStatus() {
    doTestStatus(HttpResponseStatus.OK);
    doTestStatus(new HttpResponseStatus(200, "Some custom reason phrase for 200 status code"));
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) BaseHttpTest(reactor.netty.BaseHttpTest) 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