-
Notifications
You must be signed in to change notification settings - Fork 2k
/
OAuthRestProxy.java
120 lines (106 loc) · 4.57 KB
/
OAuthRestProxy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Copyright Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.windowsazure.services.media.implementation;
import java.io.IOException;
import java.net.URI;
import javax.inject.Inject;
import javax.ws.rs.core.MediaType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import com.microsoft.windowsazure.services.core.ServiceException;
import com.microsoft.windowsazure.services.core.utils.ServiceExceptionFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.representation.Form;
/**
* The OAuth rest proxy.
*
*/
public class OAuthRestProxy implements OAuthContract {
Client channel;
private final String _grantType = "client_credentials";
static Log log = LogFactory.getLog(OAuthContract.class);
@Inject
public OAuthRestProxy(Client channel) {
this.channel = channel;
}
/**
* Gets an OAuth access token with specified OAUTH URI, client ID, client secret, and scope.
*
* @param oAuthUri
* A <code>URI</code> object which represents an OAUTH URI.
*
* @param clientId
* A <code>String</code> object which represents a client ID.
*
* @param clientSecret
* A <code>String</code> object which represents a client secret.
*
* @param scope
* A <code>String</code> object which represents the scope.
*
* @return OAuthTokenResponse
* @throws ServiceException
*/
@Override
public OAuthTokenResponse getAccessToken(URI oAuthUri, String clientId, String clientSecret, String scope)
throws ServiceException {
OAuthTokenResponse response = null;
Form requestForm = new Form();
ClientResponse clientResponse;
String responseJson;
requestForm.add("grant_type", _grantType);
requestForm.add("client_id", clientId);
requestForm.add("client_secret", clientSecret);
requestForm.add("scope", scope);
try {
clientResponse = channel.resource(oAuthUri).accept(MediaType.APPLICATION_FORM_URLENCODED)
.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, requestForm);
}
catch (UniformInterfaceException e) {
log.warn("OAuth server returned error acquiring access_token", e);
throw ServiceExceptionFactory.process("OAuth", new ServiceException(
"OAuth server returned error acquiring access_token", e));
}
responseJson = clientResponse.getEntity(String.class);
try {
ObjectMapper mapper = new ObjectMapper();
TypeReference<OAuthTokenResponse> typeReference = new TypeReference<OAuthTokenResponse>() {
};
response = mapper.readValue(responseJson, typeReference);
}
catch (JsonParseException e) {
log.warn("The response from OAuth server cannot be parsed correctly", e);
throw ServiceExceptionFactory.process("OAuth", new ServiceException(
"The response from OAuth server cannot be parsed correctly", e));
}
catch (JsonMappingException e) {
log.warn("The response from OAuth server cannot be mapped to OAuthResponse object", e);
throw ServiceExceptionFactory.process("OAuth", new ServiceException(
"The response from OAuth server cannot be mapped to OAuthResponse object", e));
}
catch (IOException e) {
log.warn("Cannot map the response from OAuth server correctly.", e);
throw ServiceExceptionFactory.process("OAuth", new ServiceException(
"Cannot map the response from OAuth server correctly.", e));
}
return response;
}
}