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.custom.conversation;
20  
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.ServletContext;
25  
26  /**
27   * we need some information we normally get from the FacesContext.getExternalContext BEFORE the FacesContext is active.
28   * So we require our own ExternalContext.
29   */
30  public class ConversationExternalContext
31  {
32      private final Map requestMap;
33      private final Map requestParameterMap;
34      private final Map initParameterMap;
35  
36      protected ConversationExternalContext(Map initParameterMap, Map requestMap, Map requestParameterMap)
37      {
38          this.requestMap = requestMap;
39          this.requestParameterMap = requestParameterMap;
40          this.initParameterMap = initParameterMap;
41      }
42  
43      public static ConversationExternalContext create(final ServletContext servletContext, final HttpServletRequest httpRequest)
44      {
45          Map initParameterMap = new FakeMap()
46          {
47              public boolean containsKey(Object key)
48              {
49                  return servletContext.getInitParameter((String) key) != null;
50              }
51  
52              public Object get(Object key)
53              {
54                  return servletContext.getInitParameter((String) key);
55              }
56          };
57  
58          Map requestParameterMap = new FakeMap()
59          {
60              public boolean containsKey(Object key)
61              {
62                  return httpRequest.getParameter((String) key) != null;
63              }
64  
65              public Object get(Object key)
66              {
67                  return httpRequest.getParameter((String) key);
68              }
69          };
70  
71          Map requestMap = new FakeMap()
72          {
73              public boolean containsKey(Object key)
74              {
75                  return httpRequest.getAttribute((String) key) != null;
76              }
77  
78              public Object get(Object key)
79              {
80                  return httpRequest.getAttribute((String) key);
81              }
82  
83              public Object put(Object key, Object value)
84              {
85                  Object prev = httpRequest.getAttribute((String) key);
86                  httpRequest.setAttribute((String) key, value);
87                  return prev;
88              }
89          };
90  
91          return new ConversationExternalContext(initParameterMap, requestMap, requestParameterMap);
92      }
93  
94      public Map getRequestMap()
95      {
96          return requestMap;
97      }
98  
99      public Map getRequestParameterMap()
100     {
101         return requestParameterMap;
102     }
103 
104     public Map getInitParameterMap()
105     {
106         return initParameterMap;
107     }
108 }