View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.services.cms.thumbnail.impl;
18  
19  import java.awt.image.BufferedImage;
20  import java.io.File;
21  import java.io.InputStream;
22  import java.util.List;
23  
24  import javax.imageio.ImageIO;
25  import javax.jcr.Node;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.commons.lang.CharSet;
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.tika.io.IOUtils;
31  import org.exoplatform.container.component.ComponentPlugin;
32  import org.exoplatform.container.xml.InitParams;
33  import org.exoplatform.services.cms.jodconverter.JodConverterService;
34  import org.exoplatform.services.cms.mimetype.DMSMimeTypeResolver;
35  import org.exoplatform.services.cms.thumbnail.ThumbnailPlugin;
36  import org.exoplatform.services.jcr.RepositoryService;
37  import org.exoplatform.services.log.ExoLogger;
38  import org.exoplatform.services.log.Log;
39  import org.exoplatform.services.pdfviewer.PDFViewerService;
40  import org.exoplatform.services.wcm.core.NodetypeConstant;
41  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
42  import org.icepdf.core.pobjects.Document;
43  import org.icepdf.core.pobjects.Page;
44  import org.icepdf.core.util.GraphicsRenderingHints;
45  
46  /**
47   * Created by The eXo Platform SARL
48   * Author : Dang Van Minh
49   *          minh.dang@exoplatform.com
50   * OCt 22, 2009
51   * 2:20:33 PM
52   */
53  public class OfficeDocumentThumbnailPlugin implements ComponentPlugin, ThumbnailPlugin {
54  
55    private ThumbnailType config;
56    private String description;
57    private String name;
58    private JodConverterService jodConverter_;
59  
60    private static final Log LOG = ExoLogger.getExoLogger(OfficeDocumentThumbnailPlugin.class.getName());
61  
62    public OfficeDocumentThumbnailPlugin(JodConverterService jodConverter, InitParams initParams) throws Exception {
63      config = initParams.getObjectParamValues(ThumbnailType.class).get(0);
64      this.jodConverter_ = jodConverter;
65    }
66  
67    public String getDescription() {
68      return description;
69    }
70  
71    public String getName() {
72      return name;
73    }
74  
75    public void setDescription(String description) {
76      this.description = description;
77    }
78  
79    public void setName(String name) {
80      this.name = name;
81    }
82  
83    public BufferedImage getBufferedImage(Node contentNode, String nodePath) throws Exception {
84      if(contentNode.isNodeType(NodetypeConstant.NT_RESOURCE)) contentNode = contentNode.getParent();
85      String extension = null;
86      if (contentNode.hasProperty("jcr:content/jcr:mimeType")) {
87        String mimeType = contentNode.getProperty("jcr:content/jcr:mimeType").getString();
88        extension = DMSMimeTypeResolver.getInstance().getExtension(mimeType);
89      } else if(contentNode.getName().contains(".")) {
90        String fileName = contentNode.getName();
91        extension = fileName.substring(fileName.lastIndexOf('.') + 1);
92      } else {
93        extension = ".officeDocument.tmp";
94      }
95      File in = File.createTempFile(name + "_tmp", "." + extension);
96      InputStream documentStream = contentNode.getProperty("jcr:content/jcr:data").getStream();
97      FileUtils.copyInputStreamToFile(documentStream, in);
98      File out = File.createTempFile(name + "_tmp", ".jpg");
99      boolean success = jodConverter_.convert(in, out,"jpg");
100     if (success) {
101       return ImageIO.read(out);
102     } else {
103       return null;
104     }
105   }
106 
107 
108   public List<String> getMimeTypes() {
109     return config.getMimeTypes();
110   }
111 
112 }