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