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 org.apache.myfaces.trinidad.context;
20  
21  import java.awt.Color;
22  
23  import java.util.ArrayDeque;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.TimeZone;
30  
31  import javax.faces.component.UIComponent;
32  import javax.faces.component.UIViewRoot;
33  import javax.faces.context.FacesContext;
34  
35  import org.apache.myfaces.trinidad.change.ChangeManager;
36  import org.apache.myfaces.trinidad.config.RegionManager;
37  import org.apache.myfaces.trinidad.webapp.UploadedFileProcessor;
38  
39  
40  public class MockRequestContext extends RequestContext
41  {
42    public MockRequestContext()
43    {
44      _componentStack = new ArrayDeque<UIComponent>();
45      attach();
46    }
47  
48    // Support setting the agent so we can create one RequestContext
49    // and mutate it
50  
51    public void setAgent(Agent agent)
52    {
53      _agent = agent;
54    }
55  
56    @Override
57    public Agent getAgent()
58    {
59      return _agent;
60    }
61  
62    @Override
63    public PageResolver getPageResolver()
64    {
65      throw new UnsupportedOperationException("Not implemented yet");
66    }
67  
68    @Override
69    public PageFlowScopeProvider getPageFlowScopeProvider()
70    {
71      throw new UnsupportedOperationException("Not implemented yet");
72    }
73  
74    @Override
75    public DialogService getDialogService()
76    {
77      throw new UnsupportedOperationException("Not implemented yet");
78    }
79  
80    @Override
81    public Map<String, Object> getPageFlowScope()
82    {
83      return new HashMap<String, Object>();
84    }
85  
86    @Override
87    public void returnFromDialog(Object returnValue, Map<Object, Object> returnParam)
88    {
89      throw new UnsupportedOperationException("Should not be called during rendering");
90    }
91  
92    @Override
93    public void launchDialog(
94        UIViewRoot dialogRoot,
95        Map<String, Object> dialogParameters,
96        UIComponent source,
97        boolean useWindow,
98        Map<String, Object> windowProperties)
99    {
100     throw new UnsupportedOperationException("Should not be called during rendering");
101   }
102 
103   @Override
104   public boolean isPostback()
105   {
106     return false;
107   }
108 
109   @Override
110   public boolean isPartialRequest(FacesContext context)
111   {
112     return false;
113   }
114 
115   @Override
116   public boolean isDebugOutput()
117   {
118     return false;
119   }
120 
121   @Override
122   public ClientValidation getClientValidation()
123   {
124     return ClientValidation.ALERT;
125   }
126 
127   @Override
128   public boolean isClientValidationDisabled()
129   {
130     return false;
131   }
132 
133   @Override
134   public String getOutputMode()
135   {
136     return null;
137   }
138 
139   public void setSkinFamily(String skin)
140   {
141     _skin = skin;
142   }
143 
144   @Override
145   public String getSkinFamily()
146   {
147     return _skin;
148   }
149 
150   @Override
151   public Accessibility getAccessibilityMode()
152   {
153     return _accMode;
154   }
155 
156   public void setAccessibilityMode(Accessibility accMode)
157   {
158     _accMode = accMode;
159   }
160 
161   @Override
162   public AccessibilityProfile getAccessibilityProfile()
163   {
164     return _accProfile;
165   }
166 
167   public void setAccessibilityProfile(AccessibilityProfile accProfile)
168   {
169     _accProfile = accProfile;
170   }
171 
172   @Override
173   public boolean isRightToLeft()
174   {
175     return _rtl;
176   }
177 
178   public void setRightToLeft(boolean rtl)
179   {
180     _rtl = rtl;
181   }
182 
183   public void setAnimationEnabled(boolean animationEnabled)
184   {
185     _animationEnabled = animationEnabled;
186   }
187 
188   @Override
189   public boolean isAnimationEnabled()
190   {
191     return _animationEnabled;
192   }
193 
194   @Override
195   public Locale getFormattingLocale()
196   {
197     return _formattingLocale;
198   }
199 
200   public void setFormattingLocale(Locale formattingLocale)
201   {
202     _formattingLocale = formattingLocale;
203   }
204 
205   @Override
206   public char getNumberGroupingSeparator()
207   {
208     return _numberGroupingSeparator;
209   }
210 
211   public void setNumberGroupingSeparator(char sep)
212   {
213     _numberGroupingSeparator = sep;
214   }
215 
216   @Override
217   public char getDecimalSeparator()
218   {
219     return _decimalSeparator;
220   }
221 
222   public void setDecimalSeparator(char sep)
223   {
224     _decimalSeparator = sep;
225   }
226 
227   @Override
228   public String getCurrencyCode()
229   {
230     return _currencyCode;
231   }
232 
233   public void setCurrencyCode(String code)
234   {
235     _currencyCode = code;
236   }
237 
238   @Override
239   public int getTwoDigitYearStart()
240   {
241     return _twoDigitYearStart;
242   }
243 
244   public void setTwoDigitYearStart(int start)
245   {
246     _twoDigitYearStart = start;
247   }
248 
249   @Override
250   public String getOracleHelpServletUrl()
251   {
252     throw new UnsupportedOperationException("Not implemented yet");
253   }
254 
255   @Override
256   public Map<String, Object> getHelpTopic()
257   {
258     throw new UnsupportedOperationException("Not implemented yet");
259   }
260 
261   @Override
262   public Map<String, Object> getHelpSystem()
263   {
264     throw new UnsupportedOperationException("Not implemented yet");
265   }
266 
267   @Override
268   public TimeZone getTimeZone()
269   {
270     return _timeZone;
271   }
272 
273   public void setTimeZone(TimeZone timeZone)
274   {
275     _timeZone = timeZone;
276   }
277 
278   public void setUploadedFileMaxMemory(Long maxMemory)
279   {
280     _maxMemory = maxMemory;
281   }
282 
283   @Override
284   public Long getUploadedFileMaxMemory()
285   {
286     return _maxMemory;
287   }
288 
289   public void setUploadedFileMaxDiskSpace(Long maxDiskSpace)
290   {
291     _maxDiskSpace = maxDiskSpace;
292   }
293 
294   @Override
295   public Long getUploadedFileMaxDiskSpace()
296   {
297     return _maxDiskSpace;
298   }
299 
300   public void setUploadedFileMaxFileSize(Long maxFileSize)
301   {
302     _maxFileSize = maxFileSize;
303   }
304 
305   @Override
306   public Long getUploadedFileMaxFileSize()
307   {
308     return _maxFileSize;
309   }
310 
311   public void setUploadedFileMaxChunkSize(Long maxChunkSize)
312   {
313     _maxChunkSize = maxChunkSize;
314   }
315 
316   @Override
317   public Long getUploadedFileMaxChunkSize()
318   {
319     return _maxChunkSize;
320   }
321 
322   public void setUploadedFileTempDir(String tempDir)
323   {
324     _tempDir= tempDir;
325   }
326 
327   @Override
328   public String getUploadedFileTempDir()
329   {
330     return _tempDir;
331   }
332 
333   @Override
334   public void addPartialTarget(UIComponent newTarget)
335   {
336     // throw new UnsupportedOperationException("Not implemented yet");
337   }
338 
339   /**
340    * @see org.apache.myfaces.trinidad.context.RequestContext#addPartialTargets(javax.faces.component.UIComponent, java.lang.String[])
341    */
342   @Override
343   public void addPartialTargets(UIComponent from, String... targets)
344   {
345 
346   }
347 
348   @Override
349   public Set<UIComponent> getPartialTargets(UIComponent from)
350   {
351     throw new UnsupportedOperationException("Not implemented yet");
352   }
353 
354   @Override
355   public void addPartialTriggerListeners(UIComponent listener, String[] trigger)
356   {
357   }
358 
359   @Override
360   public void partialUpdateNotify(UIComponent updated)
361   {
362     // Do nothing
363   }
364 
365   @Override
366   public UploadedFileProcessor getUploadedFileProcessor()
367   {
368     throw new UnsupportedOperationException("Should not be called during rendering");
369   }
370 
371   @Override
372   public Map<String, List<Color>> getColorPalette()
373   {
374     throw new UnsupportedOperationException("Not implemented yet");
375   }
376 
377   @Override
378   public Map<Object, Map<Object,String>> getFormatter()
379   {
380     throw new UnsupportedOperationException("Not implemented yet");
381   }
382 
383   @Override
384   public ChangeManager getChangeManager()
385   {
386     throw new UnsupportedOperationException("Not implemented yet");
387   }
388 
389   @Override
390   public RegionManager getRegionManager()
391   {
392     throw new UnsupportedOperationException("Not implemented yet");
393   }
394 
395   @Override
396   public Object saveComponent(UIComponent component)
397   {
398     throw new UnsupportedOperationException("Not implemented yet");
399   }
400 
401 
402   @Override
403   public UIComponent restoreComponent(Object state)
404   {
405     throw new UnsupportedOperationException("Not implemented yet");
406   }
407 
408   @Override
409   public boolean isInternalViewRequest(FacesContext context)
410   {
411     return false;
412   }
413 
414   @Override
415   public Map<String, Object> getViewMap()
416   {
417     return getViewMap(true);
418   }
419 
420   @Override
421   @SuppressWarnings("unchecked")
422   public Map<String, Object> getViewMap(boolean create)
423   {
424     // Note: replace this method body with a call to UIViewRoot.getViewMap(boolean) when
425     // Trinidad is upgraded to use JSF 2.0
426 
427     FacesContext facesContext = FacesContext.getCurrentInstance();
428     UIViewRoot viewRoot = facesContext.getViewRoot();
429     Map<String, Object> viewMap = null;
430 
431     if (viewRoot != null)
432     {
433       Map<String, Object> attrs = viewRoot.getAttributes();
434 
435       viewMap = (Map<String, Object>)attrs.get(_VIEW_MAP_KEY);
436       if (viewMap == null && create)
437       {
438         // Note, it is not valid to refer to the request context from outside of the request's
439         // thread. As such, synchronization and thread safety is not an issue here.
440         // This coincides with the JSF 2.0 code not using syncronization and using the non-thread
441         // safe HashMap.
442         viewMap = new HashMap<String, Object>();
443         attrs.put(_VIEW_MAP_KEY, viewMap);
444       }
445     }
446 
447     return viewMap;
448   }
449 
450   @Override
451   public void pushCurrentComponent(FacesContext context, UIComponent component)
452   {
453     _componentStack.addFirst(component);
454   }
455 
456   @Override
457   public void popCurrentComponent(FacesContext context, UIComponent component)
458   {
459     UIComponent topComponent = _componentStack.poll();
460 
461     // verify that we are removing the correct component
462     if (topComponent != component)
463     {
464       // put the component back
465       _componentStack.addFirst(component);
466       throw new IllegalStateException();
467     }
468   }
469 
470   @Override
471   public UIComponent getCurrentComponent()
472   {
473     return _componentStack.peek();
474   }
475   
476   static private final TimeZone _FIXED_TIME_ZONE =
477     TimeZone.getTimeZone("America/Los_Angeles");
478 
479   static private final String _VIEW_MAP_KEY =
480     MockRequestContext.class.getName() + ".VIEW_MAP";
481 
482   private char _numberGroupingSeparator = ',';
483   private char _decimalSeparator = '.';
484   private String _currencyCode = null;
485   private int _twoDigitYearStart = 1950;
486   private TimeZone _timeZone = _FIXED_TIME_ZONE;
487 
488   private String _skin;
489   private Accessibility _accMode;
490   private AccessibilityProfile _accProfile;
491   private Agent _agent;
492   private boolean _rtl = false;
493   private boolean _animationEnabled = true;
494   private Locale _formattingLocale;
495   private Long _maxMemory;
496   private Long _maxDiskSpace;
497   private Long _maxFileSize;
498   private Long _maxChunkSize;
499   private String _tempDir;
500   private ArrayDeque<UIComponent> _componentStack;
501 }