View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.context;
21  
22  import org.apache.myfaces.tobago.config.Configurable;
23  import org.apache.myfaces.tobago.internal.context.ResourceManagerFactory;
24  import org.apache.myfaces.tobago.layout.Measure;
25  
26  import javax.faces.context.FacesContext;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Locale;
30  
31  public final class ResourceManagerUtils {
32  
33    private ResourceManagerUtils() {
34      // no instance
35    }
36  
37    public static String getProperty(final FacesContext facesContext, final String bundle, final String key) {
38      return ResourceManagerFactory.getResourceManager(facesContext).getProperty(facesContext, bundle, key);
39    }
40  
41    public static String getPropertyNotNull(final FacesContext facesContext, final String bundle, final String key) {
42      final String result
43          = ResourceManagerFactory.getResourceManager(facesContext).getProperty(facesContext, bundle, key);
44      if (result == null) {
45        return "???" + key + "???";
46      } else {
47        return result;
48      }
49    }
50  
51    /**
52     * Searches for an image and return it with the context path
53     *
54     * @param facesContext the current FacesContext
55     * @param name the name of the image with extension
56     */
57    public static String getImageWithPath(final FacesContext facesContext, final String name) {
58      return getImageWithPath(facesContext, name, false);
59    }
60  
61    /**
62     * Searches for an image and return it with the context path
63     *
64     * @param facesContext the current FacesContext
65     * @param name the name of the image with extension
66     * @param ignoreMissing if set to false, an error message will be logged, when image is missing
67     */
68    public static String getImageWithPath(
69        final FacesContext facesContext, final String name, final boolean ignoreMissing) {
70      final String image
71          = ResourceManagerFactory.getResourceManager(facesContext).getImage(facesContext, name, ignoreMissing);
72      if (image == null) {
73        return null;
74      } else {
75        return facesContext.getExternalContext().getRequestContextPath() + image;
76      }
77    }
78  
79    /**
80     * Searches for an file and return it with the context path.
81     *
82     * @param facesContext the current FacesContext
83     * @param name the name of the file without extension
84     * @param extension the file extension
85     * @param ignoreMissing if set to false, an error message will be logged, when image is missing
86     */
87    public static String getFile(
88        final FacesContext facesContext, final String name, final String extension, final boolean ignoreMissing) {
89      final String image = ResourceManagerFactory.getResourceManager(facesContext)
90          .getImage(facesContext, name, extension, ignoreMissing);
91      if (image == null) {
92        return null;
93      } else {
94        return facesContext.getExternalContext().getRequestContextPath() + image;
95      }
96    }
97  
98    /**
99     * Searches for an image and return it with the context path.
100    * The extension of the image will be automatically extended (.png, .gif, .jpg).
101    * A missing image will be logged as an error.
102    *
103    * @param facesContext the current FacesContext
104    * @param name the name of the image without extension
105    */
106   public static String getImage(
107       final FacesContext facesContext, final String name) {
108     return getImage(facesContext, name, false);
109   }
110 
111   /**
112    * Searches for an image and return it with the context path.
113    * The extension of the image will be automatically extended (.png, .gif, .jpg)
114    *
115    * @param facesContext the current FacesContext
116    * @param name the name of the image without extension
117    * @param ignoreMissing if set to false, an error message will be logged, when image is missing
118    */
119   public static String getImage(
120       final FacesContext facesContext, final String name, final boolean ignoreMissing) {
121     final String image
122         = ResourceManagerFactory.getResourceManager(facesContext).getImage(facesContext, name, null, ignoreMissing);
123     if (image == null) {
124       return null;
125     } else {
126       return facesContext.getExternalContext().getRequestContextPath() + image;
127     }
128   }
129 
130   public static List<String> getStyles(final FacesContext facesContext, final String name) {
131     final String contextPath = facesContext.getExternalContext().getRequestContextPath();
132     final String[] styles = ResourceManagerFactory.getResourceManager(facesContext).getStyles(facesContext, name);
133     return addContextPath(styles, contextPath);
134   }
135 
136   private static List<String> addContextPath(final String[] strings, final String contextPath) {
137     final List<String> withContext = new ArrayList<String>(strings.length);
138     for (final String string : strings) {
139       withContext.add(contextPath + string);
140     }
141     return withContext;
142   }
143 
144   public static List<String> getScripts(final FacesContext facesContext, final String name) {
145     final String contextPath = facesContext.getExternalContext().getRequestContextPath();
146     final String[] scripts = ResourceManagerFactory.getResourceManager(facesContext).getScripts(facesContext, name);
147     return addContextPath(scripts, contextPath);
148   }
149 
150   /**
151    * @deprecated Since Tobago 2.0.0. Because of CSP.
152    */
153   @Deprecated
154   public static String getScriptsAsJSArray(final FacesContext facesContext, final String[] names) {
155     final List<String> fileNames = new ArrayList<String>();
156     for (final String name : names) {
157       fileNames.addAll(getScripts(facesContext, name));
158     }
159     return toJSArray(fileNames);
160   }
161 
162   /**
163    * @deprecated Since Tobago 2.0.0. Because of CSP.
164    */
165   @Deprecated
166   public static String getStylesAsJSArray(final FacesContext facesContext, final String[] names) {
167     final List<String> fileNames = new ArrayList<String>();
168     for (final String name : names) {
169       fileNames.addAll(getStyles(facesContext, name));
170     }
171     return toJSArray(fileNames);
172   }
173 
174   /**
175    * @deprecated Since Tobago 2.0.0. Because of CSP.
176    */
177   @Deprecated
178   public static String toJSArray(final List<String> list) {
179     final StringBuilder sb = new StringBuilder();
180     for (final String name : list) {
181       if (sb.length() > 0) {
182         sb.append(", ");
183       }
184       sb.append('\'');
185       sb.append(name);
186       sb.append('\'');
187     }
188     return "[" + sb.toString() + "]";
189   }
190 
191   public static String getDisabledImageWithPath(final FacesContext facesContext, final String image,
192                                                 final boolean ignoreMissing) {
193     final String filename = ResourceUtils.addPostfixToFilename(image, "Disabled");
194     return getImageWithPath(facesContext, filename, ignoreMissing);
195   }
196 
197   public static String getDisabledImage(final FacesContext facesContext, final String image,
198                                                 final boolean ignoreMissing) {
199     return getImage(facesContext, image + "Disabled", ignoreMissing);
200   }
201 
202   /**
203    * Blank page e. g. useful to set src of iframes (to prevent https problems in ie, see TOBAGO-538)
204    */
205   public static String getBlankPage(final FacesContext facesContext) {
206     return facesContext.getExternalContext().getRequestContextPath()
207         + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
208   }
209 
210   public static String getPageWithoutContextPath(final FacesContext facesContext, final String name) {
211     return ResourceManagerUtils.getImageWithPath(facesContext, name);
212   }
213 
214   public static Measure getThemeMeasure(
215       final FacesContext facesContext, final Configurable configurable, final String name) {
216     return ResourceManagerFactory.getResourceManager(facesContext).getThemeMeasure(
217         facesContext, configurable.getRendererType(), configurable.getCurrentMarkup(), name);
218   }
219 
220   /**
221    * Detects if the value is an absolute resource or if the value has to be processed by the theme mechanism. A resource
222    * will be treated as absolute, if the value starts with HTTP:, HTTPS:, FTP: or a slash. The case will be ignored by
223    * this check. Null values will return true.
224    *
225    * @param value the given resource link.
226    * @return true if it is an external or absolute resource.
227    */
228   public static boolean isAbsoluteResource(final String value) {
229     if (value == null) {
230       return true;
231     }
232     final String upper = value.toUpperCase(Locale.ENGLISH);
233     return (upper.startsWith("/")
234         || upper.startsWith("HTTP:")
235         || upper.startsWith("HTTPS:")
236         || upper.startsWith("FTP:"));
237   }
238 
239   public static int indexOfExtension(final String value) {
240     if (value == null) {
241       return -1;
242     }
243     int dot = value.lastIndexOf('.');
244     if (dot == -1) {
245       return dot;
246     }
247     int slash = value.lastIndexOf('/');
248     return dot > slash ? dot : -1;
249   }
250 
251   public static String getImageOrDisabledImageWithPath(
252       final FacesContext facesContext, final String image, final boolean disabled) {
253     return getImageOrDisabledImageWithPath(facesContext, image, disabled, false);
254   }
255 
256   public static String getImageOrDisabledImageWithPath(
257           final FacesContext facesContext, final String image, final boolean disabled, final boolean ignoreMissing) {
258     String imageWithPath = null;
259     if (disabled) {
260       imageWithPath = ResourceManagerUtils.getDisabledImageWithPath(facesContext, image, ignoreMissing);
261     }
262     if (imageWithPath == null) {
263       imageWithPath = ResourceManagerUtils.getImageWithPath(facesContext, image, ignoreMissing);
264     }
265     return imageWithPath;
266   }
267 
268   public static String getImageOrDisabledImage(
269       final FacesContext facesContext, final String image, final boolean disabled) {
270     return getImageOrDisabledImage(facesContext, image, disabled, false);
271   }
272 
273   public static String getImageOrDisabledImage(
274           final FacesContext facesContext, final String image, final boolean disabled, final boolean ignoreMissing) {
275     String imageWithPath = null;
276     if (disabled) {
277       imageWithPath = ResourceManagerUtils.getDisabledImage(facesContext, image, ignoreMissing);
278     }
279     if (imageWithPath == null) {
280       imageWithPath = ResourceManagerUtils.getImage(facesContext, image, ignoreMissing);
281     }
282     return imageWithPath;
283   }
284 }