View Javadoc
1   /*
2    * Copyright 2002-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.security.oauth2.client.token;
17  
18  import org.springframework.security.oauth2.common.OAuth2AccessToken;
19  import org.springframework.util.LinkedMultiValueMap;
20  import org.springframework.util.MultiValueMap;
21  
22  import java.io.Serializable;
23  import java.util.*;
24  
25  /**
26   * Local context for an access token request encapsulating the parameters that are sent by the client requesting the
27   * token, as opposed to the more static variables representing the client itself and the resource being targeted.
28   * 
29   * @author Dave Syer
30   * 
31   */
32  public class DefaultAccessTokenRequest implements AccessTokenRequest, Serializable {
33  
34  	private static final long serialVersionUID = 914967629530462926L;
35  
36  	private final MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
37  
38  	private Object state;
39  
40  	private OAuth2AccessToken existingToken;
41  
42  	private String currentUri;
43  
44  	private String cookie;
45  
46  	private Map<? extends String, ? extends List<String>> headers = new LinkedMultiValueMap<String, String>();
47  
48  	public DefaultAccessTokenRequest() {
49  	}
50  
51  	public DefaultAccessTokenRequest(Map<String, String[]> parameters) {
52  		if (parameters!=null) {
53  			for (Entry<String,String[]> entry : parameters.entrySet()) {
54  				this.parameters.put(entry.getKey(), Arrays.asList(entry.getValue()));
55  			}
56  		}
57  	}
58  
59  	public boolean isError() {
60  		return parameters.containsKey("error");
61  	}
62  
63  	public Object getPreservedState() {
64  		return state;
65  	}
66  
67  	public void setPreservedState(Object state) {
68  		this.state = state;
69  	}
70  
71  	public String getStateKey() {
72  		return getFirst("state");
73  	}
74  
75  	public void setStateKey(String state) {
76  		parameters.set("state", state);
77  	}
78  
79  	/**
80  	 * The current URI that is being handled on the client.
81  	 * 
82  	 * @return The URI.
83  	 */
84  
85  	public String getCurrentUri() {
86  		return currentUri;
87  	}
88  
89  	public void setCurrentUri(String uri) {
90  		currentUri = uri;
91  	}
92  
93  	/**
94  	 * The authorization code for this context.
95  	 * 
96  	 * @return The authorization code, or null if none.
97  	 */
98  
99  	public String getAuthorizationCode() {
100 		return getFirst("code");
101 	}
102 
103 	public void setAuthorizationCode(String code) {
104 		parameters.set("code", code);
105 	}
106 
107 	public void setCookie(String cookie) {
108 		this.cookie = cookie;	}
109 	
110 	public String getCookie() {
111 		return cookie;
112 	}
113 	
114 	public void setHeaders(Map<? extends String, ? extends List<String>> headers) {
115 		this.headers = headers;
116 	}
117 	
118 	public Map<? extends String, ? extends List<String>> getHeaders() {
119 		return headers;
120 	}
121 
122 	public void setExistingToken(OAuth2AccessToken existingToken) {
123 		this.existingToken = existingToken;
124 	}
125 
126 	public OAuth2AccessToken getExistingToken() {
127 		return existingToken;
128 	}
129 
130 	public String getFirst(String key) {
131 		return parameters.getFirst(key);
132 	}
133 
134 	public void add(String key, String value) {
135 		parameters.add(key, value);
136 	}
137 
138 	public void addAll(String key, List<? extends String> values) {
139 		for (String value : values) {
140 			this.add(key, value);
141 		}
142 	}
143 
144 	public void addAll(MultiValueMap<String, String> map) {
145 		for (Entry<String, List<String>> entry : map.entrySet()) {
146 			this.addAll(entry.getKey(), entry.getValue());
147 		}
148 	}
149 
150 	public void set(String key, String value) {
151 		parameters.set(key, value);
152 	}
153 
154 	public void setAll(Map<String, String> values) {
155 		parameters.setAll(values);
156 	}
157 
158 	public Map<String, String> toSingleValueMap() {
159 		return parameters.toSingleValueMap();
160 	}
161 
162 	public int size() {
163 		return parameters.size();
164 	}
165 
166 	public boolean isEmpty() {
167 		return parameters.isEmpty();
168 	}
169 
170 	public boolean containsKey(Object key) {
171 		return parameters.containsKey(key);
172 	}
173 
174 	public boolean containsValue(Object value) {
175 		return parameters.containsValue(value);
176 	}
177 
178 	public List<String> get(Object key) {
179 		return parameters.get(key);
180 	}
181 
182 	public List<String> put(String key, List<String> value) {
183 		return parameters.put(key, value);
184 	}
185 
186 	public List<String> remove(Object key) {
187 		return parameters.remove(key);
188 	}
189 
190 	public void putAll(Map<? extends String, ? extends List<String>> m) {
191 		parameters.putAll(m);
192 	}
193 
194 	public void clear() {
195 		parameters.clear();
196 	}
197 
198 	public Set<String> keySet() {
199 		return parameters.keySet();
200 	}
201 
202 	public Collection<List<String>> values() {
203 		return parameters.values();
204 	}
205 
206 	public Set<java.util.Map.Entry<String, List<String>>> entrySet() {
207 		return parameters.entrySet();
208 	}
209 
210 	public boolean equals(Object o) {
211 		return parameters.equals(o);
212 	}
213 
214 	public int hashCode() {
215 		return parameters.hashCode();
216 	}
217 	
218 	public String toString() {
219 		return parameters.toString();
220 	}
221 
222 }