1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.util.filter;
17
18 import java.io.IOException;
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLDecoder;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import javax.servlet.Filter;
25 import javax.servlet.FilterChain;
26 import javax.servlet.FilterConfig;
27 import javax.servlet.RequestDispatcher;
28 import javax.servlet.ServletException;
29 import javax.servlet.ServletRequest;
30 import javax.servlet.ServletResponse;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33
34 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
35 import org.seasar.tuigwaa.system.Constants;
36 import org.seasar.tuigwaa.system.SiteService;
37 import org.seasar.tuigwaa.util.TgwContext;
38
39 import com.isenshi.util.CharUtil;
40 import com.isenshi.util.PathUtils;
41
42 /***
43 * @author nishioka
44 */
45 public class ForwardFilter implements Filter {
46
47 private SiteService sites;
48
49 private static String PARAM_IGNORE_PATTERN = "ignoreExtensions";
50
51
52
53
54
55 private Set ignoreExtensions = new HashSet();
56
57 public void init(FilterConfig cfg) throws ServletException {
58 String extensionValue = cfg.getInitParameter(PARAM_IGNORE_PATTERN);
59 String[] extensions = extensionValue.split(",");
60 for (int i = 0; i < extensions.length; i++) {
61 ignoreExtensions.add(extensions[i]);
62 }
63 }
64
65 public void doFilter(ServletRequest req, ServletResponse res,
66 FilterChain chain) throws IOException, ServletException {
67
68 if (sites == null) {
69 sites = (SiteService) SingletonS2ContainerFactory.getContainer()
70 .getComponent(SiteService.class);
71 }
72
73 HttpServletRequest hrequest = (HttpServletRequest) req;
74 HttpServletResponse hresponse = (HttpServletResponse) res;
75
76 String servletPath = hrequest.getServletPath();
77
78 String contextPath = hrequest.getContextPath();
79 String pathInfo = hrequest.getPathInfo();
80 String uri = hrequest.getRequestURI();
81
82
83
84
85
86 TuigwaaPath tgwPath = new TuigwaaPath(uri, contextPath);
87
88 if (needForward(tgwPath)) {
89 String dispatchPath = tgwPath.getDispatchPath();
90 RequestDispatcher rd = req.getRequestDispatcher(dispatchPath);
91 String decodedPagePath = tgwPath.getDecodedPagePath();
92
93 TgwContext.bindPageName(decodedPagePath);
94 rd.forward(req, res);
95 } else if (isWebDavTop(servletPath, pathInfo)) {
96 hresponse.sendError(HttpServletResponse.SC_FORBIDDEN, "");
97 } else {
98 chain.doFilter(req, res);
99 }
100 }
101
102 public void destroy() {
103 }
104
105
106
107 private boolean needForward(TuigwaaPath info) {
108 if(info.isSystemSite()){
109 return false;
110 }
111 String extension = info.getExtension();
112 if (extension != null && ignoreExtensions.contains(extension)) {
113 return false;
114 }
115 return true;
116 }
117
118 private boolean isWebDavTop(String servletPath, String pathInfo) {
119 servletPath = CharUtil.chartrim(servletPath, '/');
120 boolean isWebDav = servletPath != null
121 && servletPath.equals(Constants.WEBDAV_URL_PREFIX);
122
123 pathInfo = CharUtil.charpop(pathInfo, '/');
124 boolean isTop = pathInfo == null || pathInfo.length() == 0
125 || pathInfo.equals("/");
126
127 return isWebDav && isTop;
128 }
129
130 class TuigwaaPath {
131
132 private String siteName;
133
134 private String pagePath;
135
136 private String extension;
137
138 TuigwaaPath(String uri, String context) {
139 String servletPath = uri;
140 if (context != null) {
141 servletPath = servletPath.substring(context.length());
142 }
143 siteName = CharUtil.charpop(servletPath, '/');
144 siteName = CharUtil.chartrim(siteName, '/');
145 int index = siteName.indexOf("/");
146 if (index > 0) {
147 String tmpSiteName = siteName.substring(0, index);
148 pagePath = siteName.substring(1 + index);
149 siteName = tmpSiteName;
150 extension = PathUtils.getExtension(pagePath);
151 }
152 }
153
154 public boolean isSystemSite() {
155 if (siteName == null || siteName.equals("")
156 || siteName.indexOf('-') > 0
157 || sites.getSiteConfig(siteName) == null) {
158 return true;
159 }
160 return false;
161 }
162
163 public String getDecodedPagePath() {
164 if(pagePath == null){
165 return null;
166 }
167 try {
168 return URLDecoder.decode(pagePath, "UTF-8");
169 } catch (UnsupportedEncodingException e) {
170
171 e.printStackTrace();
172 return null;
173 }
174 }
175
176 public String getSiteName() {
177 return siteName;
178 }
179
180 public String getDispatchPath() {
181 String dispatchPath = "/" + siteName + "/top.do";
182
183
184
185 return dispatchPath;
186 }
187
188 public String getExtension() {
189 return extension;
190 }
191 }
192 }