001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.context.request;
018
019import java.security.Principal;
020import java.util.Iterator;
021import java.util.Locale;
022import java.util.Map;
023import javax.faces.context.ExternalContext;
024import javax.faces.context.FacesContext;
025
026import org.springframework.util.StringUtils;
027
028/**
029 * {@link WebRequest} adapter for a JSF {@link javax.faces.context.FacesContext}.
030 *
031 * <p>Requires JSF 2.0 or higher, as of Spring 4.0.
032 *
033 * @author Juergen Hoeller
034 * @since 2.5.2
035 */
036public class FacesWebRequest extends FacesRequestAttributes implements NativeWebRequest {
037
038        /**
039         * Create a new FacesWebRequest adapter for the given FacesContext.
040         * @param facesContext the current FacesContext
041         * @see javax.faces.context.FacesContext#getCurrentInstance()
042         */
043        public FacesWebRequest(FacesContext facesContext) {
044                super(facesContext);
045        }
046
047
048        @Override
049        public Object getNativeRequest() {
050                return getExternalContext().getRequest();
051        }
052
053        @Override
054        public Object getNativeResponse() {
055                return getExternalContext().getResponse();
056        }
057
058        @Override
059        @SuppressWarnings("unchecked")
060        public <T> T getNativeRequest(Class<T> requiredType) {
061                if (requiredType != null) {
062                        Object request = getExternalContext().getRequest();
063                        if (requiredType.isInstance(request)) {
064                                return (T) request;
065                        }
066                }
067                return null;
068        }
069
070        @Override
071        @SuppressWarnings("unchecked")
072        public <T> T getNativeResponse(Class<T> requiredType) {
073                if (requiredType != null) {
074                        Object response = getExternalContext().getResponse();
075                        if (requiredType.isInstance(response)) {
076                                return (T) response;
077                        }
078                }
079                return null;
080        }
081
082
083        @Override
084        public String getHeader(String headerName) {
085                return getExternalContext().getRequestHeaderMap().get(headerName);
086        }
087
088        @Override
089        public String[] getHeaderValues(String headerName) {
090                return getExternalContext().getRequestHeaderValuesMap().get(headerName);
091        }
092
093        @Override
094        public Iterator<String> getHeaderNames() {
095                return getExternalContext().getRequestHeaderMap().keySet().iterator();
096        }
097
098        @Override
099        public String getParameter(String paramName) {
100                return getExternalContext().getRequestParameterMap().get(paramName);
101        }
102
103        @Override
104        public Iterator<String> getParameterNames() {
105                return getExternalContext().getRequestParameterNames();
106        }
107
108        @Override
109        public String[] getParameterValues(String paramName) {
110                return getExternalContext().getRequestParameterValuesMap().get(paramName);
111        }
112
113        @Override
114        public Map<String, String[]> getParameterMap() {
115                return getExternalContext().getRequestParameterValuesMap();
116        }
117
118        @Override
119        public Locale getLocale() {
120                return getFacesContext().getExternalContext().getRequestLocale();
121        }
122
123        @Override
124        public String getContextPath() {
125                return getFacesContext().getExternalContext().getRequestContextPath();
126        }
127
128        @Override
129        public String getRemoteUser() {
130                return getFacesContext().getExternalContext().getRemoteUser();
131        }
132
133        @Override
134        public Principal getUserPrincipal() {
135                return getFacesContext().getExternalContext().getUserPrincipal();
136        }
137
138        @Override
139        public boolean isUserInRole(String role) {
140                return getFacesContext().getExternalContext().isUserInRole(role);
141        }
142
143        @Override
144        public boolean isSecure() {
145                return false;
146        }
147
148        @Override
149        public boolean checkNotModified(long lastModifiedTimestamp) {
150                return false;
151        }
152
153        @Override
154        public boolean checkNotModified(String eTag) {
155                return false;
156        }
157
158        /**
159         * Last-modified handling not supported for portlet requests:
160         * As a consequence, this method always returns {@code false}.
161         * @since 4.2
162         */
163        @Override
164        public boolean checkNotModified(String etag, long lastModifiedTimestamp) {
165                return false;
166        }
167
168        @Override
169        public String getDescription(boolean includeClientInfo) {
170                ExternalContext externalContext = getExternalContext();
171                StringBuilder sb = new StringBuilder();
172                sb.append("context=").append(externalContext.getRequestContextPath());
173                if (includeClientInfo) {
174                        Object session = externalContext.getSession(false);
175                        if (session != null) {
176                                sb.append(";session=").append(getSessionId());
177                        }
178                        String user = externalContext.getRemoteUser();
179                        if (StringUtils.hasLength(user)) {
180                                sb.append(";user=").append(user);
181                        }
182                }
183                return sb.toString();
184        }
185
186
187        @Override
188        public String toString() {
189                return "FacesWebRequest: " + getDescription(true);
190        }
191
192}