001
014
015 package com.liferay.portal.servlet;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.NoSuchLayoutException;
019 import com.liferay.portal.NoSuchUserException;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.struts.LastPath;
023 import com.liferay.portal.kernel.util.CharPool;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.Validator;
027 import com.liferay.portal.model.Group;
028 import com.liferay.portal.model.User;
029 import com.liferay.portal.service.GroupLocalServiceUtil;
030 import com.liferay.portal.service.UserLocalServiceUtil;
031 import com.liferay.portal.util.Portal;
032 import com.liferay.portal.util.PortalInstances;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portal.util.WebKeys;
035
036 import java.io.IOException;
037
038 import java.util.HashMap;
039 import java.util.Map;
040
041 import javax.servlet.RequestDispatcher;
042 import javax.servlet.ServletConfig;
043 import javax.servlet.ServletContext;
044 import javax.servlet.ServletException;
045 import javax.servlet.http.HttpServlet;
046 import javax.servlet.http.HttpServletRequest;
047 import javax.servlet.http.HttpServletResponse;
048
049
053 public class FriendlyURLServlet extends HttpServlet {
054
055 public void init(ServletConfig servletConfig) throws ServletException {
056 super.init(servletConfig);
057
058 _private = GetterUtil.getBoolean(
059 servletConfig.getInitParameter("private"));
060 _user = GetterUtil.getBoolean(
061 servletConfig.getInitParameter("user"));
062 }
063
064 public void service(
065 HttpServletRequest request, HttpServletResponse response)
066 throws IOException, ServletException {
067
068 ServletContext servletContext = getServletContext();
069
070
071
072
073 String mainPath = Portal.PATH_MAIN;
074
075 String friendlyURLPath = null;
076
077 if (_private) {
078 if (_user) {
079 friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
080 }
081 else {
082 friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
083 }
084 }
085 else {
086 friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
087 }
088
089 request.setAttribute(
090 WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
091
092 String redirect = mainPath;
093
094 try {
095 redirect = getRedirect(
096 request, request.getPathInfo(), mainPath,
097 request.getParameterMap());
098
099 if (request.getAttribute(WebKeys.LAST_PATH) == null) {
100 LastPath lastPath = new LastPath(
101 friendlyURLPath, request.getPathInfo(),
102 request.getParameterMap());
103
104 request.setAttribute(WebKeys.LAST_PATH, lastPath);
105 }
106 }
107 catch (NoSuchLayoutException nsle) {
108 _log.warn(nsle);
109
110 PortalUtil.sendError(
111 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
112
113 return;
114 }
115 catch (Exception e) {
116 if (_log.isWarnEnabled()) {
117 _log.warn(e);
118 }
119 }
120
121 if (Validator.isNull(redirect)) {
122 redirect = mainPath;
123 }
124
125 if (_log.isDebugEnabled()) {
126 _log.debug("Redirect " + redirect);
127 }
128
129 if (redirect.startsWith(StringPool.SLASH)) {
130 RequestDispatcher requestDispatcher =
131 servletContext.getRequestDispatcher(redirect);
132
133 if (requestDispatcher != null) {
134 requestDispatcher.forward(request, response);
135 }
136 }
137 else {
138 response.sendRedirect(redirect);
139 }
140 }
141
142 protected String getRedirect(
143 HttpServletRequest request, String path, String mainPath,
144 Map<String, String[]> params)
145 throws Exception {
146
147 if (Validator.isNull(path)) {
148 return mainPath;
149 }
150
151 if (!path.startsWith(StringPool.SLASH)) {
152 return mainPath;
153 }
154
155
156
157 String friendlyURL = null;
158
159 int pos = path.indexOf(CharPool.SLASH, 1);
160
161 if (pos != -1) {
162 friendlyURL = path.substring(0, pos);
163 }
164 else {
165 if (path.length() > 1) {
166 friendlyURL = path.substring(0, path.length());
167 }
168 }
169
170 if (Validator.isNull(friendlyURL)) {
171 return mainPath;
172 }
173
174 long companyId = PortalInstances.getCompanyId(request);
175
176 Group group = null;
177
178 try {
179 group = GroupLocalServiceUtil.getFriendlyURLGroup(
180 companyId, friendlyURL);
181 }
182 catch (NoSuchGroupException nsge) {
183 }
184
185 if (group == null) {
186 String screenName = friendlyURL.substring(1);
187
188 if (_user || !Validator.isNumber(screenName)) {
189 try {
190 User user = UserLocalServiceUtil.getUserByScreenName(
191 companyId, screenName);
192
193 group = user.getGroup();
194 }
195 catch (NoSuchUserException nsue) {
196 if (_log.isWarnEnabled()) {
197 _log.warn(
198 "No user exists with friendly URL " + screenName);
199 }
200 }
201 }
202 else {
203 long groupId = GetterUtil.getLong(screenName);
204
205 try {
206 group = GroupLocalServiceUtil.getGroup(groupId);
207 }
208 catch (NoSuchGroupException nsge) {
209 if (_log.isDebugEnabled()) {
210 _log.debug(
211 "No group exists with friendly URL " + groupId +
212 ". Try fetching by screen name instead.");
213 }
214
215 try {
216 User user = UserLocalServiceUtil.getUserByScreenName(
217 companyId, screenName);
218
219 group = user.getGroup();
220 }
221 catch (NoSuchUserException nsue) {
222 if (_log.isWarnEnabled()) {
223 _log.warn(
224 "No user or group exists with friendly URL " +
225 groupId);
226 }
227 }
228 }
229 }
230 }
231
232 if (group == null) {
233 return mainPath;
234 }
235
236
237
238 friendlyURL = null;
239
240 if ((pos != -1) && ((pos + 1) != path.length())) {
241 friendlyURL = path.substring(pos, path.length());
242 }
243
244 Map<String, Object> requestContext = new HashMap<String, Object>();
245
246 requestContext.put("request", request);
247
248 return PortalUtil.getLayoutActualURL(
249 group.getGroupId(), _private, mainPath, friendlyURL, params,
250 requestContext);
251 }
252
253 private static Log _log = LogFactoryUtil.getLog(FriendlyURLServlet.class);
254
255 private boolean _private;
256 private boolean _user;
257
258 }