1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.repo;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.OutputStream;
30 import java.net.URL;
31 import java.net.URLStreamHandlerFactory;
32 import java.nio.file.Path;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import net.sf.jasperreports.engine.JRException;
38 import net.sf.jasperreports.engine.JRPropertiesUtil;
39 import net.sf.jasperreports.engine.JRRuntimeException;
40 import net.sf.jasperreports.engine.JasperReportsContext;
41 import net.sf.jasperreports.engine.util.JRLoader;
42 import net.sf.jasperreports.engine.util.JRResourcesUtil;
43
44
45 /**
46  * @author Teodor Danciu (teodord@users.sourceforge.net)
47  */

48 public class DefaultRepositoryService implements StreamRepositoryService
49 {
50     
51     private static final Log log = LogFactory.getLog(DefaultRepositoryService.class);
52     
53     public static final String PROPERTY_FILES_ENABLED = 
54             JRPropertiesUtil.PROPERTY_PREFIX + "default.file.repository.enabled";
55     
56     public static final String EXCEPTION_MESSAGE_KEY_NOT_IMPLEMENTED = "repo.default.not.implemented";
57     
58     /**
59      * 
60      */

61     protected JasperReportsContext jasperReportsContext;
62     private boolean filesEnabled;
63
64     /**
65      * 
66      */

67     protected ClassLoader classLoader;
68     protected URLStreamHandlerFactory urlHandlerFactory;
69     /**
70      * @deprecated To be removed. 
71      */

72     protected net.sf.jasperreports.engine.util.FileResolver fileResolver;
73
74     /**
75      *
76      */

77     public DefaultRepositoryService(JasperReportsContext jasperReportsContext) 
78     {
79         this.jasperReportsContext = jasperReportsContext;
80         this.filesEnabled = JRPropertiesUtil.getInstance(jasperReportsContext).getBooleanProperty(
81                 PROPERTY_FILES_ENABLED, true);
82     }
83     
84     /**
85      *
86      */

87     public void setClassLoader(ClassLoader classLoader) 
88     {
89         this.classLoader = classLoader;
90     }
91     
92     /**
93      *
94      */

95     public void setURLStreamHandlerFactory(URLStreamHandlerFactory urlHandlerFactory) 
96     {
97         this.urlHandlerFactory = urlHandlerFactory;
98     }
99     
100     /**
101      * @deprecated To be removed.
102      */

103     public void setFileResolver(net.sf.jasperreports.engine.util.FileResolver fileResolver) 
104     {
105         this.fileResolver = fileResolver;
106     }
107     
108     @Override
109     public InputStream getInputStream(String uri)
110     {
111         return getInputStream(SimpleRepositoryContext.of(jasperReportsContext), uri);
112     }
113     
114     @Override
115     public InputStream getInputStream(RepositoryContext context, String uri)
116     {
117         try
118         {
119             URL url = JRResourcesUtil.createURL(uri, urlHandlerFactory);
120             if (url != null)
121             {
122                 return JRLoader.getInputStream(url);
123             }
124
125             File file = resolveFile(context, uri);
126             if (file != null)
127             {
128                 return JRLoader.getInputStream(file);
129             }
130
131             url = JRResourcesUtil.findClassLoaderResource(uri, classLoader);
132             if (url != null)
133             {
134                 return JRLoader.getInputStream(url);
135             }
136         }
137         catch (JRException e)
138         {
139             throw new JRRuntimeException(e);
140         }
141         
142         return null;
143     }
144
145     /**
146      * @deprecated To be removed.
147      */

148     protected File resolveFile(RepositoryContext context, String uri)
149     {
150         if (fileResolver != null)
151         {
152             return fileResolver.resolveFile(uri);
153         }
154         
155         if (filesEnabled)
156         {
157             return JRResourcesUtil.resolveFile(context, uri);
158         }
159         
160         return null;
161     }
162     
163     @Override
164     public OutputStream getOutputStream(String uri)
165     {
166         throw new UnsupportedOperationException();
167     }
168     
169     @Override
170     public Resource getResource(String uri)
171     {
172         throw 
173             new JRRuntimeException(
174                 EXCEPTION_MESSAGE_KEY_NOT_IMPLEMENTED,
175                 (Object[])null);//FIXMEREPO
176     }
177     
178     @Override
179     public void saveResource(String uri, Resource resource)
180     {
181         throw new UnsupportedOperationException();
182     }
183     
184     @Override
185     public <K extends Resource> K getResource(String uri, Class<K> resourceType)
186     {
187         return getResource(SimpleRepositoryContext.of(jasperReportsContext), uri, resourceType);
188     }
189     
190     @Override
191     public <K extends Resource> K getResource(RepositoryContext context, String uri, Class<K> resourceType)
192     {
193         PersistenceService persistenceService = PersistenceUtil.getInstance(jasperReportsContext).getService(DefaultRepositoryService.class, resourceType);
194         if (persistenceService != null)
195         {
196             return (K) persistenceService.load(context, uri, this);
197         }
198         return null;
199     }
200
201     @Override
202     public ResourceInfo getResourceInfo(RepositoryContext context, String location)
203     {
204         //detecting URLs
205         URL url = JRResourcesUtil.createURL(location, urlHandlerFactory);
206         if (url != null)
207         {
208             //not supporting paths relative to URLs
209             return null;
210         }
211
212         if (fileResolver != null)
213         {
214             //not dealing with file resolvers
215             return null;
216         }
217         
218         File file = resolveFile(context, location);
219         if (file != null)
220         {
221             try
222             {
223                 //resolving to real path to eliminate .. and .
224                 Path path = file.toPath().toRealPath();
225                 return StandardResourceInfo.from(path);
226             }
227             catch (IOException e)
228             {
229                 log.warn("Failed to resolve real path for file " + file, e);
230                 
231                 //using the paths as present in the File object
232                 return StandardResourceInfo.from(file);
233             }
234         }
235         
236         //TODO lucianc classloader resources
237         return null;
238     }
239
240
241 }
242