1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet;
16  
17  import com.liferay.portal.NoSuchGroupException;
18  import com.liferay.portal.NoSuchLayoutException;
19  import com.liferay.portal.NoSuchUserException;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portal.model.User;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.UserLocalServiceUtil;
29  import com.liferay.portal.struts.LastPath;
30  import com.liferay.portal.util.Portal;
31  import com.liferay.portal.util.PortalInstances;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  
35  import java.io.IOException;
36  
37  import java.util.Map;
38  
39  import javax.servlet.RequestDispatcher;
40  import javax.servlet.ServletConfig;
41  import javax.servlet.ServletContext;
42  import javax.servlet.ServletException;
43  import javax.servlet.http.HttpServlet;
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  /**
48   * <a href="FriendlyURLServlet.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Jorge Ferrer
52   */
53  public class FriendlyURLServlet extends HttpServlet {
54  
55      public void init(ServletConfig servletConfig) throws ServletException {
56          super.init(servletConfig);
57  
58          _private = GetterUtil.getBoolean(
59              servletConfig.getInitParameter("private"));
60          _user = GetterUtil.getBoolean(
61              servletConfig.getInitParameter("user"));
62      }
63  
64      public void service(
65              HttpServletRequest request, HttpServletResponse response)
66          throws IOException, ServletException {
67  
68          ServletContext servletContext = getServletContext();
69  
70          // Do not set the entire full main path. See LEP-456.
71  
72          //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
73          String mainPath = Portal.PATH_MAIN;
74  
75          String friendlyURLPath = null;
76  
77          if (_private) {
78              if (_user) {
79                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
80              }
81              else {
82                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
83              }
84          }
85          else {
86              friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
87          }
88  
89          request.setAttribute(
90              WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
91  
92          String redirect = mainPath;
93  
94          try {
95              redirect = getRedirect(
96                  request, request.getPathInfo(), mainPath,
97                  request.getParameterMap());
98  
99              if (request.getAttribute(WebKeys.LAST_PATH) == null) {
100                 LastPath lastPath = new LastPath(
101                     friendlyURLPath, request.getPathInfo(),
102                     request.getParameterMap());
103 
104                 request.setAttribute(WebKeys.LAST_PATH, lastPath);
105             }
106         }
107         catch (NoSuchLayoutException nsle) {
108             _log.warn(nsle);
109 
110             PortalUtil.sendError(
111                 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
112 
113             return;
114         }
115         catch (Exception e) {
116             if (_log.isWarnEnabled()) {
117                 _log.warn(e);
118             }
119         }
120 
121         if (Validator.isNull(redirect)) {
122             redirect = mainPath;
123         }
124 
125         if (_log.isDebugEnabled()) {
126             _log.debug("Redirect " + redirect);
127         }
128 
129         if (redirect.startsWith(StringPool.SLASH)) {
130             RequestDispatcher requestDispatcher =
131                 servletContext.getRequestDispatcher(redirect);
132 
133             if (requestDispatcher != null) {
134                 requestDispatcher.forward(request, response);
135             }
136         }
137         else {
138             response.sendRedirect(redirect);
139         }
140     }
141 
142     protected String getRedirect(
143             HttpServletRequest request, String path, String mainPath,
144             Map<String, String[]> params)
145         throws Exception {
146 
147         if (Validator.isNull(path)) {
148             return mainPath;
149         }
150 
151         if (!path.startsWith(StringPool.SLASH)) {
152             return mainPath;
153         }
154 
155         // Group friendly URL
156 
157         String friendlyURL = null;
158 
159         int pos = path.indexOf(StringPool.SLASH, 1);
160 
161         if (pos != -1) {
162             friendlyURL = path.substring(0, pos);
163         }
164         else {
165             if (path.length() > 1) {
166                 friendlyURL = path.substring(0, path.length());
167             }
168         }
169 
170         if (Validator.isNull(friendlyURL)) {
171             return mainPath;
172         }
173 
174         long companyId = PortalInstances.getCompanyId(request);
175 
176         Group group = null;
177 
178         try {
179             group = GroupLocalServiceUtil.getFriendlyURLGroup(
180                 companyId, friendlyURL);
181         }
182         catch (NoSuchGroupException nsge) {
183         }
184 
185         if (group == null) {
186             String screenName = friendlyURL.substring(1);
187 
188             if (_user || !Validator.isNumber(screenName)) {
189                 try {
190                     User user = UserLocalServiceUtil.getUserByScreenName(
191                         companyId, screenName);
192 
193                     group = user.getGroup();
194                 }
195                 catch (NoSuchUserException nsue) {
196                     if (_log.isWarnEnabled()) {
197                         _log.warn(
198                             "No user exists with friendly URL " + screenName);
199                     }
200                 }
201             }
202             else {
203                 long groupId = GetterUtil.getLong(screenName);
204 
205                 try {
206                     group = GroupLocalServiceUtil.getGroup(groupId);
207                 }
208                 catch (NoSuchGroupException nsge) {
209                     if (_log.isDebugEnabled()) {
210                         _log.debug(
211                             "No group exists with friendly URL " + groupId +
212                                 ". Try fetching by screen name instead.");
213                     }
214 
215                     try {
216                         User user = UserLocalServiceUtil.getUserByScreenName(
217                             companyId, screenName);
218 
219                         group = user.getGroup();
220                     }
221                     catch (NoSuchUserException nsue) {
222                         if (_log.isWarnEnabled()) {
223                             _log.warn(
224                                 "No user or group exists with friendly URL " +
225                                     groupId);
226                         }
227                     }
228                 }
229             }
230         }
231 
232         if (group == null) {
233             return mainPath;
234         }
235 
236         // Layout friendly URL
237 
238         friendlyURL = null;
239 
240         if ((pos != -1) && ((pos + 1) != path.length())) {
241             friendlyURL = path.substring(pos, path.length());
242         }
243 
244         return PortalUtil.getLayoutActualURL(
245             group.getGroupId(), _private, mainPath, friendlyURL, params);
246     }
247 
248     private static Log _log = LogFactoryUtil.getLog(FriendlyURLServlet.class);
249 
250     private boolean _private;
251     private boolean _user;
252 
253 }