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 TestPathFilter extends HttpServerFunctionalTest {
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestPathFilter.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,
066        FilterChain chain) 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.addFilter("recording", RecordingFilter.class.getName(), null);
084      }
085    }
086  }
087
088  @Test
089  public void testPathSpecFilters() throws Exception {
090    Configuration conf = new Configuration();
091
092    //start a http server with CountingFilter
093    conf.set(HttpServer.FILTER_INITIALIZERS_PROPERTY,
094        RecordingFilter.Initializer.class.getName());
095    String[] pathSpecs = { "/path", "/path/*" };
096    HttpServer http = createTestServer(conf, pathSpecs);
097    http.start();
098
099    final String baseURL = "/path";
100    final String baseSlashURL = "/path/";
101    final String addedURL = "/path/nodes";
102    final String addedSlashURL = "/path/nodes/";
103    final String longURL = "/path/nodes/foo/job";
104    final String rootURL = "/";
105    final String allURL = "/*";
106
107    final String[] filteredUrls = { baseURL, baseSlashURL, addedURL, addedSlashURL, longURL };
108    final String[] notFilteredUrls = {rootURL, allURL};
109
110    // access the urls and verify our paths specs got added to the
111    // filters
112    final String prefix = "http://"
113        + NetUtils.getHostPortString(http.getConnectorAddress(0));
114    try {
115      for (String filteredUrl : filteredUrls) {
116        access(prefix + filteredUrl);
117      }
118      for (String notFilteredUrl : notFilteredUrls) {
119        access(prefix + notFilteredUrl);
120      }
121    } finally {
122      http.stop();
123    }
124
125    LOG.info("RECORDS = " + RECORDS);
126
127    //verify records
128    for (String filteredUrl : filteredUrls) {
129      assertTrue(RECORDS.remove(filteredUrl));
130    }
131    assertTrue(RECORDS.isEmpty());
132  }
133}