HADOOP-18249. Fix getUri() in HttpRequest has been deprecated. (#4335)

* HADOOP-18249. Fix getUri() in HttpRequest has been deprecated.
WebHdfsHandler.java req.getUri() replace uri(), req.getMethod() replace method()
HostRestrictingAuthorizationFilterHandler.java req.getUri() replace uri()
TestHostRestrictingAuthorizationFilterHandler.java remove throws Exception, channelResponse.getStatus() replace status().

* HADOOP-18249. Fix getUri() in HttpRequest has been deprecated.

* HADOOP-18249. Fix Some CheckStyle.

Co-authored-by: slfan1989 <louj1988@@>
This commit is contained in:
slfan1989 2022-05-24 10:41:10 -07:00 committed by GitHub
parent f390edaec4
commit f469b0e143
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 20 deletions

View File

@ -197,7 +197,7 @@ final class HostRestrictingAuthorizationFilterHandler
@Override @Override
public String getQueryString() { public String getQueryString() {
try { try {
return (new URI(req.getUri()).getQuery()); return (new URI(req.uri()).getQuery());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
return null; return null;
} }
@ -205,7 +205,7 @@ final class HostRestrictingAuthorizationFilterHandler
@Override @Override
public String getRequestURI() { public String getRequestURI() {
String uri = req.getUri(); String uri = req.uri();
// Netty's getUri includes the query string, while Servlet's does not // Netty's getUri includes the query string, while Servlet's does not
return (uri.substring(0, uri.indexOf("?") >= 0 ? uri.indexOf("?") : return (uri.substring(0, uri.indexOf("?") >= 0 ? uri.indexOf("?") :
uri.length())); uri.length()));

View File

@ -98,7 +98,7 @@ class SimpleHttpProxyHandler extends SimpleChannelInboundHandler<HttpRequest> {
@Override @Override
public void channelRead0 public void channelRead0
(final ChannelHandlerContext ctx, final HttpRequest req) { (final ChannelHandlerContext ctx, final HttpRequest req) {
uri = req.getUri(); uri = req.uri();
final Channel client = ctx.channel(); final Channel client = ctx.channel();
Bootstrap proxiedServer = new Bootstrap() Bootstrap proxiedServer = new Bootstrap()
.group(client.eventLoop()) .group(client.eventLoop())
@ -117,8 +117,7 @@ class SimpleHttpProxyHandler extends SimpleChannelInboundHandler<HttpRequest> {
public void operationComplete(ChannelFuture future) throws Exception { public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) { if (future.isSuccess()) {
ctx.channel().pipeline().remove(HttpResponseEncoder.class); ctx.channel().pipeline().remove(HttpResponseEncoder.class);
HttpRequest newReq = new DefaultFullHttpRequest(HTTP_1_1, HttpRequest newReq = new DefaultFullHttpRequest(HTTP_1_1, req.method(), req.uri());
req.getMethod(), req.getUri());
newReq.headers().add(req.headers()); newReq.headers().add(req.headers());
newReq.headers().set(CONNECTION, Values.CLOSE); newReq.headers().set(CONNECTION, Values.CLOSE);
future.channel().writeAndFlush(newReq); future.channel().writeAndFlush(newReq);

View File

@ -43,7 +43,7 @@ class URLDispatcher extends SimpleChannelInboundHandler<HttpRequest> {
@Override @Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req) protected void channelRead0(ChannelHandlerContext ctx, HttpRequest req)
throws Exception { throws Exception {
String uri = req.getUri(); String uri = req.uri();
ChannelPipeline p = ctx.pipeline(); ChannelPipeline p = ctx.pipeline();
if (uri.startsWith(WEBHDFS_PREFIX)) { if (uri.startsWith(WEBHDFS_PREFIX)) {
WebHdfsHandler h = new WebHdfsHandler(conf, confForCreate); WebHdfsHandler h = new WebHdfsHandler(conf, confForCreate);

View File

@ -122,8 +122,8 @@ public class WebHdfsHandler extends SimpleChannelInboundHandler<HttpRequest> {
@Override @Override
public void channelRead0(final ChannelHandlerContext ctx, public void channelRead0(final ChannelHandlerContext ctx,
final HttpRequest req) throws Exception { final HttpRequest req) throws Exception {
Preconditions.checkArgument(req.getUri().startsWith(WEBHDFS_PREFIX)); Preconditions.checkArgument(req.uri().startsWith(WEBHDFS_PREFIX));
QueryStringDecoder queryString = new QueryStringDecoder(req.getUri()); QueryStringDecoder queryString = new QueryStringDecoder(req.uri());
params = new ParameterParser(queryString, conf); params = new ParameterParser(queryString, conf);
DataNodeUGIProvider ugiProvider = new DataNodeUGIProvider(params); DataNodeUGIProvider ugiProvider = new DataNodeUGIProvider(params);
ugi = ugiProvider.ugi(); ugi = ugiProvider.ugi();
@ -144,7 +144,7 @@ public class WebHdfsHandler extends SimpleChannelInboundHandler<HttpRequest> {
LOG.warn("Error retrieving hostname: ", e); LOG.warn("Error retrieving hostname: ", e);
host = "unknown"; host = "unknown";
} }
REQLOG.info(host + " " + req.getMethod() + " " + req.getUri() + " " + REQLOG.info(host + " " + req.method() + " " + req.uri() + " " +
getResponseCode()); getResponseCode());
} }
return null; return null;
@ -154,13 +154,13 @@ public class WebHdfsHandler extends SimpleChannelInboundHandler<HttpRequest> {
int getResponseCode() { int getResponseCode() {
return (resp == null) ? INTERNAL_SERVER_ERROR.code() : return (resp == null) ? INTERNAL_SERVER_ERROR.code() :
resp.getStatus().code(); resp.status().code();
} }
public void handle(ChannelHandlerContext ctx, HttpRequest req) public void handle(ChannelHandlerContext ctx, HttpRequest req)
throws IOException, URISyntaxException { throws IOException, URISyntaxException {
String op = params.op(); String op = params.op();
HttpMethod method = req.getMethod(); HttpMethod method = req.method();
if (PutOpParam.Op.CREATE.name().equalsIgnoreCase(op) if (PutOpParam.Op.CREATE.name().equalsIgnoreCase(op)
&& method == PUT) { && method == PUT) {
onCreate(ctx); onCreate(ctx);

View File

@ -76,7 +76,7 @@ class FSImageHandler extends SimpleChannelInboundHandler<HttpRequest> {
@Override @Override
public void channelRead0(ChannelHandlerContext ctx, HttpRequest request) public void channelRead0(ChannelHandlerContext ctx, HttpRequest request)
throws Exception { throws Exception {
if (request.getMethod() != HttpMethod.GET) { if (request.method() != HttpMethod.GET) {
DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1, DefaultHttpResponse resp = new DefaultHttpResponse(HTTP_1_1,
METHOD_NOT_ALLOWED); METHOD_NOT_ALLOWED);
resp.headers().set(CONNECTION, CLOSE); resp.headers().set(CONNECTION, CLOSE);
@ -84,7 +84,7 @@ class FSImageHandler extends SimpleChannelInboundHandler<HttpRequest> {
return; return;
} }
QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
// check path. throw exception if path doesn't start with WEBHDFS_PREFIX // check path. throw exception if path doesn't start with WEBHDFS_PREFIX
String path = getPath(decoder); String path = getPath(decoder);
final String op = getOp(decoder); final String op = getOp(decoder);
@ -140,7 +140,7 @@ class FSImageHandler extends SimpleChannelInboundHandler<HttpRequest> {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception { throws Exception {
Exception e = cause instanceof Exception ? (Exception) cause : new Exception e = cause instanceof Exception ? (Exception) cause : new
Exception(cause); Exception(cause);
final String output = JsonUtil.toJsonString(e); final String output = JsonUtil.toJsonString(e);
ByteBuf content = Unpooled.wrappedBuffer(output.getBytes(Charsets.UTF_8)); ByteBuf content = Unpooled.wrappedBuffer(output.getBytes(Charsets.UTF_8));
final DefaultFullHttpResponse resp = new DefaultFullHttpResponse( final DefaultFullHttpResponse resp = new DefaultFullHttpResponse(

View File

@ -48,7 +48,7 @@ public class TestHostRestrictingAuthorizationFilterHandler {
* Test running in with no ACL rules (restrict all) * Test running in with no ACL rules (restrict all)
*/ */
@Test @Test
public void testRejectAll() throws Exception { public void testRejectAll() {
EmbeddedChannel channel = new CustomEmbeddedChannel("127.0.0.1", 1006, EmbeddedChannel channel = new CustomEmbeddedChannel("127.0.0.1", 1006,
new HostRestrictingAuthorizationFilterHandler()); new HostRestrictingAuthorizationFilterHandler());
FullHttpRequest httpRequest = FullHttpRequest httpRequest =
@ -61,7 +61,7 @@ public class TestHostRestrictingAuthorizationFilterHandler {
DefaultHttpResponse channelResponse = DefaultHttpResponse channelResponse =
(DefaultHttpResponse) channel.outboundMessages().poll(); (DefaultHttpResponse) channel.outboundMessages().poll();
assertNotNull("Expected response to exist.", channelResponse); assertNotNull("Expected response to exist.", channelResponse);
assertEquals(HttpResponseStatus.FORBIDDEN, channelResponse.getStatus()); assertEquals(HttpResponseStatus.FORBIDDEN, channelResponse.status());
assertFalse(channel.isOpen()); assertFalse(channel.isOpen());
} }
@ -70,7 +70,7 @@ public class TestHostRestrictingAuthorizationFilterHandler {
* reused * reused
*/ */
@Test @Test
public void testMultipleAcceptedGETsOneChannel() throws Exception { public void testMultipleAcceptedGETsOneChannel() {
Configuration conf = new Configuration(); Configuration conf = new Configuration();
conf.set(CONFNAME, "*,*,/allowed"); conf.set(CONFNAME, "*,*,/allowed");
HostRestrictingAuthorizationFilter filter = HostRestrictingAuthorizationFilter filter =
@ -102,7 +102,7 @@ public class TestHostRestrictingAuthorizationFilterHandler {
* single filter instance * single filter instance
*/ */
@Test @Test
public void testMultipleChannels() throws Exception { public void testMultipleChannels() {
Configuration conf = new Configuration(); Configuration conf = new Configuration();
conf.set(CONFNAME, "*,*,/allowed"); conf.set(CONFNAME, "*,*,/allowed");
HostRestrictingAuthorizationFilter filter = HostRestrictingAuthorizationFilter filter =
@ -140,7 +140,7 @@ public class TestHostRestrictingAuthorizationFilterHandler {
* Test accepting a GET request for the file checksum * Test accepting a GET request for the file checksum
*/ */
@Test @Test
public void testAcceptGETFILECHECKSUM() throws Exception { public void testAcceptGETFILECHECKSUM() {
EmbeddedChannel channel = new CustomEmbeddedChannel("127.0.0.1", 1006, EmbeddedChannel channel = new CustomEmbeddedChannel("127.0.0.1", 1006,
new HostRestrictingAuthorizationFilterHandler()); new HostRestrictingAuthorizationFilterHandler());
FullHttpRequest httpRequest = FullHttpRequest httpRequest =
@ -158,7 +158,7 @@ public class TestHostRestrictingAuthorizationFilterHandler {
*/ */
protected static class CustomEmbeddedChannel extends EmbeddedChannel { protected static class CustomEmbeddedChannel extends EmbeddedChannel {
private InetSocketAddress socketAddress; private final InetSocketAddress socketAddress;
/* /*
* A normal @{EmbeddedChannel} constructor which takes the remote client * A normal @{EmbeddedChannel} constructor which takes the remote client