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 static org.junit.Assert.assertTrue; 021 022import java.io.IOException; 023import java.util.Set; 024import java.util.TreeSet; 025import javax.servlet.Filter; 026import javax.servlet.FilterChain; 027import javax.servlet.FilterConfig; 028import javax.servlet.ServletException; 029import javax.servlet.ServletRequest; 030import javax.servlet.ServletResponse; 031import javax.servlet.http.HttpServletRequest; 032import org.apache.hadoop.conf.Configuration; 033import org.apache.hadoop.hbase.HBaseClassTestRule; 034import org.apache.hadoop.hbase.testclassification.MiscTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.net.NetUtils; 037import org.junit.ClassRule; 038import org.junit.Test; 039import org.junit.experimental.categories.Category; 040import org.slf4j.Logger; 041import org.slf4j.LoggerFactory; 042 043@Category({ MiscTests.class, SmallTests.class }) 044public class TestPathFilter extends HttpServerFunctionalTest { 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestPathFilter.class); 048 049 private static final Logger LOG = LoggerFactory.getLogger(HttpServer.class); 050 private static final Set<String> RECORDS = new TreeSet<>(); 051 052 /** A very simple filter that records accessed uri's */ 053 static public class RecordingFilter implements Filter { 054 private FilterConfig filterConfig = null; 055 056 @Override 057 public void init(FilterConfig filterConfig) { 058 this.filterConfig = filterConfig; 059 } 060 061 @Override 062 public void destroy() { 063 this.filterConfig = null; 064 } 065 066 @Override 067 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 068 throws IOException, ServletException { 069 if (filterConfig == null) { 070 return; 071 } 072 073 String uri = ((HttpServletRequest) request).getRequestURI(); 074 LOG.info("filtering " + uri); 075 RECORDS.add(uri); 076 chain.doFilter(request, response); 077 } 078 079 /** Configuration for RecordingFilter */ 080 static public class Initializer extends FilterInitializer { 081 public Initializer() { 082 } 083 084 @Override 085 public void initFilter(FilterContainer container, Configuration conf) { 086 container.addFilter("recording", RecordingFilter.class.getName(), null); 087 } 088 } 089 } 090 091 @Test 092 public void testPathSpecFilters() throws Exception { 093 Configuration conf = new Configuration(); 094 095 // start an http server with CountingFilter 096 conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY, RecordingFilter.Initializer.class.getName()); 097 String[] pathSpecs = { "/path", "/path/*" }; 098 HttpServer http = createTestServer(conf, pathSpecs); 099 http.start(); 100 101 final String baseURL = "/path"; 102 final String baseSlashURL = "/path/"; 103 final String addedURL = "/path/nodes"; 104 final String addedSlashURL = "/path/nodes/"; 105 final String longURL = "/path/nodes/foo/job"; 106 final String rootURL = "/"; 107 final String allURL = "/*"; 108 109 final String[] filteredUrls = { baseURL, baseSlashURL, addedURL, addedSlashURL, longURL }; 110 final String[] notFilteredUrls = { rootURL, allURL }; 111 112 // access the urls and verify our paths specs got added to the 113 // filters 114 final String prefix = "http://" + NetUtils.getHostPortString(http.getConnectorAddress(0)); 115 try { 116 for (String filteredUrl : filteredUrls) { 117 access(prefix + filteredUrl); 118 } 119 for (String notFilteredUrl : notFilteredUrls) { 120 access(prefix + notFilteredUrl); 121 } 122 } finally { 123 http.stop(); 124 } 125 126 LOG.info("RECORDS = " + RECORDS); 127 128 // verify records 129 for (String filteredUrl : filteredUrls) { 130 assertTrue(RECORDS.remove(filteredUrl)); 131 } 132 assertTrue(RECORDS.isEmpty()); 133 } 134}