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