View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.http;
19  
20  import java.io.BufferedReader;
21  import java.io.IOException;
22  import java.io.InputStreamReader;
23  import java.net.URL;
24  import java.net.URLConnection;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import javax.servlet.Filter;
29  import javax.servlet.FilterChain;
30  import javax.servlet.FilterConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.ServletRequest;
33  import javax.servlet.ServletResponse;
34  import javax.servlet.http.HttpServletRequest;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.apache.hadoop.conf.Configuration;
39  import org.apache.hadoop.hbase.testclassification.SmallTests;
40  import org.apache.hadoop.net.NetUtils;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(SmallTests.class)
45  public class TestGlobalFilter extends HttpServerFunctionalTest {
46    static final Log LOG = LogFactory.getLog(HttpServer.class);
47    static final Set<String> RECORDS = new TreeSet<String>(); 
48  
49    /** A very simple filter that records accessed uri's */
50    static public class RecordingFilter implements Filter {
51      private FilterConfig filterConfig = null;
52  
53      @Override
54      public void init(FilterConfig filterConfig) {
55        this.filterConfig = filterConfig;
56      }
57  
58      @Override
59      public void destroy() {
60        this.filterConfig = null;
61      }
62  
63      @Override
64      public void doFilter(ServletRequest request, ServletResponse response,
65          FilterChain chain) throws IOException, ServletException {
66        if (filterConfig == null)
67           return;
68  
69        String uri = ((HttpServletRequest)request).getRequestURI();
70        LOG.info("filtering " + uri);
71        RECORDS.add(uri);
72        chain.doFilter(request, response);
73      }
74  
75      /** Configuration for RecordingFilter */
76      static public class Initializer extends FilterInitializer {
77        public Initializer() {}
78  
79        @Override
80        public void initFilter(FilterContainer container, Configuration conf) {
81          container.addGlobalFilter("recording", RecordingFilter.class.getName(), null);
82        }
83      }
84    }
85    
86    
87    /** access a url, ignoring some IOException such as the page does not exist */
88    static void access(String urlstring) throws IOException {
89      LOG.warn("access " + urlstring);
90      URL url = new URL(urlstring);
91      URLConnection connection = url.openConnection();
92      connection.connect();
93      
94      try {
95        BufferedReader in = new BufferedReader(new InputStreamReader(
96            connection.getInputStream()));
97        try {
98          for(; in.readLine() != null; );
99        } finally {
100         in.close();
101       }
102     } catch(IOException ioe) {
103       LOG.warn("urlstring=" + urlstring, ioe);
104     }
105   }
106 
107   @Test
108   public void testServletFilter() throws Exception {
109     Configuration conf = new Configuration();
110     
111     //start a http server with CountingFilter
112     conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY,
113         RecordingFilter.Initializer.class.getName());
114     HttpServer http = createTestServer(conf);
115     http.start();
116 
117     final String fsckURL = "/fsck";
118     final String stacksURL = "/stacks";
119     final String ajspURL = "/a.jsp";
120     final String listPathsURL = "/listPaths";
121     final String dataURL = "/data";
122     final String streamFile = "/streamFile";
123     final String rootURL = "/";
124     final String allURL = "/*";
125     final String outURL = "/static/a.out";
126     final String logURL = "/logs/a.log";
127 
128     final String[] urls = {fsckURL, stacksURL, ajspURL, listPathsURL, 
129         dataURL, streamFile, rootURL, allURL, outURL, logURL};
130 
131     //access the urls
132     final String prefix = "http://"
133         + NetUtils.getHostPortString(http.getConnectorAddress(0));
134     try {
135       for(int i = 0; i < urls.length; i++) {
136         access(prefix + urls[i]);
137       }
138     } finally {
139       http.stop();
140     }
141 
142     LOG.info("RECORDS = " + RECORDS);
143     
144     //verify records
145     for(int i = 0; i < urls.length; i++) {
146       assertTrue(RECORDS.remove(urls[i]));
147     }
148     assertTrue(RECORDS.isEmpty());
149   }
150 }