|
| 1 | +/* |
| 2 | + * Copyright 2015-2016 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package jp.xet.sparwings.aws.ec2; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import javax.servlet.Filter; |
| 22 | +import javax.servlet.FilterChain; |
| 23 | +import javax.servlet.ServletException; |
| 24 | +import javax.servlet.ServletRequest; |
| 25 | +import javax.servlet.ServletResponse; |
| 26 | + |
| 27 | +import org.slf4j.MDC; |
| 28 | + |
| 29 | +/** |
| 30 | + * MDC の情報を request scope から設定する Filter. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * 経緯は以下の通り |
| 34 | + * 1. Springでコントローラーから例外をスローすると2週目のフィルターチェーンが動く |
| 35 | + * 2. FilterでMDCを操作する際にdoFilterの後MDCをクリアすると、2週目のフィルターチェーンでMDCがクリアされている、という状況になる |
| 36 | + * 3. 1リクエストに対して1回だけMDCに何かを登録したい場合、OncePerRequestFilterで一度だけMDCにを操作したい |
| 37 | + * - つまり、1つのトランザクションに対して1度だけMDCを操作したい場合、同じトランザクションの2回目以降のフィルターチェーンでもMDCを参照したい |
| 38 | + * 4. 初回のフィルターチェーンでMDCをリクエストスコープにコピー & 2回目以降のフィルターチェーンで |
| 39 | + * リクエストスコープにコピーしたMDCをThreadLocalのMDCにセットすることで、 |
| 40 | + * 1回目に組み立てたMDCを2回目以降のフィルターチェーンでも使い回せるようにする |
| 41 | + * |
| 42 | + * 本 Filter は、LogbookFilter の直前に登録する必要があります。 |
| 43 | + * DispatcherType には REQUEST, ASYNC, ERROR を含めてください。 |
| 44 | + * </p> |
| 45 | + */ |
| 46 | +public class MDCInsertingForRequestServletFilter implements Filter { |
| 47 | + |
| 48 | + static final String STORED_MDC_KEY = |
| 49 | + MDCInsertingForRequestServletFilter.class.getName() + "_STORED_MDC_KEY"; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * filter 処理. |
| 54 | + * |
| 55 | + * <p> |
| 56 | + * 初回リクエストの場合、以下を行う |
| 57 | + * |
| 58 | + * - request scope に MDC の値をコピー |
| 59 | + * - 後続の filter 呼び出し |
| 60 | + * |
| 61 | + * 2回目以降のリクエストの場合、以下を行う |
| 62 | + * |
| 63 | + * - request scope から MDC の値を設定 |
| 64 | + * - 後続の filter 呼び出し |
| 65 | + * - MDC の値をクリア |
| 66 | + * |
| 67 | + * </p> |
| 68 | + * @param request request |
| 69 | + * @param response response |
| 70 | + * @param chain chain |
| 71 | + * @throws IOException 例外 |
| 72 | + * @throws ServletException 例外 |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 76 | + throws IOException, ServletException { |
| 77 | + |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + Map<String, String> storedMdc = (Map<String, String>) request.getAttribute(STORED_MDC_KEY); |
| 80 | + if (storedMdc == null) { |
| 81 | + // request scope に未設定の場合、初回扱い |
| 82 | + firstTimeDoFilter(request, response, chain); |
| 83 | + } else { |
| 84 | + // request scope に設定済みの場合、2回目以降の呼び出し |
| 85 | + afterSecondTimeDoFilter(request, response, chain, storedMdc); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private void firstTimeDoFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 90 | + throws IOException, ServletException { |
| 91 | + // request scope に mdc の値をコピー |
| 92 | + request.setAttribute(STORED_MDC_KEY, MDC.getCopyOfContextMap()); |
| 93 | + chain.doFilter(request, response); |
| 94 | + } |
| 95 | + |
| 96 | + private void afterSecondTimeDoFilter(ServletRequest request, ServletResponse response, FilterChain chain, |
| 97 | + Map<String, String> storedMdc) throws IOException, ServletException { |
| 98 | + // request scope の mdc を MDC に展開 |
| 99 | + MDC.setContextMap(storedMdc); |
| 100 | + try { |
| 101 | + chain.doFilter(request, response); |
| 102 | + } finally { |
| 103 | + MDC.clear(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments