use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project netty by netty.
the class WebSocketServerHandler method sendHttpResponse.
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
HttpResponseStatus responseStatus = res.status();
if (responseStatus.code() != 200) {
ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
HttpUtil.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
HttpUtil.setKeepAlive(res, keepAlive);
// Flushed in channelReadComplete()
ChannelFuture future = ctx.write(res);
if (!keepAlive) {
future.addListener(ChannelFutureListener.CLOSE);
}
}
use of org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpResponseStatus in project flink by apache.
the class HandlerRedirectUtils method getResponse.
public static HttpResponse getResponse(HttpResponseStatus status, @Nullable String message) {
ByteBuf messageByteBuf = message == null ? Unpooled.buffer(0) : Unpooled.wrappedBuffer(message.getBytes(ENCODING));
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, messageByteBuf);
response.headers().set(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=" + ENCODING.name());
response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, response.content().readableBytes());
return response;
}
Aggregations