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