1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.myfaces.orchestra.frameworkAdapter.basic;
21
22 import java.io.IOException;
23
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 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import org.apache.myfaces.orchestra.lib._UrlMatcher;
35
36 /**
37 * Configures the BasicFrameworkAdapter.
38 * <p>
39 * Orchestra accesses information about the request, response, session, etc via a
40 * FrameworkAdapter so that it can be used with multiple web tier frameworks. This
41 * class selects and configures the simplest version of this adapter, which only
42 * depends on the <i>basic</i> Servlet APIs.
43 * <p>
44 * If filter config parameter "conversationMessagerClass" is set, then this is
45 * passed to the BasicFrameworkAdapter, meaning that this can be either a
46 * beanname defined in the dependency-injection framework, or an absolute
47 * classname of a type implementing ConversationMessager.
48 */
49 public class BasicFrameworkAdapterFilter implements Filter
50 {
51 private final static String INIT_CONVERSATION_MESSAGER = "conversationMessagerClass";
52
53 private final Log log = LogFactory.getLog(BasicFrameworkAdapterFilter.class);
54 private BasicFrameworkAdapter adapter;
55 private _UrlMatcher urlMatcher;
56
57 public void init(FilterConfig filterConfig) throws ServletException
58 {
59 String conversationMessager = filterConfig.getInitParameter(INIT_CONVERSATION_MESSAGER);
60
61 adapter = new BasicFrameworkAdapter(filterConfig.getServletContext(), conversationMessager);
62 urlMatcher = new _UrlMatcher(filterConfig);
63 }
64
65 public void doFilter(ServletRequest req, ServletResponse rsp, FilterChain filterChain)
66 throws IOException, ServletException
67 {
68 if (!urlMatcher.accept(req))
69 {
70 filterChain.doFilter(req, rsp);
71 return;
72 }
73
74 if (log.isDebugEnabled())
75 {
76 log.debug("doFilter");
77 }
78 try
79 {
80 adapter.beginRequest(req, rsp);
81 filterChain.doFilter(req, rsp);
82 }
83 finally
84 {
85 adapter.endRequest();
86 }
87 }
88
89 public void destroy()
90 {
91 }
92 }