1
2
3
4
5
6
7
8
9
10
11
12
13 package org.springframework.security.oauth2.http.converter;
14
15 import java.io.IOException;
16
17 import org.springframework.http.HttpInputMessage;
18 import org.springframework.http.HttpOutputMessage;
19 import org.springframework.http.MediaType;
20 import org.springframework.http.converter.AbstractHttpMessageConverter;
21 import org.springframework.http.converter.FormHttpMessageConverter;
22 import org.springframework.http.converter.HttpMessageNotReadableException;
23 import org.springframework.http.converter.HttpMessageNotWritableException;
24 import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
25 import org.springframework.security.oauth2.common.OAuth2AccessToken;
26 import org.springframework.util.MultiValueMap;
27
28
29
30
31
32
33
34
35
36 public class FormOAuth2AccessTokenMessageConverter extends AbstractHttpMessageConverter<OAuth2AccessToken> {
37
38 private final FormHttpMessageConverter delegateMessageConverter;
39
40 public FormOAuth2AccessTokenMessageConverter() {
41 super(MediaType.APPLICATION_FORM_URLENCODED, MediaType.TEXT_PLAIN);
42 this.delegateMessageConverter = new FormHttpMessageConverter();
43 }
44
45 @Override
46 protected boolean supports(Class<?> clazz) {
47 return OAuth2AccessToken.class.equals(clazz);
48 }
49
50 @Override
51 protected OAuth2AccessToken readInternal(Class<? extends OAuth2AccessToken> clazz, HttpInputMessage inputMessage)
52 throws IOException, HttpMessageNotReadableException {
53 MultiValueMap<String, String> data = delegateMessageConverter.read(null, inputMessage);
54 return DefaultOAuth2AccessToken.valueOf(data.toSingleValueMap());
55 }
56
57 @Override
58 protected void writeInternal(OAuth2AccessToken accessToken, HttpOutputMessage outputMessage) throws IOException,
59 HttpMessageNotWritableException {
60 throw new UnsupportedOperationException(
61 "This converter is only used for converting from externally aqcuired form data");
62 }
63 }