From f46b01f0c07036af6cd9cc44ca3587b661c198b6 Mon Sep 17 00:00:00 2001 From: JeethJJ Date: Sat, 26 Oct 2024 01:53:47 +0530 Subject: [PATCH] Add new method to get application enabled status. --- .../util/IdentityManagementEndpointUtil.java | 2 +- .../ApplicationDataRetrievalClient.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/IdentityManagementEndpointUtil.java b/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/IdentityManagementEndpointUtil.java index d60355b7fd9a..ccc24cf4053a 100644 --- a/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/IdentityManagementEndpointUtil.java +++ b/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/IdentityManagementEndpointUtil.java @@ -192,7 +192,7 @@ public static final String getUserPortalUrl(String userPortalUrl, String tenantD ApplicationDataRetrievalClient applicationDataRetrievalClient = new ApplicationDataRetrievalClient(); String myAccountAccessUrl; try { - if (CarbonConstants.ENABLE_LEGACY_AUTHZ_RUNTIME == true) { + if (CarbonConstants.ENABLE_LEGACY_AUTHZ_RUNTIME) { myAccountAccessUrl = applicationDataRetrievalClient.getApplicationAccessURL(SUPER_TENANT, My_ACCOUNT_APPLICATION_NAME); } else { diff --git a/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/client/ApplicationDataRetrievalClient.java b/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/client/ApplicationDataRetrievalClient.java index d376865cfc9f..06cc5e91d7de 100644 --- a/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/client/ApplicationDataRetrievalClient.java +++ b/components/identity-mgt/org.wso2.carbon.identity.mgt.endpoint.util/src/main/java/org/wso2/carbon/identity/mgt/endpoint/util/client/ApplicationDataRetrievalClient.java @@ -54,6 +54,8 @@ public class ApplicationDataRetrievalClient { private static final String APP_NAME = "name"; private static final String ACCESS_URL_KEY = "accessUrl"; private static final String APP_ID = "id"; + private static final String APP_ENABLED_STATE_QUERY = "&attributes=applicationEnabled"; + private static final String APP_ENABLED_STATE_KEY = "applicationEnabled"; /** * Gets the access url configured for the given application. @@ -108,6 +110,51 @@ public String getApplicationAccessURL(String tenant, String applicationName) return StringUtils.EMPTY; } + /** + * Gets the enabled status of the given application. + * + * @param tenant tenant domain of the application. + * @param applicationName name of the application. + * @return the enabled status of the given application. + * @throws ApplicationDataRetrievalClientException if IO exception occurs or access URL is not configured. + */ + public boolean getApplicationEnabledStatus(String tenant, String applicationName) + throws ApplicationDataRetrievalClientException { + + try (CloseableHttpClient httpclient = HTTPClientUtils.createClientWithCustomVerifier().build()) { + HttpGet request = + new HttpGet(getApplicationsEndpoint(tenant) + APP_FILTER + + Encode.forUriComponent(applicationName) + APP_ENABLED_STATE_QUERY); + setAuthorizationHeader(request); + + try (CloseableHttpResponse response = httpclient.execute(request)) { + + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + JSONObject jsonResponse = new JSONObject( + new JSONTokener(new InputStreamReader(response.getEntity().getContent()))); + JSONArray applications = jsonResponse.getJSONArray(APPLICATIONS_KEY); + if (applications.length() != 1) { + return false; + } + + JSONObject application = (JSONObject) applications.get(0); + if (application.has(APP_ENABLED_STATE_KEY)) { + return application.getBoolean(APP_ENABLED_STATE_KEY); + } + } + } finally { + request.releaseConnection(); + } + } catch (IOException | JSONException e) { + String msg = "Error while getting enabled status of " + applicationName + " in tenant : " + tenant; + if (log.isDebugEnabled()) { + log.debug(msg, e); + } + throw new ApplicationDataRetrievalClientException(msg, e); + } + return false; + } + /** * Gets the access url configured for the given application. *