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