From 38df189b297097ae4d16c388c686cccbc0be6eae Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Thu, 9 Nov 2023 18:01:10 +0530 Subject: [PATCH 1/3] 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 From 3b81ffe597ed52f42cf40d5313553498b37b7813 Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Fri, 10 Nov 2023 12:21:02 +0530 Subject: [PATCH 2/3] Address review comments --- .../identity/api/user/common/Constants.java | 2 + .../api/user/common/ContextLoader.java | 19 ++++-- .../v1/core/UserOrganizationService.java | 4 +- .../api/user/organization/v1/util/Util.java | 64 +------------------ 4 files changed, 22 insertions(+), 67 deletions(-) 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 2c2e29cd9..3828a0086 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 @@ -29,6 +29,8 @@ public class Constants { 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"; + public static final String SERVER_API_PATH_COMPONENT = "/api/servers"; + /** * Enum for user error prefixes 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 252a7b4e9..0634898af 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 @@ -41,6 +41,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.SERVER_API_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; @@ -184,8 +185,13 @@ public static URI buildURI(String endpoint) { */ public static URI buildURIForBody(String endpoint) { + return buildURIForBody(endpoint, false); + } + + public static URI buildURIForBody(String endpoint, boolean isServerResource) { + String url; - String context = getContext(endpoint); + String context = getContext(endpoint, isServerResource); try { url = ServiceURLBuilder.create().addPath(context).build().getRelativePublicURL(); @@ -207,7 +213,7 @@ public static URI buildURIForBody(String endpoint) { public static URI buildURIForHeader(String endpoint) { URI loc; - String context = getContext(endpoint); + String context = getContext(endpoint, false); try { String url = ServiceURLBuilder.create().addPath(context).build().getAbsolutePublicURL(); @@ -225,14 +231,19 @@ public static URI buildURIForHeader(String endpoint) { * in non tenant qualified mode we need to append the tenant domain to the path manually. * * @param endpoint Relative endpoint path. + * @param isServerResource Is service URL builds for server resource. * @return Context of the API. */ - private static String getContext(String endpoint) { + private static String getContext(String endpoint, boolean isServerResource) { String context; String organizationId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getOrganizationId(); if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) { - context = USER_API_PATH_COMPONENT + endpoint; + if (isServerResource) { + context = SERVER_API_PATH_COMPONENT + endpoint; + } else { + context = USER_API_PATH_COMPONENT + endpoint; + } if (StringUtils.isNotEmpty(organizationId)) { String tenantDomain = (String) IdentityUtil.threadLocalProperties.get() .get(OrganizationManagementConstants.ROOT_TENANT_DOMAIN); 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 50d085383..a11f11c67 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 @@ -59,7 +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; +import static org.wso2.carbon.identity.rest.api.user.organization.v1.util.Util.buildOrganizationURL; /** * 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(organizationGetURL(organization.getId()).toString()); + organizationDTO.setRef(buildOrganizationURL(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 21b6884b3..7df453687 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,16 +19,10 @@ 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; @@ -38,13 +32,9 @@ 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.api.user.common.ContextLoader.buildURIForBody; 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; /** @@ -150,57 +140,9 @@ private static String buildErrorDescription(OrganizationManagementConstants.Erro * @param organizationId The unique identifier of the organization. * @return URI */ - public static URI organizationGetURL(String organizationId) { + public static URI buildOrganizationURL(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); + PATH_SEPARATOR + organizationId, true); } } From 90b14074cc14f807547d405a991060efd6ac0838 Mon Sep 17 00:00:00 2001 From: sadilchamishka Date: Fri, 10 Nov 2023 13:26:31 +0530 Subject: [PATCH 3/3] Address review comments --- .../org/wso2/carbon/identity/api/user/common/Constants.java | 2 +- .../pom.xml | 5 ----- .../user/organization/v1/core/UserOrganizationService.java | 2 +- .../identity/rest/api/user/organization/v1/util/Util.java | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) 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 3828a0086..d101fb7bf 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 @@ -29,7 +29,7 @@ public class Constants { 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"; - public static final String SERVER_API_PATH_COMPONENT = "/api/servers"; + public static final String SERVER_API_PATH_COMPONENT = "/api/server"; /** 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 7b99c5dd7..2e1a504de 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,11 +122,6 @@ 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 a11f11c67..7a6ff53ab 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 @@ -56,10 +56,10 @@ 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; import static org.wso2.carbon.identity.rest.api.user.organization.v1.Constants.ORGANIZATIONS_ME_ENDPOINT; +import static org.wso2.carbon.identity.rest.api.user.organization.v1.util.Util.buildOrganizationURL; 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.buildOrganizationURL; /** * Call internal OSGi services to perform user organization management related operations. 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 7df453687..8418183a4 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 @@ -142,7 +142,7 @@ private static String buildErrorDescription(OrganizationManagementConstants.Erro */ public static URI buildOrganizationURL(String organizationId) { - return buildURIForBody(V1_API_PATH_COMPONENT + PATH_SEPARATOR + ORGANIZATION_PATH + + return buildURIForBody(PATH_SEPARATOR + V1_API_PATH_COMPONENT + PATH_SEPARATOR + ORGANIZATION_PATH + PATH_SEPARATOR + organizationId, true); } }