1
14
15 package com.liferay.portal.servlet;
16
17 import com.liferay.portal.NoSuchGroupException;
18 import com.liferay.portal.NoSuchLayoutException;
19 import com.liferay.portal.NoSuchUserException;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.struts.LastPath;
23 import com.liferay.portal.kernel.util.CharPool;
24 import com.liferay.portal.kernel.util.GetterUtil;
25 import com.liferay.portal.kernel.util.StringPool;
26 import com.liferay.portal.kernel.util.Validator;
27 import com.liferay.portal.model.Group;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.GroupLocalServiceUtil;
30 import com.liferay.portal.service.UserLocalServiceUtil;
31 import com.liferay.portal.util.Portal;
32 import com.liferay.portal.util.PortalInstances;
33 import com.liferay.portal.util.PortalUtil;
34 import com.liferay.portal.util.WebKeys;
35
36 import java.io.IOException;
37
38 import java.util.Map;
39
40 import javax.servlet.RequestDispatcher;
41 import javax.servlet.ServletConfig;
42 import javax.servlet.ServletContext;
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServlet;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48
54 public class FriendlyURLServlet extends HttpServlet {
55
56 public void init(ServletConfig servletConfig) throws ServletException {
57 super.init(servletConfig);
58
59 _private = GetterUtil.getBoolean(
60 servletConfig.getInitParameter("private"));
61 _user = GetterUtil.getBoolean(
62 servletConfig.getInitParameter("user"));
63 }
64
65 public void service(
66 HttpServletRequest request, HttpServletResponse response)
67 throws IOException, ServletException {
68
69 ServletContext servletContext = getServletContext();
70
71
73 String mainPath = Portal.PATH_MAIN;
75
76 String friendlyURLPath = null;
77
78 if (_private) {
79 if (_user) {
80 friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
81 }
82 else {
83 friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
84 }
85 }
86 else {
87 friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
88 }
89
90 request.setAttribute(
91 WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
92
93 String redirect = mainPath;
94
95 try {
96 redirect = getRedirect(
97 request, request.getPathInfo(), mainPath,
98 request.getParameterMap());
99
100 if (request.getAttribute(WebKeys.LAST_PATH) == null) {
101 LastPath lastPath = new LastPath(
102 friendlyURLPath, request.getPathInfo(),
103 request.getParameterMap());
104
105 request.setAttribute(WebKeys.LAST_PATH, lastPath);
106 }
107 }
108 catch (NoSuchLayoutException nsle) {
109 _log.warn(nsle);
110
111 PortalUtil.sendError(
112 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
113
114 return;
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 requestDispatcher =
132 servletContext.getRequestDispatcher(redirect);
133
134 if (requestDispatcher != null) {
135 requestDispatcher.forward(request, response);
136 }
137 }
138 else {
139 response.sendRedirect(redirect);
140 }
141 }
142
143 protected String getRedirect(
144 HttpServletRequest request, String path, String mainPath,
145 Map<String, String[]> params)
146 throws Exception {
147
148 if (Validator.isNull(path)) {
149 return mainPath;
150 }
151
152 if (!path.startsWith(StringPool.SLASH)) {
153 return mainPath;
154 }
155
156
158 String friendlyURL = null;
159
160 int pos = path.indexOf(CharPool.SLASH, 1);
161
162 if (pos != -1) {
163 friendlyURL = path.substring(0, pos);
164 }
165 else {
166 if (path.length() > 1) {
167 friendlyURL = path.substring(0, path.length());
168 }
169 }
170
171 if (Validator.isNull(friendlyURL)) {
172 return mainPath;
173 }
174
175 long companyId = PortalInstances.getCompanyId(request);
176
177 Group group = null;
178
179 try {
180 group = GroupLocalServiceUtil.getFriendlyURLGroup(
181 companyId, friendlyURL);
182 }
183 catch (NoSuchGroupException nsge) {
184 }
185
186 if (group == null) {
187 String screenName = friendlyURL.substring(1);
188
189 if (_user || !Validator.isNumber(screenName)) {
190 try {
191 User user = UserLocalServiceUtil.getUserByScreenName(
192 companyId, screenName);
193
194 group = user.getGroup();
195 }
196 catch (NoSuchUserException nsue) {
197 if (_log.isWarnEnabled()) {
198 _log.warn(
199 "No user exists with friendly URL " + screenName);
200 }
201 }
202 }
203 else {
204 long groupId = GetterUtil.getLong(screenName);
205
206 try {
207 group = GroupLocalServiceUtil.getGroup(groupId);
208 }
209 catch (NoSuchGroupException nsge) {
210 if (_log.isDebugEnabled()) {
211 _log.debug(
212 "No group exists with friendly URL " + groupId +
213 ". Try fetching by screen name instead.");
214 }
215
216 try {
217 User user = UserLocalServiceUtil.getUserByScreenName(
218 companyId, screenName);
219
220 group = user.getGroup();
221 }
222 catch (NoSuchUserException nsue) {
223 if (_log.isWarnEnabled()) {
224 _log.warn(
225 "No user or group exists with friendly URL " +
226 groupId);
227 }
228 }
229 }
230 }
231 }
232
233 if (group == null) {
234 return mainPath;
235 }
236
237
239 friendlyURL = null;
240
241 if ((pos != -1) && ((pos + 1) != path.length())) {
242 friendlyURL = path.substring(pos, path.length());
243 }
244
245 return PortalUtil.getLayoutActualURL(
246 group.getGroupId(), _private, mainPath, friendlyURL, params);
247 }
248
249 private static Log _log = LogFactoryUtil.getLog(FriendlyURLServlet.class);
250
251 private boolean _private;
252 private boolean _user;
253
254 }