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