From cbb03d4d1f69a469e106de65a8ad17430e03909f Mon Sep 17 00:00:00 2001 From: pasindu Date: Mon, 15 May 2023 08:31:57 +0530 Subject: [PATCH] Minor fixes in api requests --- .../pom.xml | 5 +++ .../rest/api/PublisherRESTAPIServices.java | 2 +- .../api/PublisherRESTAPIServicesImpl.java | 44 ++++++++++++------- .../rest/api/constants/Constants.java | 3 +- .../extension/rest/api/util/ScopeUtils.java | 8 ++-- .../publisher/APIPublisherServiceImpl.java | 16 ++++--- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml index 7dd0c74914..4bb277f623 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml @@ -84,6 +84,11 @@ okhttp compile + + org.wso2.orbit.com.fasterxml.jackson.core + jackson-databind + provided + 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 e129449a8e..a88db1fc6a 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 @@ -54,7 +54,7 @@ public interface PublisherRESTAPIServices { JSONObject getApis(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) throws APIServicesException, BadRequestException, UnexpectedResponseException; - API createAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) + JSONObject addAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) throws APIServicesException, BadRequestException, UnexpectedResponseException; boolean updateApi(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) 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 b8eaa46985..39a890115f 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 @@ -55,7 +55,8 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { 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 String endPointPrefix = Constants.HTTPS_PROTOCOL + Constants.SCHEME_SEPARATOR + host + + Constants.COLON + port; @Override public JSONObject getScopes(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo) @@ -127,7 +128,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { throw new BadRequestException(msg); } else if (HttpStatus.SC_NOT_FOUND == response.code()) { String msg = "Shared scope key not found"; - log.error(msg); + log.info(msg); return false; } else { String msg = "Response : " + response.code() + response.body(); @@ -144,7 +145,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { public boolean addNewSharedScope(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, Scope scope) throws APIServicesException, BadRequestException, UnexpectedResponseException { - String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT + scope.getId(); + String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT; ScopeUtils scopeUtil = new ScopeUtils(); scopeUtil.setKey(scope.getKey()); @@ -163,7 +164,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { try { Response response = client.newCall(request).execute(); - if (HttpStatus.SC_OK == response.code()) { + if (HttpStatus.SC_CREATED == response.code()) { return true; } else if (HttpStatus.SC_UNAUTHORIZED == response.code()) { APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); @@ -177,7 +178,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { log.error(msg); throw new BadRequestException(msg); } else { - String msg = "Response : " + response.code() + response.body(); + String msg = "Response : " + response.code() + response.message(); throw new UnexpectedResponseException(msg); } } catch (IOException e) { @@ -312,14 +313,22 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { } @Override - public API createAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) + public JSONObject addAPI(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, API api) throws APIServicesException, BadRequestException, UnexpectedResponseException { - String creatAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT; + String addAPIEndPoint = endPointPrefix + Constants.API_ENDPOINT; - RequestBody requestBody = RequestBody.create(JSON, String.valueOf(api)); + APIIdentifier apiIdentifier = api.getId(); + String apiString = "{\n" + + " \"name\":\"" + apiIdentifier.getName().replace(Constants.SPACE, Constants.EMPTY_STRING) + "\",\n" + + " \"description\":\"" + api.getDescription() + "\",\n" + + " \"context\":\"" + api.getContext() + "\",\n" + + " \"version\":\"" + apiIdentifier.getVersion() + "\"\n" + + "}"; + + RequestBody requestBody = RequestBody.create(JSON, apiString); Request request = new Request.Builder() - .url(creatAPIEndPoint) + .url(addAPIEndPoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + accessTokenInfo.getAccess_token()) .post(requestBody) @@ -328,21 +337,21 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { try { Response response = client.newCall(request).execute(); if (HttpStatus.SC_CREATED == response.code()) { - return gson.fromJson(response.body().string(), API.class); - + 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 createAPI(apiApplicationKey, refreshedAccessToken, api); + return addAPI(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(); + String msg = "Response status : " + response.code() + " Response message : " + response.message(); throw new UnexpectedResponseException(msg); } } catch (IOException e) { @@ -568,11 +577,12 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { String uuid, String action) throws APIServicesException, BadRequestException, UnexpectedResponseException { - String changeStatusEndPoint = endPointPrefix + Constants.API_ENDPOINT + "change-lifecycle?apiId=" + uuid + "&action=" + action; + String changeAPIStatusEndPoint = endPointPrefix + Constants.API_ENDPOINT + "change-lifecycle?apiId=" + uuid + + "&action=" + action; RequestBody requestBody = RequestBody.create(JSON, Constants.EMPTY_STRING); Request request = new Request.Builder() - .url(changeStatusEndPoint) + .url(changeAPIStatusEndPoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER + accessTokenInfo.getAccess_token()) .post(requestBody) @@ -986,7 +996,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { @Override public boolean addDocumentationContent(APIApplicationKey apiApplicationKey, AccessTokenInfo accessTokenInfo, - API api, String docId, String docContent) + API api, String docId, String docContent) throws APIServicesException, BadRequestException, UnexpectedResponseException { String addDocumentationContentEndPoint = endPointPrefix + Constants.API_ENDPOINT + api.getUuid() + "/documents/" + docId; @@ -1009,7 +1019,7 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { generateAccessTokenFromRefreshToken(accessTokenInfo.getRefresh_token(), apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); //TODO: max attempt count - return addDocumentationContent(apiApplicationKey, refreshedAccessToken, api,docId ,docContent); + 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); 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/constants/Constants.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/constants/Constants.java index d8a12810af..30074c7b4a 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/constants/Constants.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/constants/Constants.java @@ -34,7 +34,7 @@ public final class Constants { public static final String OAUTH_TOKEN_TYPE = "token_type"; public static final String REFRESH_TOKEN_GRANT_TYPE = "refresh_token"; public static final String SCOPE_PARAM_NAME = "scope"; - public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage"; + public static final String SCOPES = "apim:api_create apim:api_view apim:shared_scope_manage apim:api_import_export apim:api_publish"; public static final String DCR_END_POINT = "WorkflowConfigurations.DCREndPoint"; public static final String TOKE_END_POINT = "WorkflowConfigurations.TokenEndPoint"; public static final String ADAPTER_CONF_KEEP_ALIVE = "keepAlive"; @@ -58,6 +58,7 @@ public final class Constants { public static final String SCHEME_SEPARATOR = "://"; public static final String COLON = ":"; public static final String QUERY_KEY_VALUE_SEPARATOR = "="; + public static final String SPACE = " "; public static final String IOT_CORE_HOST = "iot.core.host"; public static final String IOT_CORE_HTTPS_PORT = "iot.core.https.port"; public static final String GET_ALL_SCOPES = "/api/am/publisher/v2/scopes?limit=1000"; 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/util/ScopeUtils.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/util/ScopeUtils.java index 18944c945c..a9fa3a3cb9 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/util/ScopeUtils.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/util/ScopeUtils.java @@ -62,11 +62,11 @@ public class ScopeUtils { public String toJSON() { String jsonString = "{\n" + - " \"name\":\" " + key + "\",\n" + - " \"displayName\":\" " + name + "\",\n" + - " \"description\":\" " + description + " \",\n" + + " \"name\":\"" + key + "\",\n" + + " \"displayName\":\"" + name + "\",\n" + + " \"description\":\"" + description + "\",\n" + " \"bindings\":[\n" + - " \" " + roles + " \"\n" + + " \"" + roles + "\"\n" + " ]\n" + "}"; return jsonString; 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 0780a4ad34..9765e14af8 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 @@ -167,7 +167,8 @@ public class APIPublisherServiceImpl implements APIPublisherService { if (!apiFound) { // add new scopes as shared scopes for (ApiScope apiScope : apiConfig.getScopes()) { - if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, apiScope.getKey())) { + if (!publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, + apiScope.getKey())) { Scope scope = new Scope(); scope.setName(apiScope.getName()); scope.setDescription(apiScope.getDescription()); @@ -178,12 +179,12 @@ public class APIPublisherServiceImpl implements APIPublisherService { } API api = getAPI(apiConfig, true); api.setId(apiIdentifier); - API createdAPI = publisherRESTAPIServices.createAPI(apiApplicationKey, accessTokenInfo , api); // add api + JSONObject createdAPI = publisherRESTAPIServices.addAPI(apiApplicationKey, accessTokenInfo, api); // add api if (apiConfig.getEndpointType() != null && "WS".equals(apiConfig.getEndpointType())) { publisherRESTAPIServices.saveAsyncApiDefinition(apiApplicationKey, accessTokenInfo, api.getUuid(), apiConfig.getAsyncApiDefinition()); } - if (CREATED_STATUS.equals(createdAPI.getStatus())) { + if (CREATED_STATUS.equals(createdAPI.getString("lifeCycleStatus"))) { // if endpoint type "dynamic" and then add in sequence if ("dynamic".equals(apiConfig.getEndpointType())) { Mediation mediation = new Mediation(); @@ -192,12 +193,13 @@ public class APIPublisherServiceImpl implements APIPublisherService { mediation.setType("in"); mediation.setGlobal(false); publisherRESTAPIServices.addApiSpecificMediationPolicy(apiApplicationKey, - accessTokenInfo, createdAPI.getUuid(), mediation); + accessTokenInfo, createdAPI.getString("id"), mediation); } - publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey, accessTokenInfo, createdAPI.getUuid(), PUBLISH_ACTION); + publisherRESTAPIServices.changeLifeCycleStatus(apiApplicationKey, accessTokenInfo, + createdAPI.getString("id"), PUBLISH_ACTION); APIRevision apiRevision = new APIRevision(); - apiRevision.setApiUUID(createdAPI.getUuid()); + apiRevision.setApiUUID(createdAPI.getString("id")); apiRevision.setDescription("Initial Revision"); String apiRevisionId = publisherRESTAPIServices.addAPIRevision(apiApplicationKey, accessTokenInfo, apiRevision).getRevisionUUID(); @@ -210,7 +212,7 @@ public class APIPublisherServiceImpl implements APIPublisherService { List apiRevisionDeploymentList = new ArrayList<>(); apiRevisionDeploymentList.add(apiRevisionDeployment); publisherRESTAPIServices.deployAPIRevision(apiApplicationKey, accessTokenInfo, - createdAPI.getUuid(), apiRevisionId, apiRevisionDeploymentList); + createdAPI.getString("id"), apiRevisionId, apiRevisionDeploymentList); } } else { if (WebappPublisherConfig.getInstance().isEnabledUpdateApi()) {