001
014
015 package com.liferay.portal.servlet.filters.virtualhost;
016
017 import com.liferay.portal.LayoutFriendlyURLException;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.struts.LastPath;
021 import com.liferay.portal.kernel.util.CharPool;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.LayoutSet;
028 import com.liferay.portal.model.impl.LayoutImpl;
029 import com.liferay.portal.service.GroupLocalServiceUtil;
030 import com.liferay.portal.servlet.I18nServlet;
031 import com.liferay.portal.servlet.filters.BasePortalFilter;
032 import com.liferay.portal.util.Portal;
033 import com.liferay.portal.util.PortalInstances;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portal.util.PropsValues;
036 import com.liferay.portal.util.WebKeys;
037 import com.liferay.portal.webserver.WebServerServlet;
038
039 import java.util.Set;
040
041 import javax.servlet.FilterChain;
042 import javax.servlet.FilterConfig;
043 import javax.servlet.RequestDispatcher;
044 import javax.servlet.ServletContext;
045 import javax.servlet.http.HttpServletRequest;
046 import javax.servlet.http.HttpServletResponse;
047
048
058 public class VirtualHostFilter extends BasePortalFilter {
059
060 @Override
061 public void init(FilterConfig filterConfig) {
062 super.init(filterConfig);
063
064 _servletContext = filterConfig.getServletContext();
065 }
066
067 @Override
068 public boolean isFilterEnabled(
069 HttpServletRequest request, HttpServletResponse response) {
070
071 StringBuffer requestURL = request.getRequestURL();
072
073 if (isValidRequestURL(requestURL)) {
074 return true;
075 }
076 else {
077 return false;
078 }
079 }
080
081 protected boolean isValidFriendlyURL(String friendlyURL) {
082 friendlyURL = friendlyURL.toLowerCase();
083
084 if (PortalInstances.isVirtualHostsIgnorePath(friendlyURL) ||
085 friendlyURL.startsWith(_PATH_MODULE_SLASH) ||
086 friendlyURL.startsWith(_PRIVATE_GROUP_SERVLET_MAPPING_SLASH) ||
087 friendlyURL.startsWith(_PRIVATE_USER_SERVLET_MAPPING_SLASH) ||
088 friendlyURL.startsWith(_PUBLIC_GROUP_SERVLET_MAPPING_SLASH)) {
089
090 return false;
091 }
092
093 if (LayoutImpl.hasFriendlyURLKeyword(friendlyURL)) {
094 return false;
095 }
096
097 int code = LayoutImpl.validateFriendlyURL(friendlyURL, false);
098
099 if ((code > -1) &&
100 (code != LayoutFriendlyURLException.ENDS_WITH_SLASH)) {
101
102 return false;
103 }
104
105 return true;
106 }
107
108 protected boolean isValidRequestURL(StringBuffer requestURL) {
109 if (requestURL == null) {
110 return false;
111 }
112
113 String url = requestURL.toString();
114
115 for (String extension : PropsValues.VIRTUAL_HOSTS_IGNORE_EXTENSIONS) {
116 if (url.endsWith(extension)) {
117 return false;
118 }
119 }
120
121 return true;
122 }
123
124 @Override
125 protected void processFilter(
126 HttpServletRequest request, HttpServletResponse response,
127 FilterChain filterChain)
128 throws Exception {
129
130 long companyId = PortalInstances.getCompanyId(request);
131
132 String contextPath = PortalUtil.getPathContext();
133
134 String originalFriendlyURL = request.getRequestURI();
135
136 String friendlyURL = originalFriendlyURL;
137
138 if (Validator.isNotNull(contextPath) &&
139 friendlyURL.contains(contextPath)) {
140
141 friendlyURL = friendlyURL.substring(contextPath.length());
142 }
143
144 int pos = friendlyURL.indexOf(StringPool.SEMICOLON);
145
146 if (pos != -1) {
147 friendlyURL = friendlyURL.substring(0, pos);
148 }
149
150 friendlyURL = StringUtil.replace(
151 friendlyURL, StringPool.DOUBLE_SLASH, StringPool.SLASH);
152
153 String i18nLanguageId = null;
154
155 Set<String> languageIds = I18nServlet.getLanguageIds();
156
157 for (String languageId : languageIds) {
158 if (StringUtil.startsWith(friendlyURL, languageId)) {
159 pos = friendlyURL.indexOf(CharPool.SLASH, 1);
160
161 if (((pos != -1) && (pos != languageId.length())) ||
162 ((pos == -1) &&
163 !friendlyURL.equalsIgnoreCase(languageId))) {
164
165 continue;
166 }
167
168 if (pos == -1) {
169 i18nLanguageId = languageId;
170 friendlyURL = StringPool.SLASH;
171 }
172 else {
173 i18nLanguageId = languageId.substring(0, pos);
174 friendlyURL = friendlyURL.substring(pos);
175 }
176
177 break;
178 }
179 }
180
181 friendlyURL = StringUtil.replace(
182 friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING, StringPool.BLANK);
183
184 if (_log.isDebugEnabled()) {
185 _log.debug("Friendly URL " + friendlyURL);
186 }
187
188 if (!friendlyURL.equals(StringPool.SLASH) &&
189 !isValidFriendlyURL(friendlyURL)) {
190
191 _log.debug("Friendly URL is not valid");
192
193 processFilter(
194 VirtualHostFilter.class, request, response, filterChain);
195
196 return;
197 }
198 else if (friendlyURL.startsWith(_PATH_DOCUMENTS)) {
199 if (WebServerServlet.hasFiles(request)) {
200 processFilter(
201 VirtualHostFilter.class, request, response, filterChain);
202
203 return;
204 }
205 }
206
207 LayoutSet layoutSet = (LayoutSet)request.getAttribute(
208 WebKeys.VIRTUAL_HOST_LAYOUT_SET);
209
210 if (_log.isDebugEnabled()) {
211 _log.debug("Layout set " + layoutSet);
212 }
213
214 if (layoutSet == null) {
215 processFilter(
216 VirtualHostFilter.class, request, response, filterChain);
217
218 return;
219 }
220
221 try {
222 LastPath lastPath = new LastPath(
223 contextPath, friendlyURL, request.getParameterMap());
224
225 request.setAttribute(WebKeys.LAST_PATH, lastPath);
226
227 StringBundler forwardURL = new StringBundler(5);
228
229 if (i18nLanguageId != null) {
230 forwardURL.append(i18nLanguageId);
231 }
232
233 if (originalFriendlyURL.startsWith(
234 PropsValues.WIDGET_SERVLET_MAPPING)) {
235
236 forwardURL.append(PropsValues.WIDGET_SERVLET_MAPPING);
237
238 friendlyURL = StringUtil.replaceFirst(
239 friendlyURL, PropsValues.WIDGET_SERVLET_MAPPING,
240 StringPool.BLANK);
241 }
242
243 long plid = PortalUtil.getPlidFromFriendlyURL(
244 companyId, friendlyURL);
245
246 if (plid <= 0) {
247 Group group = GroupLocalServiceUtil.getGroup(
248 layoutSet.getGroupId());
249
250 if (group.isGuest() && friendlyURL.equals(StringPool.SLASH) &&
251 !layoutSet.isPrivateLayout()) {
252
253 String homeURL = PortalUtil.getRelativeHomeURL(request);
254
255 if (Validator.isNotNull(homeURL)) {
256 friendlyURL = homeURL;
257 }
258 }
259 else {
260 if (layoutSet.isPrivateLayout()) {
261 if (group.isUser()) {
262 forwardURL.append(_PRIVATE_USER_SERVLET_MAPPING);
263 }
264 else {
265 forwardURL.append(_PRIVATE_GROUP_SERVLET_MAPPING);
266 }
267 }
268 else {
269 forwardURL.append(_PUBLIC_GROUP_SERVLET_MAPPING);
270 }
271
272 forwardURL.append(group.getFriendlyURL());
273 }
274 }
275
276 forwardURL.append(friendlyURL);
277
278 if (_log.isDebugEnabled()) {
279 _log.debug("Forward to " + forwardURL);
280 }
281
282 RequestDispatcher requestDispatcher =
283 _servletContext.getRequestDispatcher(forwardURL.toString());
284
285 requestDispatcher.forward(request, response);
286 }
287 catch (Exception e) {
288 _log.error(e, e);
289
290 processFilter(
291 VirtualHostFilter.class, request, response, filterChain);
292 }
293 }
294
295 private static final String _PATH_DOCUMENTS = "/documents/";
296
297 private static final String _PATH_MODULE_SLASH =
298 Portal.PATH_MODULE + StringPool.SLASH;
299
300 private static final String _PRIVATE_GROUP_SERVLET_MAPPING =
301 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
302
303 private static final String _PRIVATE_GROUP_SERVLET_MAPPING_SLASH =
304 _PRIVATE_GROUP_SERVLET_MAPPING + StringPool.SLASH;
305
306 private static final String _PRIVATE_USER_SERVLET_MAPPING =
307 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
308
309 private static final String _PRIVATE_USER_SERVLET_MAPPING_SLASH =
310 _PRIVATE_USER_SERVLET_MAPPING + StringPool.SLASH;
311
312 private static final String _PUBLIC_GROUP_SERVLET_MAPPING =
313 PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
314
315 private static final String _PUBLIC_GROUP_SERVLET_MAPPING_SLASH =
316 _PUBLIC_GROUP_SERVLET_MAPPING + StringPool.SLASH;
317
318 private static Log _log = LogFactoryUtil.getLog(VirtualHostFilter.class);
319
320 private ServletContext _servletContext;
321
322 }