From fc6e3fc9d7781efec6cd142d135a1b339081aa24 Mon Sep 17 00:00:00 2001 From: Jan Hentschel Date: Wed, 7 Feb 2018 18:52:59 +0100 Subject: [PATCH] HBASE-19762 Fixed Checkstyle errors in hbase-http --- .../hbase/checkstyle-suppressions.xml | 7 +- hbase-http/pom.xml | 7 + .../http/ClickjackingPreventionFilter.java | 39 +++--- .../apache/hadoop/hbase/http/HtmlQuoting.java | 56 ++++---- .../apache/hadoop/hbase/http/HttpConfig.java | 5 +- .../hbase/http/HttpRequestLogAppender.java | 2 +- .../apache/hadoop/hbase/http/HttpServer.java | 120 ++++++++++++------ .../apache/hadoop/hbase/http/InfoServer.java | 51 ++++---- .../hadoop/hbase/http/NoCacheFilter.java | 8 +- .../hadoop/hbase/http/ProfileServlet.java | 4 +- .../hadoop/hbase/http/conf/ConfServlet.java | 7 +- .../hadoop/hbase/http/log/LogLevel.java | 30 ++--- .../hadoop/hbase/util/ProcessUtils.java | 4 +- .../hbase/http/HttpServerFunctionalTest.java | 27 ++-- .../hadoop/hbase/http/TestGlobalFilter.java | 44 ++++--- .../hadoop/hbase/http/TestHtmlQuoting.java | 7 +- .../hadoop/hbase/http/TestHttpServer.java | 103 +++++++-------- .../hadoop/hbase/http/TestPathFilter.java | 41 +++--- .../hadoop/hbase/http/TestServletFilter.java | 38 +++--- .../hbase/http/TestSpnegoHttpServer.java | 51 ++++---- .../hadoop/hbase/http/log/TestLogLevel.java | 18 +-- .../hbase/http/resource/JerseyResource.java | 2 +- .../hbase/http/ssl/KeyStoreTestUtil.java | 13 +- 23 files changed, 369 insertions(+), 315 deletions(-) diff --git a/hbase-checkstyle/src/main/resources/hbase/checkstyle-suppressions.xml b/hbase-checkstyle/src/main/resources/hbase/checkstyle-suppressions.xml index 6e743113254..2c35aa60a61 100644 --- a/hbase-checkstyle/src/main/resources/hbase/checkstyle-suppressions.xml +++ b/hbase-checkstyle/src/main/resources/hbase/checkstyle-suppressions.xml @@ -1,7 +1,7 @@ + "-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN" + "https://checkstyle.org/dtds/suppressions_1_2.dtd"> "; - static final Pattern TAG = Pattern.compile("<[^>]*>"); + private static final String MARKER = ""; + private static final Pattern TAG = Pattern.compile("<[^>]*>"); /** * A servlet implementation @@ -98,9 +96,8 @@ public final class LogLevel { private static final long serialVersionUID = 1L; @Override - public void doGet(HttpServletRequest request, HttpServletResponse response - ) throws ServletException, IOException { - + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { // Do the authorization if (!HttpServer.hasAdministratorAccess(getServletContext(), request, response)) { @@ -175,8 +172,7 @@ public final class LogLevel { + "Set the specified log level for the specified log name." + "\n" + "\n" + "\n" + "\n" + "\n" + "

\n" + "
\n"; - private static void process(org.apache.log4j.Logger log, String level, - PrintWriter out) throws IOException { + private static void process(org.apache.log4j.Logger log, String level, PrintWriter out) { if (level != null) { if (!level.equals(org.apache.log4j.Level.toLevel(level).toString())) { out.println(MARKER + "
" + "Bad level : " + level @@ -192,14 +188,18 @@ public final class LogLevel { } private static void process(java.util.logging.Logger log, String level, - PrintWriter out) throws IOException { + PrintWriter out) { if (level != null) { log.setLevel(java.util.logging.Level.parse(level)); out.println(MARKER + "Setting Level to " + level + " ...
"); } java.util.logging.Level lev; - for(; (lev = log.getLevel()) == null; log = log.getParent()); + + while ((lev = log.getLevel()) == null) { + log = log.getParent(); + } + out.println(MARKER + "Effective level: " + lev + "
"); } } diff --git a/hbase-http/src/main/java/org/apache/hadoop/hbase/util/ProcessUtils.java b/hbase-http/src/main/java/org/apache/hadoop/hbase/util/ProcessUtils.java index 2d3835e2ece..7ed09468cb6 100644 --- a/hbase-http/src/main/java/org/apache/hadoop/hbase/util/ProcessUtils.java +++ b/hbase-http/src/main/java/org/apache/hadoop/hbase/util/ProcessUtils.java @@ -21,11 +21,11 @@ import java.io.IOException; import java.lang.management.ManagementFactory; import java.util.List; +import org.apache.yetus.audience.InterfaceAudience; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.apache.yetus.audience.InterfaceAudience; - /** * Process related utilities. */ diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java index 69972a2e123..b81826bd484 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/HttpServerFunctionalTest.java @@ -18,19 +18,19 @@ package org.apache.hadoop.hbase.http; -import org.apache.hadoop.net.NetUtils; -import org.apache.hadoop.security.authorize.AccessControlList; -import org.junit.Assert; -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hbase.http.HttpServer.Builder; - import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.ServerSocket; import java.net.URI; import java.net.URL; -import java.net.MalformedURLException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.http.HttpServer.Builder; +import org.apache.hadoop.net.NetUtils; +import org.apache.hadoop.security.authorize.AccessControlList; +import org.junit.Assert; /** * This is a base class for functional tests of the {@link HttpServer}. @@ -114,9 +114,9 @@ public class HttpServerFunctionalTest extends Assert { File testWebappDir = new File(webapps + File.separatorChar + TEST); try { - if (!testWebappDir.exists()) { - fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing"); - } + if (!testWebappDir.exists()) { + fail("Test webapp dir " + testWebappDir.getCanonicalPath() + " missing"); + } } catch (IOException e) { } @@ -158,8 +158,8 @@ public class HttpServerFunctionalTest extends Assert { return localServerBuilder(webapp).setFindPort(true).setConf(conf).build(); } - public static HttpServer createServer(String webapp, Configuration conf, AccessControlList adminsAcl) - throws IOException { + public static HttpServer createServer(String webapp, Configuration conf, + AccessControlList adminsAcl) throws IOException { return localServerBuilder(webapp).setFindPort(true).setConf(conf).setACL(adminsAcl).build(); } @@ -178,7 +178,8 @@ public class HttpServerFunctionalTest extends Assert { */ public static HttpServer createServer(String webapp, Configuration conf, String[] pathSpecs) throws IOException { - return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs).build(); + return localServerBuilder(webapp).setFindPort(true).setConf(conf).setPathSpec(pathSpecs) + .build(); } /** diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java index 81902901f81..cb2a674d152 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestGlobalFilter.java @@ -44,13 +44,12 @@ import org.slf4j.LoggerFactory; @Category({MiscTests.class, SmallTests.class}) public class TestGlobalFilter extends HttpServerFunctionalTest { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestGlobalFilter.class); private static final Logger LOG = LoggerFactory.getLogger(HttpServer.class); - static final Set RECORDS = new TreeSet<>(); + private static final Set RECORDS = new TreeSet<>(); /** A very simple filter that records accessed uri's */ static public class RecordingFilter implements Filter { @@ -67,10 +66,11 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { } @Override - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - if (filterConfig == null) - return; + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + if (filterConfig == null) { + return; + } String uri = ((HttpServletRequest)request).getRequestURI(); LOG.info("filtering " + uri); @@ -89,21 +89,21 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { } } - - /** access a url, ignoring some IOException such as the page does not exist */ - static void access(String urlstring) throws IOException { + /** + * access a url, ignoring some IOException such as the page does not exist + */ + private static void access(String urlstring) throws IOException { LOG.warn("access " + urlstring); URL url = new URL(urlstring); URLConnection connection = url.openConnection(); connection.connect(); try { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - try { - for(; in.readLine() != null; ); - } finally { - in.close(); + try (BufferedReader in = new BufferedReader( + new InputStreamReader(connection.getInputStream(), "UTF-8"))) { + for (; in.readLine() != null; ) { + // Ignoring the content of the URLs. Only checking if something is there. + } } } catch(IOException ioe) { LOG.warn("urlstring=" + urlstring, ioe); @@ -131,15 +131,17 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { final String outURL = "/static/a.out"; final String logURL = "/logs/a.log"; - final String[] urls = {fsckURL, stacksURL, ajspURL, listPathsURL, - dataURL, streamFile, rootURL, allURL, outURL, logURL}; + final String[] urls = { + fsckURL, stacksURL, ajspURL, listPathsURL, dataURL, streamFile, rootURL, allURL, + outURL, logURL + }; //access the urls final String prefix = "http://" + NetUtils.getHostPortString(http.getConnectorAddress(0)); try { - for(int i = 0; i < urls.length; i++) { - access(prefix + urls[i]); + for (String url : urls) { + access(prefix + url); } } finally { http.stop(); @@ -148,8 +150,8 @@ public class TestGlobalFilter extends HttpServerFunctionalTest { LOG.info("RECORDS = " + RECORDS); //verify records - for(int i = 0; i < urls.length; i++) { - assertTrue(RECORDS.remove(urls[i])); + for (String url : urls) { + assertTrue(RECORDS.remove(url)); } assertTrue(RECORDS.isEmpty()); } diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHtmlQuoting.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHtmlQuoting.java index f47991d9289..0f4c4d5d2a1 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHtmlQuoting.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHtmlQuoting.java @@ -17,7 +17,10 @@ */ package org.apache.hadoop.hbase.http; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import javax.servlet.http.HttpServletRequest; import org.apache.hadoop.hbase.HBaseClassTestRule; @@ -30,7 +33,6 @@ import org.mockito.Mockito; @Category({MiscTests.class, SmallTests.class}) public class TestHtmlQuoting { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestHtmlQuoting.class); @@ -73,7 +75,6 @@ public class TestHtmlQuoting { runRoundTrip(buffer.toString()); } - @Test public void testRequestQuoting() throws Exception { HttpServletRequest mockReq = Mockito.mock(HttpServletRequest.class); diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHttpServer.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHttpServer.java index 0f5b8a1a016..1ab1140a74b 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHttpServer.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestHttpServer.java @@ -23,6 +23,7 @@ import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.List; @@ -70,7 +71,6 @@ import org.slf4j.LoggerFactory; @Category({MiscTests.class, SmallTests.class}) public class TestHttpServer extends HttpServerFunctionalTest { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestHttpServer.class); @@ -84,9 +84,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { @SuppressWarnings("serial") public static class EchoMapServlet extends HttpServlet { @Override - public void doGet(HttpServletRequest request, - HttpServletResponse response - ) throws ServletException, IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { PrintWriter out = response.getWriter(); Map params = request.getParameterMap(); SortedSet keys = new TreeSet<>(params.keySet()); @@ -110,9 +108,8 @@ public class TestHttpServer extends HttpServerFunctionalTest { @SuppressWarnings("serial") public static class EchoServlet extends HttpServlet { @Override - public void doGet(HttpServletRequest request, - HttpServletResponse response - ) throws ServletException, IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws IOException { PrintWriter out = response.getWriter(); SortedSet sortedKeys = new TreeSet<>(); Enumeration keys = request.getParameterNames(); @@ -132,9 +129,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { @SuppressWarnings("serial") public static class LongHeaderServlet extends HttpServlet { @Override - public void doGet(HttpServletRequest request, - HttpServletResponse response - ) throws ServletException, IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response) { Assert.assertEquals(63 * 1024, request.getHeader("longheader").length()); response.setStatus(HttpServletResponse.SC_OK); } @@ -143,9 +138,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { @SuppressWarnings("serial") public static class HtmlContentServlet extends HttpServlet { @Override - public void doGet(HttpServletRequest request, - HttpServletResponse response - ) throws ServletException, IOException { + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("hello world"); @@ -172,30 +165,30 @@ public class TestHttpServer extends HttpServerFunctionalTest { server.stop(); } - /** Test the maximum number of threads cannot be exceeded. */ - @Test public void testMaxThreads() throws Exception { + /** + * Test the maximum number of threads cannot be exceeded. + */ + @Test + public void testMaxThreads() throws Exception { int clientThreads = MAX_THREADS * 10; Executor executor = Executors.newFixedThreadPool(clientThreads); // Run many clients to make server reach its maximum number of threads final CountDownLatch ready = new CountDownLatch(clientThreads); final CountDownLatch start = new CountDownLatch(1); for (int i = 0; i < clientThreads; i++) { - executor.execute(new Runnable() { - @Override - public void run() { - ready.countDown(); - try { - start.await(); - assertEquals("a:b\nc:d\n", - readOutput(new URL(baseUrl, "/echo?a=b&c=d"))); - int serverThreads = server.webServer.getThreadPool().getThreads(); - assertTrue("More threads are started than expected, Server Threads count: " - + serverThreads, serverThreads <= MAX_THREADS); - System.out.println("Number of threads = " + serverThreads + - " which is less or equal than the max = " + MAX_THREADS); - } catch (Exception e) { - // do nothing - } + executor.execute(() -> { + ready.countDown(); + try { + start.await(); + assertEquals("a:b\nc:d\n", + readOutput(new URL(baseUrl, "/echo?a=b&c=d"))); + int serverThreads = server.webServer.getThreadPool().getThreads(); + assertTrue("More threads are started than expected, Server Threads count: " + + serverThreads, serverThreads <= MAX_THREADS); + LOG.info("Number of threads = " + serverThreads + + " which is less or equal than the max = " + MAX_THREADS); + } catch (Exception e) { + // do nothing } }); } @@ -300,7 +293,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { } @Override - public void init(FilterConfig arg0) throws ServletException { } + public void init(FilterConfig arg0) { } } /** @@ -327,8 +320,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { * @return The HTTP response code * @throws IOException if there is a problem communicating with the server */ - static int getHttpStatusCode(String urlstring, String userName) - throws IOException { + private static int getHttpStatusCode(String urlstring, String userName) throws IOException { URL url = new URL(urlstring + "?user.name=" + userName); System.out.println("Accessing " + url + " as user " + userName); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); @@ -347,7 +339,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { } @Override - public List getGroups(String user) throws IOException { + public List getGroups(String user) { return mapping.get(user); } } @@ -356,12 +348,10 @@ public class TestHttpServer extends HttpServerFunctionalTest { * Verify the access for /logs, /stacks, /conf, /logLevel and /metrics * servlets, when authentication filters are set, but authorization is not * enabled. - * @throws Exception */ @Test @Ignore public void testDisabledAuthorizationOfDefaultServlets() throws Exception { - Configuration conf = new Configuration(); // Authorization is disabled by default @@ -371,16 +361,15 @@ public class TestHttpServer extends HttpServerFunctionalTest { MyGroupsProvider.class.getName()); Groups.getUserToGroupsMappingService(conf); MyGroupsProvider.clearMapping(); - MyGroupsProvider.mapping.put("userA", Arrays.asList("groupA")); - MyGroupsProvider.mapping.put("userB", Arrays.asList("groupB")); + MyGroupsProvider.mapping.put("userA", Collections.singletonList("groupA")); + MyGroupsProvider.mapping.put("userB", Collections.singletonList("groupB")); HttpServer myServer = new HttpServer.Builder().setName("test") .addEndpoint(new URI("http://localhost:0")).setFindPort(true).build(); myServer.setAttribute(HttpServer.CONF_CONTEXT_ATTRIBUTE, conf); myServer.start(); String serverURL = "http://" + NetUtils.getHostPortString(myServer.getConnectorAddress(0)) + "/"; - for (String servlet : new String[] { "conf", "logs", "stacks", - "logLevel", "metrics" }) { + for (String servlet : new String[] { "conf", "logs", "stacks", "logLevel", "metrics" }) { for (String user : new String[] { "userA", "userB" }) { assertEquals(HttpURLConnection.HTTP_OK, getHttpStatusCode(serverURL + servlet, user)); @@ -392,8 +381,6 @@ public class TestHttpServer extends HttpServerFunctionalTest { /** * Verify the administrator access for /logs, /stacks, /conf, /logLevel and * /metrics servlets. - * - * @throws Exception */ @Test @Ignore @@ -410,11 +397,11 @@ public class TestHttpServer extends HttpServerFunctionalTest { MyGroupsProvider.class.getName()); Groups.getUserToGroupsMappingService(conf); MyGroupsProvider.clearMapping(); - MyGroupsProvider.mapping.put("userA", Arrays.asList("groupA")); - MyGroupsProvider.mapping.put("userB", Arrays.asList("groupB")); - MyGroupsProvider.mapping.put("userC", Arrays.asList("groupC")); - MyGroupsProvider.mapping.put("userD", Arrays.asList("groupD")); - MyGroupsProvider.mapping.put("userE", Arrays.asList("groupE")); + MyGroupsProvider.mapping.put("userA", Collections.singletonList("groupA")); + MyGroupsProvider.mapping.put("userB", Collections.singletonList("groupB")); + MyGroupsProvider.mapping.put("userC", Collections.singletonList("groupC")); + MyGroupsProvider.mapping.put("userD", Collections.singletonList("groupD")); + MyGroupsProvider.mapping.put("userE", Collections.singletonList("groupE")); HttpServer myServer = new HttpServer.Builder().setName("test") .addEndpoint(new URI("http://localhost:0")).setFindPort(true).setConf(conf) @@ -424,8 +411,7 @@ public class TestHttpServer extends HttpServerFunctionalTest { String serverURL = "http://" + NetUtils.getHostPortString(myServer.getConnectorAddress(0)) + "/"; - for (String servlet : new String[] { "conf", "logs", "stacks", - "logLevel", "metrics" }) { + for (String servlet : new String[] { "conf", "logs", "stacks", "logLevel", "metrics" }) { for (String user : new String[] { "userA", "userB", "userC", "userD" }) { assertEquals(HttpURLConnection.HTTP_OK, getHttpStatusCode(serverURL + servlet, user)); @@ -437,17 +423,17 @@ public class TestHttpServer extends HttpServerFunctionalTest { } @Test - public void testRequestQuoterWithNull() throws Exception { + public void testRequestQuoterWithNull() { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); Mockito.doReturn(null).when(request).getParameterValues("dummy"); RequestQuoter requestQuoter = new RequestQuoter(request); String[] parameterValues = requestQuoter.getParameterValues("dummy"); - Assert.assertEquals("It should return null " - + "when there are no values for the parameter", null, parameterValues); + Assert.assertNull("It should return null " + + "when there are no values for the parameter", parameterValues); } @Test - public void testRequestQuoterWithNotNull() throws Exception { + public void testRequestQuoterWithNotNull() { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); String[] values = new String[] { "abc", "def" }; Mockito.doReturn(values).when(request).getParameterValues("dummy"); @@ -490,7 +476,8 @@ public class TestHttpServer extends HttpServerFunctionalTest { response = Mockito.mock(HttpServletResponse.class); conf.setBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION, true); Assert.assertFalse(HttpServer.hasAdministratorAccess(context, request, response)); - Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED), Mockito.anyString()); + Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED), + Mockito.anyString()); //authorization ON & user NOT NULL & ACLs NULL response = Mockito.mock(HttpServletResponse.class); @@ -503,7 +490,8 @@ public class TestHttpServer extends HttpServerFunctionalTest { Mockito.when(acls.isUserAllowed(Mockito.any())).thenReturn(false); Mockito.when(context.getAttribute(HttpServer.ADMINS_ACL)).thenReturn(acls); Assert.assertFalse(HttpServer.hasAdministratorAccess(context, request, response)); - Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED), Mockito.anyString()); + Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_UNAUTHORIZED), + Mockito.anyString()); //authorization ON & user NOT NULL & ACLs NOT NULL & user in in ACLs response = Mockito.mock(HttpServletResponse.class); @@ -533,7 +521,8 @@ public class TestHttpServer extends HttpServerFunctionalTest { Assert.assertFalse(HttpServer.isInstrumentationAccessAllowed(context, request, response)); } - @Test public void testBindAddress() throws Exception { + @Test + public void testBindAddress() throws Exception { checkBindAddress("localhost", 0, false).stop(); // hang onto this one for a bit more testing HttpServer myServer = checkBindAddress("localhost", 0, false); diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java index 47e6ea26f15..d8a5e647f64 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestPathFilter.java @@ -44,13 +44,12 @@ import org.slf4j.LoggerFactory; @Category({MiscTests.class, SmallTests.class}) public class TestPathFilter extends HttpServerFunctionalTest { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestPathFilter.class); private static final Logger LOG = LoggerFactory.getLogger(HttpServer.class); - static final Set RECORDS = new TreeSet<>(); + private static final Set RECORDS = new TreeSet<>(); /** A very simple filter that records accessed uri's */ static public class RecordingFilter implements Filter { @@ -69,8 +68,9 @@ public class TestPathFilter extends HttpServerFunctionalTest { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (filterConfig == null) - return; + if (filterConfig == null) { + return; + } String uri = ((HttpServletRequest)request).getRequestURI(); LOG.info("filtering " + uri); @@ -89,9 +89,10 @@ public class TestPathFilter extends HttpServerFunctionalTest { } } - - /** access a url, ignoring some IOException such as the page does not exist */ - static void access(String urlstring) throws IOException { + /** + * access a url, ignoring some IOException such as the page does not exist + */ + private static void access(String urlstring) throws IOException { LOG.warn("access " + urlstring); URL url = new URL(urlstring); @@ -99,12 +100,11 @@ public class TestPathFilter extends HttpServerFunctionalTest { connection.connect(); try { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - try { - for(; in.readLine() != null; ); - } finally { - in.close(); + try (BufferedReader in = new BufferedReader( + new InputStreamReader(connection.getInputStream(), "UTF-8"))) { + for (; in.readLine() != null; ) { + // Ignoring the content of the URLs. Only checking if something is there. + } } } catch(IOException ioe) { LOG.warn("urlstring=" + urlstring, ioe); @@ -130,8 +130,7 @@ public class TestPathFilter extends HttpServerFunctionalTest { final String rootURL = "/"; final String allURL = "/*"; - final String[] filteredUrls = {baseURL, baseSlashURL, addedURL, - addedSlashURL, longURL}; + final String[] filteredUrls = { baseURL, baseSlashURL, addedURL, addedSlashURL, longURL }; final String[] notFilteredUrls = {rootURL, allURL}; // access the urls and verify our paths specs got added to the @@ -139,11 +138,11 @@ public class TestPathFilter extends HttpServerFunctionalTest { final String prefix = "http://" + NetUtils.getHostPortString(http.getConnectorAddress(0)); try { - for(int i = 0; i < filteredUrls.length; i++) { - access(prefix + filteredUrls[i]); + for (String filteredUrl : filteredUrls) { + access(prefix + filteredUrl); } - for(int i = 0; i < notFilteredUrls.length; i++) { - access(prefix + notFilteredUrls[i]); + for (String notFilteredUrl : notFilteredUrls) { + access(prefix + notFilteredUrl); } } finally { http.stop(); @@ -152,8 +151,8 @@ public class TestPathFilter extends HttpServerFunctionalTest { LOG.info("RECORDS = " + RECORDS); //verify records - for(int i = 0; i < filteredUrls.length; i++) { - assertTrue(RECORDS.remove(filteredUrls[i])); + for (String filteredUrl : filteredUrls) { + assertTrue(RECORDS.remove(filteredUrl)); } assertTrue(RECORDS.isEmpty()); } diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java index 6c70dbc9d83..35c55319cae 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestServletFilter.java @@ -46,13 +46,12 @@ import org.slf4j.LoggerFactory; @Category({MiscTests.class, SmallTests.class}) public class TestServletFilter extends HttpServerFunctionalTest { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestServletFilter.class); private static final Logger LOG = LoggerFactory.getLogger(HttpServer.class); - static volatile String uri = null; + private static volatile String uri = null; /** A very simple filter which record the uri filtered. */ static public class SimpleFilter implements Filter { @@ -71,8 +70,9 @@ public class TestServletFilter extends HttpServerFunctionalTest { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - if (filterConfig == null) - return; + if (filterConfig == null) { + return; + } uri = ((HttpServletRequest)request).getRequestURI(); LOG.info("filtering " + uri); @@ -90,27 +90,28 @@ public class TestServletFilter extends HttpServerFunctionalTest { } } - public static void assertExceptionContains(String string, Throwable t) { + private static void assertExceptionContains(String string, Throwable t) { String msg = t.getMessage(); Assert.assertTrue( "Expected to find '" + string + "' but got unexpected exception:" + StringUtils.stringifyException(t), msg.contains(string)); } - /** access a url, ignoring some IOException such as the page does not exist */ - static void access(String urlstring) throws IOException { + /** + * access a url, ignoring some IOException such as the page does not exist + */ + private static void access(String urlstring) throws IOException { LOG.warn("access " + urlstring); URL url = new URL(urlstring); URLConnection connection = url.openConnection(); connection.connect(); try { - BufferedReader in = new BufferedReader(new InputStreamReader( - connection.getInputStream())); - try { - for(; in.readLine() != null; ); - } finally { - in.close(); + try (BufferedReader in = new BufferedReader(new InputStreamReader( + connection.getInputStream(), "UTF-8"))) { + for (; in.readLine() != null; ) { + // Ignoring the content of the URLs. Only checking if something is there. + } } } catch(IOException ioe) { LOG.warn("urlstring=" + urlstring, ioe); @@ -150,14 +151,14 @@ public class TestServletFilter extends HttpServerFunctionalTest { final String prefix = "http://" + NetUtils.getHostPortString(http.getConnectorAddress(0)); try { - for(int i = 0; i < sequence.length; i++) { - access(prefix + urls[sequence[i]]); + for (int aSequence : sequence) { + access(prefix + urls[aSequence]); //make sure everything except fsck get filtered - if (sequence[i] == 0) { - assertEquals(null, uri); + if (aSequence == 0) { + assertNull(uri); } else { - assertEquals(urls[sequence[i]], uri); + assertEquals(urls[aSequence], uri); uri = null; } } @@ -218,5 +219,4 @@ public class TestServletFilter extends HttpServerFunctionalTest { assertExceptionContains("Unable to initialize WebAppContext", e); } } - } diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestSpnegoHttpServer.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestSpnegoHttpServer.java index 1262b0c1d43..8d3cd9568a1 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestSpnegoHttpServer.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/TestSpnegoHttpServer.java @@ -69,7 +69,6 @@ import org.slf4j.LoggerFactory; */ @Category({MiscTests.class, SmallTests.class}) public class TestSpnegoHttpServer extends HttpServerFunctionalTest { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestSpnegoHttpServer.class); @@ -208,35 +207,35 @@ public class TestSpnegoHttpServer extends HttpServerFunctionalTest { final String principalName = clientPrincipals.iterator().next().getName(); // Run this code, logged in as the subject (the client) - HttpResponse resp = Subject.doAs(clientSubject, - new PrivilegedExceptionAction() { - @Override - public HttpResponse run() throws Exception { - // Logs in with Kerberos via GSS - GSSManager gssManager = GSSManager.getInstance(); - // jGSS Kerberos login constant - Oid oid = new Oid("1.2.840.113554.1.2.2"); - GSSName gssClient = gssManager.createName(principalName, GSSName.NT_USER_NAME); - GSSCredential credential = gssManager.createCredential(gssClient, - GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY); + HttpResponse resp = Subject.doAs(clientSubject, new PrivilegedExceptionAction() { + @Override + public HttpResponse run() throws Exception { + // Logs in with Kerberos via GSS + GSSManager gssManager = GSSManager.getInstance(); + // jGSS Kerberos login constant + Oid oid = new Oid("1.2.840.113554.1.2.2"); + GSSName gssClient = gssManager.createName(principalName, GSSName.NT_USER_NAME); + GSSCredential credential = gssManager.createCredential(gssClient, + GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY); - HttpClientContext context = HttpClientContext.create(); - Lookup authRegistry = RegistryBuilder.create() - .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)) - .build(); + HttpClientContext context = HttpClientContext.create(); + Lookup authRegistry = RegistryBuilder.create() + .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true, true)) + .build(); - HttpClient client = HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry).build(); - BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential)); + HttpClient client = HttpClients.custom().setDefaultAuthSchemeRegistry(authRegistry) + .build(); + BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new KerberosCredentials(credential)); - URL url = new URL(getServerURL(server), "/echo?a=b"); - context.setTargetHost(new HttpHost(url.getHost(), url.getPort())); - context.setCredentialsProvider(credentialsProvider); - context.setAuthSchemeRegistry(authRegistry); + URL url = new URL(getServerURL(server), "/echo?a=b"); + context.setTargetHost(new HttpHost(url.getHost(), url.getPort())); + context.setCredentialsProvider(credentialsProvider); + context.setAuthSchemeRegistry(authRegistry); - HttpGet get = new HttpGet(url.toURI()); - return client.execute(get, context); - } + HttpGet get = new HttpGet(url.toURI()); + return client.execute(get, context); + } }); assertNotNull(resp); diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java index 9da48193e6d..e258d8517ba 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/log/TestLogLevel.java @@ -17,6 +17,7 @@ */ package org.apache.hadoop.hbase.http.log; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.BufferedReader; @@ -24,6 +25,7 @@ import java.io.InputStreamReader; import java.io.PrintStream; import java.net.URI; import java.net.URL; +import java.util.Objects; import org.apache.hadoop.hbase.HBaseClassTestRule; import org.apache.hadoop.hbase.http.HttpServer; import org.apache.hadoop.hbase.testclassification.MiscTests; @@ -40,12 +42,11 @@ import org.slf4j.impl.Log4jLoggerAdapter; @Category({MiscTests.class, SmallTests.class}) public class TestLogLevel { - @ClassRule public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule.forClass(TestLogLevel.class); - static final PrintStream out = System.out; + private static final PrintStream out = System.out; @Test @SuppressWarnings("deprecation") @@ -53,7 +54,7 @@ public class TestLogLevel { String logName = TestLogLevel.class.getName(); org.slf4j.Logger testlog = LoggerFactory.getLogger(logName); - //only test Log4JLogger + // only test Log4JLogger if (testlog instanceof Log4jLoggerAdapter) { Logger log = LogManager.getLogger(logName); log.debug("log.debug1"); @@ -71,17 +72,17 @@ public class TestLogLevel { String authority = NetUtils.getHostPortString(server .getConnectorAddress(0)); - //servlet + // servlet URL url = new URL("http://" + authority + "/logLevel?log=" + logName + "&level=" + Level.ERROR); out.println("*** Connecting to " + url); try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) { - for(String line; (line = in.readLine()) != null; out.println(line)); + in.lines().filter(Objects::nonNull).forEach(out::println); } log.debug("log.debug2"); log.info("log.info2"); log.error("log.error2"); - assertTrue(Level.ERROR.equals(log.getEffectiveLevel())); + assertEquals(Level.ERROR, log.getEffectiveLevel()); //command line String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()}; @@ -89,14 +90,13 @@ public class TestLogLevel { log.debug("log.debug3"); log.info("log.info3"); log.error("log.error3"); - assertTrue(Level.DEBUG.equals(log.getEffectiveLevel())); + assertEquals(Level.DEBUG, log.getEffectiveLevel()); } finally { if (server != null) { server.stop(); } } - } - else { + } else { out.println(testlog.getClass() + " not tested."); } } diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/resource/JerseyResource.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/resource/JerseyResource.java index 31ff0ed4e0d..593b1c22248 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/resource/JerseyResource.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/resource/JerseyResource.java @@ -52,7 +52,7 @@ public class JerseyResource { public Response get( @PathParam(PATH) @DefaultValue("UNKNOWN_" + PATH) final String path, @QueryParam(OP) @DefaultValue("UNKNOWN_" + OP) final String op - ) throws IOException { + ) throws IOException { LOG.info("get: " + PATH + "=" + path + ", " + OP + "=" + op); final Map m = new TreeMap<>(); diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/ssl/KeyStoreTestUtil.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/ssl/KeyStoreTestUtil.java index 35d62369491..aa8c2446c61 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/ssl/KeyStoreTestUtil.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/ssl/KeyStoreTestUtil.java @@ -49,7 +49,9 @@ import org.apache.hadoop.security.ssl.FileBasedKeyStoresFactory; import org.apache.hadoop.security.ssl.SSLFactory; import org.bouncycastle.x509.X509V1CertificateGenerator; -public class KeyStoreTestUtil { +public final class KeyStoreTestUtil { + private KeyStoreTestUtil() { + } public static String getClasspathDir(Class klass) throws Exception { String file = klass.getName(); @@ -69,11 +71,12 @@ public class KeyStoreTestUtil { * @param algorithm the signing algorithm, eg "SHA1withRSA" * @return the self-signed certificate */ - public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, String algorithm) - throws CertificateEncodingException, InvalidKeyException, IllegalStateException, - NoSuchProviderException, NoSuchAlgorithmException, SignatureException { + public static X509Certificate generateCertificate(String dn, KeyPair pair, int days, + String algorithm) throws CertificateEncodingException, InvalidKeyException, + IllegalStateException, NoSuchProviderException, NoSuchAlgorithmException, + SignatureException { Date from = new Date(); - Date to = new Date(from.getTime() + days * 86400000l); + Date to = new Date(from.getTime() + days * 86400000L); BigInteger sn = new BigInteger(64, new SecureRandom()); KeyPair keyPair = pair; X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();