1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43
44
45
46 public class FacesFileResolver implements FileResolver {
47
48
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 }