Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.httpd; 16 : 17 : import com.google.gerrit.server.CurrentUser; 18 : import com.google.inject.Inject; 19 : import com.google.inject.Module; 20 : import com.google.inject.Provider; 21 : import com.google.inject.Singleton; 22 : import com.google.inject.servlet.ServletModule; 23 : import java.io.IOException; 24 : import javax.servlet.Filter; 25 : import javax.servlet.FilterChain; 26 : import javax.servlet.FilterConfig; 27 : import javax.servlet.ServletException; 28 : import javax.servlet.ServletRequest; 29 : import javax.servlet.ServletResponse; 30 : import javax.servlet.http.HttpServletRequest; 31 : 32 : @Singleton 33 : public class SetThreadNameFilter implements Filter { 34 : private static final int MAX_PATH_LENGTH = 120; 35 : 36 : public static Module module() { 37 99 : return new ServletModule() { 38 : @Override 39 : protected void configureServlets() { 40 99 : filter("/*").through(SetThreadNameFilter.class); 41 99 : } 42 : }; 43 : } 44 : 45 : private final Provider<CurrentUser> user; 46 : 47 : @Inject 48 99 : public SetThreadNameFilter(Provider<CurrentUser> user) { 49 99 : this.user = user; 50 99 : } 51 : 52 : @Override 53 99 : public void init(FilterConfig filterConfig) {} 54 : 55 : @Override 56 : public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 57 : throws IOException, ServletException { 58 38 : Thread current = Thread.currentThread(); 59 38 : String old = current.getName(); 60 : try { 61 38 : current.setName(computeName((HttpServletRequest) request)); 62 38 : chain.doFilter(request, response); 63 : } finally { 64 38 : current.setName(old); 65 : } 66 38 : } 67 : 68 : private String computeName(HttpServletRequest req) { 69 38 : StringBuilder s = new StringBuilder(); 70 38 : s.append("HTTP "); 71 38 : s.append(req.getMethod()); 72 38 : s.append(" "); 73 38 : s.append(req.getRequestURI()); 74 38 : String query = req.getQueryString(); 75 38 : if (query != null) { 76 20 : s.append("?").append(query); 77 : } 78 38 : if (s.length() > MAX_PATH_LENGTH) { 79 19 : s.delete(MAX_PATH_LENGTH, s.length()); 80 : } 81 38 : s.append(" ("); 82 38 : s.append(user.get().getUserName().orElse("N/A")); 83 38 : s.append(" from "); 84 38 : s.append(req.getRemoteAddr()); 85 38 : s.append(")"); 86 38 : return s.toString(); 87 : } 88 : 89 : @Override 90 99 : public void destroy() {} 91 : }