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 020package org.apache.oozie.servlet; 021 022import org.apache.oozie.util.XLog; 023 024import javax.servlet.Filter; 025import javax.servlet.FilterChain; 026import javax.servlet.FilterConfig; 027import javax.servlet.ServletException; 028import javax.servlet.ServletRequest; 029import javax.servlet.ServletResponse; 030import java.io.IOException; 031import java.net.InetAddress; 032import java.net.UnknownHostException; 033 034/** 035 * Filter that resolves the requester hostname. 036 */ 037public class HostnameFilter implements Filter { 038 private static final XLog LOG = XLog.getLog(HostnameFilter.class); 039 040 static final ThreadLocal<String> HOSTNAME_TL = new ThreadLocal<String>(); 041 042 /** 043 * Initializes the filter. 044 * <p> 045 * This implementation is a NOP. 046 * 047 * @param config filter configuration. 048 * 049 * @throws ServletException thrown if the filter could not be initialized. 050 */ 051 @Override 052 public void init(FilterConfig config) throws ServletException { 053 } 054 055 /** 056 * Resolves the requester hostname and delegates the request to the chain. 057 * <p> 058 * The requester hostname is available via the {@link #get} method. 059 * 060 * @param request servlet request. 061 * @param response servlet response. 062 * @param chain filter chain. 063 * 064 * @throws IOException thrown if an IO error occurrs. 065 * @throws ServletException thrown if a servet error occurrs. 066 */ 067 @Override 068 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 069 throws IOException, ServletException { 070 try { 071 String hostname; 072 try { 073 String address = request.getRemoteAddr(); 074 if (address != null) { 075 hostname = InetAddress.getByName(address).getCanonicalHostName(); 076 } else { 077 LOG.warn("Request remote address is NULL"); 078 hostname = "???"; 079 } 080 } catch (UnknownHostException ex) { 081 LOG.warn("Request remote address could not be resolved, {0}", ex.toString(), ex); 082 hostname = "???"; 083 } 084 HOSTNAME_TL.set(hostname); 085 XLog.Info.get().clear(); 086 chain.doFilter(request, response); 087 } 088 finally { 089 HOSTNAME_TL.remove(); 090 } 091 } 092 093 /** 094 * Returns the requester hostname. 095 * 096 * @return the requester hostname. 097 */ 098 public static String get() { 099 return HOSTNAME_TL.get(); 100 } 101 102 /** 103 * Destroys the filter. 104 * <p> 105 * This implementation is a NOP. 106 */ 107 @Override 108 public void destroy() { 109 } 110}