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.springBasic;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.myfaces.orchestra.frameworkAdapter.basic.BasicFrameworkAdapter;
25 import org.apache.myfaces.orchestra.frameworkAdapter.basic.BasicFrameworkAdapterFilter;
26 import org.apache.myfaces.orchestra.lib._UrlMatcher;
27
28 import javax.servlet.Filter;
29 import javax.servlet.FilterChain;
30 import javax.servlet.FilterConfig;
31 import javax.servlet.ServletException;
32 import javax.servlet.ServletRequest;
33 import javax.servlet.ServletResponse;
34 import java.io.IOException;
35
36 /**
37 * Configures the SpringBasicFrameworkAdapter.
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 a version of this adapter which looks up variables
42 * via Spring.
43 * <p>
44 * If filter config parameter "conversationMessagerClass" is set, then this is
45 * passed to the SpringBasicFrameworkAdapter, 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 * <p>
49 * This filter access the Spring framework to lookup the bean in its
50 * {@link org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter#getBean} method
51 */
52 public class SpringBasicFrameworkAdapterFilter implements Filter
53 {
54 private final static String INIT_CONVERSATION_MESSAGER = "conversationMessagerClass";
55
56 private final Log log = LogFactory.getLog(BasicFrameworkAdapterFilter.class);
57 private BasicFrameworkAdapter adapter;
58 private _UrlMatcher urlMatcher;
59
60 public void init(FilterConfig filterConfig) throws ServletException
61 {
62 String conversationMessager = filterConfig.getInitParameter(INIT_CONVERSATION_MESSAGER);
63
64 adapter = new SpringBasicFrameworkAdapter(filterConfig.getServletContext(), conversationMessager);
65 urlMatcher = new _UrlMatcher(filterConfig);
66 }
67
68 public void doFilter(ServletRequest req, ServletResponse rsp, FilterChain filterChain)
69 throws IOException, ServletException
70 {
71 if (!urlMatcher.accept(req))
72 {
73 filterChain.doFilter(req, rsp);
74 return;
75 }
76
77 log.debug("doFilter");
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 }