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 !layoutSet.isPrivateLayout()) {
250
251 String homeURL = PortalUtil.getRelativeHomeURL(request);
252
253 if (Validator.isNotNull(homeURL)) {
254 friendlyURL = homeURL;
255 }
256 }
257 else {
258 if (layoutSet.isPrivateLayout()) {
259 if (group.isUser()) {
260 forwardURL.append(_PRIVATE_USER_SERVLET_MAPPING);
261 }
262 else {
263 forwardURL.append(_PRIVATE_GROUP_SERVLET_MAPPING);
264 }
265 }
266 else {
267 forwardURL.append(_PUBLIC_GROUP_SERVLET_MAPPING);
268 }
269
270 forwardURL.append(group.getFriendlyURL());
271 }
272 }
273
274 forwardURL.append(friendlyURL);
275
276 if (_log.isDebugEnabled()) {
277 _log.debug("Forward to " + forwardURL);
278 }
279
280 RequestDispatcher requestDispatcher =
281 _servletContext.getRequestDispatcher(forwardURL.toString());
282
283 requestDispatcher.forward(request, response);
284 }
285 catch (Exception e) {
286 _log.error(e, e);
287
288 processFilter(
289 VirtualHostFilter.class, request, response, filterChain);
290 }
291 }
292
293 private static final String _PATH_DOCUMENTS = "/documents/";
294
295 private static final String _PRIVATE_GROUP_SERVLET_MAPPING =
296 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
297
298 private static final String _PRIVATE_GROUP_SERVLET_MAPPING_SLASH =
299 _PRIVATE_GROUP_SERVLET_MAPPING + StringPool.SLASH;
300
301 private static final String _PRIVATE_USER_SERVLET_MAPPING =
302 PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
303
304 private static final String _PRIVATE_USER_SERVLET_MAPPING_SLASH =
305 _PRIVATE_USER_SERVLET_MAPPING + StringPool.SLASH;
306
307 private static final String _PUBLIC_GROUP_SERVLET_MAPPING =
308 PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;
309
310 private static final String _PUBLIC_GROUP_SERVLET_MAPPING_SLASH =
311 _PUBLIC_GROUP_SERVLET_MAPPING + StringPool.SLASH;
312
313 private static Log _log = LogFactoryUtil.getLog(VirtualHostFilter.class);
314
315 private ServletContext _servletContext;
316
317 }