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