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.context.servlet;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.io.Writer;
25  import java.security.Principal;
26  import java.util.Enumeration;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  import javax.faces.context.Flash;
34  import javax.servlet.ServletContext;
35  import javax.servlet.http.HttpSession;
36  
37  /**
38   * An ExternalContext implementation for Servlet environments, which is used
39   * by StartupFacesContextImpl at container startup and shutdown and which
40   * provides ExternalContext functionality that does not require request and
41   * response objects.
42   * 
43   * @author Jakob Korherr (latest modification by $Author$)
44   * @version $Revision$ $Date$
45   */
46  public class StartupServletExternalContextImpl extends ServletExternalContextImplBase
47  {
48      public static final String EXCEPTION_TEXT = "This method is not supported during ";
49      
50      private boolean _startup;
51      private ServletContext _servletContext;
52      private HttpSession _session;
53  
54      public StartupServletExternalContextImpl(final ServletContext servletContext,
55              boolean startup)
56      {
57          super(servletContext);
58          _servletContext = servletContext;
59          _startup = startup;
60      }
61  
62      /**
63       * Allow an HttpSession to be saved for later calls to getSession*
64       *
65       * @param session
66       */
67      public void setSession(HttpSession session)
68      {
69          _session = session;
70      }
71  
72      // ~ Methods which are valid to be called during startup and shutdown------
73      
74      // Note that all methods, which are valid to be called during startup and
75      // shutdown are implemented in ServletExternalContextImplBase, because they 
76      // are exactly the same as in the real ExternalContext implementation.
77      
78      // ~ Methods which are not valid to be called during startup and shutdown, but we implement anyway ------
79      
80      // ~ Methods which are unsupported during startup and shutdown-------------
81  
82      @Override
83      public String encodeActionURL(String url)
84      {
85          throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
86      }
87  
88      @Override
89      public String encodeNamespace(String name)
90      {
91          throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
92      }
93  
94      @Override
95      public String encodeResourceURL(String url)
96      {
97          throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
98      }
99  
100     @Override
101     public String getAuthType()
102     {
103         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
104     }
105 
106     @Override
107     public String getRemoteUser()
108     {
109         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
110     }
111 
112     @Override
113     public Object getRequest()
114     {
115         return null;
116         //throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
117     }
118 
119     @Override
120     public String getRequestContextPath()
121     {
122         return _servletContext.getContextPath();
123     }
124 
125     @Override
126     public Map<String, Object> getRequestCookieMap()
127     {
128         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
129     }
130 
131     @Override
132     public Map<String, String> getRequestHeaderMap()
133     {
134         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
135     }
136 
137     @Override
138     public Map<String, String[]> getRequestHeaderValuesMap()
139     {
140         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
141     }
142 
143     @Override
144     public Locale getRequestLocale()
145     {
146         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
147     }
148 
149     @Override
150     public Iterator<Locale> getRequestLocales()
151     {
152         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
153     }
154 
155     @Override
156     public Map<String, Object> getRequestMap()
157     {
158         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
159     }
160 
161     @Override
162     public Map<String, String> getRequestParameterMap()
163     {
164         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
165     }
166 
167     @Override
168     public Iterator<String> getRequestParameterNames()
169     {
170         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
171     }
172 
173     @Override
174     public Map<String, String[]> getRequestParameterValuesMap()
175     {
176         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
177     }
178 
179     @Override
180     public String getRequestPathInfo()
181     {
182         return "";
183     }
184 
185     @Override
186     public String getRequestServletPath()
187     {
188         return "";
189     }
190 
191     @Override
192     public Object getResponse()
193     {
194         return null;
195         //throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
196     }
197 
198     @Override
199     public Object getSession(boolean create)
200     {
201         if (_session != null)
202         {
203             return _session;
204         }
205         else if (create)
206         {
207             throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
208         }
209         return null;
210     }
211 
212     @Override
213     public String getSessionId(boolean create)
214     {
215         if (_session != null)
216         {
217             return _session.getId();
218         }
219         else if (create)
220         {
221             throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
222         }
223         return null;
224     }
225 
226     @Override
227     public Map<String, Object> getSessionMap()
228     {
229         if (_session != null)
230         {
231             Map<String, Object> sessionMap = new HashMap<String, Object>();
232             Enumeration<String> attributes = _session.getAttributeNames();
233             while (attributes.hasMoreElements())
234             {
235                 String name = attributes.nextElement();
236                 Object value = _session.getAttribute(name);
237                 sessionMap.put(name, value);
238             }
239             return sessionMap;
240         }
241         else
242         {
243             throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
244         }
245     }
246 
247     @Override
248     public Principal getUserPrincipal()
249     {
250         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
251     }
252 
253     @Override
254     public boolean isUserInRole(String role)
255     {
256         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
257     }
258 
259     @Override
260     public String encodeBookmarkableURL(String baseUrl,
261             Map<String, List<String>> parameters)
262     {
263         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
264     }
265 
266     @Override
267     public String encodePartialActionURL(String url)
268     {
269         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
270     }
271 
272     @Override
273     public String encodeRedirectURL(String baseUrl,
274             Map<String, List<String>> parameters)
275     {
276         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
277     }
278 
279     @Override
280     public String getRequestCharacterEncoding()
281     {
282         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
283     }
284 
285     @Override
286     public int getRequestContentLength()
287     {
288         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
289     }
290 
291     @Override
292     public String getRequestContentType()
293     {
294         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
295     }
296 
297     @Override
298     public String getRequestScheme()
299     {
300         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
301     }
302 
303     @Override
304     public String getRequestServerName()
305     {
306         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
307     }
308 
309     @Override
310     public int getRequestServerPort()
311     {
312         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
313     }
314 
315     @Override
316     public int getResponseBufferSize()
317     {
318         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
319     }
320 
321     @Override
322     public String getResponseCharacterEncoding()
323     {
324         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
325     }
326 
327     @Override
328     public String getResponseContentType()
329     {
330         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
331     }
332 
333     @Override
334     public void invalidateSession()
335     {
336         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
337     }
338 
339     @Override
340     public boolean isResponseCommitted()
341     {
342         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
343     }
344 
345     @Override
346     public void setRequest(Object request)
347     {
348         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
349     }
350 
351     @Override
352     public void setRequestCharacterEncoding(String encoding)
353             throws UnsupportedEncodingException
354     {
355         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
356     }
357 
358     @Override
359     public void setResponse(Object response)
360     {
361         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
362     }
363 
364     @Override
365     public void setResponseBufferSize(int size)
366     {
367         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
368     }
369 
370     @Override
371     public void setResponseCharacterEncoding(String encoding)
372     {
373         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
374     }
375 
376     @Override
377     public void setResponseContentLength(int length)
378     {
379         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
380     }
381 
382     @Override
383     public void setResponseContentType(String contentType)
384     {
385         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
386     }
387 
388     @Override
389     public void setResponseHeader(String name, String value)
390     {
391         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
392     }
393     
394     @Override
395     public void setResponseStatus(int statusCode)
396     {
397         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
398     }
399     
400     /**
401      * Cannot dispatch because this is not a page request
402      */
403     @Override
404     public void dispatch(String path) throws IOException
405     {
406         throw new IllegalStateException(EXCEPTION_TEXT + _getTime());
407     }
408     
409     /**
410      * Cannot redirect because this is not a page request
411      */
412     @Override
413     public void redirect(String url) throws IOException
414     {
415         throw new IllegalStateException(EXCEPTION_TEXT + _getTime());
416     }
417     
418 
419     @Override
420     public void responseFlushBuffer() throws IOException
421     {
422         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
423     }
424 
425     @Override
426     public void responseReset()
427     {
428         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
429     }
430 
431     @Override
432     public void responseSendError(int statusCode, String message)
433             throws IOException
434     {
435         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
436     }
437     
438     @Override
439     public void addResponseCookie(String name, String value,
440             Map<String, Object> properties)
441     {
442         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
443     }
444 
445     @Override
446     public void addResponseHeader(String name, String value)
447     {
448         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
449     }
450 
451     @Override
452     public Flash getFlash()
453     {
454         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
455     }
456 
457     @Override
458     public OutputStream getResponseOutputStream() throws IOException
459     {
460         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
461     }
462 
463     @Override
464     public Writer getResponseOutputWriter() throws IOException
465     {
466         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
467     }
468 
469     @Override
470     public boolean isSecure()
471     {
472         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
473     }
474 
475     @Override
476     public int getSessionMaxInactiveInterval()
477     {
478         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());    }
479 
480     @Override
481     public void setSessionMaxInactiveInterval(int interval)
482     {
483         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
484     }
485 
486     @Override
487     public String encodeWebsocketURL(String url)
488     {
489         throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
490     }
491 
492     // ~ private Methods ------------------------------------------------------
493     
494     /**
495      * Returns startup or shutdown as String according to the field _startup.
496      * @return
497      */
498     private String _getTime()
499     {
500         return _startup ? "startup" : "shutdown";
501     }
502 
503 }