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.documentlibrary.action;
24  
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.security.permission.ActionKeys;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PrefsPropsUtil;
34  import com.liferay.portal.util.PropsUtil;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
38  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
39  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
40  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
41  import com.liferay.util.FileUtil;
42  import com.liferay.util.diff.DiffResult;
43  import com.liferay.util.diff.DiffUtil;
44  import com.liferay.util.servlet.SessionErrors;
45  
46  import java.io.ByteArrayInputStream;
47  import java.io.InputStream;
48  import java.io.InputStreamReader;
49  
50  import java.util.List;
51  
52  import javax.portlet.PortletConfig;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Bruno Farache
64   *
65   */
66  public class CompareVersionsAction extends PortletAction {
67  
68      public ActionForward render(
69              ActionMapping mapping, ActionForm form, PortletConfig config,
70              RenderRequest req, RenderResponse res)
71          throws Exception {
72  
73          try {
74              compareVersions(req);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchFileEntryException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(req, e.getClass().getName());
81  
82                  setForward(req, "portlet.document_library.error");
83              }
84              else {
85                  throw e;
86              }
87          }
88  
89          return mapping.findForward("portlet.document_library.compare_versions");
90      }
91  
92      protected void compareVersions(RenderRequest req) throws Exception {
93          ThemeDisplay themeDisplay =
94              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
95  
96          long companyId = themeDisplay.getCompanyId();
97          long userId = themeDisplay.getUserId();
98  
99          long fileEntryId = ParamUtil.getLong(req, "fileEntryId");
100 
101         long folderId = ParamUtil.getLong(req, "folderId");
102         String name = ParamUtil.getString(req, "name");
103 
104         DLFileEntryPermission.check(
105             themeDisplay.getPermissionChecker(), folderId, name,
106             ActionKeys.VIEW);
107 
108         String extension = FileUtil.getExtension(name);
109 
110         String titleWithExtension = ParamUtil.getString(
111             req, "titleWithExtension");
112 
113         double sourceVersion = ParamUtil.getDouble(req, "sourceVersion");
114         double targetVersion = ParamUtil.getDouble(req, "targetVersion");
115 
116         InputStream sourceIs = DLFileEntryLocalServiceUtil.getFileAsStream(
117             companyId, userId, folderId, name, sourceVersion);
118         InputStream targetIs = DLFileEntryLocalServiceUtil.getFileAsStream(
119             companyId, userId, folderId, name, targetVersion);
120 
121         if (extension.equals("htm") || extension.equals("html") ||
122             extension.equals("xml")) {
123 
124             String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
125             String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));
126 
127             sourceIs = new ByteArrayInputStream(
128                 escapedSource.getBytes(StringPool.UTF8));
129             targetIs = new ByteArrayInputStream(
130                 escapedTarget.getBytes(StringPool.UTF8));
131         }
132 
133         if (PrefsPropsUtil.getBoolean(
134                 PropsUtil.OPENOFFICE_SERVER_ENABLED,
135                 PropsValues.OPENOFFICE_SERVER_ENABLED) &&
136             isConvertBeforeCompare(extension)) {
137 
138             String sourceTempFileId = DocumentConversionUtil.getTempFileId(
139                 fileEntryId, sourceVersion);
140             String targetTempFileId = DocumentConversionUtil.getTempFileId(
141                 fileEntryId, targetVersion);
142 
143             sourceIs = DocumentConversionUtil.convert(
144                 sourceTempFileId, sourceIs, extension, "txt");
145             targetIs = DocumentConversionUtil.convert(
146                 targetTempFileId, targetIs, extension, "txt");
147         }
148 
149         List<DiffResult>[] diffResults = DiffUtil.diff(
150             new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
151 
152         req.setAttribute(
153             WebKeys.SOURCE_NAME,
154             titleWithExtension + StringPool.SPACE + sourceVersion);
155         req.setAttribute(
156             WebKeys.TARGET_NAME,
157             titleWithExtension + StringPool.SPACE + targetVersion);
158         req.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
159     }
160 
161     protected boolean isConvertBeforeCompare(String extension) {
162         if (extension.equals("txt")) {
163             return false;
164         }
165 
166         String[] conversions = DocumentConversionUtil.getConversions(extension);
167 
168         for (int i = 0; i < conversions.length; i++) {
169             if (conversions[i].equals("txt")) {
170                 return true;
171             }
172         }
173 
174         return false;
175     }
176 
177 }