Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve organization perspective service URL build #184

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/org.wso2.carbon.identity.api.user.common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,10 @@
<groupId>commons-lang.wso2</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management.core</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.service</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
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 @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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/<tenant-domain>/api/users/<endpoint>
* : /t/<tenant-domain>/o/api/users/<endpoint>
*
* @param endpoint Relative endpoint path.
* @return Relative URI.
Expand All @@ -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/<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 +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;
}
sadilchamishka marked this conversation as resolved.
Show resolved Hide resolved
} 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,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) {
sadilchamishka marked this conversation as resolved.
Show resolved Hide resolved

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) {
sadilchamishka marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@
<maven.findbugsplugin.version>3.0.5</maven.findbugsplugin.version>
<carbon.business-process.version>4.5.2</carbon.business-process.version>
<identity.oauth.version>6.7.71</identity.oauth.version>
<identity.org.mgt.core.version>1.0.50</identity.org.mgt.core.version>
<identity.org.mgt.core.version>1.0.85</identity.org.mgt.core.version>
<testng.version>6.9.10</testng.version>
<fido2.version>5.1.17</fido2.version>
<identity.totp.version>3.3.1</identity.totp.version>
Expand Down