Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CubbyFilter |
|
| 3.3333333333333335;3.333 |
1 | /* | |
2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
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, | |
13 | * either express or implied. See the License for the specific language | |
14 | * governing permissions and limitations under the License. | |
15 | */ | |
16 | package org.seasar.cubby.filter; | |
17 | ||
18 | import java.io.IOException; | |
19 | ||
20 | import javax.servlet.Filter; | |
21 | import javax.servlet.FilterChain; | |
22 | import javax.servlet.FilterConfig; | |
23 | import javax.servlet.ServletException; | |
24 | import javax.servlet.ServletRequest; | |
25 | import javax.servlet.ServletResponse; | |
26 | import javax.servlet.http.HttpServletRequest; | |
27 | import javax.servlet.http.HttpServletResponse; | |
28 | ||
29 | import org.seasar.cubby.controller.ActionProcessor; | |
30 | import org.seasar.cubby.controller.ActionResultWrapper; | |
31 | import org.seasar.cubby.controller.ThreadContext; | |
32 | import org.seasar.framework.container.S2Container; | |
33 | import org.seasar.framework.container.factory.SingletonS2ContainerFactory; | |
34 | import org.seasar.framework.log.Logger; | |
35 | ||
36 | /** | |
37 | * Cubby用のフィルター。 | |
38 | * <p> | |
39 | * リクエストの処理を{@link ActionProcessor}に委譲します。 | |
40 | * </p> | |
41 | * | |
42 | * @author agata | |
43 | * @author baba | |
44 | * @since 1.0.0 | |
45 | */ | |
46 | 0 | public class CubbyFilter implements Filter { |
47 | ||
48 | /** ロガー。 */ | |
49 | 0 | private static final Logger logger = Logger.getLogger(CubbyFilter.class); |
50 | ||
51 | /** | |
52 | * {@inheritDoc} | |
53 | */ | |
54 | public void init(final FilterConfig config) throws ServletException { | |
55 | 0 | } |
56 | ||
57 | /** | |
58 | * {@inheritDoc} | |
59 | */ | |
60 | public void destroy() { | |
61 | 0 | } |
62 | ||
63 | /** | |
64 | * フィルター処理。 | |
65 | * <p> | |
66 | * リクエストの処理を{@link SingletonS2ContainerFactory#getContainer() コンテナ}から取得した{@link ActionProcessor}に委譲します。 | |
67 | * </p> | |
68 | * | |
69 | * @param req | |
70 | * リクエスト | |
71 | * @param res | |
72 | * レスポンス | |
73 | * @param chain | |
74 | * フィルタチェイン | |
75 | * @throws IOException | |
76 | * リクエストディスパッチャやフィルタチェインで例外が発生した場合 | |
77 | * @throws ServletException | |
78 | * リクエストディスパッチャやフィルタチェインで例外が発生した場合 | |
79 | */ | |
80 | public void doFilter(final ServletRequest req, final ServletResponse res, | |
81 | final FilterChain chain) throws IOException, ServletException { | |
82 | 0 | final HttpServletRequest request = new CubbyHttpServletRequestWrapper( |
83 | (HttpServletRequest) req); | |
84 | 0 | final HttpServletResponse response = (HttpServletResponse) res; |
85 | 0 | ThreadContext.setRequest(request); |
86 | try { | |
87 | 0 | final S2Container container = SingletonS2ContainerFactory |
88 | .getContainer(); | |
89 | 0 | final ActionProcessor processor = (ActionProcessor) container |
90 | .getComponent(ActionProcessor.class); | |
91 | 0 | final ActionResultWrapper actionResultWrapper = processor.process( |
92 | request, response); | |
93 | 0 | if (actionResultWrapper != null) { |
94 | 0 | actionResultWrapper.execute(request, response); |
95 | } else { | |
96 | 0 | chain.doFilter(request, response); |
97 | } | |
98 | 0 | } catch (final Exception e) { |
99 | 0 | if (e instanceof IOException) { |
100 | 0 | throw (IOException) e; |
101 | 0 | } else if (e instanceof ServletException) { |
102 | 0 | throw (ServletException) e; |
103 | } else { | |
104 | 0 | logger.log(e); |
105 | 0 | throw new ServletException(e); |
106 | } | |
107 | } finally { | |
108 | 0 | ThreadContext.remove(); |
109 | 0 | } |
110 | 0 | } |
111 | ||
112 | } |