1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.util;
21  
22  import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
23  import com.artofsolving.jodconverter.DocumentConverter;
24  import com.artofsolving.jodconverter.DocumentFormat;
25  import com.artofsolving.jodconverter.DocumentFormatRegistry;
26  import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
27  import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
28  import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
29  import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
30  
31  import com.liferay.portal.SystemException;
32  import com.liferay.portal.kernel.util.ArrayUtil;
33  import com.liferay.portal.kernel.util.FileUtil;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.util.PrefsPropsUtil;
37  import com.liferay.portal.util.PropsKeys;
38  import com.liferay.portal.util.PropsValues;
39  import com.liferay.util.SystemProperties;
40  
41  import java.io.ByteArrayOutputStream;
42  import java.io.File;
43  import java.io.FileInputStream;
44  import java.io.IOException;
45  import java.io.InputStream;
46  
47  import java.util.ArrayList;
48  import java.util.HashMap;
49  import java.util.List;
50  import java.util.Map;
51  
52  /**
53   * <a href="DocumentConversionUtil.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Bruno Farache
56   *
57   */
58  public class DocumentConversionUtil {
59  
60      public static InputStream convert(
61              String id, InputStream is, String sourceExtension,
62              String targetExtension)
63          throws IOException, SystemException {
64  
65          return _instance._convert(id, is, sourceExtension, targetExtension);
66      }
67  
68      public static void disconnect() {
69          _instance._disconnect();
70      }
71  
72      public static String[] getConversions(String extension) {
73          return _instance._getConversions(extension);
74      }
75  
76      public static String getTempFileId(long id, double version) {
77          StringBuilder sb = new StringBuilder();
78  
79          sb.append(id);
80          sb.append(StringPool.PERIOD);
81          sb.append(version);
82  
83          return sb.toString();
84      }
85  
86      private DocumentConversionUtil() {
87          _conversionsMap.put("svg", _DRAWING_CONVERSIONS);
88          _conversionsMap.put("swf", _DRAWING_CONVERSIONS);
89  
90          _conversionsMap.put("odp", _PRESENTATION_CONVERSIONS);
91          _conversionsMap.put("ppt", _PRESENTATION_CONVERSIONS);
92          _conversionsMap.put("sxi", _PRESENTATION_CONVERSIONS);
93  
94          _conversionsMap.put("csv", _SPREADSHEET_CONVERSIONS);
95          _conversionsMap.put("ods", _SPREADSHEET_CONVERSIONS);
96          _conversionsMap.put("sxc", _SPREADSHEET_CONVERSIONS);
97          _conversionsMap.put("tsv", _SPREADSHEET_CONVERSIONS);
98          _conversionsMap.put("xls", _SPREADSHEET_CONVERSIONS);
99  
100         _conversionsMap.put("doc", _TEXT_CONVERSIONS);
101         _conversionsMap.put("htm", _TEXT_CONVERSIONS);
102         _conversionsMap.put("html", _TEXT_CONVERSIONS);
103         _conversionsMap.put("odt", _TEXT_CONVERSIONS);
104         _conversionsMap.put("rtf", _TEXT_CONVERSIONS);
105         _conversionsMap.put("sxw", _TEXT_CONVERSIONS);
106         _conversionsMap.put("txt", _TEXT_CONVERSIONS);
107         _conversionsMap.put("wpd", _TEXT_CONVERSIONS);
108     }
109 
110     private InputStream _convert(
111             String id, InputStream is, String sourceExtension,
112             String targetExtension)
113         throws IOException, SystemException {
114 
115         if (!PrefsPropsUtil.getBoolean(
116                 PropsKeys.OPENOFFICE_SERVER_ENABLED,
117                 PropsValues.OPENOFFICE_SERVER_ENABLED)) {
118 
119             return null;
120         }
121 
122         StringBuilder sb = new StringBuilder();
123 
124         sb.append(SystemProperties.get(SystemProperties.TMP_DIR));
125         sb.append("/liferay/document_conversion/");
126         sb.append(id);
127         sb.append(StringPool.PERIOD);
128         sb.append(targetExtension);
129 
130         String fileName = sb.toString();
131 
132         File file = new File(fileName);
133 
134         if (!PropsValues.OPENOFFICE_CACHE_ENABLED || !file.exists()) {
135             DocumentFormatRegistry registry =
136                 new DefaultDocumentFormatRegistry();
137 
138             DocumentConverter converter = _getConverter(registry);
139 
140             if (sourceExtension.equals("htm")) {
141                 sourceExtension = "html";
142             }
143 
144             DocumentFormat inputFormat = registry.getFormatByFileExtension(
145                 sourceExtension);
146 
147             ByteArrayOutputStream baos = new ByteArrayOutputStream();
148 
149             DocumentFormat outputFormat = registry.getFormatByFileExtension(
150                 targetExtension);
151 
152             converter.convert(is, inputFormat, baos, outputFormat);
153 
154             FileUtil.write(file, baos.toByteArray());
155         }
156 
157         return new FileInputStream(file);
158     }
159 
160     private void _disconnect() {
161         if (_connection != null) {
162             _connection.disconnect();
163         }
164     }
165 
166     private String[] _getConversions(String extension) {
167         String[] conversions = _conversionsMap.get(extension);
168 
169         if (conversions == null) {
170             conversions = _DEFAULT_CONVERSIONS;
171         }
172         else {
173             if (ArrayUtil.contains(conversions, extension)) {
174                 List<String> list = new ArrayList<String>();
175 
176                 for (int i = 0; i < conversions.length; i++) {
177                     String conversion = conversions[i];
178 
179                     if (!conversion.equals(extension)) {
180                         list.add(conversion);
181                     }
182                 }
183 
184                 conversions = list.toArray(new String[list.size()]);
185             }
186         }
187 
188         return conversions;
189     }
190 
191     private DocumentConverter _getConverter(DocumentFormatRegistry registry)
192         throws SystemException {
193 
194         if ((_connection == null) || (_converter == null)) {
195             String host = PrefsPropsUtil.getString(
196                 PropsKeys.OPENOFFICE_SERVER_HOST);
197             int port = PrefsPropsUtil.getInteger(
198                 PropsKeys.OPENOFFICE_SERVER_PORT,
199                 PropsValues.OPENOFFICE_SERVER_PORT);
200 
201             if (_isRemoteOpenOfficeHost(host)) {
202                 _connection = new SocketOpenOfficeConnection(host, port);
203                 _converter = new StreamOpenOfficeDocumentConverter(_connection);
204             }
205             else {
206                 _connection = new SocketOpenOfficeConnection(port);
207                 _converter = new OpenOfficeDocumentConverter(_connection);
208             }
209         }
210 
211         return _converter;
212     }
213 
214     private boolean _isRemoteOpenOfficeHost(String host) {
215         if (Validator.isNotNull(host) && !host.equals(_LOCALHOST_IP) &&
216             !host.startsWith(_LOCALHOST)) {
217 
218             return true;
219         }
220         else {
221             return false;
222         }
223     }
224 
225     private static final String[] _DEFAULT_CONVERSIONS = new String[0];
226 
227     private static final String[] _DRAWING_CONVERSIONS = new String[] {"odg"};
228 
229     private static final String _LOCALHOST = "localhost";
230 
231     private static final String _LOCALHOST_IP = "127.0.0.1";
232 
233     private static final String[] _PRESENTATION_CONVERSIONS = new String[] {
234         "odp", "pdf", "ppt", "swf", "sxi"
235     };
236 
237     private static final String[] _SPREADSHEET_CONVERSIONS = new String[] {
238         "csv", "ods", "pdf", "sxc", "tsv", "xls"
239     };
240 
241     private static final String[] _TEXT_CONVERSIONS = new String[] {
242         "doc", "odt", "pdf", "rtf", "sxw", "txt"
243     };
244 
245     private static DocumentConversionUtil _instance =
246         new DocumentConversionUtil();
247 
248     private Map<String, String[]> _conversionsMap =
249         new HashMap<String, String[]>();
250     private OpenOfficeConnection _connection;
251     private DocumentConverter _converter;
252 
253 }