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