Skip to content

Commit

Permalink
Imrpove organization perspective service URL build
Browse files Browse the repository at this point in the history
  • Loading branch information
sadilchamishka committed Nov 9, 2023
1 parent 22d8817 commit 52d7f8e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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;
Expand Down Expand Up @@ -175,6 +176,7 @@ public static URI buildURI(String endpoint) {
/**
* Builds URI prepending the user API context with the proxy context path to the endpoint.
* Ex: /t/<tenant-domain>/api/users/<endpoint>
* : /t/<tenant-domain>/o/api/users/<endpoint>
*
* @param endpoint Relative endpoint path.
* @return Relative URI.
Expand All @@ -196,6 +198,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/<tenant-domain>/api/users/<endpoint>
* : https://localhost:9443/t/<tenant-domain>/o/api/users/<endpoint>
*
* @param endpoint Relative endpoint path.
* @return Fully qualified and complete URI.
Expand Down Expand Up @@ -226,8 +229,14 @@ 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("OrgPerspectiveResourceAccessedTenant");
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@
<artifactId>org.wso2.carbon.identity.api.user.common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.common</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
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;
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.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.
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -126,4 +143,63 @@ 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("OrgPerspectiveResourceAccessedTenant");
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);
}
}

0 comments on commit 52d7f8e

Please sign in to comment.