Skip to content

Commit 7969218

Browse files
committed
migrate API changes
1 parent aa37ab5 commit 7969218

File tree

6 files changed

+407
-12
lines changed

6 files changed

+407
-12
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Intuit
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+
* http://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 com.intuit.oauth2.client;
17+
18+
import org.json.JSONObject;
19+
import org.slf4j.Logger;
20+
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.databind.ObjectReader;
23+
import com.intuit.oauth2.config.Environment;
24+
import com.intuit.oauth2.config.Scope;
25+
import com.intuit.oauth2.data.OAuthMigrationRequest;
26+
import com.intuit.oauth2.data.OAuthMigrationResponse;
27+
import com.intuit.oauth2.exception.ConnectionException;
28+
import com.intuit.oauth2.http.HttpRequestClient;
29+
import com.intuit.oauth2.http.MethodType;
30+
import com.intuit.oauth2.http.Request;
31+
import com.intuit.oauth2.http.Response;
32+
import com.intuit.oauth2.utils.LoggerImpl;
33+
import com.intuit.oauth2.utils.MapperImpl;
34+
import com.intuit.oauth2.utils.PropertiesConfig;
35+
36+
/**
37+
* Client class for OAuthMigration API
38+
*
39+
* @author dderose
40+
*
41+
*/
42+
public class OAuthMigrationClient {
43+
44+
45+
private OAuthMigrationRequest oAuthMigrationRequest;
46+
47+
private static final Logger logger = LoggerImpl.getInstance();
48+
private static final ObjectMapper mapper = MapperImpl.getInstance();
49+
50+
public OAuthMigrationClient(OAuthMigrationRequest request) {
51+
this.oAuthMigrationRequest = request;
52+
}
53+
54+
/**
55+
* Hiding the default constructor as OAuth2PlatformClient is always required to function properly
56+
*/
57+
protected OAuthMigrationClient() {
58+
59+
}
60+
61+
/**
62+
* Calls the migrate API based on the the request provided and
63+
* returns an object with oauth2 tokens
64+
*
65+
* @param environment
66+
* @return
67+
* @throws ConnectionException
68+
*/
69+
public OAuthMigrationResponse migrate() throws ConnectionException {
70+
71+
logger.debug("Enter OAuthMigrationClient::migrate");
72+
73+
try {
74+
75+
76+
HttpRequestClient client = new HttpRequestClient(oAuthMigrationRequest.getOauth2config().getProxyConfig());
77+
78+
//prepare post json
79+
String requestjson = new JSONObject().put("scope", getScopeValue(oAuthMigrationRequest.getScope()))
80+
.put("redirect_uri", getRedirectUrl() )
81+
.put("client_id", oAuthMigrationRequest.getOauth2config().getClientId())
82+
.put("client_secret",oAuthMigrationRequest.getOauth2config().getClientSecret())
83+
.toString();
84+
85+
Request request = new Request.RequestBuilder(MethodType.GET, getMigrationAPIUrl(oAuthMigrationRequest.getEnvironment()))
86+
.requiresAuthentication(true)
87+
.postJson(requestjson)
88+
.build();
89+
//make the API call
90+
Response response = client.makeJsonRequest(request, oAuthMigrationRequest);
91+
92+
logger.debug("Response Code : " + response.getStatusCode());
93+
if (response.getStatusCode() == 200) {
94+
ObjectReader reader = mapper.readerFor(OAuthMigrationResponse.class);
95+
OAuthMigrationResponse oAuthMigrationResponse = reader.readValue(response.getContent());
96+
return oAuthMigrationResponse;
97+
98+
} else {
99+
logger.debug("failed calling migrate API");
100+
throw new ConnectionException("failed calling migrate API", response.getStatusCode() + "");
101+
}
102+
} catch (Exception ex) {
103+
logger.error("Exception while calling migrate", ex);
104+
throw new ConnectionException(ex.getMessage(), ex);
105+
}
106+
}
107+
108+
109+
private static String getMigrationAPIUrl(Environment environment) {
110+
return PropertiesConfig.getInstance().getProperty("OAUTH_MIGRATION_URL_" + environment.value());
111+
}
112+
113+
public String getScopeValue(Scope scope) {
114+
return PropertiesConfig.getInstance().getProperty(scope.value());
115+
}
116+
117+
public String getRedirectUrl() {
118+
String url = PropertiesConfig.getInstance().getProperty("REDIRECT_URL");
119+
return (oAuthMigrationRequest.getRedirectUri() == null || oAuthMigrationRequest.getRedirectUri().isEmpty()) ? url: oAuthMigrationRequest.getRedirectUri();
120+
}
121+
122+
123+
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Intuit
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+
* http://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 com.intuit.oauth2.data;
17+
18+
import com.intuit.oauth2.config.Environment;
19+
import com.intuit.oauth2.config.OAuth2Config;
20+
import com.intuit.oauth2.config.Scope;
21+
22+
public class OAuthMigrationRequest {
23+
24+
//Environment
25+
private Environment environment;
26+
27+
//OAuth2 client id, secret
28+
private OAuth2Config oauth2config;
29+
30+
//OAuth1 consumer data
31+
private String consumerKey;
32+
private String consumerSecret;
33+
private String accessToken;
34+
private String accessSecret;
35+
36+
//Scope
37+
private Scope scope;
38+
39+
//Redirect URL
40+
private String redirectUri;
41+
42+
private OAuthMigrationRequest(OAuthMigrationRequestBuilder builder) {
43+
44+
this.environment = builder.environment;
45+
this.oauth2config = builder.oauth2config;
46+
this.consumerKey = builder.consumerKey;
47+
this.consumerSecret = builder.consumerSecret;
48+
this.accessToken = builder.accessToken;
49+
this.accessSecret = builder.accessSecret;
50+
this.scope = builder.scope;
51+
this.redirectUri = builder.redirectUri;
52+
53+
}
54+
55+
public Environment getEnvironment() {
56+
return environment;
57+
}
58+
59+
public OAuth2Config getOauth2config() {
60+
return oauth2config;
61+
}
62+
63+
public String getConsumerKey() {
64+
return consumerKey;
65+
}
66+
67+
public String getConsumerSecret() {
68+
return consumerSecret;
69+
}
70+
71+
public String getAccessToken() {
72+
return accessToken;
73+
}
74+
75+
public String getAccessSecret() {
76+
return accessSecret;
77+
}
78+
79+
public Scope getScope() {
80+
return scope;
81+
}
82+
83+
public String getRedirectUri() {
84+
return redirectUri;
85+
}
86+
87+
public static class OAuthMigrationRequestBuilder {
88+
89+
//Environment
90+
private Environment environment;
91+
92+
//OAuth2 client id, secret
93+
private OAuth2Config oauth2config;
94+
95+
//OAuth1 consumer data
96+
private String consumerKey;
97+
private String consumerSecret;
98+
private String accessToken;
99+
private String accessSecret;
100+
101+
//Scope
102+
private Scope scope;
103+
104+
//Redirect URL
105+
private String redirectUri;
106+
107+
public OAuthMigrationRequestBuilder(Environment environment, Scope scope) {
108+
this.environment = environment;
109+
this.scope = scope;
110+
}
111+
112+
public OAuthMigrationRequestBuilder consumerKey(String consumerKey) {
113+
this.consumerKey = consumerKey;
114+
return this;
115+
}
116+
117+
public OAuthMigrationRequestBuilder consumerSecret(String consumerSecret) {
118+
this.consumerSecret = consumerSecret;
119+
return this;
120+
}
121+
122+
public OAuthMigrationRequestBuilder accessToken(String accessToken) {
123+
this.accessToken = accessToken;
124+
return this;
125+
}
126+
127+
public OAuthMigrationRequestBuilder accessSecret(String accessSecret) {
128+
this.accessSecret = accessSecret;
129+
return this;
130+
}
131+
132+
public OAuthMigrationRequestBuilder oAuth2Config(OAuth2Config oAuth2Config) {
133+
this.oauth2config = oAuth2Config;
134+
return this;
135+
}
136+
137+
public OAuthMigrationRequestBuilder redirectUri(String redirectUri) {
138+
this.redirectUri = redirectUri;
139+
return this;
140+
}
141+
142+
public OAuthMigrationRequest build() {
143+
return new OAuthMigrationRequest(this);
144+
}
145+
146+
}
147+
148+
149+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 Intuit
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+
* http://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 com.intuit.oauth2.data;
17+
18+
import javax.annotation.Generated;
19+
20+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
21+
import com.fasterxml.jackson.annotation.JsonInclude;
22+
import com.fasterxml.jackson.annotation.JsonProperty;
23+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
24+
25+
@JsonInclude(JsonInclude.Include.NON_NULL)
26+
@Generated("org.jsonschema2pojo")
27+
@JsonPropertyOrder({
28+
"realmId"
29+
})
30+
31+
@JsonIgnoreProperties(ignoreUnknown = true)
32+
public class OAuthMigrationResponse extends BearerTokenResponse{
33+
34+
@JsonProperty("realmId")
35+
private String realmId;
36+
37+
@JsonProperty("realmId")
38+
public String getRealmId() {
39+
return realmId;
40+
}
41+
42+
@JsonProperty("realmId")
43+
public void setRealmId(String realmId) {
44+
this.realmId = realmId;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)