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