Line data Source code
1 : // Copyright (C) 2009 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.util.RequestContext; 18 : import com.google.gerrit.server.util.ThreadLocalRequestContext; 19 : import com.google.inject.Inject; 20 : import com.google.inject.Module; 21 : import com.google.inject.Provider; 22 : import com.google.inject.Singleton; 23 : import com.google.inject.servlet.ServletModule; 24 : import java.io.IOException; 25 : import javax.servlet.Filter; 26 : import javax.servlet.FilterChain; 27 : import javax.servlet.FilterConfig; 28 : import javax.servlet.ServletException; 29 : import javax.servlet.ServletRequest; 30 : import javax.servlet.ServletResponse; 31 : 32 : /** Set the request context for the downstream filters and invocation. */ 33 : @Singleton 34 : public class RequestContextFilter implements Filter { 35 : public static Module module() { 36 99 : return new ServletModule() { 37 : @Override 38 : protected void configureServlets() { 39 99 : filter("/*").through(RequestContextFilter.class); 40 99 : } 41 : }; 42 : } 43 : 44 : private final Provider<HttpRequestContext> requestContext; 45 : private final ThreadLocalRequestContext local; 46 : 47 : @Inject 48 99 : RequestContextFilter(Provider<HttpRequestContext> c, ThreadLocalRequestContext l) { 49 99 : requestContext = c; 50 99 : local = l; 51 99 : } 52 : 53 : @Override 54 99 : public void init(FilterConfig filterConfig) {} 55 : 56 : @Override 57 99 : public void destroy() {} 58 : 59 : @Override 60 : public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 61 : throws IOException, ServletException { 62 38 : RequestContext old = local.setContext(requestContext.get()); 63 : try { 64 38 : chain.doFilter(request, response); 65 : } finally { 66 38 : local.setContext(old); 67 : } 68 38 : } 69 : }