From da1485d04f514d38ebd840a7a5e869f11762e4cb Mon Sep 17 00:00:00 2001 From: pasindu Date: Fri, 16 Jun 2023 11:58:52 +0530 Subject: [PATCH] Fix getApplication method with Rest api calls --- .../api/bean/APIMConsumer/Application.java | 9 +++-- .../extension/service/KeyMgtServiceImpl.java | 39 ++++++++++++------- 2 files changed, 30 insertions(+), 18 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/bean/APIMConsumer/Application.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/bean/APIMConsumer/Application.java index 50fb64147de..d56de0331da 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/bean/APIMConsumer/Application.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/bean/APIMConsumer/Application.java @@ -18,9 +18,10 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer; -import org.json.JSONObject; +import org.wso2.carbon.apimgt.api.model.APIKey; import java.util.List; +import java.util.Map; /** * This class represents the Consumer Application Information. @@ -36,7 +37,7 @@ public class Application { private List groups; private int subscriptionCount; private List keys; - private JSONObject attributes; + private Map attributes; private List subscriptionScopes; private String owner; private boolean hashEnabled; @@ -113,11 +114,11 @@ public class Application { this.keys = keys; } - public JSONObject getAttributes() { + public Map getAttributes() { return attributes; } - public void setAttributes(JSONObject attributes) { + public void setAttributes(Map attributes) { this.attributes = attributes; } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java index 9de536da5e1..36b40846f19 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java @@ -35,6 +35,7 @@ import org.apache.commons.logging.LogFactory; import org.json.JSONObject; import org.wso2.carbon.apimgt.api.APIConsumer; import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.model.APIKey; import org.wso2.carbon.apimgt.api.model.Application; import org.wso2.carbon.apimgt.impl.APIManagerFactory; import org.wso2.carbon.apimgt.impl.utils.APIUtil; @@ -429,30 +430,40 @@ public class KeyMgtServiceImpl implements KeyMgtService { * @throws KeyMgtException if any error occurs while retrieving the application */ private Application getApplication(String applicationName, String accessToken) throws KeyMgtException { - try { - APIManagerFactory apiManagerFactory = APIManagerFactory.getInstance(); -// APIConsumer apiConsumer = apiManagerFactory.getAPIConsumer(owner); + try { ConsumerRESTAPIServices consumerRESTAPIServices = KeyMgtDataHolder.getInstance().getConsumerRESTAPIServices(); io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application[] applications = consumerRESTAPIServices.getAllApplications(null, accessToken, applicationName); - //todo map Application and return - //todo modify the method signature and use access token and call REST API to get application data - return null; // todo:apim - apiConsumer.getApplicationsByName(owner, applicationName, ""); - // // curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://localhost:9443/api/am/devportal/v3/applications?query=CalculatorApp" - } + io.entgra.device.mgt.core.apimgt.extension.rest.api.bean.APIMConsumer.Application applicationFromRestCall; + if (applications.length == 1) { + applicationFromRestCall = applications[0]; + } else { + String msg = + "Found invalid number of applications. No of applications found from the APIM: " + applications.length; + throw new KeyMgtException(msg); + } -// catch (APIManagementException e) { -// msg = "Error while trying to retrieve the application"; -// log.error(msg); -// throw new KeyMgtException(msg); -// } + Application application = null; + application.setUUID(applicationFromRestCall.getApplicationId()); + application.setName(applicationFromRestCall.getName()); + application.setDescription(applicationFromRestCall.getDescription()); + application.setApplicationAttributes(applicationFromRestCall.getAttributes()); + application.setTokenType(applicationFromRestCall.getTokenType()); + application.setStatus(applicationFromRestCall.getStatus()); + application.setSubscriptionCount(applicationFromRestCall.getSubscriptionCount()); + application.setOwner(applicationFromRestCall.getOwner()); + application.setIsBlackListed(applicationFromRestCall.isHashEnabled()); + return application; + } catch (io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException e) { e.printStackTrace(); - throw new KeyMgtException(""); + msg = "Error while trying to retrieve the application"; + log.error(msg); + throw new KeyMgtException(msg); } catch (UnexpectedResponseException e) { throw new KeyMgtException(""); } catch (APIServicesException e) {