1
14
15 package com.liferay.portlet.journalcontent.util;
16
17 import com.liferay.portal.kernel.cache.MultiVMPoolUtil;
18 import com.liferay.portal.kernel.cache.PortalCache;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.GetterUtil;
22 import com.liferay.portal.kernel.util.StringBundler;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.kernel.util.Validator;
26 import com.liferay.portal.model.Layout;
27 import com.liferay.portal.model.LayoutSet;
28 import com.liferay.portal.security.permission.ActionKeys;
29 import com.liferay.portal.theme.ThemeDisplay;
30 import com.liferay.portal.util.PropsValues;
31 import com.liferay.portlet.journal.model.JournalArticleDisplay;
32 import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
33 import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
34
35 import java.util.regex.Matcher;
36 import java.util.regex.Pattern;
37
38 import org.apache.commons.lang.time.StopWatch;
39
40
47 public class JournalContentImpl implements JournalContent {
48
49 public void clearCache() {
50 portalCache.removeAll();
51 }
52
53 public void clearCache(
54 long groupId, String articleId, String templateId) {
55
56 clearCache();
57 }
58
59 public String getContent(
60 long groupId, String articleId, String viewMode, String languageId,
61 String xmlRequest) {
62
63 return getContent(
64 groupId, articleId, null, viewMode, languageId, null, xmlRequest);
65 }
66
67 public String getContent(
68 long groupId, String articleId, String viewMode, String languageId,
69 ThemeDisplay themeDisplay) {
70
71 return getContent(
72 groupId, articleId, null, viewMode, languageId, themeDisplay);
73 }
74
75 public String getContent(
76 long groupId, String articleId, String templateId, String viewMode,
77 String languageId, String xmlRequest) {
78
79 return getContent(
80 groupId, articleId, templateId, viewMode, languageId, null,
81 xmlRequest);
82 }
83
84 public String getContent(
85 long groupId, String articleId, String templateId, String viewMode,
86 String languageId, ThemeDisplay themeDisplay) {
87
88 return getContent(
89 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
90 null);
91 }
92
93 public String getContent(
94 long groupId, String articleId, String templateId, String viewMode,
95 String languageId, ThemeDisplay themeDisplay, String xmlRequest) {
96
97 JournalArticleDisplay articleDisplay = getDisplay(
98 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
99 1, xmlRequest);
100
101 if (articleDisplay != null) {
102 return articleDisplay.getContent();
103 }
104 else {
105 return null;
106 }
107 }
108
109 public JournalArticleDisplay getDisplay(
110 long groupId, String articleId, String viewMode, String languageId,
111 String xmlRequest) {
112
113 return getDisplay(
114 groupId, articleId, null, viewMode, languageId, null, 1,
115 xmlRequest);
116 }
117
118 public JournalArticleDisplay getDisplay(
119 long groupId, String articleId, String viewMode, String languageId,
120 ThemeDisplay themeDisplay) {
121
122 return getDisplay(
123 groupId, articleId, viewMode, languageId, themeDisplay, 1);
124 }
125
126 public JournalArticleDisplay getDisplay(
127 long groupId, String articleId, String viewMode, String languageId,
128 ThemeDisplay themeDisplay, int page) {
129
130 return getDisplay(
131 groupId, articleId, null, viewMode, languageId, themeDisplay, page,
132 null);
133 }
134
135 public JournalArticleDisplay getDisplay(
136 long groupId, String articleId, String templateId, String viewMode,
137 String languageId, String xmlRequest) {
138
139 return getDisplay(
140 groupId, articleId, templateId, viewMode, languageId, null, 1,
141 xmlRequest);
142 }
143
144 public JournalArticleDisplay getDisplay(
145 long groupId, String articleId, String templateId, String viewMode,
146 String languageId, ThemeDisplay themeDisplay) {
147
148 return getDisplay(
149 groupId, articleId, templateId, viewMode, languageId, themeDisplay,
150 1, null);
151 }
152
153 public JournalArticleDisplay getDisplay(
154 long groupId, String articleId, String templateId, String viewMode,
155 String languageId, ThemeDisplay themeDisplay, int page,
156 String xmlRequest) {
157
158 StopWatch stopWatch = null;
159
160 if (_log.isDebugEnabled()) {
161 stopWatch = new StopWatch();
162
163 stopWatch.start();
164 }
165
166 articleId = GetterUtil.getString(articleId).toUpperCase();
167 templateId = GetterUtil.getString(templateId).toUpperCase();
168
169 long layoutSetId = 0;
170 boolean secure = false;
171
172 if (themeDisplay != null) {
173 try {
174 Layout layout = themeDisplay.getLayout();
175
176 LayoutSet layoutSet = layout.getLayoutSet();
177
178 layoutSetId = layoutSet.getLayoutSetId();
179 }
180 catch (Exception e) {
181 }
182
183 secure = themeDisplay.isSecure();
184 }
185
186 String key = encodeKey(
187 groupId, articleId, templateId, layoutSetId, viewMode, languageId,
188 page, secure);
189
190 JournalArticleDisplay articleDisplay =
191 (JournalArticleDisplay)portalCache.get(key);
192
193 boolean lifecycleRender = isLifecycleRender(themeDisplay, xmlRequest);
194
195 if ((articleDisplay == null) || !lifecycleRender) {
196 articleDisplay = getArticleDisplay(
197 groupId, articleId, templateId, viewMode, languageId, page,
198 xmlRequest, themeDisplay);
199
200 if ((articleDisplay != null) && (articleDisplay.isCacheable()) &&
201 (lifecycleRender)) {
202
203 portalCache.put(key, articleDisplay);
204 }
205 }
206
207 try {
208 if ((PropsValues.JOURNAL_ARTICLE_VIEW_PERMISSION_CHECK_ENABLED) &&
209 (articleDisplay != null) && (themeDisplay != null) &&
210 (!JournalArticlePermission.contains(
211 themeDisplay.getPermissionChecker(), groupId, articleId,
212 ActionKeys.VIEW))) {
213
214 articleDisplay = null;
215 }
216 }
217 catch (Exception e) {
218 }
219
220 if (_log.isDebugEnabled()) {
221 _log.debug(
222 "getDisplay for {" + groupId + ", " + articleId + ", " +
223 templateId + ", " + viewMode + ", " + languageId + ", " +
224 page + "} takes " + stopWatch.getTime() + " ms");
225 }
226
227 return articleDisplay;
228 }
229
230 protected String encodeKey(
231 long groupId, String articleId, String templateId, long layoutSetId,
232 String viewMode, String languageId, int page, boolean secure) {
233
234 StringBundler sb = new StringBundler();
235
236 sb.append(CACHE_NAME);
237 sb.append(StringPool.POUND);
238 sb.append(StringUtil.toHexString(groupId));
239 sb.append(ARTICLE_SEPARATOR);
240 sb.append(articleId);
241 sb.append(TEMPLATE_SEPARATOR);
242 sb.append(templateId);
243
244 if (layoutSetId > 0) {
245 sb.append(LAYOUT_SET_SEPARATOR);
246 sb.append(StringUtil.toHexString(layoutSetId));
247 }
248
249 if (Validator.isNotNull(viewMode)) {
250 sb.append(VIEW_MODE_SEPARATOR);
251 sb.append(viewMode);
252 }
253
254 if (Validator.isNotNull(languageId)) {
255 sb.append(LANGUAGE_SEPARATOR);
256 sb.append(languageId);
257 }
258
259 if (page > 0) {
260 sb.append(PAGE_SEPARATOR);
261 sb.append(StringUtil.toHexString(page));
262 }
263
264 sb.append(SECURE_SEPARATOR);
265 sb.append(secure);
266
267 return sb.toString();
268 }
269
270 protected JournalArticleDisplay getArticleDisplay(
271 long groupId, String articleId, String templateId, String viewMode,
272 String languageId, int page, String xmlRequest,
273 ThemeDisplay themeDisplay) {
274
275 try {
276 if (_log.isInfoEnabled()) {
277 _log.info(
278 "Get article display {" + groupId + ", " + articleId +
279 ", " + templateId + "}");
280 }
281
282 return JournalArticleLocalServiceUtil.getArticleDisplay(
283 groupId, articleId, templateId, viewMode, languageId, page,
284 xmlRequest, themeDisplay);
285 }
286 catch (Exception e) {
287 if (_log.isWarnEnabled()) {
288 _log.warn(
289 "Unable to get display for " + groupId + " " +
290 articleId + " " + languageId);
291 }
292
293 return null;
294 }
295 }
296
297 protected boolean isLifecycleRender(
298 ThemeDisplay themeDisplay, String xmlRequest) {
299
300 if (themeDisplay != null) {
301 return themeDisplay.isLifecycleRender();
302 }
303 else if (Validator.isNotNull(xmlRequest)) {
304 Matcher matcher = lifecycleRenderPhasePatern.matcher(xmlRequest);
305
306 return matcher.find();
307 }
308 else {
309 return false;
310 }
311 }
312
313 protected static Pattern lifecycleRenderPhasePatern = Pattern.compile(
314 "<lifecycle>\\s*RENDER_PHASE\\s*</lifecycle>");
315 protected static PortalCache portalCache = MultiVMPoolUtil.getCache(
316 CACHE_NAME);
317
318 private static Log _log = LogFactoryUtil.getLog(JournalContentUtil.class);
319
320 }