From c6b43cc794997c51789378db7b7068bc5370a9bd Mon Sep 17 00:00:00 2001 From: pasindu Date: Tue, 9 May 2023 14:21:53 +0530 Subject: [PATCH] Implment API layer for API publisher --- .../rest/api/PublisherRESTAPIServices.java | 58 ++-- .../api/PublisherRESTAPIServicesImpl.java | 283 +++++++++++++++++- .../publisher/APIPublisherServiceImpl.java | 93 ++++-- 3 files changed, 379 insertions(+), 55 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServices.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServices.java index e17a480400..29f0b912df 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServices.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServices.java @@ -37,7 +37,7 @@ public interface PublisherRESTAPIServices { throws APIServicesException, BadRequestException, UnexpectedResponseException; boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + throws APIServicesException, BadRequestException, UnexpectedResponseException; boolean updateSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) throws APIServicesException, BadRequestException, UnexpectedResponseException; @@ -46,23 +46,23 @@ public interface PublisherRESTAPIServices { throws APIServicesException, BadRequestException, UnexpectedResponseException; JSONObject getApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + throws APIServicesException, BadRequestException, UnexpectedResponseException; API createAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + throws APIServicesException, BadRequestException, UnexpectedResponseException; boolean updateApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + throws APIServicesException, BadRequestException, UnexpectedResponseException; boolean saveAsyncApiDefinition(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, String asyncApiDefinition) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + throws APIServicesException, BadRequestException, UnexpectedResponseException; JSONObject getAllApiSpecificMediationPolicies(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier) throws APIServicesException, BadRequestException, UnexpectedResponseException; - boolean addApiSpecificMediationPolicy (APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, - String uuid, Mediation mediation) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + boolean addApiSpecificMediationPolicy(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + String uuid, Mediation mediation) + throws APIServicesException, BadRequestException, UnexpectedResponseException; boolean updateApiSpecificMediationPolicyContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, Mediation mediation) @@ -70,19 +70,43 @@ public interface PublisherRESTAPIServices { boolean changeLifeCycleStatus(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, String action) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + throws APIServicesException, BadRequestException, UnexpectedResponseException; - JSONObject getAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + JSONObject getAPIRevisions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, + String deploymentStatus) + throws APIServicesException, BadRequestException, UnexpectedResponseException; JSONObject getAPIRevisionDeployment(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid) throws APIServicesException, BadRequestException, UnexpectedResponseException; - APIRevision addAPIRevision (APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, - APIRevision apiRevision) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + APIRevision addAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIRevision apiRevision) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + boolean deployAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, + String apiRevisionId, List apiRevisionDeploymentList) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + abstract boolean undeployAPIRevisionDeployment(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIRevisionDeployment apiRevisionDeployment, String uuid) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + boolean deleteAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIRevision apiRevision, String uuid) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + JSONObject getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier) + throws APIServicesException, BadRequestException, UnexpectedResponseException; - boolean deployAPIRevision (APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, - String apiRevisionId, List apiRevisionDeploymentList) - throws APIServicesException, BadRequestException, UnexpectedResponseException; + boolean deleteDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIIdentifier apiIdentifier, String documentID) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + Documentation addDocumentation(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIIdentifier apiIdentifier, Documentation documentation) + throws APIServicesException, BadRequestException, UnexpectedResponseException; + + boolean addDocumentationContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + API api, String docId, String docContent) + throws APIServicesException, BadRequestException, UnexpectedResponseException; } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java index 19c3c42a01..591603ef40 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/PublisherRESTAPIServicesImpl.java @@ -139,7 +139,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { public boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) throws APIServicesException, BadRequestException, UnexpectedResponseException { - String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + String addNewSharedScopeEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + Constants.COLON + port + Constants.SCOPE_API_ENDPOINT + scope.getId(); ScopeUtils scopeUtil = new ScopeUtils(); @@ -151,7 +151,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { RequestBody requestBody = RequestBody.create(JSON, scopeString); Request request = new Request.Builder() - .url(addNewScope) + .url(addNewSharedScopeEndPoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + accessTokenInfo.getAccess_token()) .post(requestBody) @@ -167,7 +167,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); //TODO: max attempt count - return updateSharedScope(apiApplicationKey, refreshedAccessToken, scope); + return addNewSharedScope(apiApplicationKey, refreshedAccessToken, scope); } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { String msg = "Bad Request, Invalid scope object"; log.error(msg); @@ -552,7 +552,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); //TODO: max attempt count - return addApiSpecificMediationPolicy(apiApplicationKey, refreshedAccessToken, uuid, mediation); + return updateApiSpecificMediationPolicyContent(apiApplicationKey, refreshedAccessToken, uuid, mediation); } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { String msg = "Bad Request, Invalid scope object"; log.error(msg); @@ -614,14 +614,15 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { } @Override - public JSONObject getAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid) - throws APIServicesException, BadRequestException, UnexpectedResponseException { + public JSONObject getAPIRevisions(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, String uuid, + String deploymentStatus) + throws APIServicesException, BadRequestException, UnexpectedResponseException { - String getLatestRevisionUUIDEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host - + Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/revisions?query=deployed:true"; + String getAPIRevisionsEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/revisions" + deploymentStatus; Request request = new Request.Builder() - .url(getLatestRevisionUUIDEndPoint) + .url(getAPIRevisionsEndPoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + accessTokenInfo.getAccess_token()) .get() @@ -638,7 +639,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); //TODO: max attempt count - return getAPIRevision(apiApplicationKey, refreshedAccessToken, uuid); + return getAPIRevisions(apiApplicationKey, refreshedAccessToken, uuid, deploymentStatus); } else if (HttpStatus.SC_BAD_REQUEST == response.code()) { String msg = "Bad Request, Invalid scope object"; log.error(msg); @@ -784,4 +785,266 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { throw new APIServicesException(e); } } + + @Override + public boolean undeployAPIRevisionDeployment(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIRevisionDeployment apiRevisionDeployment, String uuid) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String apiRevisionEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/revisions/" + + apiRevisionDeployment.getRevisionUUID(); + + RequestBody requestBody = RequestBody.create(JSON, String.valueOf(apiRevisionDeployment)); + Request request = new Request.Builder() + .url(apiRevisionEndPoint) + .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 undeployAPIRevisionDeployment(apiApplicationKey, refreshedAccessToken, apiRevisionDeployment, 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 boolean deleteAPIRevision(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIRevision apiRevision, String uuid) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String apiRevisionEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + uuid + "/revisions/" + + apiRevision.getRevisionUUID(); + + Request request = new Request.Builder() + .url(apiRevisionEndPoint) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .delete() + .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 deleteAPIRevision(apiApplicationKey, refreshedAccessToken, apiRevision, 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 getDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, APIIdentifier apiIdentifier) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getDocumentationsEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + apiIdentifier.getUUID() + "/documents?limit=1000"; + + Request request = new Request.Builder() + .url(getDocumentationsEndPoint) + .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 getDocumentations(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 deleteDocumentations(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIIdentifier apiIdentifier, String documentID) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String getDocumentationsEndPoint = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + apiIdentifier.getUUID() + "/documents/" + documentID; + + Request request = new Request.Builder() + .url(getDocumentationsEndPoint) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + + accessTokenInfo.getAccess_token()) + .delete() + .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 deleteDocumentations(apiApplicationKey, refreshedAccessToken, apiIdentifier, documentID); + } 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 Documentation addDocumentation(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + APIIdentifier apiIdentifier, Documentation documentation) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + apiIdentifier.getUUID() + "/documents"; + + String document = "{\n" + + " \"name\": \" " + documentation.getName() + " \",\n" + + " \"type\": \" " + documentation.getType() + " \",\n" + + " \"summary\": \" " + documentation.getSummary() + " \",\n" + + " \"sourceType\": \" " + documentation.getSourceType() + " \",\n" + + " \"inlineContent\": \" " + documentation.getSourceType() + " \",\n" + + " \"visibility\": \" " + documentation.getVisibility() + " \",\n" + + " \"createdBy\": \" admin \"\n" + + "}"; + + RequestBody requestBody = RequestBody.create(JSON, document); + 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_CREATED == response.code()) { + return gson.fromJson(response.body().string(), Documentation.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 addDocumentation(apiApplicationKey, refreshedAccessToken, apiIdentifier, documentation); + } 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 addDocumentationContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, + API api, String docId, String docContent) + throws APIServicesException, BadRequestException, UnexpectedResponseException { + + String addNewScope = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port + Constants.API_ENDPOINT + api.getUuid() + "/documents/" + docId; + + RequestBody requestBody = RequestBody.create(JSON, docContent); + 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 addDocumentationContent(apiApplicationKey, refreshedAccessToken, api,docId ,docContent); + } 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); + } + } + } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java index 38b19c9061..8b54defd83 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -242,10 +242,9 @@ public class APIPublisherServiceImpl implements APIPublisherService { // Set allSharedScopeKeys = apiProvider.getAllSharedScopeKeys(tenantDomain); //get all scopes Set scopesToMoveAsSharedScopes = new HashSet<>(); for (ApiScope apiScope : apiConfig.getScopes()) { - // if the scope is not available as shared scope and it is assigned to an API as a local scope + // if the scope is not available as shared scope, and it is assigned to an API as a local scope // need remove the local scope and add as a shared scope if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, apiScope.getKey())) { -// if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) { if (apiProvider.isScopeKeyAssignedLocally(apiIdentifier, apiScope.getKey(), tenantId)) { // collect scope to move as shared scopes scopesToMoveAsSharedScopes.add(apiScope); @@ -315,8 +314,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { for (Mediation m : mediationList) { if (apiConfig.getInSequenceName().equals(m.getName())) { m.setConfig(apiConfig.getInSequenceConfig()); -// apiProvider -// .updateApiSpecificMediationPolicyContent(existingAPI.getUuid(), m, +// apiProvider.updateApiSpecificMediationPolicyContent(existingAPI.getUuid(), m, // tenantDomain); publisherRESTAPIServices. updateApiSpecificMediationPolicyContent(apiApplicationKey, @@ -334,36 +332,59 @@ public class APIPublisherServiceImpl implements APIPublisherService { } // Assumption: Assume the latest revision is the published one - String latestRevisionUUID = apiProvider.getLatestRevisionUUID(existingAPI.getUuid()); -// List latestRevisionDeploymentList = +// String latestRevisionUUID = apiProvider.getLatestRevisionUUID(existingAPI.getUuid()); +// List revisionDeploymentList = // apiProvider.getAPIRevisionDeploymentList(latestRevisionUUID); - List latestRevisionDeploymentList = (List) + + // This will retrieve the deployed revision list + List revisionDeploymentList = (List) publisherRESTAPIServices.getAPIRevisionDeployment(apiApplicationKey, accessTokenInfo, existingAPI.getUuid()); + // This will retrieve the un deployed revision list + List undeployedRevisionList = (List) + publisherRESTAPIServices.getAPIRevisions(apiApplicationKey, accessTokenInfo, + existingAPI.getUuid(), "?query=deployed:false").get("list"); // List apiRevisionList = apiProvider.getAPIRevisions(existingAPI.getUuid()); - List apiRevisionList = (List) publisherRESTAPIServices.getAPIRevision(apiApplicationKey, - accessTokenInfo, existingAPI.getUuid()).get("list"); - - if (apiRevisionList.size() >= 5) { - String earliestRevisionUUID = apiProvider.getEarliestRevisionUUID(existingAPI.getUuid()); -// List earliestRevisionDeploymentList = +// List apiRevisionList = (List) publisherRESTAPIServices.getAPIRevision(apiApplicationKey, +// accessTokenInfo, existingAPI.getUuid()).get("list"); + int apiRevisionCount = (int) publisherRESTAPIServices.getAPIRevisions(apiApplicationKey, + accessTokenInfo, existingAPI.getUuid(), "").get("count"); + + if (apiRevisionCount >= 5) { +// String earliestRevisionUUID = apiProvider.getEarliestRevisionUUID(existingAPI.getUuid()); +// List undeployedRevisionList = // apiProvider.getAPIRevisionDeploymentList(earliestRevisionUUID); - List earliestRevisionDeploymentList = (List) - publisherRESTAPIServices.getAPIRevisionDeployment(apiApplicationKey, accessTokenInfo, existingAPI.getUuid()); - apiProvider.undeployAPIRevisionDeployment(existingAPI.getUuid(), earliestRevisionUUID, earliestRevisionDeploymentList); - apiProvider.deleteAPIRevision(existingAPI.getUuid(), earliestRevisionUUID, tenantDomain); +// List undeployedRevisionList = (List) +// publisherRESTAPIServices.getAPIRevisionDeployment(apiApplicationKey, accessTokenInfo, existingAPI.getUuid()); + + APIRevisionDeployment latestRevisionDeployment = revisionDeploymentList.get(0); + APIRevision earliestUndeployRevision = undeployedRevisionList.get(0); + +// apiProvider.undeployAPIRevisionDeployment(existingAPI.getUuid(), earliestRevisionUUID, undeployedRevisionList); + publisherRESTAPIServices.undeployAPIRevisionDeployment(apiApplicationKey, + accessTokenInfo, latestRevisionDeployment, existingAPI.getUuid()); + +// apiProvider.deleteAPIRevision(existingAPI.getUuid(), earliestRevisionUUID, tenantDomain); + publisherRESTAPIServices.deleteAPIRevision(apiApplicationKey, accessTokenInfo, + earliestUndeployRevision, existingAPI.getUuid()); } // create new revision APIRevision apiRevision = new APIRevision(); apiRevision.setApiUUID(existingAPI.getUuid()); apiRevision.setDescription("Updated Revision"); - String apiRevisionId = apiProvider.addAPIRevision(apiRevision, tenantDomain); +// String apiRevisionId = apiProvider.addAPIRevision(apiRevision, tenantDomain); + String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationKey, + accessTokenInfo, apiRevision).getRevisionUUID(); - apiProvider.deployAPIRevision(existingAPI.getUuid(), apiRevisionId, latestRevisionDeploymentList); +// apiProvider.deployAPIRevision(existingAPI.getUuid(), apiRevisionId, revisionDeploymentList); + publisherRESTAPIServices.deployAPIRevision(apiApplicationKey, accessTokenInfo, + existingAPI.getUuid(), apiRevisionId, revisionDeploymentList); if (CREATED_STATUS.equals(existingAPI.getStatus())) { - apiProvider.changeLifeCycleStatus(tenantDomain, existingAPI.getUuid(), PUBLISH_ACTION, null); +// apiProvider.changeLifeCycleStatus(tenantDomain, existingAPI.getUuid(), PUBLISH_ACTION, null); + publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey,accessTokenInfo, + existingAPI.getUuid(), PUBLISH_ACTION); } } } @@ -395,17 +416,33 @@ public class APIPublisherServiceImpl implements APIPublisherService { apiDocumentation.setSummary(apiConfig.getApiDocumentationSummary()); apiDocumentation.setOtherTypeName(null); - try { - //Including below code lines inside the try block because 'getDocumentation' method returns an APIManagementException exception when it doesn't have any existing doc - Documentation existingDoc = apiProvider.getDocumentation(api.getId(), DocumentationType.HOWTO, apiConfig.getApiDocumentationName()); - apiProvider.removeDocumentation(api.getId(), existingDoc.getId(), null); - } catch (APIManagementException e) { + //Including below code lines inside the try block because 'getDocumentation' method returns an APIManagementException exception when it doesn't have any existing doc +// Documentation existingDoc = apiProvider.getDocumentation(api.getId(), DocumentationType.HOWTO, apiConfig.getApiDocumentationName()); + JSONArray documentList = (JSONArray) publisherRESTAPIServices.getDocumentations(apiApplicationKey, + accessTokenInfo, api.getId()).get("list"); + + if (!(documentList.length() == 0)) { + for (int i = 0; i < documentList.length(); i++) { + JSONObject existingDoc = documentList.getJSONObject(i); + if (existingDoc.getString("name").equals(apiConfig.getApiDocumentationName()) + && existingDoc.getString("type").equals(DocumentationType.HOWTO)) { + //apiProvider.removeDocumentation(api.getId(), existingDoc.getId(), null); + publisherRESTAPIServices.deleteDocumentations(apiApplicationKey, accessTokenInfo, + api.getId(), existingDoc.getString("documentId")); + } + } + } else { log.info("There is no any existing api documentation."); } - apiProvider.addDocumentation(api.getId(), apiDocumentation); - apiProvider.addDocumentationContent(api, apiConfig.getApiDocumentationName(), docContent); +// apiProvider.addDocumentation(api.getId(), apiDocumentation); + Documentation createdDoc = publisherRESTAPIServices.addDocumentation(apiApplicationKey, accessTokenInfo, + api.getId(), apiDocumentation); + +// apiProvider.addDocumentationContent(api, apiConfig.getApiDocumentationName(), docContent); + publisherRESTAPIServices.addDocumentationContent(apiApplicationKey, accessTokenInfo, api, + createdDoc.getId(), docContent); } - } catch (FaultGatewaysException | APIManagementException | IOException | APIServicesException | + } catch (APIManagementException | IOException | APIServicesException | BadRequestException | UnexpectedResponseException e) { String msg = "Error occurred while publishing api"; log.error(msg, e);