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  package javax.faces.application;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.stream.Stream;
25  
26  import javax.faces.context.FacesContext;
27  
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
29  
30  /**
31   * @since 2.0
32   */
33  public abstract class ResourceHandler
34  {
35      public static final String LOCALE_PREFIX = "javax.faces.resource.localePrefix";
36      public static final String RESOURCE_EXCLUDES_DEFAULT_VALUE = ".class .jsp .jspx .properties .xhtml .groovy";
37      
38      /**
39       * Space separated file extensions that will not be served by the default ResourceHandler implementation.
40       */
41      @JSFWebConfigParam(defaultValue=".class .jsp .jspx .properties .xhtml .groovy",since="2.0", group="resources")
42      public static final String RESOURCE_EXCLUDES_PARAM_NAME = "javax.faces.RESOURCE_EXCLUDES";
43      public static final String RESOURCE_IDENTIFIER = "/javax.faces.resource";
44      
45      /**
46       * @since 2.2
47       */
48      public static final String RESOURCE_CONTRACT_XML = "javax.faces.contract.xml";
49      
50      /**
51       * @since 2.2
52       */
53      public static final String WEBAPP_CONTRACTS_DIRECTORY_PARAM_NAME = "javax.faces.WEBAPP_CONTRACTS_DIRECTORY";
54  
55      /**
56       * @since 2.2
57       */
58      public static final String WEBAPP_RESOURCES_DIRECTORY_PARAM_NAME = "javax.faces.WEBAPP_RESOURCES_DIRECTORY";
59  
60      /**
61       * @since 2.3
62       */
63      public static final String JSF_SCRIPT_RESOURCE_NAME = "jsf.js";
64  
65      /**
66       * @since 2.3
67       */
68      public static final String JSF_SCRIPT_LIBRARY_NAME = "javax.faces";
69      
70      private final static String MYFACES_JS_RESOURCE_NAME = "oamSubmit.js";
71      private final static String MYFACES_JS_RESOURCE_NAME_UNCOMPRESSED = "oamSubmit-uncompressed.js";
72      private final static String RENDERED_RESOURCES_SET = "org.apache.myfaces.RENDERED_RESOURCES_SET";
73      private final static String MYFACES_LIBRARY_NAME = "org.apache.myfaces";
74      private final static String RENDERED_MYFACES_JS = "org.apache.myfaces.RENDERED_MYFACES_JS";
75  
76      public abstract Resource createResource(String resourceName);
77      
78      public abstract Resource createResource(String resourceName, String libraryName);
79      
80      public abstract Resource createResource(String resourceName, String libraryName, String contentType);
81      
82      public abstract String getRendererTypeForResourceName(String resourceName);
83      
84      public abstract void handleResourceRequest(FacesContext context) throws IOException;
85      
86      public abstract boolean isResourceRequest(FacesContext context);
87      
88      public abstract  boolean libraryExists(String libraryName);
89      
90      /**
91       * @since 2.2
92       * @param resourceId
93       * @return 
94       */
95      public Resource createResourceFromId(String resourceId)
96      {
97          return null;
98      }
99      
100     /**
101      * 
102      * @since 2.2
103      * @param context
104      * @param resourceName
105      * @return 
106      */
107     public ViewResource createViewResource(FacesContext context,
108                                        String resourceName)
109     {
110         return context.getApplication().getResourceHandler().createResource(resourceName);
111     }
112     
113     public boolean isResourceURL(java.lang.String url)
114     {
115         if (url == null)
116         {
117             throw new NullPointerException();
118         }
119         return url.contains(RESOURCE_IDENTIFIER);
120     }
121     
122     /**
123      * @since 2.3
124      * @param facesContext
125      * @param path
126      * @param options
127      * @return 
128      */
129     public Stream<java.lang.String> getViewResources(
130             FacesContext facesContext, String path, ResourceVisitOption... options)
131     {
132         return getViewResources(facesContext, path, Integer.MAX_VALUE, options);
133     }
134     
135     /**
136      * @since 2.3
137      * @param facesContext
138      * @param path
139      * @param maxDepth
140      * @param options
141      * @return 
142      */
143     public Stream<java.lang.String> getViewResources(FacesContext facesContext, 
144             String path, int maxDepth, ResourceVisitOption... options)
145     {
146         return null;
147     }
148     
149     /**
150      * @since 2.3
151      * @param facesContext
152      * @param resourceName
153      * @param libraryName
154      * @return 
155      */
156     public boolean isResourceRendered(FacesContext facesContext, String resourceName, String libraryName)
157     {
158         return getRenderedResources(facesContext).containsKey(
159                 libraryName != null ? libraryName+'/'+resourceName : resourceName);
160     }
161     
162     /**
163      * @since 2.3
164      * @param facesContext
165      * @param resourceName
166      * @param libraryName 
167      */
168     public void markResourceRendered(FacesContext facesContext, String resourceName, String libraryName)
169     {
170         getRenderedResources(facesContext).put(
171                 libraryName != null ? libraryName+'/'+resourceName : resourceName, Boolean.TRUE);
172         if (ResourceHandler.JSF_SCRIPT_LIBRARY_NAME.equals(libraryName) &&
173             ResourceHandler.JSF_SCRIPT_RESOURCE_NAME.equals(resourceName))
174         {
175             // If we are calling this method, it is expected myfaces core is being used as runtime and note
176             // oamSubmit script is included inside jsf.js, so mark this one too.
177             getRenderedResources(facesContext).put(
178                     MYFACES_LIBRARY_NAME+'/'+MYFACES_JS_RESOURCE_NAME, Boolean.TRUE);
179         }
180     }
181     
182     /**
183      * Return a set of already rendered resources by this renderer on the current
184      * request. 
185      * 
186      * @param facesContext
187      * @return
188      */
189     @SuppressWarnings("unchecked")
190     private static Map<String, Boolean> getRenderedResources(FacesContext facesContext)
191     {
192         Map<String, Boolean> map = (Map<String, Boolean>) facesContext.getViewRoot().getTransientStateHelper()
193                 .getTransient(RENDERED_RESOURCES_SET);
194         if (map == null)
195         {
196             map = new HashMap<String, Boolean>();
197             facesContext.getViewRoot().getTransientStateHelper().putTransient(RENDERED_RESOURCES_SET,map);
198         }
199         return map;
200     }
201 
202 }