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