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