1
22
23 package com.liferay.portal.servlet.filters.virtualhost;
24
25 import com.liferay.portal.LayoutFriendlyURLException;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.StringPool;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.model.Group;
31 import com.liferay.portal.model.LayoutSet;
32 import com.liferay.portal.model.impl.LayoutImpl;
33 import com.liferay.portal.service.GroupLocalServiceUtil;
34 import com.liferay.portal.servlet.AbsoluteRedirectsResponse;
35 import com.liferay.portal.servlet.filters.BasePortalFilter;
36 import com.liferay.portal.struts.LastPath;
37 import com.liferay.portal.util.PortalInstances;
38 import com.liferay.portal.util.PortalUtil;
39 import com.liferay.portal.util.WebKeys;
40
41 import java.io.IOException;
42
43 import javax.servlet.FilterChain;
44 import javax.servlet.FilterConfig;
45 import javax.servlet.RequestDispatcher;
46 import javax.servlet.ServletContext;
47 import javax.servlet.ServletException;
48 import javax.servlet.ServletRequest;
49 import javax.servlet.ServletResponse;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52 import javax.servlet.http.HttpSession;
53
54
70 public class VirtualHostFilter extends BasePortalFilter {
71
72 public void init(FilterConfig filterConfig) {
73 super.init(filterConfig);
74
75 _servletContext = filterConfig.getServletContext();
76 }
77
78 public void doFilter(
79 ServletRequest servletRequest, ServletResponse servletResponse,
80 FilterChain filterChain)
81 throws IOException, ServletException {
82
83 if (_log.isDebugEnabled()) {
84 if (isFilterEnabled()) {
85 _log.debug(VirtualHostFilter.class + " is enabled");
86 }
87 else {
88 _log.debug(VirtualHostFilter.class + " is disabled");
89 }
90 }
91
92 HttpServletRequest request = (HttpServletRequest)servletRequest;
93 HttpServletResponse response = (HttpServletResponse)servletResponse;
94
95 request.setCharacterEncoding(StringPool.UTF8);
96
98
100 response = new AbsoluteRedirectsResponse(request, response);
101
102
105 long companyId = PortalInstances.getCompanyId(request);
106
107 if (_log.isDebugEnabled()) {
108 _log.debug("Company id " + companyId);
109 }
110
111 PortalUtil.getCurrentURL(request);
112
113 HttpSession session = request.getSession();
114
115 Boolean httpsInitial = (Boolean)session.getAttribute(
116 WebKeys.HTTPS_INITIAL);
117
118 if (httpsInitial == null) {
119 httpsInitial = Boolean.valueOf(request.isSecure());
120
121 session.setAttribute(WebKeys.HTTPS_INITIAL, httpsInitial);
122
123 if (_log.isDebugEnabled()) {
124 _log.debug("Setting httpsInitial to " + httpsInitial);
125 }
126 }
127
128 if (!isFilterEnabled()) {
129 processFilter(
130 VirtualHostFilter.class, request, response, filterChain);
131
132 return;
133 }
134
135 StringBuffer requestURL = request.getRequestURL();
136
137 if (_log.isDebugEnabled()) {
138 _log.debug("Received " + requestURL);
139 }
140
141 if (!isValidRequestURL(requestURL)) {
142 processFilter(
143 VirtualHostFilter.class, request, response, filterChain);
144
145 return;
146 }
147
148 String contextPath = PortalUtil.getPathContext();
149
150 String friendlyURL = request.getRequestURI();
151
152 if ((!contextPath.equals(StringPool.SLASH)) &&
153 (friendlyURL.indexOf(contextPath) != -1)) {
154
155 friendlyURL = friendlyURL.substring(
156 contextPath.length(), friendlyURL.length());
157 }
158
159 friendlyURL = StringUtil.replace(
160 friendlyURL, StringPool.DOUBLE_SLASH, StringPool.SLASH);
161
162 if (_log.isDebugEnabled()) {
163 _log.debug("Friendly URL " + friendlyURL);
164 }
165
166 if (!isValidFriendlyURL(friendlyURL)) {
167 processFilter(
168 VirtualHostFilter.class, request, response, filterChain);
169
170 return;
171 }
172
173 LayoutSet layoutSet = (LayoutSet)servletRequest.getAttribute(
174 WebKeys.VIRTUAL_HOST_LAYOUT_SET);
175
176 if (_log.isDebugEnabled()) {
177 _log.debug("Layout set " + layoutSet);
178 }
179
180 if (layoutSet != null) {
181 try {
182 LastPath lastPath = new LastPath(
183 StringPool.BLANK, friendlyURL,
184 servletRequest.getParameterMap());
185
186 servletRequest.setAttribute(WebKeys.LAST_PATH, lastPath);
187
188 StringBuilder prefix = new StringBuilder();
189
190 if (layoutSet.isPrivateLayout()) {
191 prefix.append(PortalUtil.getPathFriendlyURLPrivateGroup());
192 }
193 else {
194 prefix.append(PortalUtil.getPathFriendlyURLPublic());
195 }
196
197 Group group = GroupLocalServiceUtil.getGroup(
198 layoutSet.getGroupId());
199
200 prefix.append(group.getFriendlyURL());
201
202 StringBuilder redirect = new StringBuilder();
203
204 redirect.append(prefix);
205 redirect.append(friendlyURL);
206
207 String query = request.getQueryString();
208
209 if (query != null) {
210 redirect.append(StringPool.QUESTION);
211 redirect.append(query);
212 }
213
214 if (_log.isDebugEnabled()) {
215 _log.debug("Redirect to " + redirect);
216 }
217
218 RequestDispatcher requestDispatcher =
219 _servletContext.getRequestDispatcher(redirect.toString());
220
221 requestDispatcher.forward(servletRequest, response);
222
223 return;
224 }
225 catch (Exception e) {
226 _log.error(e, e);
227 }
228 }
229
230 processFilter(VirtualHostFilter.class, request, response, filterChain);
231 }
232
233 protected boolean isValidFriendlyURL(String friendlyURL) {
234 friendlyURL = friendlyURL.toLowerCase();
235
236 if (PortalInstances.isVirtualHostsIgnorePath(friendlyURL) ||
237 friendlyURL.startsWith(
238 PortalUtil.getPathFriendlyURLPrivateGroup()) ||
239 friendlyURL.startsWith(PortalUtil.getPathFriendlyURLPublic()) ||
240 friendlyURL.startsWith(
241 PortalUtil.getPathFriendlyURLPrivateUser()) ||
242 friendlyURL.startsWith(_PATH_C) ||
243 friendlyURL.startsWith(_PATH_DELEGATE) ||
244 friendlyURL.startsWith(_PATH_HTML) ||
245 friendlyURL.startsWith(_PATH_IMAGE) ||
246 friendlyURL.startsWith(_PATH_LANGUAGE) ||
247 friendlyURL.startsWith(_PATH_SITEMAP_XML) ||
248 friendlyURL.startsWith(_PATH_SOFTWARE_CATALOG) ||
249 friendlyURL.startsWith(_PATH_WAP) ||
250 friendlyURL.startsWith(_PATH_WSRP)) {
251
252 return false;
253 }
254
255 int code = LayoutImpl.validateFriendlyURL(friendlyURL);
256
257 if ((code > -1) &&
258 (code != LayoutFriendlyURLException.ENDS_WITH_SLASH)) {
259
260 return false;
261 }
262
263 return true;
264 }
265
266 protected boolean isValidRequestURL(StringBuffer requestURL) {
267 if (requestURL == null) {
268 return false;
269 }
270
271 String url = requestURL.toString();
272
273 if (url.endsWith(_EXT_C) || url.endsWith(_EXT_CSS) ||
274 url.endsWith(_EXT_GIF) || url.endsWith(_EXT_IMAGE_COMPANY_LOGO) ||
275 url.endsWith(_EXT_ICO) || url.endsWith(_EXT_JS) ||
276 url.endsWith(_EXT_JPEG) || url.endsWith(_EXT_PORTAL_CSS_CACHED) ||
277 url.endsWith(_EXT_PORTAL_JAVASCRIPT_CACHED) ||
278 url.endsWith(_EXT_PORTAL_LAYOUT) ||
279 url.endsWith(_EXT_PORTAL_LOGIN) ||
280 url.endsWith(_EXT_PORTAL_LOGOUT) || url.endsWith(_EXT_PNG)) {
281
282 return false;
283 }
284 else {
285 return true;
286 }
287 }
288
289 protected void processFilter(
290 HttpServletRequest request, HttpServletResponse response,
291 FilterChain filterChain) {
292 }
293
294 private static Log _log = LogFactoryUtil.getLog(VirtualHostFilter.class);
295
296 private static String _EXT_C = "/c";
297
298 private static String _EXT_CSS = ".css";
299
300 private static String _EXT_GIF = ".gif";
301
302 private static String _EXT_IMAGE_COMPANY_LOGO = "/image/company_logo";
303
304 private static String _EXT_ICO = ".ico";
305
306 private static String _EXT_JS = ".js";
307
308 private static String _EXT_JPEG = ".jpeg";
309
310 private static String _EXT_PORTAL_CSS_CACHED = "/portal/css_cached";
311
312 private static String _EXT_PORTAL_JAVASCRIPT_CACHED =
313 "/portal/javascript_cached";
314
315 private static String _EXT_PORTAL_LAYOUT = "/portal/layout";
316
317 private static String _EXT_PORTAL_LOGIN = "/portal/login";
318
319 private static String _EXT_PORTAL_LOGOUT = "/portal/logout";
320
321 private static String _EXT_PNG = ".png";
322
323 private static String _PATH_C = "/c/";
324
325 private static String _PATH_DELEGATE = "/delegate/";
326
327 private static String _PATH_HTML = "/html/";
328
329 private static String _PATH_IMAGE = "/image/";
330
331 private static String _PATH_LANGUAGE = "/language/";
332
333 private static String _PATH_SITEMAP_XML = "/sitemap.xml";
334
335 private static String _PATH_SOFTWARE_CATALOG = "/software_catalog/";
336
337 private static String _PATH_WAP = "/wap/";
338
339 private static String _PATH_WSRP = "/wsrp/";
340
341 private ServletContext _servletContext;
342
343 }