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 all commits
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,7 +27,10 @@ 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";
public static final String SERVER_API_PATH_COMPONENT = "/api/server";


/**
* Enum for user error prefixes
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,8 @@

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;
Expand Down Expand Up @@ -175,14 +178,20 @@ 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.
*/
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();
Expand All @@ -196,14 +205,15 @@ 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.
*/
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();
Expand All @@ -221,13 +231,25 @@ 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);
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 @@ -52,11 +52,11 @@
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.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;
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(buildOrganizationURL(organization.getId()).toString());
organizationDTOs.add(organizationDTO);
}
organizationsResponse.setOrganizations(organizationDTOs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@
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.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.V1_API_PATH_COMPONENT;

/**
* This class provides util functions to the user organization management endpoint.
*/
Expand Down Expand Up @@ -126,4 +133,16 @@ 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 buildOrganizationURL(String organizationId) {

return buildURIForBody(PATH_SEPARATOR + V1_API_PATH_COMPONENT + PATH_SEPARATOR + ORGANIZATION_PATH +
PATH_SEPARATOR + organizationId, true);
}
}
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