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 TestGlobalFilter extends HttpServerFunctionalTest { 044 045 private static final Logger LOG = LoggerFactory.getLogger(TestGlobalFilter.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.addGlobalFilter("recording", RecordingFilter.class.getName(), null); 083 } 084 } 085 } 086 087 @Test 088 public void testServletFilter() 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 HttpServer http = createTestServer(conf); 094 http.start(); 095 096 final String fsckURL = "/fsck"; 097 final String stacksURL = "/stacks"; 098 final String ajspURL = "/a.jsp"; 099 final String listPathsURL = "/listPaths"; 100 final String dataURL = "/data"; 101 final String streamFile = "/streamFile"; 102 final String rootURL = "/"; 103 final String allURL = "/*"; 104 final String outURL = "/static/a.out"; 105 final String logURL = "/logs/a.log"; 106 107 final String[] urls = { fsckURL, stacksURL, ajspURL, listPathsURL, dataURL, streamFile, rootURL, 108 allURL, outURL, logURL }; 109 110 // access the urls 111 final String prefix = "http://" + NetUtils.getHostPortString(http.getConnectorAddress(0)); 112 try { 113 for (String url : urls) { 114 access(prefix + url); 115 } 116 } finally { 117 http.stop(); 118 } 119 120 LOG.info("RECORDS = " + RECORDS); 121 122 // verify records 123 for (String url : urls) { 124 assertTrue(RECORDS.remove(url)); 125 } 126 assertTrue(RECORDS.isEmpty()); 127 } 128}