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.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 | |
---|
40 | public class TestGlobalFilter extends junit.framework.TestCase { |
---|
41 | static final Log LOG = LogFactory.getLog(HttpServer.class); |
---|
42 | static final Set<String> RECORDS = new TreeSet<String>(); |
---|
43 | |
---|
44 | /** A very simple filter that records accessed uri's */ |
---|
45 | static public class RecordingFilter implements Filter { |
---|
46 | private FilterConfig filterConfig = null; |
---|
47 | |
---|
48 | public void init(FilterConfig filterConfig) { |
---|
49 | this.filterConfig = filterConfig; |
---|
50 | } |
---|
51 | |
---|
52 | public void destroy() { |
---|
53 | this.filterConfig = null; |
---|
54 | } |
---|
55 | |
---|
56 | public void doFilter(ServletRequest request, ServletResponse response, |
---|
57 | FilterChain chain) throws IOException, ServletException { |
---|
58 | if (filterConfig == null) |
---|
59 | return; |
---|
60 | |
---|
61 | String uri = ((HttpServletRequest)request).getRequestURI(); |
---|
62 | LOG.info("filtering " + uri); |
---|
63 | RECORDS.add(uri); |
---|
64 | chain.doFilter(request, response); |
---|
65 | } |
---|
66 | |
---|
67 | /** Configuration for RecordingFilter */ |
---|
68 | static public class Initializer extends FilterInitializer { |
---|
69 | public Initializer() {} |
---|
70 | |
---|
71 | void initFilter(FilterContainer container) { |
---|
72 | container.addGlobalFilter("recording", RecordingFilter.class.getName(), null); |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | /** access a url, ignoring some IOException such as the page does not exist */ |
---|
79 | static void access(String urlstring) throws IOException { |
---|
80 | LOG.warn("access " + urlstring); |
---|
81 | URL url = new URL(urlstring); |
---|
82 | URLConnection connection = url.openConnection(); |
---|
83 | connection.connect(); |
---|
84 | |
---|
85 | try { |
---|
86 | BufferedReader in = new BufferedReader(new InputStreamReader( |
---|
87 | connection.getInputStream())); |
---|
88 | try { |
---|
89 | for(; in.readLine() != null; ); |
---|
90 | } finally { |
---|
91 | in.close(); |
---|
92 | } |
---|
93 | } catch(IOException ioe) { |
---|
94 | LOG.warn("urlstring=" + urlstring, ioe); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | public void testServletFilter() throws Exception { |
---|
99 | Configuration conf = new Configuration(); |
---|
100 | |
---|
101 | //start a http server with CountingFilter |
---|
102 | conf.set(HttpServer.FILTER_INITIALIZER_PROPERTY, |
---|
103 | RecordingFilter.Initializer.class.getName()); |
---|
104 | HttpServer http = new HttpServer("datanode", "localhost", 0, true, conf); |
---|
105 | http.start(); |
---|
106 | |
---|
107 | final String fsckURL = "/fsck"; |
---|
108 | final String stacksURL = "/stacks"; |
---|
109 | final String ajspURL = "/a.jsp"; |
---|
110 | final String listPathsURL = "/listPaths"; |
---|
111 | final String dataURL = "/data"; |
---|
112 | final String streamFile = "/streamFile"; |
---|
113 | final String rootURL = "/"; |
---|
114 | final String allURL = "/*"; |
---|
115 | final String outURL = "/static/a.out"; |
---|
116 | final String logURL = "/logs/a.log"; |
---|
117 | |
---|
118 | final String[] urls = {fsckURL, stacksURL, ajspURL, listPathsURL, |
---|
119 | dataURL, streamFile, rootURL, allURL, outURL, logURL}; |
---|
120 | |
---|
121 | //access the urls |
---|
122 | final String prefix = "http://localhost:" + http.getPort(); |
---|
123 | try { |
---|
124 | for(int i = 0; i < urls.length; i++) { |
---|
125 | access(prefix + urls[i]); |
---|
126 | } |
---|
127 | } finally { |
---|
128 | http.stop(); |
---|
129 | } |
---|
130 | |
---|
131 | LOG.info("RECORDS = " + RECORDS); |
---|
132 | |
---|
133 | //verify records |
---|
134 | for(int i = 0; i < urls.length; i++) { |
---|
135 | assertTrue(RECORDS.remove(urls[i])); |
---|
136 | } |
---|
137 | assertTrue(RECORDS.isEmpty()); |
---|
138 | } |
---|
139 | } |
---|