use of io.netty.handler.codec.http.FullHttpResponse in project LogHub by fbacchella.
the class RootRedirect method processRequest.
@Override
protected boolean processRequest(FullHttpRequest request, ChannelHandlerContext ctx) throws HttpRequestFailure {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.MOVED_PERMANENTLY);
boolean keepAlive = addKeepAlive(request, response, 0);
response.headers().set(HttpHeaderNames.LOCATION, "/static/index.html");
ChannelFuture redirectfutur = ctx.writeAndFlush(response);
addLogger(redirectfutur, request.method().name(), request.uri(), response.status().code(), "redirect");
return keepAlive;
}
use of io.netty.handler.codec.http.FullHttpResponse in project component-runtime by Talend.
the class Handlers method sendError.
static void sendError(final ChannelHandlerContext ctx, final HttpResponseStatus status) {
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().set("X-Talend-Proxy-JUnit", "default-response");
ctx.writeAndFlush(response);
}
use of io.netty.handler.codec.http.FullHttpResponse in project component-runtime by Talend.
the class PassthroughHandler method channelRead0.
@Override
protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest request) {
if (HttpMethod.CONNECT.name().equalsIgnoreCase(request.method().name())) {
final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.EMPTY_BUFFER);
setKeepAlive(response, true);
setContentLength(response, 0);
if (api.getSslContext() != null) {
final SSLEngine sslEngine = api.getSslContext().createSSLEngine();
sslEngine.setUseClientMode(false);
ctx.channel().pipeline().addFirst("ssl", new SslHandler(sslEngine, true));
final String uri = request.uri();
final String[] parts = uri.split(":");
ctx.channel().attr(BASE).set("https://" + parts[0] + (parts.length > 1 && !"443".equals(parts[1]) ? ":" + parts[1] : ""));
}
ctx.writeAndFlush(response);
return;
}
// copy to use in a separated thread
final FullHttpRequest req = request.copy();
api.getExecutor().execute(() -> doHttpRequest(req, ctx));
}
use of io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class NettyHttpRequestHandler method buildHttpResponse.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected FullHttpResponse buildHttpResponse(Response response, boolean keepAlive) throws Exception {
Object value = response.getValue();
byte[] responseBytes = null;
if (value instanceof Message) {
Marshaller marshaller = ProtoUtils.jsonMarshaller((Message) value.getClass().getMethod("getDefaultInstance", null).invoke(null, null));
InputStream is = marshaller.stream(value);
responseBytes = new byte[is.available()];
is.read(responseBytes);
} else {
// TODO not pb
}
FullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(responseBytes));
httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/x-www-form-urlencoded");
httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, httpResponse.content().readableBytes());
if (keepAlive) {
httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
} else {
httpResponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
}
return httpResponse;
}
use of io.netty.handler.codec.http.FullHttpResponse in project motan by weibocom.
the class NettyHttpRequestHandler method processHttpRequest.
protected void processHttpRequest(ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
FullHttpResponse httpResponse = null;
try {
httpResponse = (FullHttpResponse) messageHandler.handle(serverChannel, httpRequest);
} catch (Exception e) {
LoggerUtil.error("NettyHttpHandler process http request fail.", e);
httpResponse = buildErrorResponse(e.getMessage());
} finally {
httpRequest.content().release();
}
sendResponse(ctx, httpResponse);
}
Aggregations