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