001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.http; 019 020import java.io.BufferedReader; 021import java.io.IOException; 022import java.io.InputStreamReader; 023import java.net.URL; 024import java.net.URLConnection; 025import java.util.Set; 026import java.util.TreeSet; 027import javax.servlet.Filter; 028import javax.servlet.FilterChain; 029import javax.servlet.FilterConfig; 030import javax.servlet.ServletException; 031import javax.servlet.ServletRequest; 032import javax.servlet.ServletResponse; 033import javax.servlet.http.HttpServletRequest; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.HBaseClassTestRule; 036import org.apache.hadoop.hbase.testclassification.MiscTests; 037import org.apache.hadoop.hbase.testclassification.SmallTests; 038import org.apache.hadoop.net.NetUtils; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045@Category({MiscTests.class, SmallTests.class}) 046public class TestPathFilter extends HttpServerFunctionalTest { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestPathFilter.class); 051 052 private static final Logger LOG = LoggerFactory.getLogger(HttpServer.class); 053 static final Set<String> RECORDS = new TreeSet<>(); 054 055 /** A very simple filter that records accessed uri's */ 056 static public class RecordingFilter implements Filter { 057 private FilterConfig filterConfig = null; 058 059 @Override 060 public void init(FilterConfig filterConfig) { 061 this.filterConfig = filterConfig; 062 } 063 064 @Override 065 public void destroy() { 066 this.filterConfig = null; 067 } 068 069 @Override 070 public void doFilter(ServletRequest request, ServletResponse response, 071 FilterChain chain) throws IOException, ServletException { 072 if (filterConfig == null) 073 return; 074 075 String uri = ((HttpServletRequest)request).getRequestURI(); 076 LOG.info("filtering " + uri); 077 RECORDS.add(uri); 078 chain.doFilter(request, response); 079 } 080 081 /** Configuration for RecordingFilter */ 082 static public class Initializer extends FilterInitializer { 083 public Initializer() {} 084 085 @Override 086 public void initFilter(FilterContainer container, Configuration conf) { 087 container.addFilter("recording", RecordingFilter.class.getName(), null); 088 } 089 } 090 } 091 092 093 /** access a url, ignoring some IOException such as the page does not exist */ 094 static void access(String urlstring) throws IOException { 095 LOG.warn("access " + urlstring); 096 URL url = new URL(urlstring); 097 098 URLConnection connection = url.openConnection(); 099 connection.connect(); 100 101 try { 102 BufferedReader in = new BufferedReader(new InputStreamReader( 103 connection.getInputStream())); 104 try { 105 for(; in.readLine() != null; ); 106 } finally { 107 in.close(); 108 } 109 } catch(IOException ioe) { 110 LOG.warn("urlstring=" + urlstring, ioe); 111 } 112 } 113 114 @Test 115 public void testPathSpecFilters() throws Exception { 116 Configuration conf = new Configuration(); 117 118 //start a http server with CountingFilter 119 conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY, 120 RecordingFilter.Initializer.class.getName()); 121 String[] pathSpecs = { "/path", "/path/*" }; 122 HttpServer http = createTestServer(conf, pathSpecs); 123 http.start(); 124 125 final String baseURL = "/path"; 126 final String baseSlashURL = "/path/"; 127 final String addedURL = "/path/nodes"; 128 final String addedSlashURL = "/path/nodes/"; 129 final String longURL = "/path/nodes/foo/job"; 130 final String rootURL = "/"; 131 final String allURL = "/*"; 132 133 final String[] filteredUrls = {baseURL, baseSlashURL, addedURL, 134 addedSlashURL, longURL}; 135 final String[] notFilteredUrls = {rootURL, allURL}; 136 137 // access the urls and verify our paths specs got added to the 138 // filters 139 final String prefix = "http://" 140 + NetUtils.getHostPortString(http.getConnectorAddress(0)); 141 try { 142 for(int i = 0; i < filteredUrls.length; i++) { 143 access(prefix + filteredUrls[i]); 144 } 145 for(int i = 0; i < notFilteredUrls.length; i++) { 146 access(prefix + notFilteredUrls[i]); 147 } 148 } finally { 149 http.stop(); 150 } 151 152 LOG.info("RECORDS = " + RECORDS); 153 154 //verify records 155 for(int i = 0; i < filteredUrls.length; i++) { 156 assertTrue(RECORDS.remove(filteredUrls[i])); 157 } 158 assertTrue(RECORDS.isEmpty()); 159 } 160}