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