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.getPathMain();
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,
147 Map<String, String[]> params)
148 throws Exception {
149
150 if (Validator.isNull(path)) {
151 return mainPath;
152 }
153
154 if (!path.startsWith(StringPool.SLASH)) {
155 return mainPath;
156 }
157
158
160 String friendlyURL = null;
161
162 int pos = path.indexOf(StringPool.SLASH, 1);
163
164 if (pos != -1) {
165 friendlyURL = path.substring(0, pos);
166 }
167 else {
168 if (path.length() > 1) {
169 friendlyURL = path.substring(0, path.length());
170 }
171 }
172
173 if (Validator.isNull(friendlyURL)) {
174 return mainPath;
175 }
176
177 long companyId = PortalInstances.getCompanyId(req);
178
179 Group group = null;
180
181 try {
182 group = GroupLocalServiceUtil.getFriendlyURLGroup(
183 companyId, friendlyURL);
184 }
185 catch (NoSuchGroupException nsge) {
186 }
187
188 if (group == null) {
189 String screenName = friendlyURL.substring(1);
190
191 if (_user || !Validator.isNumber(screenName)) {
192 try {
193 User user = UserLocalServiceUtil.getUserByScreenName(
194 companyId, screenName);
195
196 group = user.getGroup();
197 }
198 catch (NoSuchUserException nsue) {
199 if (_log.isWarnEnabled()) {
200 _log.warn(
201 "No user exists with friendly URL " + screenName);
202 }
203 }
204 }
205 else {
206 long groupId = GetterUtil.getLong(screenName);
207
208 try {
209 group = GroupLocalServiceUtil.getGroup(groupId);
210 }
211 catch (NoSuchGroupException nsge) {
212 if (_log.isDebugEnabled()) {
213 _log.debug(
214 "No group exists with friendly URL " + groupId +
215 ". Try fetching by screen name instead.");
216 }
217
218 try {
219 User user = UserLocalServiceUtil.getUserByScreenName(
220 companyId, screenName);
221
222 group = user.getGroup();
223 }
224 catch (NoSuchUserException nsue) {
225 if (_log.isWarnEnabled()) {
226 _log.warn(
227 "No user or group exists with friendly URL " +
228 groupId);
229 }
230 }
231 }
232 }
233 }
234
235 if (group == null) {
236 return mainPath;
237 }
238
239
241 friendlyURL = null;
242
243 if ((pos != -1) && ((pos + 1) != path.length())) {
244 friendlyURL = path.substring(pos, path.length());
245 }
246
247 return PortalUtil.getLayoutActualURL(
248 group.getGroupId(), _private, mainPath, friendlyURL, params);
249 }
250
251 private static Log _log = LogFactory.getLog(FriendlyURLServlet.class);
252
253 private boolean _private;
254 private boolean _user;
255
256 }