1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.resource;
20
21 import java.util.Map;
22
23 import javax.faces.context.ExternalContext;
24 import javax.faces.context.FacesContext;
25
26 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
27 import org.apache.myfaces.shared.application.FacesServletMapping;
28 import org.apache.myfaces.shared.util.WebConfigParamUtils;
29
30
31
32
33
34 public class BaseResourceHandlerSupport extends ResourceHandlerSupport
35 {
36
37
38
39
40
41
42 @JSFWebConfigParam(since="2.0", defaultValue="604800000", group="resources", tags="performance")
43 public static final String RESOURCE_MAX_TIME_EXPIRES = "org.apache.myfaces.RESOURCE_MAX_TIME_EXPIRES";
44
45
46
47
48 private static final String CACHED_SERVLET_MAPPING =
49 BaseResourceHandlerSupport.class.getName() + ".CACHED_SERVLET_MAPPING";
50
51 private static final ResourceLoader[] EMPTY_RESOURCE_LOADERS = new ResourceLoader[]{};
52 private static final ContractResourceLoader[] EMPTY_CONTRACT_RESOURCE_LOADERS =
53 new ContractResourceLoader[]{};
54
55 private Long _startupTime;
56
57 private Long _maxTimeExpires;
58
59 public BaseResourceHandlerSupport()
60 {
61 _startupTime = System.currentTimeMillis();
62 }
63
64 public ResourceLoader[] getResourceLoaders()
65 {
66 return EMPTY_RESOURCE_LOADERS;
67 }
68
69 public ContractResourceLoader[] getContractResourceLoaders()
70 {
71 return EMPTY_CONTRACT_RESOURCE_LOADERS;
72 }
73
74 public ResourceLoader[] getViewResourceLoaders()
75 {
76 return EMPTY_RESOURCE_LOADERS;
77 }
78
79 public String calculateResourceBasePath(FacesContext facesContext)
80 {
81 FacesServletMapping mapping = getFacesServletMapping(facesContext);
82 ExternalContext externalContext = facesContext.getExternalContext();
83
84 if (mapping != null)
85 {
86 String resourceBasePath = null;
87 if (mapping.isExtensionMapping())
88 {
89
90
91
92
93
94
95
96
97 resourceBasePath = externalContext.getRequestServletPath();
98 int stripPoint = resourceBasePath.lastIndexOf('.');
99 if (stripPoint > 0)
100 {
101 resourceBasePath = resourceBasePath.substring(0, stripPoint);
102 }
103 }
104 else
105 {
106
107
108
109
110
111
112
113
114
115 resourceBasePath = externalContext.getRequestPathInfo();
116 }
117 return resourceBasePath;
118 }
119 else
120 {
121
122
123
124 return externalContext.getRequestPathInfo();
125 }
126 }
127
128 public boolean isExtensionMapping()
129 {
130 FacesServletMapping mapping = getFacesServletMapping(
131 FacesContext.getCurrentInstance());
132 if (mapping != null)
133 {
134 if (mapping.isExtensionMapping())
135 {
136 return true;
137 }
138 }
139 return false;
140 }
141
142 public String getMapping()
143 {
144 FacesServletMapping mapping = getFacesServletMapping(
145 FacesContext.getCurrentInstance());
146 if (mapping != null)
147 {
148 if (mapping.isExtensionMapping())
149 {
150 return mapping.getExtension();
151 }
152 else
153 {
154 return mapping.getPrefix();
155 }
156 }
157 return "";
158 }
159
160
161
162
163
164 protected FacesServletMapping getFacesServletMapping(FacesContext context)
165 {
166 Map<Object, Object> attributes = context.getAttributes();
167
168
169 FacesServletMapping mapping = (FacesServletMapping) attributes.get(CACHED_SERVLET_MAPPING);
170 if (mapping == null)
171 {
172 ExternalContext externalContext = context.getExternalContext();
173 mapping = calculateFacesServletMapping(externalContext.getRequestServletPath(),
174 externalContext.getRequestPathInfo());
175
176 attributes.put(CACHED_SERVLET_MAPPING, mapping);
177 }
178 return mapping;
179 }
180
181
182
183
184
185
186
187
188
189
190 protected static FacesServletMapping calculateFacesServletMapping(
191 String servletPath, String pathInfo)
192 {
193 if (pathInfo != null)
194 {
195
196
197
198
199
200
201 return FacesServletMapping.createPrefixMapping(servletPath);
202 }
203 else
204 {
205
206
207
208
209
210 int slashPos = servletPath.lastIndexOf('/');
211 int extensionPos = servletPath.lastIndexOf('.');
212 if (extensionPos > -1 && extensionPos > slashPos)
213 {
214 String extension = servletPath.substring(extensionPos);
215 return FacesServletMapping.createExtensionMapping(extension);
216 }
217 else
218 {
219
220
221 return FacesServletMapping.createPrefixMapping(servletPath);
222 }
223 }
224 }
225
226 public long getStartupTime()
227 {
228 return _startupTime;
229 }
230
231 public long getMaxTimeExpires()
232 {
233 if (_maxTimeExpires == null)
234 {
235 _maxTimeExpires = WebConfigParamUtils.getLongInitParameter(
236 FacesContext.getCurrentInstance().getExternalContext(),
237 RESOURCE_MAX_TIME_EXPIRES, 604800000L);
238 }
239 return _maxTimeExpires;
240 }
241 }