001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.repository.model.FileEntry;
019    import com.liferay.portal.kernel.repository.model.FileVersion;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.DiffResult;
022    import com.liferay.portal.kernel.util.DiffUtil;
023    import com.liferay.portal.kernel.util.HtmlUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
031    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
032    import com.liferay.portlet.documentlibrary.util.DLUtil;
033    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
034    
035    import java.io.FileInputStream;
036    import java.io.InputStream;
037    import java.io.InputStreamReader;
038    
039    import java.util.List;
040    
041    import javax.portlet.PortletConfig;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Bruno Farache
051     */
052    public class CompareVersionsAction extends PortletAction {
053    
054            @Override
055            public ActionForward render(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, RenderRequest renderRequest,
058                            RenderResponse renderResponse)
059                    throws Exception {
060    
061                    try {
062                            compareVersions(renderRequest);
063                    }
064                    catch (Exception e) {
065                            if (e instanceof NoSuchFileEntryException ||
066                                    e instanceof PrincipalException) {
067    
068                                    SessionErrors.add(renderRequest, e.getClass());
069    
070                                    setForward(renderRequest, "portlet.document_library.error");
071                            }
072                            else {
073                                    throw e;
074                            }
075                    }
076    
077                    return actionMapping.findForward(
078                            "portlet.document_library.compare_versions");
079            }
080    
081            protected void compareVersions(RenderRequest renderRequest)
082                    throws Exception {
083    
084                    long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");
085    
086                    String sourceVersion = ParamUtil.getString(
087                            renderRequest, "sourceVersion");
088                    String targetVersion = ParamUtil.getString(
089                            renderRequest, "targetVersion");
090    
091                    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);
092    
093                    FileVersion sourceFileVersion = fileEntry.getFileVersion(sourceVersion);
094    
095                    InputStream sourceIs = sourceFileVersion.getContentStream(false);
096    
097                    String sourceExtension = sourceFileVersion.getExtension();
098    
099                    if (sourceExtension.equals("css") || sourceExtension.equals("htm") ||
100                            sourceExtension.equals("html") || sourceExtension.equals("js") ||
101                            sourceExtension.equals("txt") || sourceExtension.equals("xml")) {
102    
103                            String sourceContent = HtmlUtil.escape(StringUtil.read(sourceIs));
104    
105                            sourceIs = new UnsyncByteArrayInputStream(
106                                    sourceContent.getBytes(StringPool.UTF8));
107                    }
108    
109                    FileVersion targetFileVersion = fileEntry.getFileVersion(targetVersion);
110    
111                    InputStream targetIs = targetFileVersion.getContentStream(false);
112    
113                    String targetExtension = targetFileVersion.getExtension();
114    
115                    if (targetExtension.equals("css") || targetExtension.equals("htm") ||
116                            targetExtension.equals("html") || targetExtension.equals("js") ||
117                            targetExtension.equals("txt") || targetExtension.equals("xml")) {
118    
119                            String targetContent = HtmlUtil.escape(StringUtil.read(targetIs));
120    
121                            targetIs = new UnsyncByteArrayInputStream(
122                                    targetContent.getBytes(StringPool.UTF8));
123                    }
124    
125                    if (DocumentConversionUtil.isEnabled()) {
126                            if (DocumentConversionUtil.isConvertBeforeCompare(
127                                            sourceExtension)) {
128    
129                                    String sourceTempFileId = DLUtil.getTempFileId(
130                                            fileEntryId, sourceVersion);
131    
132                                    sourceIs = new FileInputStream(
133                                            DocumentConversionUtil.convert(
134                                                    sourceTempFileId, sourceIs, sourceExtension, "txt"));
135                            }
136    
137                            if (DocumentConversionUtil.isConvertBeforeCompare(
138                                            targetExtension)) {
139    
140                                    String targetTempFileId = DLUtil.getTempFileId(
141                                            fileEntryId, targetVersion);
142    
143                                    targetIs = new FileInputStream(
144                                            DocumentConversionUtil.convert(
145                                                    targetTempFileId, targetIs, targetExtension, "txt"));
146                            }
147                    }
148    
149                    List<DiffResult>[] diffResults = DiffUtil.diff(
150                            new InputStreamReader(sourceIs), new InputStreamReader(targetIs));
151    
152                    renderRequest.setAttribute(
153                            WebKeys.SOURCE_NAME,
154                            sourceFileVersion.getTitle() + StringPool.SPACE + sourceVersion);
155                    renderRequest.setAttribute(
156                            WebKeys.TARGET_NAME,
157                            targetFileVersion.getTitle() + StringPool.SPACE + targetVersion);
158                    renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
159            }
160    
161    }