1   /**
2    * Copyright (c) 2000-2007 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.PortalInstances;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PropsUtil;
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 config) throws ServletException {
66          super.init(config);
67  
68          _private = GetterUtil.getBoolean(config.getInitParameter("private"));
69          _user = GetterUtil.getBoolean(config.getInitParameter("user"));
70      }
71  
72      public void service(HttpServletRequest req, HttpServletResponse res)
73          throws IOException, ServletException {
74  
75          ServletContext ctx = getServletContext();
76  
77          // Do not set the entire full main path. See LEP-456.
78  
79          //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
80          String mainPath = PortalUtil.PATH_MAIN;
81  
82          String friendlyURLPath = null;
83  
84          if (_private) {
85              if (_user) {
86                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
87              }
88              else {
89                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
90              }
91          }
92          else {
93              friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
94          }
95  
96          req.setAttribute(
97              WebKeys.FRIENDLY_URL, friendlyURLPath + req.getPathInfo());
98  
99          String redirect = mainPath;
100 
101         try {
102             redirect = getRedirect(
103                 req, req.getPathInfo(), mainPath, req.getParameterMap());
104 
105             LastPath lastPath = new LastPath(
106                 friendlyURLPath, req.getPathInfo(), req.getParameterMap());
107 
108             req.setAttribute(WebKeys.LAST_PATH, lastPath);
109         }
110         catch (NoSuchLayoutException nsle) {
111             _log.warn(nsle);
112 
113             redirect = PropsUtil.get(
114                 PropsUtil.LAYOUT_FRIENDLY_URL_PAGE_NOT_FOUND);
115         }
116         catch (Exception e) {
117             if (_log.isWarnEnabled()) {
118                 _log.warn(e);
119             }
120         }
121 
122         if (Validator.isNull(redirect)) {
123             redirect = mainPath;
124         }
125 
126         if (_log.isDebugEnabled()) {
127             _log.debug("Redirect " + redirect);
128         }
129 
130         if (redirect.startsWith(StringPool.SLASH)) {
131             RequestDispatcher rd = ctx.getRequestDispatcher(redirect);
132 
133             if (rd != null) {
134                 rd.forward(req, res);
135             }
136         }
137         else {
138             res.sendRedirect(redirect);
139         }
140     }
141 
142     protected String getRedirect(
143             HttpServletRequest req, String path, String mainPath, Map params)
144         throws Exception {
145 
146         if (Validator.isNull(path)) {
147             return mainPath;
148         }
149 
150         if (!path.startsWith(StringPool.SLASH)) {
151             return mainPath;
152         }
153 
154         // Group friendly URL
155 
156         String friendlyURL = null;
157 
158         int pos = path.indexOf(StringPool.SLASH, 1);
159 
160         if (pos != -1) {
161             friendlyURL = path.substring(0, pos);
162         }
163         else {
164             if (path.length() > 1) {
165                 friendlyURL = path.substring(0, path.length());
166             }
167         }
168 
169         if (Validator.isNull(friendlyURL)) {
170             return mainPath;
171         }
172 
173         long companyId = PortalInstances.getCompanyId(req);
174 
175         Group group = null;
176 
177         try {
178             group = GroupLocalServiceUtil.getFriendlyURLGroup(
179                 companyId, friendlyURL);
180         }
181         catch (NoSuchGroupException nsge) {
182         }
183 
184         if (group == null) {
185             String screenName = friendlyURL.substring(1);
186 
187             if (_user || !Validator.isNumber(screenName)) {
188                 try {
189                     User user = UserLocalServiceUtil.getUserByScreenName(
190                         companyId, screenName);
191 
192                     group = user.getGroup();
193                 }
194                 catch (NoSuchUserException nsue) {
195                     if (_log.isWarnEnabled()) {
196                         _log.warn(
197                             "No user exists with friendly URL " + screenName);
198                     }
199                 }
200             }
201             else {
202                 long groupId = GetterUtil.getLong(screenName);
203 
204                 try {
205                     group = GroupLocalServiceUtil.getGroup(groupId);
206                 }
207                 catch (NoSuchGroupException nsge) {
208                     if (_log.isDebugEnabled()) {
209                         _log.debug(
210                             "No group exists with friendly URL " + groupId +
211                                 ". Try fetching by screen name instead.");
212                     }
213 
214                     try {
215                         User user = UserLocalServiceUtil.getUserByScreenName(
216                             companyId, screenName);
217 
218                         group = user.getGroup();
219                     }
220                     catch (NoSuchUserException nsue) {
221                         if (_log.isWarnEnabled()) {
222                             _log.warn(
223                                 "No user or group exists with friendly URL " +
224                                     groupId);
225                         }
226                     }
227                 }
228             }
229         }
230 
231         if (group == null) {
232             return mainPath;
233         }
234 
235         // Layout friendly URL
236 
237         friendlyURL = null;
238 
239         if ((pos != -1) && ((pos + 1) != path.length())) {
240             friendlyURL = path.substring(pos, path.length());
241         }
242 
243         return PortalUtil.getLayoutActualURL(
244             group.getGroupId(), _private, mainPath, friendlyURL, params);
245     }
246 
247     private static Log _log = LogFactory.getLog(FriendlyURLServlet.class);
248 
249     private boolean _private;
250     private boolean _user;
251 
252 }