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.cms.servlet;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.util.ContentTypes;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.model.Company;
030: import com.liferay.portal.service.CompanyLocalServiceUtil;
031: import com.liferay.portal.theme.ThemeDisplay;
032: import com.liferay.portal.theme.ThemeDisplayFactory;
033: import com.liferay.portal.util.PortalInstances;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.util.ExtPropertiesLoader;
036: import com.liferay.util.servlet.ServletResponseUtil;
037:
038: import java.io.IOException;
039:
040: import java.util.Properties;
041:
042: import javax.servlet.ServletConfig;
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: import org.apache.commons.logging.Log;
049: import org.apache.commons.logging.LogFactory;
050:
051: /**
052: * <a href="CMSServlet.java.html"><b><i>View Source</i></b></a>
053: *
054: * @author Brian Wing Shun Chan
055: * @author Jorge Ferrer
056: * @author Raymond Augé
057: *
058: */
059: public class CMSServlet extends HttpServlet {
060:
061: public void init(ServletConfig config) throws ServletException {
062: super .init(config);
063:
064: _groupId = GetterUtil.getLong(config
065: .getInitParameter("group_id"));
066:
067: String redirectsConf = config
068: .getInitParameter("redirects_conf");
069:
070: if (redirectsConf != null) {
071: _redirectProperties = ExtPropertiesLoader.getInstance(
072: redirectsConf).getProperties();
073: }
074:
075: _redirectsEnabled = GetterUtil.getBoolean(config
076: .getInitParameter("redirects_enabled"));
077: }
078:
079: public void service(HttpServletRequest req, HttpServletResponse res)
080: throws IOException, ServletException {
081:
082: long groupId = _groupId;
083:
084: if (groupId <= 0) {
085: groupId = ParamUtil.getLong(req, "groupId");
086: }
087:
088: String path = GetterUtil.getString(req.getPathInfo());
089:
090: if (path.endsWith(StringPool.SLASH)) {
091: path = path.substring(0, path.length() - 1);
092: }
093:
094: if (path.startsWith(StringPool.SLASH)) {
095: path = path.substring(1, path.length());
096: }
097:
098: if ((_redirectProperties != null) && _redirectsEnabled) {
099: String redirect = _redirectProperties.getProperty(path);
100:
101: if (Validator.isNotNull(redirect)) {
102: res.sendRedirect(redirect);
103:
104: return;
105: }
106: }
107:
108: String languageId = LanguageUtil.getLanguageId(req);
109:
110: ThemeDisplay themeDisplay = null;
111:
112: try {
113: themeDisplay = ThemeDisplayFactory.create();
114:
115: long companyId = PortalInstances.getCompanyId(req);
116:
117: Company company = CompanyLocalServiceUtil
118: .getCompanyById(companyId);
119:
120: String contextPath = PortalUtil.getPathContext();
121:
122: themeDisplay.setCompany(company);
123: themeDisplay.setPortletGroupId(groupId);
124: themeDisplay.setPathContext(contextPath);
125: themeDisplay.setPathFriendlyURLPrivateGroup(PortalUtil
126: .getPathFriendlyURLPrivateGroup());
127: themeDisplay.setPathFriendlyURLPrivateUser(PortalUtil
128: .getPathFriendlyURLPrivateUser());
129: themeDisplay.setPathFriendlyURLPublic(PortalUtil
130: .getPathFriendlyURLPublic());
131: themeDisplay.setPathImage(PortalUtil.getPathImage());
132: themeDisplay.setPathMain(PortalUtil.getPathMain());
133:
134: String content = getContent(groupId, path, languageId,
135: themeDisplay);
136:
137: if (Validator.isNotNull(content)) {
138: if (_log.isDebugEnabled()) {
139: _log.debug("Content found for " + path);
140: }
141:
142: String mimeType = ParamUtil.getString(req, "mimeType",
143: ContentTypes.TEXT_HTML_UTF8);
144:
145: res.setContentType(mimeType);
146:
147: try {
148: ServletResponseUtil.write(res, content);
149: } catch (Exception e) {
150: if (_log.isWarnEnabled()) {
151: _log.warn(e, e);
152: }
153: }
154: } else {
155: if (_log.isDebugEnabled()) {
156: _log.debug("Content NOT found for " + path);
157: }
158: }
159: } catch (Exception e) {
160: } finally {
161: try {
162: ThemeDisplayFactory.recycle(themeDisplay);
163: } catch (Exception e) {
164: }
165: }
166: }
167:
168: protected String getContent(long groupId, String articleId,
169: String languageId, ThemeDisplay themeDisplay) {
170:
171: return CMSServletUtil.getContent(groupId, articleId,
172: languageId, themeDisplay);
173: }
174:
175: private static Log _log = LogFactory.getLog(CMSServlet.class);
176:
177: private long _groupId;
178: private Properties _redirectProperties;
179: private boolean _redirectsEnabled;
180:
181: }
|