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 */ 018 019 package org.apache.hadoop.lib.servlet; 020 021 022 import org.apache.hadoop.classification.InterfaceAudience; 023 024 import javax.servlet.Filter; 025 import javax.servlet.FilterChain; 026 import javax.servlet.FilterConfig; 027 import javax.servlet.ServletException; 028 import javax.servlet.ServletRequest; 029 import javax.servlet.ServletResponse; 030 import java.io.IOException; 031 import java.net.InetAddress; 032 033 /** 034 * Filter that resolves the requester hostname. 035 */ 036 @InterfaceAudience.Private 037 public class HostnameFilter implements Filter { 038 static final ThreadLocal<String> HOSTNAME_TL = new ThreadLocal<String>(); 039 040 /** 041 * Initializes the filter. 042 * <p/> 043 * This implementation is a NOP. 044 * 045 * @param config filter configuration. 046 * 047 * @throws ServletException thrown if the filter could not be initialized. 048 */ 049 @Override 050 public void init(FilterConfig config) throws ServletException { 051 } 052 053 /** 054 * Resolves the requester hostname and delegates the request to the chain. 055 * <p/> 056 * The requester hostname is available via the {@link #get} method. 057 * 058 * @param request servlet request. 059 * @param response servlet response. 060 * @param chain filter chain. 061 * 062 * @throws IOException thrown if an IO error occurrs. 063 * @throws ServletException thrown if a servet error occurrs. 064 */ 065 @Override 066 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 067 throws IOException, ServletException { 068 try { 069 String hostname = InetAddress.getByName(request.getRemoteAddr()).getCanonicalHostName(); 070 HOSTNAME_TL.set(hostname); 071 chain.doFilter(request, response); 072 } finally { 073 HOSTNAME_TL.remove(); 074 } 075 } 076 077 /** 078 * Returns the requester hostname. 079 * 080 * @return the requester hostname. 081 */ 082 public static String get() { 083 return HOSTNAME_TL.get(); 084 } 085 086 /** 087 * Destroys the filter. 088 * <p/> 089 * This implementation is a NOP. 090 */ 091 @Override 092 public void destroy() { 093 } 094 }