001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.chain.web.jakarta.faces;
018
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.apache.commons.chain.web.jakarta.WebContext;
024
025import jakarta.faces.context.FacesContext;
026import jakarta.servlet.http.Cookie;
027
028/**
029 * Concrete implementation of {@link WebContext} suitable for use in
030 * JavaServer Faces apps. The abstract methods are mapped to the appropriate
031 * collections of the underlying {@code FacesContext} instance
032 * that is passed to the constructor (or the initialize method).
033 *
034 * @author Craig R. McClanahan
035 * @version $Revision$ $Date$
036 */
037public class FacesWebContext extends WebContext {
038    private static final long serialVersionUID = -1429681424077509130L;
039
040    // ------------------------------------------------------ Instance Variables
041
042    /**
043     * The {@code FacesContext} instance for the request represented
044     * by this {@link WebContext}.
045     */
046    private transient FacesContext context = null;
047
048    /**
049     * The lazily instantiated {@code Map} of cookies.
050     */
051    private transient Map<String, Cookie> cookieValues = null;
052
053    // ------------------------------------------------------------ Constructors
054
055    /**
056     * Construct an uninitialized {@link FacesWebContext} instance.
057     */
058    public FacesWebContext() {
059    }
060
061    /**
062     * Construct a {@link FacesWebContext} instance that is initialized
063     * with the specified JavaServer Faces API objects.
064     *
065     * @param context The {@code FacesContext} for this request
066     */
067    public FacesWebContext(FacesContext context) {
068        initialize(context);
069    }
070    // ---------------------------------------------------------- Public Methods
071
072    /**
073     * Return the {@code FacesContext} instance for the request
074     * associated with this {@link FacesWebContext}.
075     *
076     * @return The {@code FacesContext} for this request
077     */
078    public FacesContext getContext() {
079        return this.context;
080    }
081
082    /**
083     * Initialize (or reinitialize) this {@link FacesWebContext} instance
084     * for the specified JavaServer Faces API objects.
085     *
086     * @param context The {@code FacesContext} for this request
087     */
088    public void initialize(FacesContext context) {
089        this.context = context;
090    }
091
092    /**
093     * Release references to allocated resources acquired in
094     * {@code initialize()} of via subsequent processing. After this
095     * method is called, subsequent calls to any other method than
096     * {@code initialize()} will return undefined results.
097     */
098    public void release() {
099        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}