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
019package org.apache.hadoop.lib.servlet;
020
021import org.slf4j.MDC;
022
023import javax.servlet.Filter;
024import javax.servlet.FilterChain;
025import javax.servlet.FilterConfig;
026import javax.servlet.ServletException;
027import javax.servlet.ServletRequest;
028import javax.servlet.ServletResponse;
029import javax.servlet.http.HttpServletRequest;
030import java.io.IOException;
031import java.security.Principal;
032
033/**
034 * Filter that sets request contextual information for the slf4j MDC.
035 * <p/>
036 * It sets the following values:
037 * <ul>
038 * <li>hostname: if the {@link HostnameFilter} is present and configured
039 * before this filter</li>
040 * <li>user: the <code>HttpServletRequest.getUserPrincipal().getName()</code></li>
041 * <li>method: the HTTP method fo the request (GET, POST, ...)</li>
042 * <li>path: the path of the request URL</li>
043 * </ul>
044 */
045public class MDCFilter implements Filter {
046
047  /**
048   * Initializes the filter.
049   * <p/>
050   * This implementation is a NOP.
051   *
052   * @param config filter configuration.
053   *
054   * @throws ServletException thrown if the filter could not be initialized.
055   */
056  @Override
057  public void init(FilterConfig config) throws ServletException {
058  }
059
060  /**
061   * Sets the slf4j <code>MDC</code> and delegates the request to the chain.
062   *
063   * @param request servlet request.
064   * @param response servlet response.
065   * @param chain filter chain.
066   *
067   * @throws IOException thrown if an IO error occurrs.
068   * @throws ServletException thrown if a servet error occurrs.
069   */
070  @Override
071  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
072    throws IOException, ServletException {
073    try {
074      MDC.clear();
075      String hostname = HostnameFilter.get();
076      if (hostname != null) {
077        MDC.put("hostname", HostnameFilter.get());
078      }
079      Principal principal = ((HttpServletRequest) request).getUserPrincipal();
080      String user = (principal != null) ? principal.getName() : null;
081      if (user != null) {
082        MDC.put("user", user);
083      }
084      MDC.put("method", ((HttpServletRequest) request).getMethod());
085      MDC.put("path", ((HttpServletRequest) request).getPathInfo());
086      chain.doFilter(request, response);
087    } finally {
088      MDC.clear();
089    }
090  }
091
092  /**
093   * Destroys the filter.
094   * <p/>
095   * This implementation is a NOP.
096   */
097  @Override
098  public void destroy() {
099  }
100}
101