View Javadoc

1   /*
2    * $Id: AbstractProcessContext.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    */
4   package org.rcfaces.core.internal.renderkit;
5   
6   import java.util.Calendar;
7   import java.util.Locale;
8   import java.util.Map;
9   import java.util.TimeZone;
10  
11  import javax.faces.FacesException;
12  import javax.faces.context.ExternalContext;
13  import javax.faces.context.FacesContext;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.rcfaces.core.internal.RcfacesContext;
18  import org.rcfaces.core.internal.tools.ContextTools;
19  import org.rcfaces.core.internal.tools.PageConfiguration;
20  import org.rcfaces.core.internal.util.PathUtil;
21  
22  /**
23   * 
24   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
25   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
26   */
27  public abstract class AbstractProcessContext implements IProcessContext {
28  
29      private static final String REVISION = "$Revision: 1.18 $";
30  
31      private static final Log LOG = LogFactory
32              .getLog(AbstractProcessContext.class);
33  
34      private static final String EXTERNAL_CONTEXT_PROPERTY = "org.rcfaces.renderkit.core.EXTERNAL_CONTEXT";
35  
36      protected final RcfacesContext rcfacesContext;
37  
38      protected final FacesContext facesContext;
39  
40      private final String contextPath;
41  
42      private final String servletPath;
43  
44      private String baseHREF;
45  
46      private Locale userLocale;
47  
48      private boolean designerMode;
49  
50      private boolean pageConfiguratorInitialized;
51  
52      private Locale defaultAttributesLocale;
53  
54      private String scriptType;
55  
56      private TimeZone timeZone;
57  
58      private Calendar calendar;
59  
60      private TimeZone forcedDateTimeZone;
61  
62      private TimeZone defaultTimeZone;
63  
64      private Calendar forcedDateCalendar;
65  
66      protected AbstractProcessContext(FacesContext facesContext) {
67          this.facesContext = facesContext;
68  
69          ExternalContext externalContext = facesContext.getExternalContext();
70  
71          contextPath = externalContext.getRequestContextPath();
72          String servletPath = externalContext.getRequestServletPath();
73          int idx = servletPath.lastIndexOf('/');
74          if (idx >= 0) {
75              servletPath = servletPath.substring(0, idx);
76          }
77  
78          this.servletPath = servletPath;
79  
80          rcfacesContext = RcfacesContext.getInstance(facesContext);
81  
82          this.designerMode = rcfacesContext.isDesignerMode();
83  
84      }
85  
86      public final FacesContext getFacesContext() {
87          return facesContext;
88      }
89  
90      public Boolean getDebugMode() {
91          return null;
92      }
93  
94      public Boolean getProfilerMode() {
95          return null;
96      }
97  
98      public boolean isDesignerMode() {
99          return designerMode;
100     }
101 
102     public final Locale getUserLocale() {
103         if (userLocale != null) {
104             return userLocale;
105         }
106 
107         userLocale = ContextTools.getUserLocale(null);
108 
109         return userLocale;
110     }
111 
112     public Calendar getUserCalendar() {
113         if (calendar != null) {
114             return calendar;
115         }
116 
117         TimeZone timeZone = getUserTimeZone();
118         Locale locale = getUserLocale();
119 
120         if (timeZone != null) {
121             calendar = Calendar.getInstance(timeZone, locale);
122             return calendar;
123         }
124 
125         calendar = Calendar.getInstance(locale);
126 
127         return calendar;
128     }
129 
130     public TimeZone getUserTimeZone() {
131         if (timeZone != null) {
132             return timeZone;
133         }
134 
135         timeZone = ContextTools.getUserTimeZone(null);
136 
137         if (timeZone == null) {
138 
139         }
140 
141         return timeZone;
142     }
143 
144     public final String getAbsolutePath(String uri, boolean containsContextPath) {
145 
146         String contextPath;
147         if (containsContextPath) {
148             contextPath = this.contextPath;
149         } else {
150             contextPath = "";
151         }
152 
153         if (uri == null || uri.length() < 1) {
154             // Retourne le context path
155 
156             String p;
157             if (containsContextPath) {
158                 p = contextPath + servletPath;
159             } else {
160                 p = servletPath;
161             }
162             if (LOG.isDebugEnabled()) {
163                 LOG.debug("Returns path='" + p + "' [uri=null]  ('"
164                         + contextPath + "''" + servletPath + "'.)");
165             }
166 
167             return p;
168         }
169 
170         if (uri.charAt(0) == '/') {
171             // URL absolue
172             String p;
173             if (containsContextPath) {
174                 p = contextPath + uri;
175 
176             } else if (uri.startsWith(this.contextPath)) {
177                 p = uri.substring(this.contextPath.length());
178 
179             } else {
180                 p = uri;
181             }
182 
183             p = PathUtil.normalizePath(p);
184             if (LOG.isDebugEnabled()) {
185                 LOG
186                         .debug("Returns path='" + p + "' [uri=absolute]  ('"
187                                 + contextPath + "''" + servletPath + "''" + uri
188                                 + "'.)");
189             }
190             return p;
191         }
192 
193         // C'est un URI relatif !
194 
195         if (baseHREF != null) {
196             if (baseHREF.charAt(0) == '/') {
197                 // base HREF absolue !
198 
199                 String p = PathUtil.normalizePath(baseHREF + uri);
200 
201                 if (LOG.isDebugEnabled()) {
202                     LOG.debug("Returns path='" + p
203                             + "' [uri=relative,baseHREF=absolute]  ('"
204                             + baseHREF + uri + "'.)");
205                 }
206                 return p;
207             }
208             // base HREF relatif !
209 
210             String p = PathUtil.normalizePath(contextPath + servletPath + "/"
211                     + baseHREF + uri);
212 
213             if (LOG.isDebugEnabled()) {
214                 LOG.debug("Returns path='" + p
215                         + "' [uri=relative,baseHREF=relative]  ('"
216                         + contextPath + "''" + servletPath + "'/'" + baseHREF
217                         + uri + "'.)");
218             }
219             return p;
220         }
221 
222         String p = PathUtil
223                 .normalizePath(contextPath + servletPath + "/" + uri);
224 
225         if (LOG.isDebugEnabled()) {
226             LOG.debug("Returns path='" + p
227                     + "' [uri=relative,baseHREF=null] ('" + contextPath + "''"
228                     + servletPath + "'/'" + uri + "'.)");
229         }
230         return p;
231     }
232 
233     public final String getRelativePath(String uri) {
234         return null;
235     }
236 
237     /*
238      * public final String computeFromContextPath(String uri, boolean
239      * canBeRelative) { String baseURI = getAbsolutePath(canBeRelative);
240      * 
241      * String ret; if (uri == null) { ret = baseURI; } / else if
242      * (baseURI.length() == 0) { if (uri.length() > 0) { } } /else {
243      * StringAppender u = new StringAppender(baseURI, uri.length() + 2);
244      * 
245      * if (baseURI.length() > 0) { u.append('/'); }
246      * 
247      * if (uri.length() > 0) { if (uri.charAt(0) == '/') {
248      * u.append(uri.substring(1)); } else if (u.length() > 1 &&
249      * u.charAt(u.length() - 1) == '/') { u.setLength(u.length() - 1);
250      * u.append(uri); } else { u.append(uri); } }
251      * 
252      * ret = u.toString(); }
253      * 
254      * if (Constants.ENCODE_URI) { ret = externalContext.encodeResourceURL(ret);
255      * }
256      * 
257      * if (LOG.isDebugEnabled()) { LOG.debug("Compute uri '" + uri + "' => '" +
258      * ret + "'."); }
259      * 
260      * return ret; }
261      */
262 
263     public final String getBaseHREF() {
264         return baseHREF;
265     }
266 
267     public final void changeBaseHREF(String baseHREF) {
268         String base = baseHREF;
269 
270         if (base != null) {
271             base = normalizeBaseHREF(base);
272         }
273 
274         this.baseHREF = base;
275 
276         if (LOG.isDebugEnabled()) {
277             LOG.debug("Set baseHREF to '" + base + "' (param '" + baseHREF
278                     + "'.");
279         }
280     }
281 
282     private String normalizeBaseHREF(String baseHREF) {
283         if (baseHREF.equals("/")) {
284             return baseHREF;
285         }
286 
287         int idx = baseHREF.lastIndexOf('/'); // Retire le dernier segment qui
288         // doit être un fichier
289         if (idx < 1) {
290             return null;
291         }
292 
293         baseHREF = baseHREF.substring(0, idx);
294 
295         if (baseHREF.charAt(0) == '/') {
296             return baseHREF;
297         }
298 
299         return PathUtil.normalizePath(contextPath + servletPath + baseHREF);
300     }
301 
302     protected static void setProcessContext(IProcessContext externalContext) {
303         Map requestMap = externalContext.getFacesContext().getExternalContext()
304                 .getRequestMap();
305         IProcessContext old = (IProcessContext) requestMap.put(
306                 EXTERNAL_CONTEXT_PROPERTY, externalContext);
307         if (old != null) {
308             throw new FacesException("External constext is already defined ! ("
309                     + old + ")");
310         }
311     }
312 
313     public static IProcessContext getProcessContext(FacesContext facesContext) {
314 
315         if (facesContext == null) {
316             facesContext = FacesContext.getCurrentInstance();
317         }
318 
319         ExternalContext externalContext = facesContext.getExternalContext();
320 
321         Map requestMap = externalContext.getRequestMap();
322         return (IProcessContext) requestMap.get(EXTERNAL_CONTEXT_PROPERTY);
323 
324     }
325 
326     public final String getScriptType() {
327         initializePageConfigurator();
328 
329         return scriptType;
330     }
331 
332     public final Locale getDefaultLiteralLocale() {
333         initializePageConfigurator();
334 
335         return defaultAttributesLocale;
336     }
337 
338     public TimeZone getDefaultTimeZone() {
339         initializePageConfigurator();
340 
341         return defaultTimeZone;
342     }
343 
344     public TimeZone getForcedDateTimeZone() {
345         initializePageConfigurator();
346 
347         return forcedDateTimeZone;
348     }
349 
350     public Calendar getForcedDateCalendar() {
351         if (forcedDateCalendar != null) {
352             return forcedDateCalendar;
353         }
354 
355         TimeZone timeZone = getForcedDateTimeZone();
356         if (timeZone == null) {
357             return null;
358         }
359 
360         forcedDateCalendar = Calendar.getInstance(forcedDateTimeZone);
361 
362         return forcedDateCalendar;
363     }
364 
365     private void initializePageConfigurator() {
366         if (pageConfiguratorInitialized) {
367             return;
368         }
369 
370         pageConfiguratorInitialized = true;
371 
372         FacesContext facesContext = getFacesContext();
373 
374         scriptType = PageConfiguration.getScriptType(facesContext);
375         defaultAttributesLocale = PageConfiguration
376                 .getDefaultLiteralLocale(facesContext);
377 
378         Map applicationMap = facesContext.getExternalContext()
379                 .getApplicationMap();
380 
381         defaultTimeZone = getTimeZone(applicationMap,
382                 DEFAULT_TIMEZONE_PARAMETER);
383 
384         forcedDateTimeZone = getTimeZone(applicationMap,
385                 FORCED_DATE_TIMEZONE_PARAMETER);
386 
387         if (LOG.isDebugEnabled()) {
388             LOG.debug("Page configurator of view "
389                     + facesContext.getViewRoot().getId() + ": scriptType="
390                     + scriptType + " defaultAttributesLocale="
391                     + defaultAttributesLocale);
392 
393         }
394     }
395 
396     private TimeZone getTimeZone(Map applicationMap,
397             String defaultTimezoneParameter) {
398 
399         Object defaultTimeZone = applicationMap.get(defaultTimezoneParameter);
400         if ((defaultTimeZone instanceof String)
401                 && ((String) defaultTimeZone).length() > 0) {
402             TimeZone timeZone = TimeZone.getTimeZone((String) defaultTimeZone);
403 
404             if (timeZone == null) {
405                 throw new FacesException("Can not get timeZone associated to '"
406                         + defaultTimeZone + "'");
407             }
408 
409             applicationMap.put(defaultTimezoneParameter, timeZone);
410 
411             return timeZone;
412         }
413 
414         return (TimeZone) defaultTimeZone;
415     }
416 
417     public RcfacesContext getRcfacesContext() {
418         return rcfacesContext;
419     }
420 
421 }