Line data Source code
1 : // Copyright (C) 2019 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.RequestCleanup; 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 : 31 : /** Executes any pending {@link RequestCleanup} at the end of a request. */ 32 : @Singleton 33 : public class RequestCleanupFilter implements Filter { 34 : public static Module module() { 35 99 : return new ServletModule() { 36 : @Override 37 : protected void configureServlets() { 38 99 : filter("/*").through(RequestCleanupFilter.class); 39 99 : } 40 : }; 41 : } 42 : 43 : private final Provider<RequestCleanup> cleanup; 44 : 45 : @Inject 46 99 : RequestCleanupFilter(Provider<RequestCleanup> r) { 47 99 : cleanup = r; 48 99 : } 49 : 50 : @Override 51 99 : public void init(FilterConfig filterConfig) {} 52 : 53 : @Override 54 99 : public void destroy() {} 55 : 56 : @Override 57 : public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 58 : throws IOException, ServletException { 59 : try { 60 38 : chain.doFilter(request, response); 61 : } finally { 62 38 : cleanup.get().run(); 63 : } 64 38 : } 65 : }