Search in sources :

Example 76 with DefaultFullHttpResponse

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

the class FSImageHandler method channelRead0.

@Override
public void channelRead0(ChannelHandlerContext ctx, HttpRequest request) throws Exception {
    if (request.getMethod() != HttpMethod.GET) {
        DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED);
        resp.headers().set(CONNECTION, CLOSE);
        ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
        return;
    }
    QueryStringDecoder decoder = new QueryStringDecoder(request.getUri());
    final String op = getOp(decoder);
    final String content;
    String path = getPath(decoder);
    switch(op) {
        case "GETFILESTATUS":
            content = image.getFileStatus(path);
            break;
        case "LISTSTATUS":
            content = image.listStatus(path);
            break;
        case "GETACLSTATUS":
            content = image.getAclStatus(path);
            break;
        case "GETXATTRS":
            List<String> names = getXattrNames(decoder);
            String encoder = getEncoder(decoder);
            content = image.getXAttrs(path, names, encoder);
            break;
        case "LISTXATTRS":
            content = image.listXAttrs(path);
            break;
        case "GETCONTENTSUMMARY":
            content = image.getContentSummary(path);
            break;
        default:
            throw new IllegalArgumentException("Invalid value for webhdfs parameter" + " \"op\"");
    }
    LOG.info("op=" + op + " target=" + path);
    DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content.getBytes(Charsets.UTF_8)));
    resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
    resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
    resp.headers().set(CONNECTION, CLOSE);
    ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse)

Example 77 with DefaultFullHttpResponse

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

the class HttpUploadServerHandler method writeResponse.

private void writeResponse(Channel channel, HttpResponseStatus statusCode) {
    // Convert the response content to a ChannelBuffer.
    ByteBuf buf = copiedBuffer(responseContent.toString(), CharsetUtil.UTF_8);
    responseContent.setLength(0);
    // Decide whether to close the connection or not.
    boolean close = HttpHeaders.Values.CLOSE.equalsIgnoreCase(request.headers().get(CONNECTION)) || request.getProtocolVersion().equals(HttpVersion.HTTP_1_0) && !HttpHeaders.Values.KEEP_ALIVE.equalsIgnoreCase(request.headers().get(CONNECTION));
    // Build the response object.
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, statusCode, buf);
    response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
    if (!close) {
        // There's no need to add 'Content-Length' header if this is the last response.
        response.headers().set(CONTENT_LENGTH, buf.readableBytes());
    }
    // Write the response.
    ChannelFuture future = channel.writeAndFlush(response);
    // Close the connection after the write operation is done if necessary.
    if (close) {
        future.addListener(ChannelFutureListener.CLOSE);
    }
}
Also used : 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) ByteBuf(io.netty.buffer.ByteBuf)

Example 78 with DefaultFullHttpResponse

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

the class ProxyUtils method createFullHttpResponse.

public static FullHttpResponse createFullHttpResponse(HttpVersion httpVersion, HttpResponseStatus status, String contentType, ByteBuf body, int contentLength) {
    DefaultFullHttpResponse response;
    if (body != null) {
        response = new DefaultFullHttpResponse(httpVersion, status, body);
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, contentLength);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, contentType);
    } else {
        response = new DefaultFullHttpResponse(httpVersion, status);
    }
    return response;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse)

Example 79 with DefaultFullHttpResponse

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

the class DubboTransformHttpRequestFilter method doFilter.

@Override
public HttpResponse doFilter(HttpRequest originalRequest, HttpObject httpObject, ChannelHandlerContext channelHandlerContext) {
    if (originalRequest instanceof FullHttpRequest && dubboClient != null) {
        FullHttpRequest request = (FullHttpRequest) originalRequest;
        String urlPath = request.uri();
        FilterRpcDO rpc = routeRuleCache.getRpc(urlPath);
        if (rpc != null) {
            ByteBuf jsonBuf = request.content();
            String jsonInput = jsonBuf.toString(CharsetUtil.UTF_8);
            String jsonOutput = dubboClient.doRemoteCall(rpc, jsonInput);
            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(jsonOutput.getBytes(CharsetUtil.UTF_8)));
        } else {
            // 如果从缓存没有查到dubbo的映射信息,说明不是泛化调用,返回空,继续走下一个filter或者去走rest服务发现等
            return null;
        }
    }
    return null;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) FilterRpcDO(io.github.tesla.filter.domain.FilterRpcDO)

Example 80 with DefaultFullHttpResponse

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

the class GrpcTransformHttpRequestFilter method doFilter.

@Override
public HttpResponse doFilter(HttpRequest originalRequest, HttpObject httpObject, ChannelHandlerContext channelHandlerContext) {
    if (originalRequest instanceof FullHttpRequest && grpcClient != null) {
        FullHttpRequest request = (FullHttpRequest) originalRequest;
        String urlPath = request.uri();
        FilterRpcDO rpc = routeRuleCache.getRpc(urlPath);
        if (rpc != null) {
            ByteBuf jsonBuf = request.content();
            String jsonInput = jsonBuf.toString(CharsetUtil.UTF_8);
            String jsonOutput = grpcClient.doRemoteCall(rpc, jsonInput);
            return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(jsonOutput.getBytes(CharsetUtil.UTF_8)));
        } else {
            // 如果从缓存没有查到grpc的映射信息,说明不是泛化调用,返回空,继续走下一个filter或者去走rest服务发现等
            return null;
        }
    }
    return null;
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ByteBuf(io.netty.buffer.ByteBuf) FilterRpcDO(io.github.tesla.filter.domain.FilterRpcDO)

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