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;
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations