View Javadoc
1   /*
2    * JaspertReports JSF Plugin Copyright (C) 2011 A. Alonso Dominguez
3    *
4    * This library is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as published by
6    * the Free Software Foundation; either version 2.1 of the License, or (at
7    * your option) any later version. This library is distributed in the hope
8    * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9    * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   *
11   * See the GNU Lesser General Public License for more details. You should have
12   * received a copy of the GNU Lesser General Public License along with this
13   * library; if not, write to the Free Software Foundation, Inc., 59 Temple
14   * Place, Suite 330, Boston, MA 02111-1307 USA A.
15   *
16   * Alonso Dominguez
17   * alonsoft@users.sf.net
18   */
19  package net.sf.jasperreports.jsf.engine.interop;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.net.URL;
27  import java.util.logging.Level;
28  import java.util.logging.Logger;
29  
30  import javax.faces.context.FacesContext;
31  
32  import net.sf.jasperreports.engine.util.FileResolver;
33  import net.sf.jasperreports.jsf.Constants;
34  import net.sf.jasperreports.jsf.JRFacesException;
35  import net.sf.jasperreports.jsf.component.UIReport;
36  import net.sf.jasperreports.jsf.context.ExternalContextHelper;
37  import net.sf.jasperreports.jsf.context.JRFacesContext;
38  import net.sf.jasperreports.jsf.resource.Resource;
39  
40  /**
41   * Integration of the JasperReports' <tt>FileResolver</tt>
42   * with the plugin's resource resolving mechanism.
43   * 
44   * @author A. Alonso Dominguez
45   */
46  public class FacesFileResolver implements FileResolver {
47  
48  	/** The logger instance. */
49      private static final Logger logger = Logger.getLogger(
50              FacesFileResolver.class.getPackage().getName(),
51              Constants.LOG_MESSAGES_BUNDLE);
52  
53      private static final int BUFFER_SIZE = 2048;
54      
55      private final UIReport report;
56  
57      public FacesFileResolver(final UIReport report) {
58          super();
59          if (report == null) {
60              throw new IllegalArgumentException("'context' can't be null");
61          }
62          this.report = report;
63      }
64  
65      public File resolveFile(final String name) {
66          File resultFile;
67  
68          Resource resource;
69          try {
70              resource = resolveResource(name);
71              if (isRemote(resource)) {
72                  resultFile = downloadResource(resource);
73              } else {
74                  resultFile = new File(resource.getName());
75              }
76          } catch (final IOException e) {
77              throw new JRFacesException(e);
78          }
79          
80          if (logger.isLoggable(Level.FINE)) {
81          	logger.log(Level.FINE, "JRJSF_0038", new Object[]{ 
82          			resultFile.getAbsolutePath(),
83          			report.getClientId(getFacesContext())
84          	});
85          }
86  
87          return resultFile;
88      }
89  
90      protected Resource resolveResource(String name) throws IOException {
91          return getJRFacesContext().createResource(
92                  getFacesContext(), report, name);
93      }
94  
95      protected File downloadResource(Resource resource) throws IOException {
96      	File tempFile = File.createTempFile(resource.getSimpleName(), null);
97          if (logger.isLoggable(Level.FINE)) {
98              logger.log(Level.FINE, "JRJSF_0035", new Object[]{
99                  resource.getLocation(), tempFile
100             });
101         }
102 
103         tempFile.createNewFile();
104         
105         InputStream is = resource.getInputStream();
106         OutputStream os = new FileOutputStream(tempFile);
107         
108         try {
109             int read;
110             byte[] buff = new byte[BUFFER_SIZE];
111             while (0 < (read = is.read(buff))) {
112                 os.write(buff, 0, read);
113             }
114         } finally {
115             try {
116                 is.close();
117                 is = null;
118             } catch (IOException e) { ; }
119             try {
120                 os.close();
121                 os = null;
122             } catch (IOException e) { ; }
123         }
124 
125         return tempFile;
126     }
127 
128     protected FacesContext getFacesContext() {
129         return FacesContext.getCurrentInstance();
130     }
131 
132     protected JRFacesContext getJRFacesContext() {
133         return JRFacesContext.getInstance(getFacesContext());
134     }
135 
136     protected boolean isRemote(Resource resource) throws IOException {
137         URL resourceURL = resource.getLocation();
138         if (!"file".equals(resourceURL.getProtocol())) {
139             ExternalContextHelper helper = getJRFacesContext()
140                     .getExternalContextHelper(getFacesContext());
141             return !(resourceURL.getHost().equals(helper.getRequestServerName(
142                     getFacesContext().getExternalContext())));
143         }
144         return false;
145     }
146     
147 }