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.resource;
20  
21  import java.io.File;
22  import java.net.MalformedURLException;
23  import java.net.URL;
24  import java.util.logging.Level;
25  import java.util.logging.Logger;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  
30  import net.sf.jasperreports.jsf.Constants;
31  import net.sf.jasperreports.jsf.component.UIReport;
32  import net.sf.jasperreports.jsf.context.ExternalContextHelper;
33  import net.sf.jasperreports.jsf.context.JRFacesContext;
34  import net.sf.jasperreports.jsf.util.Util;
35  
36  import static org.apache.commons.io.FilenameUtils.*;
37  
38  /**
39   *
40   * @author aalonsodominguez
41   */
42  public final class DefaultResourceResolver implements ResourceResolver {
43  
44  	private static final Logger logger = Logger.getLogger(
45  			DefaultResourceResolver.class.getPackage().getName(),
46  			Constants.LOG_MESSAGES_BUNDLE);
47  	
48      public Resource resolveResource(FacesContext context, UIComponent component,
49              String name) {
50          if (name == null || name.length() == 0) {
51              throw new IllegalArgumentException(
52                      "Resource name must be provided.");
53          }
54  
55          Resource resource = null;
56          try {
57              final URL url = new URL(name);
58              resource = new URLResource(url);
59          } catch (MalformedURLException e) {
60              final ClassLoader loader = Util.getClassLoader(this);
61              if (name.startsWith(ClasspathResource.PREFIX)) {
62                  resource = new ClasspathResource(name, loader);
63              } else if (name.startsWith("/")) {
64                  resource = new ContextResource(name);
65              } else if (loader.getResource(name) != null) {
66                  resource = new ClasspathResource(name, loader);
67              } else {
68                  resource = resolveRelative(context, component, name);
69              }
70          }
71  
72          // If at this point 'resource' is null then the resource couldn't
73          // be resolved by this ResourceResolver implementation
74          return resource;
75      }
76  
77      private Resource resolveRelative(FacesContext context, 
78              UIComponent component, String name) {
79      	if (logger.isLoggable(Level.FINE)) {
80      		logger.log(Level.FINE, "JRJSF_0039", name);
81      	}
82      	
83          Resource resource = null;
84          JRFacesContext jrContext = JRFacesContext.getInstance(context);
85          ExternalContextHelper helper =
86                  jrContext.getExternalContextHelper(context);
87          String rootPath = null;
88  
89          if ((component != null) && (component instanceof UIReport)) {
90              // If caller component is a report-based component then try to
91              // resolve the resource relative to the report resource (if given).
92  
93              Object value = ((UIReport) component).getValue();
94              if (value != null && (value instanceof String)) {
95              	String valueStr = (String) value;
96              	if (!valueStr.equals(name)) {
97              		// If the resource we are trying to resolve is the one
98              		// established in the report itself then resolve it
99              		// using the current viewRoot
100 	                rootPath = helper.getResourceRealPath(
101 	                    context.getExternalContext(),
102 	                    "/" + getPath((String) value));
103             	}
104             }
105         }
106         
107         if (rootPath == null) {
108             // If caller component is not a report-based component or
109             // there is not any component at all, resolve the resource
110             // name relative to the current view.
111 
112             String viewId = context.getViewRoot().getViewId();
113             rootPath = helper.getResourceRealPath(
114                     context.getExternalContext(), "/" + getPath(viewId));
115         }
116 
117         if (rootPath != null) {
118             String resourceFileName = rootPath + File.separator + name;
119             File resourceFile = new File(normalize(resourceFileName));
120             if (resourceFile.exists()) {
121                 resource = new FileResource(resourceFile);
122             }
123         }
124 
125         return resource;
126     }
127 
128 }