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