1   /**
2    * Copyright (c) 2000-2008 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.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.servlet.ImageServletTokenUtil;
27  import com.liferay.portal.kernel.util.Constants;
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.User;
32  import com.liferay.portal.service.impl.ImageLocalUtil;
33  import com.liferay.portal.struts.ActionConstants;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.journal.model.JournalArticle;
38  import com.liferay.portlet.journal.model.JournalTemplate;
39  import com.liferay.portlet.journal.model.impl.JournalArticleImpl;
40  import com.liferay.portlet.journal.service.JournalArticleImageLocalServiceUtil;
41  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
42  import com.liferay.portlet.journal.service.JournalTemplateLocalServiceUtil;
43  import com.liferay.portlet.journal.util.JournalUtil;
44  import com.liferay.util.FileUtil;
45  import com.liferay.util.PwdGenerator;
46  import com.liferay.util.servlet.UploadServletRequest;
47  
48  import java.io.File;
49  import java.io.StringReader;
50  
51  import java.util.Date;
52  import java.util.Iterator;
53  import java.util.Map;
54  
55  import javax.servlet.http.HttpServletRequest;
56  import javax.servlet.http.HttpServletResponse;
57  import javax.servlet.jsp.PageContext;
58  
59  import org.apache.commons.logging.Log;
60  import org.apache.commons.logging.LogFactory;
61  import org.apache.struts.action.Action;
62  import org.apache.struts.action.ActionForm;
63  import org.apache.struts.action.ActionForward;
64  import org.apache.struts.action.ActionMapping;
65  
66  import org.dom4j.Document;
67  import org.dom4j.Element;
68  import org.dom4j.io.SAXReader;
69  
70  /**
71   * <a href="ViewArticleContentAction.java.html"><b><i>View Source</i></b></a>
72   *
73   * @author Brian Wing Shun Chan
74   * @author Raymond Augé
75   *
76   */
77  public class ViewArticleContentAction extends Action {
78  
79      public ActionForward execute(
80              ActionMapping mapping, ActionForm form, HttpServletRequest req,
81              HttpServletResponse res)
82          throws Exception {
83  
84          UploadServletRequest uploadReq = null;
85  
86          try {
87              String cmd = ParamUtil.getString(req, Constants.CMD);
88  
89              ThemeDisplay themeDisplay =
90                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
91  
92              long groupId = ParamUtil.getLong(req, "groupId");
93              String articleId = ParamUtil.getString(req, "articleId");
94              double version = ParamUtil.getDouble(
95                  req, "version", JournalArticleImpl.DEFAULT_VERSION);
96  
97              String languageId = LanguageUtil.getLanguageId(req);
98  
99              String output = null;
100 
101             if (cmd.equals(Constants.PREVIEW)) {
102                 uploadReq = new UploadServletRequest(req);
103 
104                 String title = ParamUtil.getString(uploadReq, "title");
105                 String description = ParamUtil.getString(
106                     uploadReq, "description");
107 
108                 Date now = new Date();
109 
110                 Date createDate = now;
111                 Date modifiedDate = now;
112                 Date displayDate = now;
113 
114                 User user = PortalUtil.getUser(uploadReq);
115 
116                 String xml = ParamUtil.getString(uploadReq, "xml");
117 
118                 SAXReader reader = new SAXReader();
119 
120                 Document doc = reader.read(new StringReader(xml));
121 
122                 Element root = doc.getRootElement();
123 
124                 String previewArticleId =
125                     "PREVIEW_" +
126                     PwdGenerator.getPassword(PwdGenerator.KEY3, 10);
127 
128                 format(
129                     groupId, articleId, version, previewArticleId, root,
130                     uploadReq);
131 
132                 Map<String, String> tokens = JournalUtil.getTokens(
133                     groupId, themeDisplay);
134 
135                 tokens.put("article_resource_pk", "-1");
136 
137                 JournalArticle article = new JournalArticleImpl();
138 
139                 article.setUserId(user.getUserId());
140                 article.setCreateDate(createDate);
141                 article.setModifiedDate(modifiedDate);
142                 article.setArticleId(articleId);
143                 article.setVersion(version);
144                 article.setTitle(title);
145                 article.setDescription(description);
146                 article.setDisplayDate(displayDate);
147 
148                 JournalUtil.addAllReservedEls(root, tokens, article);
149 
150                 xml = JournalUtil.formatXML(doc);
151 
152                 String templateId = ParamUtil.getString(
153                     uploadReq, "templateId");
154 
155                 JournalTemplate template =
156                     JournalTemplateLocalServiceUtil.getTemplate(
157                         groupId, templateId);
158 
159                 String langType = template.getLangType();
160                 String script = template.getXsl();
161 
162                 try {
163                     output = JournalUtil.transform(
164                         tokens, languageId, xml, script, langType);
165                 }
166                 catch (Exception e1) {
167                     _log.error(e1, e1);
168 
169                     PortalUtil.sendError(
170                         HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e1, req,
171                         res);
172                 }
173             }
174             else {
175                 output = JournalArticleServiceUtil.getArticleContent(
176                     groupId, articleId, version, languageId, themeDisplay);
177             }
178 
179             req.setAttribute(WebKeys.JOURNAL_ARTICLE_CONTENT, output);
180 
181             if (output.startsWith("<?xml ")) {
182                 return mapping.findForward(
183                     "portlet.journal.raw_article_content");
184             }
185             else {
186                 return mapping.findForward(
187                     "portlet.journal.view_article_content");
188             }
189         }
190         catch (Exception e2) {
191             req.setAttribute(PageContext.EXCEPTION, e2);
192 
193             return mapping.findForward(ActionConstants.COMMON_ERROR);
194         }
195         finally {
196             if (uploadReq != null) {
197                 uploadReq.cleanUp();
198             }
199         }
200     }
201 
202     protected void format(
203             long groupId, String articleId, double version,
204             String previewArticleId, Element root, UploadServletRequest req)
205         throws Exception {
206 
207         Iterator<Element> itr = root.elements().iterator();
208 
209         while (itr.hasNext()) {
210             Element el = itr.next();
211 
212             Element dynamicContent = el.element("dynamic-content");
213 
214             String elName = el.attributeValue("name", StringPool.BLANK);
215             String elType = el.attributeValue("type", StringPool.BLANK);
216             String elContent = StringPool.BLANK;
217             String elLanguage = StringPool.BLANK;
218 
219             if (dynamicContent != null) {
220                 elContent = dynamicContent.getTextTrim();
221 
222                 elLanguage = dynamicContent.attributeValue(
223                     "language-id", StringPool.BLANK);
224 
225                 if (!elLanguage.equals(StringPool.BLANK)) {
226                     elLanguage = "_" + elLanguage;
227                 }
228             }
229 
230             if (elType.equals("image") && Validator.isNull(elContent)) {
231                 File file = req.getFile(
232                     "structure_image_" + elName + elLanguage);
233                 byte[] bytes = FileUtil.getBytes(file);
234 
235                 if ((bytes != null) && (bytes.length > 0)) {
236                     long imageId =
237                         JournalArticleImageLocalServiceUtil.getArticleImageId(
238                             groupId, previewArticleId, version, elName,
239                             elLanguage, true);
240 
241                     dynamicContent.setText(
242                         "/image/journal/article?img_id=" + imageId + "&t=" +
243                             ImageServletTokenUtil.getToken(imageId));
244 
245                     ImageLocalUtil.updateImage(imageId, bytes);
246                 }
247                 else {
248                     if (Validator.isNotNull(articleId)) {
249                         long imageId = JournalArticleImageLocalServiceUtil.
250                             getArticleImageId(
251                                 groupId, articleId, version, elName,
252                                 elLanguage);
253 
254                         dynamicContent.setText(
255                             "/image/journal/article?img_id=" + imageId + "&t=" +
256                                 ImageServletTokenUtil.getToken(imageId));
257                     }
258                 }
259             }
260 
261             format(
262                 groupId, articleId, version, previewArticleId, el, req);
263         }
264     }
265 
266     private static Log _log = LogFactory.getLog(ViewArticleContentAction.class);
267 
268 }