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