1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.action;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
19  import com.liferay.portal.kernel.util.DiffResult;
20  import com.liferay.portal.kernel.util.DiffUtil;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.HtmlUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.PropsKeys;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PrefsPropsUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
36  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
37  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
38  
39  import java.io.InputStream;
40  import java.io.InputStreamReader;
41  
42  import java.util.List;
43  
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Bruno Farache
56   */
57  public class CompareVersionsAction extends PortletAction {
58  
59      public ActionForward render(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              RenderRequest renderRequest, RenderResponse renderResponse)
62          throws Exception {
63  
64          try {
65              compareVersions(renderRequest);
66          }
67          catch (Exception e) {
68              if (e instanceof NoSuchFileEntryException ||
69                  e instanceof PrincipalException) {
70  
71                  SessionErrors.add(renderRequest, e.getClass().getName());
72  
73                  setForward(renderRequest, "portlet.document_library.error");
74              }
75              else {
76                  throw e;
77              }
78          }
79  
80          return mapping.findForward("portlet.document_library.compare_versions");
81      }
82  
83      protected void compareVersions(RenderRequest renderRequest)
84          throws Exception {
85  
86          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
87              WebKeys.THEME_DISPLAY);
88  
89          long companyId = themeDisplay.getCompanyId();
90          long userId = themeDisplay.getUserId();
91  
92          long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");
93  
94          long groupId = themeDisplay.getScopeGroupId();
95          long folderId = ParamUtil.getLong(renderRequest, "folderId");
96          String name = ParamUtil.getString(renderRequest, "name");
97  
98          DLFileEntryPermission.check(
99              themeDisplay.getPermissionChecker(), groupId, folderId, name,
100             ActionKeys.VIEW);
101 
102         String extension = FileUtil.getExtension(name);
103 
104         String titleWithExtension = ParamUtil.getString(
105             renderRequest, "titleWithExtension");
106 
107         String sourceVersion = ParamUtil.getString(
108             renderRequest, "sourceVersion");
109         String targetVersion = ParamUtil.getString(
110             renderRequest, "targetVersion");
111 
112         InputStream sourceIs = DLFileEntryLocalServiceUtil.getFileAsStream(
113             companyId, userId, groupId, folderId, name, sourceVersion);
114         InputStream targetIs = DLFileEntryLocalServiceUtil.getFileAsStream(
115             companyId, userId, groupId, folderId, name, targetVersion);
116 
117         if (extension.equals("htm") || extension.equals("html") ||
118             extension.equals("xml")) {
119 
120             String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
121             String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));
122 
123             sourceIs = new UnsyncByteArrayInputStream(
124                 escapedSource.getBytes(StringPool.UTF8));
125             targetIs = new UnsyncByteArrayInputStream(
126                 escapedTarget.getBytes(StringPool.UTF8));
127         }
128 
129         if (PrefsPropsUtil.getBoolean(
130                 PropsKeys.OPENOFFICE_SERVER_ENABLED,
131                 PropsValues.OPENOFFICE_SERVER_ENABLED) &&
132             isConvertBeforeCompare(extension)) {
133 
134             String sourceTempFileId = DocumentConversionUtil.getTempFileId(
135                 fileEntryId, sourceVersion);
136             String targetTempFileId = DocumentConversionUtil.getTempFileId(
137                 fileEntryId, targetVersion);
138 
139             sourceIs = DocumentConversionUtil.convert(
140                 sourceTempFileId, sourceIs, extension, "txt");
141             targetIs = DocumentConversionUtil.convert(
142                 targetTempFileId, targetIs, extension, "txt");
143         }
144 
145         List<DiffResult>[] diffResults = DiffUtil.diff(
146             new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
147 
148         renderRequest.setAttribute(
149             WebKeys.SOURCE_NAME,
150             titleWithExtension + StringPool.SPACE + sourceVersion);
151         renderRequest.setAttribute(
152             WebKeys.TARGET_NAME,
153             titleWithExtension + StringPool.SPACE + targetVersion);
154         renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
155     }
156 
157     protected boolean isConvertBeforeCompare(String extension) {
158         if (extension.equals("txt")) {
159             return false;
160         }
161 
162         String[] conversions = DocumentConversionUtil.getConversions(extension);
163 
164         for (int i = 0; i < conversions.length; i++) {
165             if (conversions[i].equals("txt")) {
166                 return true;
167             }
168         }
169 
170         return false;
171     }
172 
173 }