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.GetterUtil;
024 import com.liferay.portal.kernel.util.StringPool;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.service.GroupLocalServiceUtil;
029 import com.liferay.portal.service.UserLocalServiceUtil;
030 import com.liferay.portal.util.Portal;
031 import com.liferay.portal.util.PortalInstances;
032 import com.liferay.portal.util.PortalUtil;
033 import com.liferay.portal.util.WebKeys;
034
035 import java.io.IOException;
036
037 import java.util.HashMap;
038 import java.util.Map;
039
040 import javax.servlet.RequestDispatcher;
041 import javax.servlet.ServletConfig;
042 import javax.servlet.ServletContext;
043 import javax.servlet.ServletException;
044 import javax.servlet.http.HttpServlet;
045 import javax.servlet.http.HttpServletRequest;
046 import javax.servlet.http.HttpServletResponse;
047
048
052 public class FriendlyURLServlet extends HttpServlet {
053
054 public void init(ServletConfig servletConfig) throws ServletException {
055 super.init(servletConfig);
056
057 _private = GetterUtil.getBoolean(
058 servletConfig.getInitParameter("private"));
059 _user = GetterUtil.getBoolean(
060 servletConfig.getInitParameter("user"));
061 }
062
063 public void service(
064 HttpServletRequest request, HttpServletResponse response)
065 throws IOException, ServletException {
066
067 ServletContext servletContext = getServletContext();
068
069
070
071
072 String mainPath = Portal.PATH_MAIN;
073
074 String friendlyURLPath = null;
075
076 if (_private) {
077 if (_user) {
078 friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateUser();
079 }
080 else {
081 friendlyURLPath = PortalUtil.getPathFriendlyURLPrivateGroup();
082 }
083 }
084 else {
085 friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
086 }
087
088 request.setAttribute(
089 WebKeys.FRIENDLY_URL, friendlyURLPath + request.getPathInfo());
090
091 String redirect = mainPath;
092
093 try {
094 redirect = getRedirect(
095 request, request.getPathInfo(), mainPath,
096 request.getParameterMap());
097
098 if (request.getAttribute(WebKeys.LAST_PATH) == null) {
099 LastPath lastPath = new LastPath(
100 friendlyURLPath, request.getPathInfo(),
101 request.getParameterMap());
102
103 request.setAttribute(WebKeys.LAST_PATH, lastPath);
104 }
105 }
106 catch (NoSuchLayoutException nsle) {
107 _log.warn(nsle);
108
109 PortalUtil.sendError(
110 HttpServletResponse.SC_NOT_FOUND, nsle, request, response);
111
112 return;
113 }
114 catch (Exception e) {
115 if (_log.isWarnEnabled()) {
116 _log.warn(e);
117 }
118 }
119
120 if (Validator.isNull(redirect)) {
121 redirect = mainPath;
122 }
123
124 if (_log.isDebugEnabled()) {
125 _log.debug("Redirect " + redirect);
126 }
127
128 if (redirect.startsWith(StringPool.SLASH)) {
129 RequestDispatcher requestDispatcher =
130 servletContext.getRequestDispatcher(redirect);
131
132 if (requestDispatcher != null) {
133 requestDispatcher.forward(request, response);
134 }
135 }
136 else {
137 response.sendRedirect(redirect);
138 }
139 }
140
141 protected String getRedirect(
142 HttpServletRequest request, String path, String mainPath,
143 Map<String, String[]> 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
155
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(request);
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
236
237 friendlyURL = null;
238
239 if ((pos != -1) && ((pos + 1) != path.length())) {
240 friendlyURL = path.substring(pos, path.length());
241 }
242
243 Map<String, Object> requestContext = new HashMap<String, Object>();
244
245 requestContext.put("request", request);
246
247 return PortalUtil.getLayoutActualURL(
248 group.getGroupId(), _private, mainPath, friendlyURL, params,
249 requestContext);
250 }
251
252 private static Log _log = LogFactoryUtil.getLog(FriendlyURLServlet.class);
253
254 private boolean _private;
255 private boolean _user;
256
257 }