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