1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.portlet.journalcontent.util;
24  
25  import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
26  import com.liferay.portal.kernel.cache.PortalCache;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portlet.journal.model.JournalArticleDisplay;
36  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
37  import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
38  
39  import java.util.regex.Matcher;
40  import java.util.regex.Pattern;
41  
42  import org.apache.commons.lang.time.StopWatch;
43  
44  /**
45   * <a href="JournalContentImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Raymond Augé
49   * @author Michael Young
50   */
51  public class JournalContentImpl implements JournalContent {
52  
53      public void clearCache() {
54          cache.removeAll();
55      }
56  
57      public void clearCache(
58          long groupId, String articleId, String templateId) {
59  
60          clearCache();
61      }
62  
63      public String getContent(
64          long groupId, String articleId, String viewMode, String languageId,
65          String xmlRequest) {
66  
67          return getContent(
68              groupId, articleId, null, viewMode, languageId, null, xmlRequest);
69      }
70  
71      public String getContent(
72          long groupId, String articleId, String viewMode, String languageId,
73          ThemeDisplay themeDisplay) {
74  
75          return getContent(
76              groupId, articleId, null, viewMode, languageId, themeDisplay);
77      }
78  
79      public String getContent(
80          long groupId, String articleId, String templateId, String viewMode,
81          String languageId, String xmlRequest) {
82  
83          return getContent(
84              groupId, articleId, templateId, viewMode, languageId, null,
85              xmlRequest);
86      }
87  
88      public String getContent(
89          long groupId, String articleId, String templateId, String viewMode,
90          String languageId, ThemeDisplay themeDisplay) {
91  
92          return getContent(
93              groupId, articleId, templateId, viewMode, languageId, themeDisplay,
94              null);
95      }
96  
97      public String getContent(
98          long groupId, String articleId, String templateId, String viewMode,
99          String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
100 
101         JournalArticleDisplay articleDisplay = getDisplay(
102             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
103             1, xmlRequest);
104 
105         if (articleDisplay != null) {
106             return articleDisplay.getContent();
107         }
108         else {
109             return null;
110         }
111     }
112 
113     public JournalArticleDisplay getDisplay(
114         long groupId, String articleId, String viewMode, String languageId,
115         String xmlRequest) {
116 
117         return getDisplay(
118             groupId, articleId, null, viewMode, languageId, null, 1,
119             xmlRequest);
120     }
121 
122     public JournalArticleDisplay getDisplay(
123         long groupId, String articleId, String viewMode, String languageId,
124         ThemeDisplay themeDisplay) {
125 
126         return getDisplay(
127             groupId, articleId, viewMode, languageId, themeDisplay, 1);
128     }
129 
130     public JournalArticleDisplay getDisplay(
131         long groupId, String articleId, String viewMode, String languageId,
132         ThemeDisplay themeDisplay, int page) {
133 
134         return getDisplay(
135             groupId, articleId, null, viewMode, languageId, themeDisplay, page,
136             null);
137     }
138 
139     public JournalArticleDisplay getDisplay(
140         long groupId, String articleId, String templateId, String viewMode,
141         String languageId, String xmlRequest) {
142 
143         return getDisplay(
144             groupId, articleId, templateId, viewMode, languageId, null, 1,
145             xmlRequest);
146     }
147 
148     public JournalArticleDisplay getDisplay(
149         long groupId, String articleId, String templateId, String viewMode,
150         String languageId, ThemeDisplay themeDisplay) {
151 
152         return getDisplay(
153             groupId, articleId, templateId, viewMode, languageId, themeDisplay,
154             1, null);
155     }
156 
157     public JournalArticleDisplay getDisplay(
158         long groupId, String articleId, String templateId, String viewMode,
159         String languageId, ThemeDisplay themeDisplay, int page,
160         String xmlRequest) {
161 
162         StopWatch stopWatch = null;
163 
164         if (_log.isDebugEnabled()) {
165             stopWatch = new StopWatch();
166 
167             stopWatch.start();
168         }
169 
170         articleId = GetterUtil.getString(articleId).toUpperCase();
171         templateId = GetterUtil.getString(templateId).toUpperCase();
172 
173         boolean secure = false;
174 
175         if (themeDisplay != null) {
176             secure = themeDisplay.isSecure();
177         }
178 
179         String key = encodeKey(
180             groupId, articleId, templateId, viewMode, languageId, page, secure);
181 
182         JournalArticleDisplay articleDisplay = (JournalArticleDisplay)cache.get(
183             key);
184 
185         boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
186 
187         if ((articleDisplay == null) || !lifecycleRender) {
188             articleDisplay = getArticleDisplay(
189                 groupId, articleId, templateId, viewMode, languageId, page,
190                 xmlRequest, themeDisplay);
191 
192             if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
193                 (lifecycleRender)) {
194 
195                 cache.put(key, articleDisplay);
196             }
197         }
198 
199         try {
200             if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
201                 (articleDisplay != null) && (themeDisplay != null) &&
202                 (!JournalArticlePermission.contains(
203                     themeDisplay.getPermissionChecker(), groupId, articleId,
204                     ActionKeys.VIEW))) {
205 
206                 articleDisplay = null;
207             }
208         }
209         catch (Exception e) {
210         }
211 
212         if (_log.isDebugEnabled()) {
213             _log.debug(
214                 "getDisplay for {" + groupId + ", " + articleId + ", " +
215                     templateId + ", " + viewMode + ", " + languageId + ", " +
216                         page + "} takes " + stopWatch.getTime() + " ms");
217         }
218 
219         return articleDisplay;
220     }
221 
222     protected String encodeKey(
223         long groupId, String articleId, String templateId, String viewMode,
224         String languageId, int page, boolean secure) {
225 
226         StringBuilder sb = new StringBuilder();
227 
228         sb.append(CACHE_NAME);
229         sb.append(StringPool.POUND);
230         sb.append(groupId);
231         sb.append(ARTICLE_SEPARATOR);
232         sb.append(articleId);
233         sb.append(TEMPLATE_SEPARATOR);
234         sb.append(templateId);
235 
236         if (Validator.isNotNull(viewMode)) {
237             sb.append(VIEW_MODE_SEPARATOR);
238             sb.append(viewMode);
239         }
240 
241         if (Validator.isNotNull(languageId)) {
242             sb.append(LANGUAGE_SEPARATOR);
243             sb.append(languageId);
244         }
245 
246         if (page > 0) {
247             sb.append(PAGE_SEPARATOR);
248             sb.append(page);
249         }
250 
251         sb.append(SECURE_SEPARATOR);
252         sb.append(secure);
253 
254         return sb.toString();
255     }
256 
257     protected JournalArticleDisplay getArticleDisplay(
258         long groupId, String articleId, String templateId, String viewMode,
259         String languageId, int page, String xmlRequest,
260         ThemeDisplay themeDisplay) {
261 
262         try {
263             if (_log.isInfoEnabled()) {
264                 _log.info(
265                     "Get article display {" + groupId + ", " + articleId +
266                         ", " + templateId + "}");
267             }
268 
269             return JournalArticleLocalServiceUtil.getArticleDisplay(
270                 groupId, articleId, templateId, viewMode, languageId, page,
271                 xmlRequest, themeDisplay);
272         }
273         catch (Exception e) {
274             if (_log.isWarnEnabled()) {
275                 _log.warn(
276                     "Unable to get display for " + groupId + " " +
277                         articleId + " " + languageId);
278             }
279 
280             return null;
281         }
282     }
283 
284     protected boolean isLifecycleRender(
285         ThemeDisplay themeDisplay, String xmlRequest) {
286 
287         if (themeDisplay != null) {
288             return themeDisplay.isLifecycleRender();
289         }
290         else if (Validator.isNotNull(xmlRequest)) {
291             Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
292 
293             return matcher.find();
294         }
295         else {
296             return false;
297         }
298     }
299 
300     protected static PortalCache cache = MultiVMPoolUtil.getCache(CACHE_NAME);
301 
302     protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
303         "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
304 
305     private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
306 
307 }