View Javadoc

1   /*
2    * Copyright 2004-2006 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.tuigwaa.util.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  
28  import org.apache.struts.action.ActionServlet;
29  import org.apache.struts.util.ModuleUtils;
30  import org.seasar.framework.container.S2Container;
31  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
32  import org.seasar.tuigwaa.system.Constants;
33  import org.seasar.tuigwaa.system.SiteConfig;
34  import org.seasar.tuigwaa.system.SiteService;
35  import org.seasar.tuigwaa.util.TgwContext;
36  import org.seasar.tuigwaa.util.TgwUtils;
37  
38  public class TgwFilter implements Filter {
39  
40  	public void init(FilterConfig arg0) throws ServletException {
41  	}
42  
43  	public void doFilter(ServletRequest request, ServletResponse response,
44  			FilterChain chain) throws IOException, ServletException {
45  		S2Container container = SingletonS2ContainerFactory.getContainer();
46  
47  		// Set SiteConfig
48  		ActionServlet servlet = (ActionServlet) TgwUtils
49  				.getTuigwaaActionServlet();
50  
51  		String siteName = null;
52  		String prefix = ModuleUtils.getInstance().getModuleName(
53  				(HttpServletRequest) request, servlet.getServletContext());
54  		if (prefix != null && prefix.length() > 0)
55  			siteName = prefix.substring(1);
56  
57  		SiteService siteService = (SiteService) container
58  				.getComponent(SiteService.class);
59  		SiteConfig config = siteService.getSiteConfig(siteName);
60  		request.setAttribute(Constants.RATTR_SITECONFIG, config);
61  
62  		// bind page name
63  		// String pageName = request.getParameter(Constants.PARAM_PAGENAME);
64  		String pageName = TgwContext.getPageName();
65  		if (pageName == null || pageName.equals("")) {
66  			pageName = request.getParameter(Constants.PARAM_PAGENAME);
67  			if (pageName != null && !pageName.equals("")) {
68  				TgwContext.bindPageName(pageName);
69  			}
70  		}
71  
72  		// bind entity name
73  		String entityName = request.getParameter(Constants.PARAM_ENTITY_NAME);
74  		TgwContext.bindEntityName(entityName);
75  
76  		// bind pageSettings
77  		TgwContext.bindPageSettings();
78  
79  		// bind contextPath for jsp files
80  		TgwContext.bindContextPath();
81  
82  		chain.doFilter(request, response);
83  	}
84  
85  	public void destroy() {
86  	}
87  }