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 javax.faces.context;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.Writer;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.Set;
29  import javax.faces.lifecycle.ClientWindow;
30  
31  /**
32   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
33   */
34  public abstract class ExternalContext
35  {
36      public static final String BASIC_AUTH = "BASIC";
37      public static final String CLIENT_CERT_AUTH = "CLIENT_CERT";
38      public static final String DIGEST_AUTH = "DIGEST";
39      public static final String FORM_AUTH = "FORM";
40      
41      /**
42       *
43       * @param name
44       * @param value
45       * @param properties
46       *
47       * @since 2.0
48       */
49      public void addResponseCookie(String name, String value, Map<String, Object> properties)
50      {
51          ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
52          
53          if (ctx == null)
54          {
55              throw new UnsupportedOperationException();
56          }
57          
58          ctx.addResponseCookie(name, value, properties);
59      }
60  
61      /**
62       *
63       * @param name
64       * @param value
65       *
66       * @since 2.0
67       */
68      public void addResponseHeader(String name, String value)
69      {
70          ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
71          
72          if (ctx == null)
73          {
74              throw new UnsupportedOperationException();
75          }
76          
77          ctx.addResponseHeader(name, value);
78      }
79  
80      public abstract void dispatch(String path) throws IOException;
81  
82      public abstract String encodeActionURL(String url);
83  
84      /**
85       *
86       * @param baseUrl
87       * @param parameters
88       *
89       * @since 2.0
90       */
91      public String encodeBookmarkableURL(String baseUrl, Map<String,List<String>> parameters)
92      {
93          ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
94          
95          if (ctx == null)
96          {
97              throw new UnsupportedOperationException();
98          }
99          
100         return ctx.encodeBookmarkableURL(baseUrl, parameters);
101     }
102 
103     public abstract String encodeNamespace(String name);
104 
105 
106     /**
107      * @since 2.0
108      */
109     public String encodePartialActionURL(String url)
110     {
111         // TODO: IMPLEMENT IMPL
112         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
113         
114         if (ctx == null)
115         {
116             throw new UnsupportedOperationException();
117         }
118         
119         return ctx.encodePartialActionURL(url);
120     }
121 
122     /**
123      *
124      * @param baseUrl
125      * @param parameters
126      *
127      * @since 2.0
128      */
129     public String encodeRedirectURL(String baseUrl, Map<String,List<String>> parameters)
130     {
131         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
132         
133         if (ctx == null)
134         {
135             throw new UnsupportedOperationException();
136         }
137         
138         return ctx.encodeRedirectURL(baseUrl, parameters);
139     }
140 
141     public abstract String encodeResourceURL(String url);
142 
143     public abstract Map<String, Object> getApplicationMap();
144 
145     public abstract String getAuthType();
146 
147     public abstract Object getContext();
148 
149     /**
150      * Returns the name of the underlying context
151      *
152      * @return the name or null
153      *
154      * @since 2.0
155      */
156     public String getContextName()
157     {
158         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
159         
160         if (ctx == null)
161         {
162             throw new UnsupportedOperationException();
163         }
164         
165         return ctx.getContextName();
166     }
167 
168     /**
169      * @since 2.0
170      */
171     public Flash getFlash()
172     {
173         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
174         
175         if (ctx == null)
176         {
177             throw new UnsupportedOperationException();
178         }
179         
180         return ctx.getFlash();
181     }
182 
183     public abstract String getInitParameter(String name);
184 
185     public abstract Map<String,String> getInitParameterMap();
186 
187     /**
188      * @since JSF 2.0
189      */
190     public String getMimeType(String file)
191     {
192         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
193         
194         if (ctx == null)
195         {
196             throw new UnsupportedOperationException();
197         }
198         
199         return ctx.getMimeType(file);
200     }
201 
202     /**
203      * @since JSF 2.0
204      */
205     public String getRealPath(String path)
206     {
207         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
208         
209         if (ctx == null)
210         {
211             throw new UnsupportedOperationException();
212         }
213         
214         return ctx.getRealPath(path);
215     }
216 
217     public abstract String getRemoteUser();
218 
219     public abstract Object getRequest();
220 
221     public String getRequestCharacterEncoding()
222     {
223         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
224         
225         if (ctx == null)
226         {
227             throw new UnsupportedOperationException();
228         }
229         
230         return ctx.getRequestCharacterEncoding();
231     }
232 
233     /**
234      *
235      * @return
236      *
237      * @since 2.0
238      */
239     public int getRequestContentLength()
240     {
241         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
242         
243         if (ctx == null)
244         {
245             throw new UnsupportedOperationException();
246         }
247         
248         return ctx.getRequestContentLength();
249     }
250 
251     public String getRequestContentType()
252     {
253         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
254         
255         if (ctx == null)
256         {
257             throw new UnsupportedOperationException();
258         }
259         
260         return ctx.getRequestContentType();
261     }
262 
263     public abstract String getRequestContextPath();
264 
265     public abstract Map<String, Object> getRequestCookieMap();
266 
267     public abstract Map<String, String> getRequestHeaderMap();
268 
269     public abstract Map<String, String[]> getRequestHeaderValuesMap();
270 
271     public abstract Locale getRequestLocale();
272 
273     public abstract Iterator<Locale> getRequestLocales();
274 
275     public abstract Map<String, Object> getRequestMap();
276 
277     public abstract Map<String, String> getRequestParameterMap();
278 
279     public abstract Iterator<String> getRequestParameterNames();
280 
281     public abstract Map<String, String[]> getRequestParameterValuesMap();
282 
283     public abstract String getRequestPathInfo();
284 
285     /**
286      * @since JSF 2.0
287      */
288     public String getRequestScheme()
289     {
290         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
291         
292         if (ctx == null)
293         {
294             throw new UnsupportedOperationException();
295         }
296         
297         return ctx.getRequestScheme();
298     }
299 
300     /**
301      * @since JSF 2.0
302      */
303     public String getRequestServerName()
304     {
305         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
306         
307         if (ctx == null)
308         {
309             throw new UnsupportedOperationException();
310         }
311         
312         return ctx.getRequestServerName();
313     }
314 
315     /**
316      * @since JSF 2.0
317      */
318     public int getRequestServerPort()
319     {
320         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
321         
322         if (ctx == null)
323         {
324             throw new UnsupportedOperationException();
325         }
326         
327         return ctx.getRequestServerPort();
328     }
329 
330     public abstract String getRequestServletPath();
331 
332     public abstract java.net.URL getResource(String path) throws java.net.MalformedURLException;
333 
334     public abstract java.io.InputStream getResourceAsStream(String path);
335 
336     public abstract Set<String> getResourcePaths(String path);
337 
338     public abstract Object getResponse();
339 
340     /**
341      *
342      * @return
343      *
344      * @since 2.0
345      */
346     public int getResponseBufferSize()
347     {
348         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
349         
350         if (ctx == null)
351         {
352             throw new UnsupportedOperationException();
353         }
354         
355         return ctx.getResponseBufferSize();
356     }
357 
358     public String getResponseCharacterEncoding()
359     {
360         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
361         
362         if (ctx == null)
363         {
364             throw new UnsupportedOperationException("JSF 1.2 : figure out how to tell if this is a Portlet request");
365         }
366         
367         return ctx.getResponseCharacterEncoding();
368     }
369 
370     /**
371      * throws <code>UnsupportedOperationException</code> by default.
372      *
373      * @since JSF 1.2
374      */
375     public String getResponseContentType()
376     {
377         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
378         
379         if (ctx == null)
380         {
381             throw new UnsupportedOperationException();
382         }
383         
384         return ctx.getResponseContentType();
385     }
386 
387     /**
388      * @since JSF 2.0
389      */
390     public OutputStream getResponseOutputStream() throws IOException
391     {
392         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
393         
394         if (ctx == null)
395         {
396             throw new UnsupportedOperationException();
397         }
398         
399         return ctx.getResponseOutputStream();
400     }
401 
402     /**
403      * @since JSF 2.0
404      */
405     public Writer getResponseOutputWriter() throws IOException
406     {
407         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
408         
409         if (ctx == null)
410         {
411             throw new UnsupportedOperationException();
412         }
413         
414         return ctx.getResponseOutputWriter();
415     }
416 
417     public abstract Object getSession(boolean create);
418 
419     public abstract Map<String, Object> getSessionMap();
420 
421     public abstract java.security.Principal getUserPrincipal();
422 
423     /**
424      * @since 2.0
425      */
426     public void invalidateSession()
427     {
428         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
429         
430         if (ctx == null)
431         {
432             throw new UnsupportedOperationException();
433         }
434         
435         ctx.invalidateSession();
436     }
437 
438     /**
439      * @since 2.0
440      */
441     public boolean isResponseCommitted()
442     {
443         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
444         
445         if (ctx == null)
446         {
447             throw new UnsupportedOperationException();
448         }
449         
450         return ctx.isResponseCommitted();
451     }
452 
453     public abstract boolean isUserInRole(String role);
454 
455     /**
456      * @since 2.0
457      */
458     public abstract void log(String message);
459 
460     /**
461      * @since 2.0
462      */
463     public abstract void log(String message, Throwable exception);
464 
465     public abstract void redirect(String url) throws java.io.IOException;
466 
467     /**
468      *
469      * @throws IOException
470      *
471      * @since 2.0
472      */
473     public void responseFlushBuffer() throws IOException
474     {
475         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
476         
477         if (ctx == null)
478         {
479             throw new UnsupportedOperationException();
480         }
481         
482         ctx.responseFlushBuffer();
483     }
484 
485     /**
486      *
487      * @since 2.0
488      */
489     public void responseReset()
490     {
491         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
492         
493         if (ctx == null)
494         {
495             throw new UnsupportedOperationException();
496         }
497         
498         ctx.responseReset();
499     }
500 
501     /**
502      *
503      * @param statusCode
504      * @param message
505      * @throws IOException
506      *
507      * @since 2.0
508      */
509     public void responseSendError(int statusCode, String message) throws IOException
510     {
511         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
512         
513         if (ctx == null)
514         {
515             throw new UnsupportedOperationException();
516         }
517         
518         ctx.responseSendError(statusCode, message);
519     }
520 
521     /**
522      * throws <code>UnsupportedOperationException</code> by default.
523      *
524      * @since JSF 1.2
525      * @param request
526      */
527     public void setRequest(Object request)
528     {
529         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
530         
531         if (ctx == null)
532         {
533             throw new UnsupportedOperationException();
534         }
535         
536         ctx.setRequest(request);
537     }
538 
539     /**
540      * throws <code>UnsupportedOperationException</code> by default.
541      *
542      * @since JSF 1.2
543      * @param encoding
544      * @throws java.io.UnsupportedEncodingException
545      */
546     public void setRequestCharacterEncoding(java.lang.String encoding)
547             throws java.io.UnsupportedEncodingException
548     {
549         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
550         
551         if (ctx == null)
552         {
553             throw new UnsupportedOperationException();
554         }
555         
556         ctx.setRequestCharacterEncoding(encoding);
557     }
558 
559     /**
560      * throws <code>UnsupportedOperationException</code> by default.
561      *
562      * @since JSF 1.2
563      * @param response
564      */
565     public void setResponse(Object response)
566     {
567         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
568         
569         if (ctx == null)
570         {
571             throw new UnsupportedOperationException();
572         }
573         
574         ctx.setResponse(response);
575     }
576 
577     /**
578      *
579      * @param size
580      *
581      * @since 2.0
582      */
583     public void setResponseBufferSize(int size)
584     {
585         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
586         
587         if (ctx == null)
588         {
589             throw new UnsupportedOperationException();
590         }
591         
592         ctx.setResponseBufferSize(size);
593     }
594 
595     /**
596      * throws <code>UnsupportedOperationException</code> by default.
597      *
598      * @since JSF 1.2
599      * @param encoding
600      */
601     public void setResponseCharacterEncoding(String encoding)
602     {
603         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
604         
605         if (ctx == null)
606         {
607             throw new UnsupportedOperationException();
608         }
609         
610         ctx.setResponseCharacterEncoding(encoding);
611     }
612 
613     /**
614      *
615      * @param length
616      *
617      * @since 2.0
618      */
619     public void setResponseContentLength(int length)
620     {
621         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
622         
623         if (ctx == null)
624         {
625             throw new UnsupportedOperationException();
626         }
627         
628         ctx.setResponseContentLength(length);
629     }
630 
631     /**
632      *
633      * @param contentType
634      *
635      * @since 2.0
636      */
637     public void setResponseContentType(String contentType)
638     {
639         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
640         
641         if (ctx == null)
642         {
643             throw new UnsupportedOperationException();
644         }
645         
646         ctx.setResponseContentType(contentType);
647     }
648 
649     /**
650      *
651      * @param name
652      * @param value
653      *
654      * @since 2.0
655      */
656     public void setResponseHeader(String name, String value)
657     {
658         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
659         
660         if (ctx == null)
661         {
662             throw new UnsupportedOperationException();
663         }
664         
665         ctx.setResponseHeader(name, value);
666     }
667 
668     /**
669      *
670      * @param statusCode
671      *
672      * @since 2.0
673      */
674     public void setResponseStatus(int statusCode)
675     {
676         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
677         
678         if (ctx == null)
679         {
680             throw new UnsupportedOperationException();
681         }
682         
683         ctx.setResponseStatus(statusCode);
684     }
685     
686     /**
687      * 
688      * @since 2.1
689      * @return
690      */
691     public boolean isSecure()
692     {
693         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
694         
695         if (ctx == null)
696         {
697             throw new UnsupportedOperationException();
698         }
699 
700         return ctx.isSecure();
701     }
702     
703     /**
704      * 
705      * @since 2.1
706      * @return
707      */
708     public int getSessionMaxInactiveInterval()
709     {
710         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
711         
712         if (ctx == null)
713         {
714             throw new UnsupportedOperationException();
715         }
716 
717         return ctx.getSessionMaxInactiveInterval();
718     }
719     
720     /**
721      * 
722      * @since 2.1
723      * @param interval
724      */
725     public void setSessionMaxInactiveInterval(int interval)
726     {
727         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
728         
729         if (ctx == null)
730         {
731             throw new UnsupportedOperationException();
732         }
733         
734         ctx.setSessionMaxInactiveInterval(interval);
735     }
736 
737     /**
738      * @since 2.2
739      * @return 
740      */
741     public ClientWindow getClientWindow()
742     {
743         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
744         
745         if (ctx == null)
746         {
747             /*throw new UnsupportedOperationException();*/
748             // TODO: Return null for now, but it should throw exception
749             // in JSF 2.2
750             return null;
751         }
752         
753         return ctx.getClientWindow();
754     }
755     
756     /**
757      * @since 2.2
758      * @param window 
759      */
760     public void setClientWindow(ClientWindow window)
761     {
762         // No op for now.
763         /*
764         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
765         
766         if (ctx == null)
767         {
768             throw new UnsupportedOperationException();
769         }
770         
771         ctx.setClientWindow(window);
772         */
773     }
774     
775     /**
776      * @since 2.2
777      * @param create
778      * @return 
779      */
780     public String getSessionId(boolean create)
781     {
782         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
783         
784         if (ctx == null)
785         {
786             throw new UnsupportedOperationException();
787         }
788         
789         return ctx.getSessionId(create);
790     }
791     
792     /**
793      * @since 2.2
794      * @return
795      */
796     public String getApplicationContextPath()
797     {
798         ExternalContext ctx = _MyFacesExternalContextHelper.firstInstance.get();
799         
800         if (ctx == null)
801         {
802             throw new UnsupportedOperationException();
803         }
804         
805         return ctx.getApplicationContextPath();
806     }
807     
808     /**
809      * @since 2.3
810      * @param url
811      * @return 
812      */
813     public abstract String encodeWebsocketURL(String url);
814 
815 }