1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class FriendlyURLServlet extends HttpServlet {
62  
63      public void init(ServletConfig servletConfig) throws ServletException {
64          super.init(servletConfig);
65  
66          _private = GetterUtil.getBoolean(
67              servletConfig.getInitParameter("private"));
68          _user = GetterUtil.getBoolean(
69              servletConfig.getInitParameter("user"));
70      }
71  
72      public void service(
73              HttpServletRequest request, HttpServletResponse response)
74          throws IOException, ServletException {
75  
76          ServletContext servletContext = getServletContext();
77  
78          // Do not set the entire full main path. See LEP-456.
79  
80          //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
81          String mainPath = Portal.PATH_MAIN;
82  
83          String friendlyURLPath = null;
84  
85          if (_private) {
86              if (_user) {
87                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
88              }
89              else {
90                  friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
91              }
92          }
93          else {
94              friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
95          }
96  
97          request.setAttribute(
98              WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
99  
100         String redirect = mainPath;
101 
102         try {
103             redirect = getRedirect(
104                 request, request.getPathInfo(), mainPath,
105                 request.getParameterMap());
106 
107             if (request.getAttribute(WebKeys.LAST_PATH) == null) {
108                 LastPath lastPath = new LastPath(
109                     friendlyURLPath, request.getPathInfo(),
110                     request.getParameterMap());
111 
112                 request.setAttribute(WebKeys.LAST_PATH, lastPath);
113             }
114         }
115         catch (NoSuchLayoutException nsle) {
116             _log.warn(nsle);
117 
118             PortalUtil.sendError(
119                 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
120 
121             return;
122         }
123         catch (Exception e) {
124             if (_log.isWarnEnabled()) {
125                 _log.warn(e);
126             }
127         }
128 
129         if (Validator.isNull(redirect)) {
130             redirect = mainPath;
131         }
132 
133         if (_log.isDebugEnabled()) {
134             _log.debug("Redirect " + redirect);
135         }
136 
137         if (redirect.startsWith(StringPool.SLASH)) {
138             RequestDispatcher requestDispatcher =
139                 servletContext.getRequestDispatcher(redirect);
140 
141             if (requestDispatcher != null) {
142                 requestDispatcher.forward(request, response);
143             }
144         }
145         else {
146             response.sendRedirect(redirect);
147         }
148     }
149 
150     protected String getRedirect(
151             HttpServletRequest request, String path, String mainPath,
152             Map<String, String[]> params)
153         throws Exception {
154 
155         if (Validator.isNull(path)) {
156             return mainPath;
157         }
158 
159         if (!path.startsWith(StringPool.SLASH)) {
160             return mainPath;
161         }
162 
163         // Group friendly URL
164 
165         String friendlyURL = null;
166 
167         int pos = path.indexOf(StringPool.SLASH, 1);
168 
169         if (pos != -1) {
170             friendlyURL = path.substring(0, pos);
171         }
172         else {
173             if (path.length() > 1) {
174                 friendlyURL = path.substring(0, path.length());
175             }
176         }
177 
178         if (Validator.isNull(friendlyURL)) {
179             return mainPath;
180         }
181 
182         long companyId = PortalInstances.getCompanyId(request);
183 
184         Group group = null;
185 
186         try {
187             group = GroupLocalServiceUtil.getFriendlyURLGroup(
188                 companyId, friendlyURL);
189         }
190         catch (NoSuchGroupException nsge) {
191         }
192 
193         if (group == null) {
194             String screenName = friendlyURL.substring(1);
195 
196             if (_user || !Validator.isNumber(screenName)) {
197                 try {
198                     User user = UserLocalServiceUtil.getUserByScreenName(
199                         companyId, screenName);
200 
201                     group = user.getGroup();
202                 }
203                 catch (NoSuchUserException nsue) {
204                     if (_log.isWarnEnabled()) {
205                         _log.warn(
206                             "No user exists with friendly URL " + screenName);
207                     }
208                 }
209             }
210             else {
211                 long groupId = GetterUtil.getLong(screenName);
212 
213                 try {
214                     group = GroupLocalServiceUtil.getGroup(groupId);
215                 }
216                 catch (NoSuchGroupException nsge) {
217                     if (_log.isDebugEnabled()) {
218                         _log.debug(
219                             "No group exists with friendly URL " + groupId +
220                                 ". Try fetching by screen name instead.");
221                     }
222 
223                     try {
224                         User user = UserLocalServiceUtil.getUserByScreenName(
225                             companyId, screenName);
226 
227                         group = user.getGroup();
228                     }
229                     catch (NoSuchUserException nsue) {
230                         if (_log.isWarnEnabled()) {
231                             _log.warn(
232                                 "No user or group exists with friendly URL " +
233                                     groupId);
234                         }
235                     }
236                 }
237             }
238         }
239 
240         if (group == null) {
241             return mainPath;
242         }
243 
244         // Layout friendly URL
245 
246         friendlyURL = null;
247 
248         if ((pos != -1) && ((pos + 1) != path.length())) {
249             friendlyURL = path.substring(pos, path.length());
250         }
251 
252         return PortalUtil.getLayoutActualURL(
253             group.getGroupId(), _private, mainPath, friendlyURL, params);
254     }
255 
256     private static Log _log = LogFactoryUtil.getLog(FriendlyURLServlet.class);
257 
258     private boolean _private;
259     private boolean _user;
260 
261 }