From 38df189b297097ae4d16c388c686cccbc0be6eae Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Thu, 9 Nov 2023 18:01:10 +0530 Subject: [PATCH] Improve organization perspective service URL build --- .../pom.xml | 5 ++ .../identity/api/user/common/Constants.java | 1 + .../api/user/common/ContextLoader.java | 11 +++ .../pom.xml | 5 ++ .../v1/core/UserOrganizationService.java | 4 +- .../api/user/organization/v1/util/Util.java | 77 +++++++++++++++++++ pom.xml | 2 +- 7 files changed, 102 insertions(+), 3 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.user.common/pom.xml b/components/org.wso2.carbon.identity.api.user.common/pom.xml index e4a598f1c..92e40d731 100644 --- a/components/org.wso2.carbon.identity.api.user.common/pom.xml +++ b/components/org.wso2.carbon.identity.api.user.common/pom.xml @@ -116,5 +116,10 @@ commons-lang.wso2 commons-lang + + org.wso2.carbon.identity.organization.management.core + org.wso2.carbon.identity.organization.management.service + provided + diff --git a/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/Constants.java b/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/Constants.java index d576ab3bb..2c2e29cd9 100644 --- a/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/Constants.java +++ b/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/Constants.java @@ -27,6 +27,7 @@ public class Constants { public static final String ERROR_CODE_DELIMITER = "-"; public static final String CORRELATION_ID_MDC = "Correlation-ID"; public static final String TENANT_CONTEXT_PATH_COMPONENT = "/t/%s"; + public static final String ORGANIZATION_CONTEXT_PATH_COMPONENT = "/o"; public static final String USER_API_PATH_COMPONENT = "/api/users"; /** diff --git a/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/ContextLoader.java b/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/ContextLoader.java index a372f9786..252a7b4e9 100644 --- a/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/ContextLoader.java +++ b/components/org.wso2.carbon.identity.api.user.common/src/main/java/org/wso2/carbon/identity/api/user/common/ContextLoader.java @@ -29,6 +29,7 @@ import org.wso2.carbon.identity.core.URLBuilderException; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.core.util.IdentityUtil; +import org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.core.UserStoreConfigConstants; @@ -39,6 +40,7 @@ import static org.wso2.carbon.identity.api.user.common.Constants.ErrorMessage.ERROR_CODE_INVALID_USERNAME; import static org.wso2.carbon.identity.api.user.common.Constants.ErrorMessage.ERROR_CODE_SERVER_ERROR; +import static org.wso2.carbon.identity.api.user.common.Constants.ORGANIZATION_CONTEXT_PATH_COMPONENT; import static org.wso2.carbon.identity.api.user.common.Constants.TENANT_CONTEXT_PATH_COMPONENT; import static org.wso2.carbon.identity.api.user.common.Constants.TENANT_NAME_FROM_CONTEXT; import static org.wso2.carbon.identity.api.user.common.Constants.USER_API_PATH_COMPONENT; @@ -175,6 +177,7 @@ public static URI buildURI(String endpoint) { /** * Builds URI prepending the user API context with the proxy context path to the endpoint. * Ex: /t//api/users/ + * : /t//o/api/users/ * * @param endpoint Relative endpoint path. * @return Relative URI. @@ -196,6 +199,7 @@ public static URI buildURIForBody(String endpoint) { /** * Builds the complete URI prepending the user API context without the proxy context path, to the endpoint. * Ex: https://localhost:9443/t//api/users/ + * : https://localhost:9443/t//o/api/users/ * * @param endpoint Relative endpoint path. * @return Fully qualified and complete URI. @@ -226,8 +230,15 @@ public static URI buildURIForHeader(String endpoint) { private static String getContext(String endpoint) { String context; + String organizationId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getOrganizationId(); if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) { context = USER_API_PATH_COMPONENT + endpoint; + if (StringUtils.isNotEmpty(organizationId)) { + String tenantDomain = (String) IdentityUtil.threadLocalProperties.get() + .get(OrganizationManagementConstants.ROOT_TENANT_DOMAIN); + context = String.format(TENANT_CONTEXT_PATH_COMPONENT, tenantDomain) + + ORGANIZATION_CONTEXT_PATH_COMPONENT + context; + } } else { context = String.format(TENANT_CONTEXT_PATH_COMPONENT, IdentityTenantUtil.resolveTenantDomain()) + USER_API_PATH_COMPONENT + endpoint; diff --git a/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/pom.xml b/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/pom.xml index 2e1a504de..7b99c5dd7 100644 --- a/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/pom.xml +++ b/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/pom.xml @@ -122,6 +122,11 @@ org.wso2.carbon.identity.api.user.common provided + + org.wso2.carbon.identity.framework + org.wso2.carbon.identity.application.common + provided + diff --git a/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/core/UserOrganizationService.java b/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/core/UserOrganizationService.java index 0fee6c466..50d085383 100644 --- a/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/core/UserOrganizationService.java +++ b/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/core/UserOrganizationService.java @@ -52,7 +52,6 @@ import static org.wso2.carbon.identity.api.user.common.Constants.USER_API_PATH_COMPONENT; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_BUILDING_PAGINATED_RESPONSE_URL; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_USER_ROOT_ORGANIZATION_NOT_FOUND; -import static org.wso2.carbon.identity.organization.management.service.util.Utils.buildURIForBody; import static org.wso2.carbon.identity.organization.management.service.util.Utils.getOrganizationId; import static org.wso2.carbon.identity.rest.api.user.organization.v1.Constants.ASC_SORT_ORDER; import static org.wso2.carbon.identity.rest.api.user.organization.v1.Constants.DESC_SORT_ORDER; @@ -60,6 +59,7 @@ import static org.wso2.carbon.identity.rest.api.user.organization.v1.util.Util.getError; import static org.wso2.carbon.identity.rest.api.user.organization.v1.util.Util.handleError; import static org.wso2.carbon.identity.rest.api.user.organization.v1.util.Util.handleOrganizationManagementException; +import static org.wso2.carbon.identity.rest.api.user.organization.v1.util.Util.organizationGetURL; /** * Call internal OSGi services to perform user organization management related operations. @@ -238,7 +238,7 @@ private OrganizationsResponse getAuthorizedOrganizationsResponse(Integer limit, organizationDTO.setId(organization.getId()); organizationDTO.setName(organization.getName()); organizationDTO.setStatus(Organization.StatusEnum.valueOf(organization.getStatus())); - organizationDTO.setRef(buildURIForBody(organization.getId())); + organizationDTO.setRef(organizationGetURL(organization.getId()).toString()); organizationDTOs.add(organizationDTO); } organizationsResponse.setOrganizations(organizationDTOs); diff --git a/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/util/Util.java b/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/util/Util.java index ed992b461..21b6884b3 100644 --- a/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/util/Util.java +++ b/components/org.wso2.carbon.identity.api.user.organization/org.wso2.carbon.identity.rest.api.user.organization.v1/src/main/java/org/wso2/carbon/identity/rest/api/user/organization/v1/util/Util.java @@ -19,17 +19,34 @@ package org.wso2.carbon.identity.rest.api.user.organization.v1.util; import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.identity.api.user.common.error.APIError; import org.wso2.carbon.identity.api.user.common.error.ErrorResponse; +import org.wso2.carbon.identity.core.ServiceURLBuilder; +import org.wso2.carbon.identity.core.URLBuilderException; +import org.wso2.carbon.identity.core.util.IdentityTenantUtil; +import org.wso2.carbon.identity.core.util.IdentityUtil; import org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementClientException; import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; import org.wso2.carbon.identity.rest.api.user.organization.v1.model.Error; +import java.net.URI; + import javax.ws.rs.core.Response; +import static org.wso2.carbon.identity.api.user.common.Constants.ORGANIZATION_CONTEXT_PATH_COMPONENT; +import static org.wso2.carbon.identity.api.user.common.Constants.TENANT_CONTEXT_PATH_COMPONENT; +import static org.wso2.carbon.identity.api.user.common.Constants.USER_API_PATH_COMPONENT; +import static org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants.Error.UNEXPECTED_SERVER_ERROR; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ORGANIZATION_PATH; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.PATH_SEPARATOR; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.SERVER_API_PATH_COMPONENT; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.V1_API_PATH_COMPONENT; + /** * This class provides util functions to the user organization management endpoint. */ @@ -126,4 +143,64 @@ private static String buildErrorDescription(OrganizationManagementConstants.Erro } return errorDescription; } + + /** + * The relative URL to get the organization. + * + * @param organizationId The unique identifier of the organization. + * @return URI + */ + public static URI organizationGetURL(String organizationId) { + + return buildURIForBody(V1_API_PATH_COMPONENT + PATH_SEPARATOR + ORGANIZATION_PATH + + PATH_SEPARATOR + organizationId); + } + + private static URI buildURIForBody(String endpoint) { + + String url; + String context = getContext(endpoint); + + try { + url = ServiceURLBuilder.create().addPath(context).build().getRelativePublicURL(); + } catch (URLBuilderException e) { + String errorDescription = "Server encountered an error while building URL for response body."; + throw buildInternalServerError(e, errorDescription); + } + return URI.create(url); + } + + private static String getContext(String endpoint) { + + String context; + String organizationId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getOrganizationId(); + if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) { + context = SERVER_API_PATH_COMPONENT + endpoint; + if (StringUtils.isNotEmpty(organizationId)) { + String tenantDomain = (String) IdentityUtil.threadLocalProperties.get() + .get(OrganizationManagementConstants.ROOT_TENANT_DOMAIN); + context = String.format(TENANT_CONTEXT_PATH_COMPONENT, tenantDomain) + + ORGANIZATION_CONTEXT_PATH_COMPONENT + context; + } + } else { + context = String.format(TENANT_CONTEXT_PATH_COMPONENT, IdentityTenantUtil.resolveTenantDomain()) + + USER_API_PATH_COMPONENT + endpoint; + } + return context; + } + + private static APIError buildInternalServerError(Exception e, String errorDescription) { + + String errorCode = UNEXPECTED_SERVER_ERROR.getCode(); + String errorMessage = "Error while building response."; + + ErrorResponse errorResponse = new ErrorResponse.Builder(). + withCode(errorCode) + .withMessage(errorMessage) + .withDescription(errorDescription) + .build(LOG, e, errorDescription); + + Response.Status status = Response.Status.INTERNAL_SERVER_ERROR; + return new APIError(status, errorResponse); + } } diff --git a/pom.xml b/pom.xml index 66bb4b7c3..b35cfe5ba 100644 --- a/pom.xml +++ b/pom.xml @@ -427,7 +427,7 @@ 3.0.5 4.5.2 6.7.71 - 1.0.50 + 1.0.85 6.9.10 5.1.17 3.3.1