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.PropsUtil;
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 = PortalUtil.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 LastPath lastPath = new LastPath(
106 friendlyURLPath, req.getPathInfo(), req.getParameterMap());
107
108 req.setAttribute(WebKeys.LAST_PATH, lastPath);
109 }
110 catch (NoSuchLayoutException nsle) {
111 _log.warn(nsle);
112
113 redirect = PropsUtil.get(
114 PropsUtil.LAYOUT_FRIENDLY_URL_PAGE_NOT_FOUND);
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 rd = ctx.getRequestDispatcher(redirect);
132
133 if (rd != null) {
134 rd.forward(req, res);
135 }
136 }
137 else {
138 res.sendRedirect(redirect);
139 }
140 }
141
142 protected String getRedirect(
143 HttpServletRequest req, String path, String mainPath, Map params)
144 throws Exception {
145
146 if (Validator.isNull(path)) {
147 return mainPath;
148 }
149
150 if (!path.startsWith(StringPool.SLASH)) {
151 return mainPath;
152 }
153
154
156 String friendlyURL = null;
157
158 int pos = path.indexOf(StringPool.SLASH, 1);
159
160 if (pos != -1) {
161 friendlyURL = path.substring(0, pos);
162 }
163 else {
164 if (path.length() > 1) {
165 friendlyURL = path.substring(0, path.length());
166 }
167 }
168
169 if (Validator.isNull(friendlyURL)) {
170 return mainPath;
171 }
172
173 long companyId = PortalInstances.getCompanyId(req);
174
175 Group group = null;
176
177 try {
178 group = GroupLocalServiceUtil.getFriendlyURLGroup(
179 companyId, friendlyURL);
180 }
181 catch (NoSuchGroupException nsge) {
182 }
183
184 if (group == null) {
185 String screenName = friendlyURL.substring(1);
186
187 if (_user || !Validator.isNumber(screenName)) {
188 try {
189 User user = UserLocalServiceUtil.getUserByScreenName(
190 companyId, screenName);
191
192 group = user.getGroup();
193 }
194 catch (NoSuchUserException nsue) {
195 if (_log.isWarnEnabled()) {
196 _log.warn(
197 "No user exists with friendly URL " + screenName);
198 }
199 }
200 }
201 else {
202 long groupId = GetterUtil.getLong(screenName);
203
204 try {
205 group = GroupLocalServiceUtil.getGroup(groupId);
206 }
207 catch (NoSuchGroupException nsge) {
208 if (_log.isDebugEnabled()) {
209 _log.debug(
210 "No group exists with friendly URL " + groupId +
211 ". Try fetching by screen name instead.");
212 }
213
214 try {
215 User user = UserLocalServiceUtil.getUserByScreenName(
216 companyId, screenName);
217
218 group = user.getGroup();
219 }
220 catch (NoSuchUserException nsue) {
221 if (_log.isWarnEnabled()) {
222 _log.warn(
223 "No user or group exists with friendly URL " +
224 groupId);
225 }
226 }
227 }
228 }
229 }
230
231 if (group == null) {
232 return mainPath;
233 }
234
235
237 friendlyURL = null;
238
239 if ((pos != -1) && ((pos + 1) != path.length())) {
240 friendlyURL = path.substring(pos, path.length());
241 }
242
243 return PortalUtil.getLayoutActualURL(
244 group.getGroupId(), _private, mainPath, friendlyURL, params);
245 }
246
247 private static Log _log = LogFactory.getLog(FriendlyURLServlet.class);
248
249 private boolean _private;
250 private boolean _user;
251
252 }