|
| 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 | +} |
0 commit comments