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