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