Search in sources :

Example 61 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project knotx by Cognifide.

the class RequestProcessorKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    HttpResponseStatus statusCode;
    if (error instanceof NoSuchElementException) {
        statusCode = HttpResponseStatus.NOT_FOUND;
    } else {
        statusCode = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    }
    knotContext.getClientResponse().setStatusCode(statusCode.code());
    return knotContext;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoSuchElementException(java.util.NoSuchElementException)

Example 62 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project knotx by Cognifide.

the class GatewayKnotProxyImpl method processError.

@Override
protected KnotContext processError(KnotContext knotContext, Throwable error) {
    HttpResponseStatus statusCode;
    if (error instanceof NoSuchElementException) {
        statusCode = HttpResponseStatus.NOT_FOUND;
    } else {
        statusCode = HttpResponseStatus.INTERNAL_SERVER_ERROR;
    }
    knotContext.getClientResponse().setStatusCode(statusCode.code());
    return knotContext;
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) NoSuchElementException(java.util.NoSuchElementException)

Example 63 with HttpResponseStatus

use of io.netty.handler.codec.http.HttpResponseStatus in project zuul by Netflix.

the class PushMessageSender method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest request) throws Exception {
    if (!request.decoderResult().isSuccess()) {
        sendHttpResponse(ctx, request, BAD_REQUEST, null);
        return;
    }
    final String path = request.uri();
    if (path == null) {
        sendHttpResponse(ctx, request, BAD_REQUEST, null);
        return;
    }
    if (path.endsWith("/push")) {
        logPushAttempt();
        final HttpMethod method = request.method();
        if ((method != HttpMethod.POST) && (method != HttpMethod.GET)) {
            sendHttpResponse(ctx, request, METHOD_NOT_ALLOWED, null);
            return;
        }
        final PushUserAuth userAuth = getPushUserAuth(request);
        if (!userAuth.isSuccess()) {
            sendHttpResponse(ctx, request, UNAUTHORIZED, userAuth);
            logNoIdentity();
            return;
        }
        final PushConnection pushConn = pushConnectionRegistry.get(userAuth.getClientIdentity());
        if (pushConn == null) {
            sendHttpResponse(ctx, request, NOT_FOUND, userAuth);
            logClientNotConnected();
            return;
        }
        if (!verifySecureToken(request, pushConn)) {
            sendHttpResponse(ctx, request, FORBIDDEN, userAuth);
            logSecurityTokenVerificationFail();
            return;
        }
        if (method == HttpMethod.GET) {
            // client only checking if particular CID + ESN is connected to this instance
            sendHttpResponse(ctx, request, OK, userAuth);
            return;
        }
        final ByteBuf body = request.content().retain();
        if (body.readableBytes() <= 0) {
            sendHttpResponse(ctx, request, NO_CONTENT, userAuth);
            return;
        }
        if (pushConn.isRateLimited()) {
            sendHttpResponse(ctx, request, HttpResponseStatus.SERVICE_UNAVAILABLE, userAuth);
            logRateLimited();
            return;
        }
        final ChannelFuture clientFuture = pushConn.sendPushMessage(body);
        clientFuture.addListener(cf -> {
            HttpResponseStatus status;
            if (cf.isSuccess()) {
                logPushSuccess();
                status = OK;
            } else {
                logPushError(cf.cause());
                status = INTERNAL_SERVER_ERROR;
            }
            sendHttpResponse(ctx, request, status, userAuth);
        });
    } else {
        // Last handler in the chain
        sendHttpResponse(ctx, request, BAD_REQUEST, null);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ByteBuf(io.netty.buffer.ByteBuf) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 64 with HttpResponseStatus

use of 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);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) RemoteDigestBlob(io.crate.blob.RemoteDigestBlob) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus)

Example 65 with HttpResponseStatus

use of 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);
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ArrayList(java.util.ArrayList) URI(java.net.URI) Span(wavefront.report.Span) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

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 URI (java.net.URI)7 Test (org.junit.Test)7 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)6 URISyntaxException (java.net.URISyntaxException)6 Map (java.util.Map)6 Test (org.junit.jupiter.api.Test)6 Channel (io.netty.channel.Channel)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 HttpHeaderValues (io.netty.handler.codec.http.HttpHeaderValues)4 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4