|
|
|
@ -19,121 +19,88 @@
|
|
|
|
|
package io.entgra.device.mgt.core.apimgt.extension.rest.api;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.*;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.APIInfo;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.ApplicationKey;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.KeyManager;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Scopes;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Subscription;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.OAuthClientResponse;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.constants.Constants;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.ApiApplicationInfo;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.OAuthClientException;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.internal.APIManagerServiceDataHolder;
|
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
|
import okhttp3.Request;
|
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
|
import org.apache.commons.httpclient.HttpStatus;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(ConsumerRESTAPIServicesImpl.class);
|
|
|
|
|
private static final OkHttpClient client = new OkHttpClient(HttpsTrustManagerUtils.getSSLClient().newBuilder());
|
|
|
|
|
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
|
private static final String host = System.getProperty(Constants.IOT_CORE_HOST);
|
|
|
|
|
private static final String port = System.getProperty(Constants.IOT_CORE_HTTPS_PORT);
|
|
|
|
|
private static final String endPointPrefix = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port;
|
|
|
|
|
private static final IOAuthClientService client =
|
|
|
|
|
APIManagerServiceDataHolder.getInstance().getIoAuthClientService();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Application[] getAllApplications(ApiApplicationInfo apiApplicationInfo, String appName)
|
|
|
|
|
public Application[] getAllApplications(String appName)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllApplicationsUrl = endPointPrefix + Constants.APPLICATIONS_API + "?query=" + appName;
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getAllApplicationsUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.get();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONArray applicationList = (JSONArray) new JSONObject(response.body().string()).get("list");
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
JSONArray applicationList = (JSONArray) new JSONObject(response.getBody()).get("list");
|
|
|
|
|
return gson.fromJson(applicationList.toString(), Application[].class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return getAllApplications(refreshedApiApplicationInfo, appName);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving applications for application name : [ " + appName + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Application getDetailsOfAnApplication(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
|
|
|
|
public Application getDetailsOfAnApplication(String applicationId)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getDetailsOfAPPUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getDetailsOfAPPUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.get();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), Application.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return getDetailsOfAnApplication(refreshedApiApplicationInfo, applicationId);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), Application.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving details of application ID : [ " + applicationId + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Application createApplication(ApiApplicationInfo apiApplicationInfo, Application application)
|
|
|
|
|
public Application createApplication(Application application)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API;
|
|
|
|
|
|
|
|
|
|
JSONArray groups = new JSONArray();
|
|
|
|
@ -161,123 +128,68 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getAllScopesUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.post(requestBody);
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_CREATED == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), Application.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return createApplication(refreshedApiApplicationInfo, application);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request body";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), Application.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while creating application : [ " + application.getName() + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Boolean deleteApplication(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
|
|
|
|
public Boolean deleteApplication(String applicationId)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String deleteScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId;
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(deleteScopesUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.delete();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return deleteApplication(refreshedApiApplicationInfo, applicationId);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request body";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return HttpStatus.SC_OK == response.getCode();
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg =
|
|
|
|
|
"Error occurred while deleting application associated with application ID : [ " + applicationId + "]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Subscription[] getAllSubscriptions(ApiApplicationInfo apiApplicationInfo, String applicationId)
|
|
|
|
|
public Subscription[] getAllSubscriptions(String applicationId)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "?applicationId=" + applicationId + "&limit=1000";
|
|
|
|
|
String getAllScopesUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "?applicationId=" + applicationId +
|
|
|
|
|
"&limit=1000";
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getAllScopesUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.get();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONArray subscriptionList = (JSONArray) new JSONObject(response.body().string()).get("list");
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
JSONArray subscriptionList = (JSONArray) new JSONObject(response.getBody()).get("list");
|
|
|
|
|
return gson.fromJson(subscriptionList.toString(), Subscription[].class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return getAllSubscriptions(refreshedApiApplicationInfo, applicationId);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg =
|
|
|
|
|
"Error occurred while retrieving all subscription of application associated with application ID :" +
|
|
|
|
|
" [ " + applicationId + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public APIInfo[] getAllApis(ApiApplicationInfo apiApplicationInfo, Map<String, String> queryParams, Map<String, String> headerParams)
|
|
|
|
|
public APIInfo[] getAllApis(Map<String, String> queryParams, Map<String, String> headerParams)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
StringBuilder getAPIsURL = new StringBuilder(endPointPrefix + Constants.DEV_PORTAL_API);
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, String> query : queryParams.entrySet()) {
|
|
|
|
@ -286,8 +198,6 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getAPIsURL.toString());
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, String> header : headerParams.entrySet()) {
|
|
|
|
|
builder.addHeader(header.getKey(), header.getValue());
|
|
|
|
@ -296,86 +206,48 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONArray apiList = (JSONArray) new JSONObject(response.body().string()).get("list");
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
JSONArray apiList = (JSONArray) new JSONObject(response.getBody()).get("list");
|
|
|
|
|
return gson.fromJson(apiList.toString(), APIInfo[].class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return getAllApis(refreshedApiApplicationInfo, queryParams, headerParams);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving all APIs";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Subscription createSubscription(ApiApplicationInfo apiApplicationInfo, Subscription subscriptions)
|
|
|
|
|
public Subscription createSubscription(Subscription subscription)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String createSubscriptionUrl = endPointPrefix + Constants.SUBSCRIPTION_API;
|
|
|
|
|
|
|
|
|
|
JSONObject subscriptionObject = new JSONObject();
|
|
|
|
|
subscriptionObject.put("applicationId", subscriptions.getApplicationId());
|
|
|
|
|
subscriptionObject.put("apiId", subscriptions.getApiId());
|
|
|
|
|
subscriptionObject.put("throttlingPolicy", subscriptions.getThrottlingPolicy());
|
|
|
|
|
subscriptionObject.put("requestedThrottlingPolicy", subscriptions.getRequestedThrottlingPolicy());
|
|
|
|
|
subscriptionObject.put("applicationId", subscription.getApplicationId());
|
|
|
|
|
subscriptionObject.put("apiId", subscription.getApiId());
|
|
|
|
|
subscriptionObject.put("throttlingPolicy", subscription.getThrottlingPolicy());
|
|
|
|
|
subscriptionObject.put("requestedThrottlingPolicy", subscription.getRequestedThrottlingPolicy());
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, subscriptionObject.toString());
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(createSubscriptionUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
|
|
|
|
|
builder.post(requestBody);
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_CREATED == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), Subscription.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return createSubscription(refreshedApiApplicationInfo, subscriptions);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request body";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), Subscription.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while adding subscription : [ " + subscription.getSubscriptionId() + " ] for" +
|
|
|
|
|
" application associated with application ID : [ " + subscription.getApplicationId() + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Subscription[] createSubscriptions(ApiApplicationInfo apiApplicationInfo, List<Subscription> subscriptions)
|
|
|
|
|
public Subscription[] createSubscriptions(List<Subscription> subscriptions)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String createSubscriptionsUrl = endPointPrefix + Constants.SUBSCRIPTION_API + "/multiple";
|
|
|
|
|
|
|
|
|
|
String subscriptionsList = gson.toJson(subscriptions);
|
|
|
|
@ -383,109 +255,64 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(createSubscriptionsUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
|
|
|
|
|
builder.post(requestBody);
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), Subscription[].class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return createSubscriptions(refreshedApiApplicationInfo, subscriptions);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request body";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), Subscription[].class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while adding subscriptions";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ApplicationKey generateApplicationKeys(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyManager,
|
|
|
|
|
String validityTime, String keyType)
|
|
|
|
|
public ApplicationKey generateApplicationKeys(String applicationId, String keyManager, String validityTime,
|
|
|
|
|
String keyType, String grantTypesToBeSupported, String callbackUrl)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String generateApplicationKeysUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
|
|
|
|
|
applicationId + "/generate-keys";
|
|
|
|
|
|
|
|
|
|
JSONArray grantTypesToBeSupported = new JSONArray();
|
|
|
|
|
grantTypesToBeSupported.put("password");
|
|
|
|
|
grantTypesToBeSupported.put("client_credentials");
|
|
|
|
|
|
|
|
|
|
JSONArray scopes = new JSONArray();
|
|
|
|
|
scopes.put("am_application_scope");
|
|
|
|
|
scopes.put("default");
|
|
|
|
|
|
|
|
|
|
JSONObject keyInfo = new JSONObject();
|
|
|
|
|
keyInfo.put("keyType", keyType);
|
|
|
|
|
keyInfo.put("keyManager", keyManager);
|
|
|
|
|
keyInfo.put("grantTypesToBeSupported", grantTypesToBeSupported);
|
|
|
|
|
keyInfo.put("callbackUrl", "");
|
|
|
|
|
keyInfo.put("scopes", scopes);
|
|
|
|
|
keyInfo.put("validityTime", 3600);
|
|
|
|
|
keyInfo.put("additionalProperties", new JSONObject());
|
|
|
|
|
keyInfo.put("grantTypesToBeSupported", grantTypesToBeSupported.split(Constants.SPACE));
|
|
|
|
|
if (!StringUtils.isEmpty(callbackUrl)) {
|
|
|
|
|
keyInfo.put("callbackUrl", callbackUrl);
|
|
|
|
|
}
|
|
|
|
|
keyInfo.put("validityTime", validityTime);
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, keyInfo.toString());
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(generateApplicationKeysUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.post(requestBody);
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), ApplicationKey.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return generateApplicationKeys(refreshedApiApplicationInfo, applicationId, keyManager, validityTime, keyType);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request body";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), ApplicationKey.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while generating application keys for application associated with " +
|
|
|
|
|
"application ID : [ " + applicationId + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ApplicationKey mapApplicationKeys(ApiApplicationInfo apiApplicationInfo, Application application, String keyManager, String keyType)
|
|
|
|
|
public ApplicationKey mapApplicationKeys(String consumerKey, String consumerSecret, Application application,
|
|
|
|
|
String keyManager, String keyType)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllScopesUrl = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH +
|
|
|
|
|
application.getApplicationId() + "/map-keys";
|
|
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
payload.put("consumerKey", apiApplicationInfo.getClientId());
|
|
|
|
|
payload.put("consumerSecret", apiApplicationInfo.getClientSecret());
|
|
|
|
|
payload.put("consumerKey", consumerKey);
|
|
|
|
|
payload.put("consumerSecret", consumerSecret);
|
|
|
|
|
payload.put("keyManager", keyManager);
|
|
|
|
|
payload.put("keyType", keyType);
|
|
|
|
|
|
|
|
|
@ -493,101 +320,76 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getAllScopesUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.post(requestBody);
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), ApplicationKey.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return mapApplicationKeys(refreshedApiApplicationInfo, application, keyManager, keyType);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request body";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), ApplicationKey.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while mapping application keys";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ApplicationKey getKeyDetails(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyMapId)
|
|
|
|
|
public ApplicationKey getKeyDetails(String applicationId, String keyMapId)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId + "/oauth-keys/" + keyMapId;
|
|
|
|
|
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId +
|
|
|
|
|
"/oauth-keys/" + keyMapId;
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getKeyDetails);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.get();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), ApplicationKey.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return getKeyDetails(refreshedApiApplicationInfo, applicationId, keyMapId);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), ApplicationKey.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg =
|
|
|
|
|
"Error occurred while retrieving key details of application associated with application ID : [ " + applicationId + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ApplicationKey updateGrantType(ApiApplicationInfo apiApplicationInfo, String applicationId, String keyMapId, String keyManager,
|
|
|
|
|
List<String> supportedGrantTypes, String callbackUrl)
|
|
|
|
|
public ApplicationKey[] getAllKeys(String applicationId)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId + "/oauth-keys/" + keyMapId;
|
|
|
|
|
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId +
|
|
|
|
|
"/oauth-keys";
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getKeyDetails);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.get();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
JSONArray supportedGrantTypeList = new JSONArray();
|
|
|
|
|
for (String string : supportedGrantTypes) {
|
|
|
|
|
supportedGrantTypeList.put(string);
|
|
|
|
|
try {
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
JSONArray keyList = (JSONArray) new JSONObject(response.getBody()).get("list");
|
|
|
|
|
return gson.fromJson(keyList.toString(), ApplicationKey[].class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg =
|
|
|
|
|
"Error occurred while retrieving key details of application associated with application ID : [ " + applicationId + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ApplicationKey updateGrantType(String applicationId, String keyMapId, List<String> supportedGrantTypes, String callbackUrl)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
String getKeyDetails = endPointPrefix + Constants.APPLICATIONS_API + Constants.SLASH + applicationId +
|
|
|
|
|
"/oauth-keys/" + keyMapId;
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getKeyDetails);
|
|
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
payload.put("keyMappingId", keyMapId);
|
|
|
|
|
payload.put("keyManager", keyManager);
|
|
|
|
|
payload.put("supportedGrantTypes", supportedGrantTypeList);
|
|
|
|
|
payload.put("supportedGrantTypes", supportedGrantTypes);
|
|
|
|
|
payload.put("callbackUrl", (callbackUrl != null ? callbackUrl : ""));
|
|
|
|
|
payload.put("additionalProperties", new JSONObject());
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, payload.toString());
|
|
|
|
|
|
|
|
|
@ -595,80 +397,34 @@ public class ConsumerRESTAPIServicesImpl implements ConsumerRESTAPIServices {
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), ApplicationKey.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return updateGrantType(refreshedApiApplicationInfo, applicationId, keyMapId, keyManager, supportedGrantTypes, callbackUrl);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
return gson.fromJson(response.getBody(), ApplicationKey.class);
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while updating the grant types of the application associated with " +
|
|
|
|
|
"application ID : [ " + applicationId + " ]";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public KeyManager[] getAllKeyManagers(ApiApplicationInfo apiApplicationInfo)
|
|
|
|
|
public KeyManager[] getAllKeyManagers()
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllKeyManagersUrl = endPointPrefix + Constants.KEY_MANAGERS_API;
|
|
|
|
|
|
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
|
builder.url(getAllKeyManagersUrl);
|
|
|
|
|
builder.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ apiApplicationInfo.getAccess_token());
|
|
|
|
|
builder.get();
|
|
|
|
|
Request request = builder.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONArray keyManagerList = (JSONArray) new JSONObject(response.body().string()).get("list");
|
|
|
|
|
OAuthClientResponse response = client.execute(request);
|
|
|
|
|
JSONArray keyManagerList = (JSONArray) new JSONObject(response.getBody()).get("list");
|
|
|
|
|
return gson.fromJson(keyManagerList.toString(), KeyManager[].class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(apiApplicationInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationInfo.getClientId(), apiApplicationInfo.getClientSecret());
|
|
|
|
|
ApiApplicationInfo refreshedApiApplicationInfo = returnApplicationInfo(apiApplicationInfo, refreshedAccessToken);
|
|
|
|
|
return getAllKeyManagers(refreshedApiApplicationInfo);
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
} catch (OAuthClientException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving all key managers";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ApiApplicationInfo returnApplicationInfo(ApiApplicationInfo apiApplicationInfo, AccessTokenInfo refreshedToken) {
|
|
|
|
|
|
|
|
|
|
ApiApplicationInfo applicationInfo = new ApiApplicationInfo();
|
|
|
|
|
applicationInfo.setClientId(apiApplicationInfo.getClientId());
|
|
|
|
|
applicationInfo.setClientSecret(apiApplicationInfo.getClientSecret());
|
|
|
|
|
applicationInfo.setAccess_token(refreshedToken.getAccess_token());
|
|
|
|
|
applicationInfo.setRefresh_token(refreshedToken.getRefresh_token());
|
|
|
|
|
return applicationInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|