View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.web.jakarta.faces;
18  
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.apache.commons.chain.web.jakarta.WebContext;
24  
25  import jakarta.faces.context.FacesContext;
26  import jakarta.servlet.http.Cookie;
27  
28  /**
29   * Concrete implementation of {@link WebContext} suitable for use in
30   * JavaServer Faces apps. The abstract methods are mapped to the appropriate
31   * collections of the underlying {@code FacesContext} instance
32   * that is passed to the constructor (or the initialize method).
33   *
34   * @author Craig R. McClanahan
35   * @version $Revision$ $Date$
36   */
37  public class FacesWebContext extends WebContext {
38      private static final long serialVersionUID = -1429681424077509130L;
39  
40      // ------------------------------------------------------ Instance Variables
41  
42      /**
43       * The {@code FacesContext} instance for the request represented
44       * by this {@link WebContext}.
45       */
46      private transient FacesContext context = null;
47  
48      /**
49       * The lazily instantiated {@code Map} of cookies.
50       */
51      private transient Map<String, Cookie> cookieValues = null;
52  
53      // ------------------------------------------------------------ Constructors
54  
55      /**
56       * Construct an uninitialized {@link FacesWebContext} instance.
57       */
58      public FacesWebContext() {
59      }
60  
61      /**
62       * Construct a {@link FacesWebContext} instance that is initialized
63       * with the specified JavaServer Faces API objects.
64       *
65       * @param context The {@code FacesContext} for this request
66       */
67      public FacesWebContext(FacesContext context) {
68          initialize(context);
69      }
70      // ---------------------------------------------------------- Public Methods
71  
72      /**
73       * Return the {@code FacesContext} instance for the request
74       * associated with this {@link FacesWebContext}.
75       *
76       * @return The {@code FacesContext} for this request
77       */
78      public FacesContext getContext() {
79          return this.context;
80      }
81  
82      /**
83       * Initialize (or reinitialize) this {@link FacesWebContext} instance
84       * for the specified JavaServer Faces API objects.
85       *
86       * @param context The {@code FacesContext} for this request
87       */
88      public void initialize(FacesContext context) {
89          this.context = context;
90      }
91  
92      /**
93       * Release references to allocated resources acquired in
94       * {@code initialize()} of via subsequent processing. After this
95       * method is called, subsequent calls to any other method than
96       * {@code initialize()} will return undefined results.
97       */
98      public void release() {
99          context = null;
100         cookieValues = null;
101     }
102 
103     // ------------------------------------------------------ WebContext Methods
104 
105     /**
106      * See the {@link WebContext}'s Javadoc.
107      *
108      * @return Application scope Map.
109      */
110     @Override
111     public Map<String, Object> getApplicationScope() {
112         return context.getExternalContext().getApplicationMap();
113     }
114 
115     /**
116      * See the {@link WebContext}'s Javadoc.
117      *
118      * @return Header values Map.
119      */
120     @Override
121     public Map<String, String> getHeader() {
122         return context.getExternalContext().getRequestHeaderMap();
123     }
124 
125     /**
126      * See the {@link WebContext}'s Javadoc.
127      *
128      * @return Header values Map.
129      */
130     @Override
131     public Map<String, String[]> getHeaderValues() {
132         return context.getExternalContext().getRequestHeaderValuesMap();
133     }
134 
135     /**
136      * See the {@link WebContext}'s Javadoc.
137      *
138      * @return Initialization parameter Map.
139      */
140     @Override
141     public Map<String, String> getInitParam() {
142         return context.getExternalContext().getInitParameterMap();
143     }
144 
145     /**
146      * See the {@link WebContext}'s Javadoc.
147      *
148      * @return Request parameter Map.
149      */
150     @Override
151     public Map<String, String> getParam() {
152         return context.getExternalContext().getRequestParameterMap();
153     }
154 
155     /**
156      * See the {@link WebContext}'s Javadoc.
157      *
158      * @return Request parameter Map.
159      */
160     @Override
161     public Map<String, String[]> getParamValues() {
162         return context.getExternalContext().getRequestParameterValuesMap();
163     }
164 
165     /**
166      * See the {@link WebContext}'s Javadoc.
167      *
168      * @return Map of Cookies.
169      * @since Chain 1.1
170      */
171     @Override
172     public Map<String, Cookie> getCookies() {
173         if (cookieValues == null) {
174             final Map<String, Object> cookiesSrc = context.getExternalContext().getRequestCookieMap();
175             final Map<String, Cookie> cookiesDest = new HashMap<>(cookiesSrc.size());
176 
177             cookiesSrc.forEach((k, v) -> cookiesDest.put(k, (Cookie) v));
178 
179             cookieValues = Collections.unmodifiableMap(cookiesDest);
180         }
181 
182         return cookieValues;
183     }
184 
185     /**
186      * See the {@link WebContext}'s Javadoc.
187      *
188      * @return Request scope Map.
189      */
190     @Override
191     public Map<String, Object> getRequestScope() {
192         return context.getExternalContext().getRequestMap();
193     }
194 
195     /**
196      * See the {@link WebContext}'s Javadoc.
197      *
198      * @return Session scope Map.
199      */
200     @Override
201     public Map<String, Object> getSessionScope() {
202         return context.getExternalContext().getSessionMap();
203     }
204 }