1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.NoSuchLayoutException;
27  import com.liferay.portal.NoSuchUserException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.Group;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.service.GroupLocalServiceUtil;
36  import com.liferay.portal.service.UserLocalServiceUtil;
37  import com.liferay.portal.struts.LastPath;
38  import com.liferay.portal.util.Portal;
39  import com.liferay.portal.util.PortalInstances;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.WebKeys;
42  
43  import java.io.IOException;
44  
45  import java.util.Map;
46  
47  import javax.servlet.RequestDispatcher;
48  import javax.servlet.ServletConfig;
49  import javax.servlet.ServletContext;
50  import javax.servlet.ServletException;
51  import javax.servlet.http.HttpServlet;
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  /**
56   * <a href="FriendlyURLServlet.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Jorge Ferrer
60   *
61   */
62  public class FriendlyURLServlet extends HttpServlet {
63  
64      public void init(ServletConfig servletConfig) throws ServletException {
65          super.init(servletConfig);
66  
67          _private = GetterUtil.getBoolean(
68              servletConfig.getInitParameter("private"));
69          _user = GetterUtil.getBoolean(
70              servletConfig.getInitParameter("user"));
71      }
72  
73      public void service(
74              HttpServletRequest request, HttpServletResponse response)
75          throws IOException, ServletException {
76  
77          ServletContext servletContext = getServletContext();
78  
79          // Do not set the entire full main path. See LEP-456.
80  
81          //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
82          String mainPath = Portal.PATH_MAIN;
83  
84          String friendlyURLPath = null;
85  
86          if (_private) {
87              if (_user) {
88                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
89              }
90              else {
91                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
92              }
93          }
94          else {
95              friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
96          }
97  
98          request.setAttribute(
99              WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
100 
101         String redirect = mainPath;
102 
103         try {
104             redirect = getRedirect(
105                 request, request.getPathInfo(), mainPath,
106                 request.getParameterMap());
107 
108             if (request.getAttribute(WebKeys.LAST_PATH) == null) {
109                 LastPath lastPath = new LastPath(
110                     friendlyURLPath, request.getPathInfo(),
111                     request.getParameterMap());
112 
113                 request.setAttribute(WebKeys.LAST_PATH, lastPath);
114             }
115         }
116         catch (NoSuchLayoutException nsle) {
117             _log.warn(nsle);
118 
119             PortalUtil.sendError(
120                 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
121 
122             return;
123         }
124         catch (Exception e) {
125             if (_log.isWarnEnabled()) {
126                 _log.warn(e);
127             }
128         }
129 
130         if (Validator.isNull(redirect)) {
131             redirect = mainPath;
132         }
133 
134         if (_log.isDebugEnabled()) {
135             _log.debug("Redirect " + redirect);
136         }
137 
138         if (redirect.startsWith(StringPool.SLASH)) {
139             RequestDispatcher requestDispatcher =
140                 servletContext.getRequestDispatcher(redirect);
141 
142             if (requestDispatcher != null) {
143                 requestDispatcher.forward(request, response);
144             }
145         }
146         else {
147             response.sendRedirect(redirect);
148         }
149     }
150 
151     protected String getRedirect(
152             HttpServletRequest request, String path, String mainPath,
153             Map<String, String[]> params)
154         throws Exception {
155 
156         if (Validator.isNull(path)) {
157             return mainPath;
158         }
159 
160         if (!path.startsWith(StringPool.SLASH)) {
161             return mainPath;
162         }
163 
164         // Group friendly URL
165 
166         String friendlyURL = null;
167 
168         int pos = path.indexOf(StringPool.SLASH, 1);
169 
170         if (pos != -1) {
171             friendlyURL = path.substring(0, pos);
172         }
173         else {
174             if (path.length() > 1) {
175                 friendlyURL = path.substring(0, path.length());
176             }
177         }
178 
179         if (Validator.isNull(friendlyURL)) {
180             return mainPath;
181         }
182 
183         long companyId = PortalInstances.getCompanyId(request);
184 
185         Group group = null;
186 
187         try {
188             group = GroupLocalServiceUtil.getFriendlyURLGroup(
189                 companyId, friendlyURL);
190         }
191         catch (NoSuchGroupException nsge) {
192         }
193 
194         if (group == null) {
195             String screenName = friendlyURL.substring(1);
196 
197             if (_user || !Validator.isNumber(screenName)) {
198                 try {
199                     User user = UserLocalServiceUtil.getUserByScreenName(
200                         companyId, screenName);
201 
202                     group = user.getGroup();
203                 }
204                 catch (NoSuchUserException nsue) {
205                     if (_log.isWarnEnabled()) {
206                         _log.warn(
207                             "No user exists with friendly URL " + screenName);
208                     }
209                 }
210             }
211             else {
212                 long groupId = GetterUtil.getLong(screenName);
213 
214                 try {
215                     group = GroupLocalServiceUtil.getGroup(groupId);
216                 }
217                 catch (NoSuchGroupException nsge) {
218                     if (_log.isDebugEnabled()) {
219                         _log.debug(
220                             "No group exists with friendly URL " + groupId +
221                                 ". Try fetching by screen name instead.");
222                     }
223 
224                     try {
225                         User user = UserLocalServiceUtil.getUserByScreenName(
226                             companyId, screenName);
227 
228                         group = user.getGroup();
229                     }
230                     catch (NoSuchUserException nsue) {
231                         if (_log.isWarnEnabled()) {
232                             _log.warn(
233                                 "No user or group exists with friendly URL " +
234                                     groupId);
235                         }
236                     }
237                 }
238             }
239         }
240 
241         if (group == null) {
242             return mainPath;
243         }
244 
245         // Layout friendly URL
246 
247         friendlyURL = null;
248 
249         if ((pos != -1) && ((pos + 1) != path.length())) {
250             friendlyURL = path.substring(pos, path.length());
251         }
252 
253         return PortalUtil.getLayoutActualURL(
254             group.getGroupId(), _private, mainPath, friendlyURL, params);
255     }
256 
257     private static Log _log = LogFactoryUtil.getLog(FriendlyURLServlet.class);
258 
259     private boolean _private;
260     private boolean _user;
261 
262 }