1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.cms.servlet;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.service.CompanyLocalServiceUtil;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.theme.ThemeDisplayFactory;
35  import com.liferay.portal.util.PortalInstances;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.util.ExtPropertiesLoader;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.io.IOException;
41  
42  import java.util.Properties;
43  
44  import javax.servlet.ServletConfig;
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServlet;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.commons.logging.Log;
51  import org.apache.commons.logging.LogFactory;
52  
53  /**
54   * <a href="CMSServlet.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   * @author Jorge Ferrer
58   * @author Raymond Augé
59   *
60   */
61  public class CMSServlet extends HttpServlet {
62  
63      public void init(ServletConfig config) throws ServletException {
64          super.init(config);
65  
66          _groupId = GetterUtil.getLong(config.getInitParameter("group_id"));
67  
68          String redirectsConf = config.getInitParameter("redirects_conf");
69  
70          if (redirectsConf != null) {
71              _redirectProperties = ExtPropertiesLoader.getInstance(
72                  redirectsConf).getProperties();
73          }
74  
75          _redirectsEnabled = GetterUtil.getBoolean(
76              config.getInitParameter("redirects_enabled"));
77      }
78  
79      public void service(HttpServletRequest req, HttpServletResponse res)
80          throws IOException, ServletException {
81  
82          long groupId = _groupId;
83  
84          if (groupId <= 0) {
85              groupId = ParamUtil.getLong(req, "groupId");
86          }
87  
88          String path = GetterUtil.getString(req.getPathInfo());
89  
90          if (path.endsWith(StringPool.SLASH)) {
91              path = path.substring(0, path.length() - 1);
92          }
93  
94          if (path.startsWith(StringPool.SLASH)) {
95              path = path.substring(1, path.length());
96          }
97  
98          if ((_redirectProperties != null) && _redirectsEnabled) {
99              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.getCompanyById(companyId);
118 
119             String contextPath = PortalUtil.getPathContext();
120 
121             themeDisplay.setCompany(company);
122             themeDisplay.setPortletGroupId(groupId);
123             themeDisplay.setPathContext(contextPath);
124             themeDisplay.setPathFriendlyURLPrivateGroup(
125                 PortalUtil.getPathFriendlyURLPrivateGroup());
126             themeDisplay.setPathFriendlyURLPrivateUser(
127                 PortalUtil.getPathFriendlyURLPrivateUser());
128             themeDisplay.setPathFriendlyURLPublic(
129                 PortalUtil.getPathFriendlyURLPublic());
130             themeDisplay.setPathImage(PortalUtil.getPathImage());
131             themeDisplay.setPathMain(PortalUtil.getPathMain());
132 
133             String content = getContent(
134                 groupId, path, languageId, themeDisplay);
135 
136             if (Validator.isNotNull(content)) {
137                 if (_log.isDebugEnabled()) {
138                     _log.debug("Content found for " + path);
139                 }
140 
141                 String mimeType = ParamUtil.getString(
142                     req, "mimeType", ContentTypes.TEXT_HTML_UTF8);
143 
144                 res.setContentType(mimeType);
145 
146                 try {
147                     ServletResponseUtil.write(res, content);
148                 }
149                 catch (Exception e) {
150                     if (_log.isWarnEnabled()) {
151                         _log.warn(e, e);
152                     }
153                 }
154             }
155             else {
156                 if (_log.isDebugEnabled()) {
157                     _log.debug("Content NOT found for " + path);
158                 }
159             }
160         }
161         catch (Exception e) {
162         }
163         finally {
164             try {
165                 ThemeDisplayFactory.recycle(themeDisplay);
166             }
167             catch (Exception e) {
168             }
169         }
170     }
171 
172     protected String getContent(
173         long groupId, String articleId, String languageId,
174         ThemeDisplay themeDisplay) {
175 
176         return CMSServletUtil.getContent(
177             groupId, articleId, languageId, themeDisplay);
178     }
179 
180     private static Log _log = LogFactory.getLog(CMSServlet.class);
181 
182     private long _groupId;
183     private Properties _redirectProperties;
184     private boolean _redirectsEnabled;
185 
186 }