001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.servlet;
022:
023: import com.liferay.portal.NoSuchGroupException;
024: import com.liferay.portal.NoSuchLayoutException;
025: import com.liferay.portal.NoSuchUserException;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Group;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.service.GroupLocalServiceUtil;
032: import com.liferay.portal.service.UserLocalServiceUtil;
033: import com.liferay.portal.struts.LastPath;
034: import com.liferay.portal.util.PortalInstances;
035: import com.liferay.portal.util.PortalUtil;
036: import com.liferay.portal.util.WebKeys;
037:
038: import java.io.IOException;
039:
040: import java.util.Map;
041:
042: import javax.servlet.RequestDispatcher;
043: import javax.servlet.ServletConfig;
044: import javax.servlet.ServletContext;
045: import javax.servlet.ServletException;
046: import javax.servlet.http.HttpServlet;
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049:
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052:
053: /**
054: * <a href="FriendlyURLServlet.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Brian Wing Shun Chan
057: * @author Jorge Ferrer
058: *
059: */
060: public class FriendlyURLServlet extends HttpServlet {
061:
062: public void init(ServletConfig config) throws ServletException {
063: super .init(config);
064:
065: _private = GetterUtil.getBoolean(config
066: .getInitParameter("private"));
067: _user = GetterUtil.getBoolean(config.getInitParameter("user"));
068: }
069:
070: public void service(HttpServletRequest req, HttpServletResponse res)
071: throws IOException, ServletException {
072:
073: ServletContext ctx = getServletContext();
074:
075: // Do not set the entire full main path. See LEP-456.
076:
077: //String mainPath = (String)ctx.getAttribute(WebKeys.MAIN_PATH);
078: String mainPath = PortalUtil.PATH_MAIN;
079:
080: String friendlyURLPath = null;
081:
082: if (_private) {
083: if (_user) {
084: friendlyURLPath = PortalUtil
085: .getPathFriendlyURLPrivateUser();
086: } else {
087: friendlyURLPath = PortalUtil
088: .getPathFriendlyURLPrivateGroup();
089: }
090: } else {
091: friendlyURLPath = PortalUtil.getPathFriendlyURLPublic();
092: }
093:
094: req.setAttribute(WebKeys.FRIENDLY_URL, friendlyURLPath
095: + req.getPathInfo());
096:
097: String redirect = mainPath;
098:
099: try {
100: redirect = getRedirect(req, req.getPathInfo(), mainPath,
101: req.getParameterMap());
102:
103: if (req.getAttribute(WebKeys.LAST_PATH) == null) {
104: LastPath lastPath = new LastPath(friendlyURLPath, req
105: .getPathInfo(), req.getParameterMap());
106:
107: req.setAttribute(WebKeys.LAST_PATH, lastPath);
108: }
109: } catch (NoSuchLayoutException nsle) {
110: _log.warn(nsle);
111:
112: PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND,
113: nsle, req, res);
114:
115: return;
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: } else {
137: res.sendRedirect(redirect);
138: }
139: }
140:
141: protected String getRedirect(HttpServletRequest req, String path,
142: String mainPath, Map params) throws Exception {
143:
144: if (Validator.isNull(path)) {
145: return mainPath;
146: }
147:
148: if (!path.startsWith(StringPool.SLASH)) {
149: return mainPath;
150: }
151:
152: // Group friendly URL
153:
154: String friendlyURL = null;
155:
156: int pos = path.indexOf(StringPool.SLASH, 1);
157:
158: if (pos != -1) {
159: friendlyURL = path.substring(0, pos);
160: } else {
161: if (path.length() > 1) {
162: friendlyURL = path.substring(0, path.length());
163: }
164: }
165:
166: if (Validator.isNull(friendlyURL)) {
167: return mainPath;
168: }
169:
170: long companyId = PortalInstances.getCompanyId(req);
171:
172: Group group = null;
173:
174: try {
175: group = GroupLocalServiceUtil.getFriendlyURLGroup(
176: companyId, friendlyURL);
177: } catch (NoSuchGroupException nsge) {
178: }
179:
180: if (group == null) {
181: String screenName = friendlyURL.substring(1);
182:
183: if (_user || !Validator.isNumber(screenName)) {
184: try {
185: User user = UserLocalServiceUtil
186: .getUserByScreenName(companyId, screenName);
187:
188: group = user.getGroup();
189: } catch (NoSuchUserException nsue) {
190: if (_log.isWarnEnabled()) {
191: _log.warn("No user exists with friendly URL "
192: + screenName);
193: }
194: }
195: } else {
196: long groupId = GetterUtil.getLong(screenName);
197:
198: try {
199: group = GroupLocalServiceUtil.getGroup(groupId);
200: } catch (NoSuchGroupException nsge) {
201: if (_log.isDebugEnabled()) {
202: _log
203: .debug("No group exists with friendly URL "
204: + groupId
205: + ". Try fetching by screen name instead.");
206: }
207:
208: try {
209: User user = UserLocalServiceUtil
210: .getUserByScreenName(companyId,
211: screenName);
212:
213: group = user.getGroup();
214: } catch (NoSuchUserException nsue) {
215: if (_log.isWarnEnabled()) {
216: _log
217: .warn("No user or group exists with friendly URL "
218: + groupId);
219: }
220: }
221: }
222: }
223: }
224:
225: if (group == null) {
226: return mainPath;
227: }
228:
229: // Layout friendly URL
230:
231: friendlyURL = null;
232:
233: if ((pos != -1) && ((pos + 1) != path.length())) {
234: friendlyURL = path.substring(pos, path.length());
235: }
236:
237: return PortalUtil.getLayoutActualURL(group.getGroupId(),
238: _private, mainPath, friendlyURL, params);
239: }
240:
241: private static Log _log = LogFactory
242: .getLog(FriendlyURLServlet.class);
243:
244: private boolean _private;
245: private boolean _user;
246:
247: }
|