|
|
|
@ -18,6 +18,7 @@
|
|
|
|
|
|
|
|
|
|
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.constants.Constants;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
|
|
|
|
|
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo;
|
|
|
|
@ -36,14 +37,16 @@ import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.apache.commons.ssl.Base64;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
import org.wso2.carbon.apimgt.api.model.Scope;
|
|
|
|
|
import org.wso2.carbon.apimgt.api.model.*;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
|
|
|
|
private static final Log log = LogFactory.getLog(PublisherRESTAPIServicesImpl.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);
|
|
|
|
|
|
|
|
|
@ -94,7 +97,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
|
|
|
|
String keyValue = new String(Base64.encodeBase64((key).getBytes())).replace(Constants.QUERY_KEY_VALUE_SEPARATOR,
|
|
|
|
|
Constants.EMPTY_STRING);
|
|
|
|
|
String getScopeUrl = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + Constants.COLON
|
|
|
|
|
+ port + Constants.GET_SCOPE + keyValue;
|
|
|
|
|
+ port + Constants.SCOPE_API_ENDPOINT + keyValue;
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(getScopeUrl)
|
|
|
|
@ -117,6 +120,58 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
|
|
|
|
String msg = "Bad Request, Invalid request";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else if (HttpStatus.SC_NOT_FOUND == response.code()) {
|
|
|
|
|
String msg = "Shared scope key not found";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
String msg = "Error occurred while processing the response";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.SCOPE_API_ENDPOINT + scope.getId();
|
|
|
|
|
|
|
|
|
|
ScopeUtils scopeUtil = new ScopeUtils();
|
|
|
|
|
scopeUtil.setKey(scope.getKey());
|
|
|
|
|
scopeUtil.setName(scope.getName());
|
|
|
|
|
scopeUtil.setDescription(scope.getDescription());
|
|
|
|
|
scopeUtil.setRoles(scope.getRoles());
|
|
|
|
|
String scopeString = scopeUtil.toJSON();
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, scopeString);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.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(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
log.error(msg);
|
|
|
|
|
throw new BadRequestException(msg);
|
|
|
|
|
} else {
|
|
|
|
|
String msg = "Response : " + response.code() + response.body();
|
|
|
|
|
throw new UnexpectedResponseException(msg);
|
|
|
|
@ -133,7 +188,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String updateScopeUrl = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.GET_SCOPE + scope.getId();
|
|
|
|
|
+ Constants.COLON + port + Constants.SCOPE_API_ENDPOINT + scope.getId();
|
|
|
|
|
|
|
|
|
|
ScopeUtils scopeUtil = new ScopeUtils();
|
|
|
|
|
scopeUtil.setKey(scope.getKey());
|
|
|
|
@ -175,4 +230,558 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public API getApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllApis = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + Constants.COLON
|
|
|
|
|
+ port + Constants.API_ENDPOINT + apiIdentifier.getUUID();
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(getAllApis)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), API.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return getApi(apiApplicationKey, refreshedAccessToken, apiIdentifier);
|
|
|
|
|
} 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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JSONObject getApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getAllApis = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + Constants.COLON
|
|
|
|
|
+ port + Constants.GET_ALL_APIS;
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(getAllApis)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONObject jsonObject = new JSONObject(response.body().string());
|
|
|
|
|
return jsonObject;
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return getApis(apiApplicationKey, refreshedAccessToken);
|
|
|
|
|
} 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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public API createAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String updateScopeUrl = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT;
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(api));
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(updateScopeUrl)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_CREATED == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), API.class);
|
|
|
|
|
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return createAPI(apiApplicationKey, refreshedAccessToken, api);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean updateApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String updateScopeUrl = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + api.getUuid();
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(api));
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(updateScopeUrl)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_CREATED == response.code()) {
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return updateApi(apiApplicationKey, refreshedAccessToken, api);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean saveAsyncApiDefinition(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
|
|
|
|
|
String uuid, String asyncApiDefinition)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + uuid;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, asyncApiDefinition);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.put(requestBody)
|
|
|
|
|
.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(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return saveAsyncApiDefinition(apiApplicationKey, refreshedAccessToken, uuid, asyncApiDefinition);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JSONObject getAllApiSpecificMediationPolicies(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + apiIdentifier.getUUID() + "/mediation-policies";
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONObject jsonObject = new JSONObject(response.body().string());
|
|
|
|
|
return jsonObject;
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return getAllApiSpecificMediationPolicies(apiApplicationKey, refreshedAccessToken, apiIdentifier);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean addApiSpecificMediationPolicy(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
|
|
|
|
|
String uuid, Mediation mediation)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/mediation-policies/" + mediation.getUuid()
|
|
|
|
|
+ "/content";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(mediation));
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.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(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return addApiSpecificMediationPolicy(apiApplicationKey, refreshedAccessToken, uuid, mediation);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean updateApiSpecificMediationPolicyContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
|
|
|
|
|
String uuid, Mediation mediation)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String updateApiSpecificMediationPolicyContentAPI = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/mediation-policies/" + mediation.getUuid()
|
|
|
|
|
+ "/content";
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(mediation));
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(updateApiSpecificMediationPolicyContentAPI)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.put(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_CREATED == response.code()) {
|
|
|
|
|
return true;
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return addApiSpecificMediationPolicy(apiApplicationKey, refreshedAccessToken, uuid, mediation);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean changeLifeCycleStatus(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
|
|
|
|
|
String uuid, String action)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + "change-lifecycle?apiId=" + uuid
|
|
|
|
|
+ "&action=" + action;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, Constants.EMPTY_STRING);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.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(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return changeLifeCycleStatus(apiApplicationKey, refreshedAccessToken, uuid, action);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JSONObject getAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getLatestRevisionUUIDEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/revisions?query=deployed:true";
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(getLatestRevisionUUIDEndPoint)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONObject jsonObject = new JSONObject(response.body().string());
|
|
|
|
|
return jsonObject;
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return getAPIRevision(apiApplicationKey, refreshedAccessToken, uuid);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public JSONObject getAPIRevisionDeployment(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String getLatestRevisionUUIDEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/deployments";
|
|
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(getLatestRevisionUUIDEndPoint)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.get()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
JSONObject jsonObject = new JSONObject(response.body().string());
|
|
|
|
|
return jsonObject;
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return getAPIRevisionDeployment(apiApplicationKey, refreshedAccessToken, uuid);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public APIRevision addAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo,
|
|
|
|
|
APIRevision apiRevision)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + apiRevision.getApiUUID() + "/revisions";
|
|
|
|
|
|
|
|
|
|
String apiRevisionDescription = "{\n" +
|
|
|
|
|
" \"description\":\" " + apiRevision.getDescription() + "\",\n" +
|
|
|
|
|
"}";
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, apiRevisionDescription);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
if (HttpStatus.SC_OK == response.code()) {
|
|
|
|
|
return gson.fromJson(response.body().string(), APIRevision.class);
|
|
|
|
|
} else if (HttpStatus.SC_UNAUTHORIZED == response.code()) {
|
|
|
|
|
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
|
|
|
|
|
AccessTokenInfo refreshedAccessToken = apiApplicationServices.
|
|
|
|
|
generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return addAPIRevision(apiApplicationKey, refreshedAccessToken, apiRevision);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean deployAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid,
|
|
|
|
|
String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeploymentList)
|
|
|
|
|
throws APIServicesException, BadRequestException, UnexpectedResponseException {
|
|
|
|
|
|
|
|
|
|
String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host
|
|
|
|
|
+ Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/deploy-revision?revisionId="
|
|
|
|
|
+ apiRevisionId;
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON, String.valueOf(apiRevisionDeploymentList));
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(addNewScope)
|
|
|
|
|
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER
|
|
|
|
|
+ accessTokenInfo.getAccess_token())
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.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(accessTokenInfo.getRefresh_token(),
|
|
|
|
|
apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret());
|
|
|
|
|
//TODO: max attempt count
|
|
|
|
|
return deployAPIRevision(apiApplicationKey, refreshedAccessToken, uuid, apiRevisionId,
|
|
|
|
|
apiRevisionDeploymentList);
|
|
|
|
|
} else if (HttpStatus.SC_BAD_REQUEST == response.code()) {
|
|
|
|
|
String msg = "Bad Request, Invalid scope object";
|
|
|
|
|
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";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new APIServicesException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|