Search in sources :

Example 81 with DefaultFullHttpResponse

use of io.netty.handler.codec.http.DefaultFullHttpResponse in project tesla by linking12.

the class HttpRequestFilter method createResponse.

protected HttpResponse createResponse(HttpResponseStatus httpResponseStatus, HttpRequest originalRequest) {
    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders.add("Transfer-Encoding", "chunked");
    HttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpResponseStatus);
    List<String> originHeader = FilterUtil.getHeaderValues(originalRequest, "Origin");
    if (originHeader.size() > 0) {
        httpHeaders.set("Access-Control-Allow-Credentials", "true");
        httpHeaders.set("Access-Control-Allow-Origin", originHeader.get(0));
    }
    httpResponse.headers().add(httpHeaders);
    return httpResponse;
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 82 with DefaultFullHttpResponse

use of io.netty.handler.codec.http.DefaultFullHttpResponse in project cxf by apache.

the class NettyHttpServletHandler method handleHttpServletRequest.

protected void handleHttpServletRequest(ChannelHandlerContext ctx, HttpRequest request, NettyHttpContextHandler nettyHttpContextHandler) throws Exception {
    interceptOnRequestReceived(ctx, request);
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    NettyServletResponse nettyServletResponse = buildHttpServletResponse(response);
    NettyHttpServletRequest nettyServletRequest = buildHttpServletRequest(request, nettyHttpContextHandler.getContextPath(), ctx);
    nettyHttpContextHandler.handle(nettyServletRequest.getRequestURI(), nettyServletRequest, nettyServletResponse);
    interceptOnRequestSuccessed(ctx, response);
    nettyServletResponse.getWriter().flush();
    boolean keepAlive = HttpHeaders.isKeepAlive(request);
    if (keepAlive) {
        // Add 'Content-Length' header only for a keep-alive connection.
        response.headers().set(Names.CONTENT_LENGTH, response.content().readableBytes());
        // Add keep alive header as per:
        // -
        // http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    }
    // write response...
    ChannelFuture future = ctx.write(response);
    if (!keepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : NettyServletResponse(org.apache.cxf.transport.http.netty.server.servlet.NettyServletResponse) ChannelFuture(io.netty.channel.ChannelFuture) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) NettyHttpServletRequest(org.apache.cxf.transport.http.netty.server.servlet.NettyHttpServletRequest)

Example 83 with DefaultFullHttpResponse

use of io.netty.handler.codec.http.DefaultFullHttpResponse in project cxf by apache.

the class NettyHttpServletHandler method sendError.

private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    ByteBuf content = Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8);
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
    response.headers().set(Names.CONTENT_TYPE, "text/plain; charset=UTF-8");
    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) ByteBuf(io.netty.buffer.ByteBuf)

Example 84 with DefaultFullHttpResponse

use of io.netty.handler.codec.http.DefaultFullHttpResponse in project janusgraph by JanusGraph.

the class HttpHMACAuthenticationHandler method replyWithToken.

private void replyWithToken(final ChannelHandlerContext ctx, final Object msg, final String token) {
    final String json = "{\"token\": \"" + token + "\"}";
    final byte[] jsonBytes = json.getBytes();
    final FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(jsonBytes));
    response.headers().set(CONTENT_TYPE, "application/json");
    response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    ReferenceCountUtil.release(msg);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Aggregations

DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)84 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)51 ByteBuf (io.netty.buffer.ByteBuf)31 HttpResponse (io.netty.handler.codec.http.HttpResponse)13 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)10 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)8 Test (org.junit.Test)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)6 ChannelFuture (io.netty.channel.ChannelFuture)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)4 IOException (java.io.IOException)4 Map (java.util.Map)4 HttpContent (io.netty.handler.codec.http.HttpContent)3 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)3 File (java.io.File)3 FilterRpcDO (io.github.tesla.filter.domain.FilterRpcDO)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2