001/* 002 * Copyright 2002-2016 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; 023 024import javax.faces.context.ExternalContext; 025import javax.faces.context.FacesContext; 026 027import org.springframework.lang.Nullable; 028import org.springframework.util.StringUtils; 029 030/** 031 * {@link WebRequest} adapter for a JSF {@link javax.faces.context.FacesContext}. 032 * 033 * <p>Requires JSF 2.0 or higher, as of Spring 4.0. 034 * 035 * @author Juergen Hoeller 036 * @since 2.5.2 037 */ 038public class FacesWebRequest extends FacesRequestAttributes implements NativeWebRequest { 039 040 /** 041 * Create a new FacesWebRequest adapter for the given FacesContext. 042 * @param facesContext the current FacesContext 043 * @see javax.faces.context.FacesContext#getCurrentInstance() 044 */ 045 public FacesWebRequest(FacesContext facesContext) { 046 super(facesContext); 047 } 048 049 050 @Override 051 public Object getNativeRequest() { 052 return getExternalContext().getRequest(); 053 } 054 055 @Override 056 public Object getNativeResponse() { 057 return getExternalContext().getResponse(); 058 } 059 060 @Override 061 @SuppressWarnings("unchecked") 062 public <T> T getNativeRequest(@Nullable Class<T> requiredType) { 063 if (requiredType != null) { 064 Object request = getExternalContext().getRequest(); 065 if (requiredType.isInstance(request)) { 066 return (T) request; 067 } 068 } 069 return null; 070 } 071 072 @Override 073 @SuppressWarnings("unchecked") 074 public <T> T getNativeResponse(@Nullable Class<T> requiredType) { 075 if (requiredType != null) { 076 Object response = getExternalContext().getResponse(); 077 if (requiredType.isInstance(response)) { 078 return (T) response; 079 } 080 } 081 return null; 082 } 083 084 085 @Override 086 @Nullable 087 public String getHeader(String headerName) { 088 return getExternalContext().getRequestHeaderMap().get(headerName); 089 } 090 091 @Override 092 @Nullable 093 public String[] getHeaderValues(String headerName) { 094 return getExternalContext().getRequestHeaderValuesMap().get(headerName); 095 } 096 097 @Override 098 public Iterator<String> getHeaderNames() { 099 return getExternalContext().getRequestHeaderMap().keySet().iterator(); 100 } 101 102 @Override 103 @Nullable 104 public String getParameter(String paramName) { 105 return getExternalContext().getRequestParameterMap().get(paramName); 106 } 107 108 @Override 109 public Iterator<String> getParameterNames() { 110 return getExternalContext().getRequestParameterNames(); 111 } 112 113 @Override 114 @Nullable 115 public String[] getParameterValues(String paramName) { 116 return getExternalContext().getRequestParameterValuesMap().get(paramName); 117 } 118 119 @Override 120 public Map<String, String[]> getParameterMap() { 121 return getExternalContext().getRequestParameterValuesMap(); 122 } 123 124 @Override 125 public Locale getLocale() { 126 return getFacesContext().getExternalContext().getRequestLocale(); 127 } 128 129 @Override 130 public String getContextPath() { 131 return getFacesContext().getExternalContext().getRequestContextPath(); 132 } 133 134 @Override 135 @Nullable 136 public String getRemoteUser() { 137 return getFacesContext().getExternalContext().getRemoteUser(); 138 } 139 140 @Override 141 @Nullable 142 public Principal getUserPrincipal() { 143 return getFacesContext().getExternalContext().getUserPrincipal(); 144 } 145 146 @Override 147 public boolean isUserInRole(String role) { 148 return getFacesContext().getExternalContext().isUserInRole(role); 149 } 150 151 @Override 152 public boolean isSecure() { 153 return false; 154 } 155 156 @Override 157 public boolean checkNotModified(long lastModifiedTimestamp) { 158 return false; 159 } 160 161 @Override 162 public boolean checkNotModified(@Nullable String eTag) { 163 return false; 164 } 165 166 @Override 167 public boolean checkNotModified(@Nullable String etag, long lastModifiedTimestamp) { 168 return false; 169 } 170 171 @Override 172 public String getDescription(boolean includeClientInfo) { 173 ExternalContext externalContext = getExternalContext(); 174 StringBuilder sb = new StringBuilder(); 175 sb.append("context=").append(externalContext.getRequestContextPath()); 176 if (includeClientInfo) { 177 Object session = externalContext.getSession(false); 178 if (session != null) { 179 sb.append(";session=").append(getSessionId()); 180 } 181 String user = externalContext.getRemoteUser(); 182 if (StringUtils.hasLength(user)) { 183 sb.append(";user=").append(user); 184 } 185 } 186 return sb.toString(); 187 } 188 189 190 @Override 191 public String toString() { 192 return "FacesWebRequest: " + getDescription(true); 193 } 194 195}