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