1   /**
2    * Copyright (c) 2000-2008 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.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.service.GroupLocalServiceUtil;
34  import com.liferay.portal.service.UserLocalServiceUtil;
35  import com.liferay.portal.struts.LastPath;
36  import com.liferay.portal.util.PortalImpl;
37  import com.liferay.portal.util.PortalInstances;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.WebKeys;
40  
41  import java.io.IOException;
42  
43  import java.util.Map;
44  
45  import javax.servlet.RequestDispatcher;
46  import javax.servlet.ServletConfig;
47  import javax.servlet.ServletContext;
48  import javax.servlet.ServletException;
49  import javax.servlet.http.HttpServlet;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.apache.commons.logging.Log;
54  import org.apache.commons.logging.LogFactory;
55  
56  /**
57   * <a href="FriendlyURLServlet.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   * @author Jorge Ferrer
61   *
62   */
63  public class FriendlyURLServlet extends HttpServlet {
64  
65      public void init(ServletConfig filterConfig) throws ServletException {
66          super.init(filterConfig);
67  
68          _private = GetterUtil.getBoolean(
69              filterConfig.getInitParameter("private"));
70          _user = GetterUtil.getBoolean(
71              filterConfig.getInitParameter("user"));
72      }
73  
74      public void service(
75              HttpServletRequest request, HttpServletResponse response)
76          throws IOException, ServletException {
77  
78          ServletContext servletContext = getServletContext();
79  
80          // Do not set the entire full main path. See LEP-456.
81  
82          //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
83          String mainPath = PortalImpl.PATH_MAIN;
84  
85          String friendlyURLPath = null;
86  
87          if (_private) {
88              if (_user) {
89                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
90              }
91              else {
92                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
93              }
94          }
95          else {
96              friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
97          }
98  
99          request.setAttribute(
100             WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
101 
102         String redirect = mainPath;
103 
104         try {
105             redirect = getRedirect(
106                 request, request.getPathInfo(), mainPath,
107                 request.getParameterMap());
108 
109             if (request.getAttribute(WebKeys.LAST_PATH) == null) {
110                 LastPath lastPath = new LastPath(
111                     friendlyURLPath, request.getPathInfo(),
112                     request.getParameterMap());
113 
114                 request.setAttribute(WebKeys.LAST_PATH, lastPath);
115             }
116         }
117         catch (NoSuchLayoutException nsle) {
118             _log.warn(nsle);
119 
120             PortalUtil.sendError(
121                 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
122 
123             return;
124         }
125         catch (Exception e) {
126             if (_log.isWarnEnabled()) {
127                 _log.warn(e);
128             }
129         }
130 
131         if (Validator.isNull(redirect)) {
132             redirect = mainPath;
133         }
134 
135         if (_log.isDebugEnabled()) {
136             _log.debug("Redirect " + redirect);
137         }
138 
139         if (redirect.startsWith(StringPool.SLASH)) {
140             RequestDispatcher requestDispatcher =
141                 servletContext.getRequestDispatcher(redirect);
142 
143             if (requestDispatcher != null) {
144                 requestDispatcher.forward(request, response);
145             }
146         }
147         else {
148             response.sendRedirect(redirect);
149         }
150     }
151 
152     protected String getRedirect(
153             HttpServletRequest request, String path, String mainPath,
154             Map<String, String[]> params)
155         throws Exception {
156 
157         if (Validator.isNull(path)) {
158             return mainPath;
159         }
160 
161         if (!path.startsWith(StringPool.SLASH)) {
162             return mainPath;
163         }
164 
165         // Group friendly URL
166 
167         String friendlyURL = null;
168 
169         int pos = path.indexOf(StringPool.SLASH, 1);
170 
171         if (pos != -1) {
172             friendlyURL = path.substring(0, pos);
173         }
174         else {
175             if (path.length() > 1) {
176                 friendlyURL = path.substring(0, path.length());
177             }
178         }
179 
180         if (Validator.isNull(friendlyURL)) {
181             return mainPath;
182         }
183 
184         long companyId = PortalInstances.getCompanyId(request);
185 
186         Group group = null;
187 
188         try {
189             group = GroupLocalServiceUtil.getFriendlyURLGroup(
190                 companyId, friendlyURL);
191         }
192         catch (NoSuchGroupException nsge) {
193         }
194 
195         if (group == null) {
196             String screenName = friendlyURL.substring(1);
197 
198             if (_user || !Validator.isNumber(screenName)) {
199                 try {
200                     User user = UserLocalServiceUtil.getUserByScreenName(
201                         companyId, screenName);
202 
203                     group = user.getGroup();
204                 }
205                 catch (NoSuchUserException nsue) {
206                     if (_log.isWarnEnabled()) {
207                         _log.warn(
208                             "No user exists with friendly URL " + screenName);
209                     }
210                 }
211             }
212             else {
213                 long groupId = GetterUtil.getLong(screenName);
214 
215                 try {
216                     group = GroupLocalServiceUtil.getGroup(groupId);
217                 }
218                 catch (NoSuchGroupException nsge) {
219                     if (_log.isDebugEnabled()) {
220                         _log.debug(
221                             "No group exists with friendly URL " + groupId +
222                                 ". Try fetching by screen name instead.");
223                     }
224 
225                     try {
226                         User user = UserLocalServiceUtil.getUserByScreenName(
227                             companyId, screenName);
228 
229                         group = user.getGroup();
230                     }
231                     catch (NoSuchUserException nsue) {
232                         if (_log.isWarnEnabled()) {
233                             _log.warn(
234                                 "No user or group exists with friendly URL " +
235                                     groupId);
236                         }
237                     }
238                 }
239             }
240         }
241 
242         if (group == null) {
243             return mainPath;
244         }
245 
246         // Layout friendly URL
247 
248         friendlyURL = null;
249 
250         if ((pos != -1) && ((pos + 1) != path.length())) {
251             friendlyURL = path.substring(pos, path.length());
252         }
253 
254         return PortalUtil.getLayoutActualURL(
255             group.getGroupId(), _private, mainPath, friendlyURL, params);
256     }
257 
258     private static Log _log = LogFactory.getLog(FriendlyURLServlet.class);
259 
260     private boolean _private;
261     private boolean _user;
262 
263 }