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  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  	//private static final String[] DEFAULT_IGNORE_PATTERNS = {"-",".do",".js",".css",".ico"};
52  	
53  	//private List ignorePatterns = new ArrayList(Arrays.asList(DEFAULT_IGNORE_PATTERNS));
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(); // encoded
77  //		StringBuffer requestedURL = hrequest.getRequestURL();
78  		String contextPath = hrequest.getContextPath();
79  		String pathInfo = hrequest.getPathInfo();
80  		String uri = hrequest.getRequestURI();
81  
82  //		StringBuffer requestedURL = hrequest.getRequestURL();
83  	//	String pathTrans = hrequest.getPathTranslated();
84  //		String query = hrequest.getQueryString();
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  			//TgwContext.bindPageName(
93  			TgwContext.bindPageName(decodedPagePath);
94  			rd.forward(req, res);
95  		} else if (isWebDavTop(servletPath, pathInfo)) {
96  			hresponse.sendError(HttpServletResponse.SC_FORBIDDEN, "");
97  		} else { // usual request handling
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 				// TODO Auto-generated catch block
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 			//if (pagePath != null) {
183 			//dispatchPath += "?" + Constants.PARAM_PAGENAME + "=" + pagePath;
184 			//}
185 			return dispatchPath;
186 		}
187 
188 		public String getExtension() {
189 			return extension;
190 		}
191 	}
192 }