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 #529

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.server.common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,10 @@
<artifactId>org.wso2.carbon.utils</artifactId>
<scope>provided</scope>
</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 @@ -29,7 +29,7 @@ public class Constants {
public static final String SERVER_API_PATH_COMPONENT = "/api/server";
public static final String V1_API_PATH_COMPONENT = "/v1";
public static final String TENANT_CONTEXT_PATH_COMPONENT = "/t/%s";
public static final String ORGANIZATION_CONTEXT_PATH_COMPONENT = "/o/%s";
public static final String ORGANIZATION_CONTEXT_PATH_COMPONENT = "/o";
public static final String ERROR_CODE_RESOURCE_LIMIT_REACHED = "RLS-10001";

public static final String REGEX_COMMA = ",";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2019-2023, WSO2 LLC. (http://www.wso2.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.api.server.common;
Expand All @@ -27,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 java.net.URI;

Expand Down Expand Up @@ -81,7 +84,7 @@ public static String getOrganizationIdFromContext() {

/**
* Build URI prepending the server API context with the proxy context path to the endpoint.
* Ex: /t/<tenant-domain>/api/server/<endpoint> or /o/<organization-id>/api/server/<endpoint>
* Ex: /t/<tenant-domain>/api/server/<endpoint> or /t/<tenant-domain>/o/api/server/<endpoint>
*
* @param endpoint relative endpoint path.
* @return Relative URI.
Expand All @@ -103,7 +106,7 @@ public static URI buildURIForBody(String endpoint) {
/**
* Build the complete URI prepending the server API context without the proxy context path, to the endpoint.
* Ex: https://localhost:9443/t/<tenant-domain>/api/server/<endpoint> or
* https://localhost:9443/o/<organization-id>/api/server/<endpoint>
* https://localhost:9443/t/<tenant-domain>/o/api/server/<endpoint>
*
* @param endpoint relative endpoint path.
* @return Fully qualified and complete URI.
Expand Down Expand Up @@ -137,16 +140,18 @@ private static String getContext(String endpoint) {

String context;
String organizationId = getOrganizationIdFromContext();
if (StringUtils.isNotBlank(organizationId)) {
context = String.format(ORGANIZATION_CONTEXT_PATH_COMPONENT, organizationId) +
SERVER_API_PATH_COMPONENT + endpoint;
} else {
if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) {
context = SERVER_API_PATH_COMPONENT + endpoint;
} else {
context = String.format(TENANT_CONTEXT_PATH_COMPONENT, getTenantDomainFromContext()) +
SERVER_API_PATH_COMPONENT + endpoint;
if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) {
context = SERVER_API_PATH_COMPONENT + endpoint;
if (StringUtils.isNotEmpty(organizationId)) {
// When accessing organization resources, the URL should be in this format. "/t/<tenant-domain>/o/.."
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, getTenantDomainFromContext()) +
SERVER_API_PATH_COMPONENT + endpoint;
}
return context;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@
import static org.wso2.carbon.identity.api.server.organization.management.v1.constants.OrganizationManagementEndpointConstants.ASC_SORT_ORDER;
import static org.wso2.carbon.identity.api.server.organization.management.v1.constants.OrganizationManagementEndpointConstants.DESC_SORT_ORDER;
import static org.wso2.carbon.identity.api.server.organization.management.v1.constants.OrganizationManagementEndpointConstants.DISCOVERY_PATH;
import static org.wso2.carbon.identity.api.server.organization.management.v1.util.OrganizationManagementEndpointUtil.buildOrganizationURL;
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_INVALID_PAGINATION_PARAMETER_NEGATIVE_LIMIT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_SHARE_APPLICATION_EMPTY_REQUEST_BODY;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_SHARE_APPLICATION_REQUEST_BODY;
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.util.Utils.buildURIForBody;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.generateUniqueID;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.getOrganizationId;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.handleClientException;
Expand Down Expand Up @@ -597,6 +597,7 @@ private OrganizationResponse getOrganizationResponse(Organization organization)

ParentOrganizationDO parentOrganizationDO = organization.getParent();
if (parentOrganizationDO != null) {
parentOrganizationDO.setRef(buildOrganizationURL(organization.getParent().getId()).toString());
organizationResponse.setParent(getParentOrganization(parentOrganizationDO));
}

Expand Down Expand Up @@ -634,6 +635,7 @@ private GetOrganizationResponse getOrganizationResponseWithPermission(Organizati

ParentOrganizationDO parentOrganizationDO = organization.getParent();
if (parentOrganizationDO != null) {
parentOrganizationDO.setRef(buildOrganizationURL(organization.getParent().getId()).toString());
organizationResponse.setParent(getParentOrganization(parentOrganizationDO));
}

Expand Down Expand Up @@ -749,7 +751,7 @@ private OrganizationsResponse getOrganizationsResponse(Integer limit, String aft
organizationDTO.setId(organization.getId());
organizationDTO.setName(organization.getName());
organizationDTO.setStatus(BasicOrganizationResponse.StatusEnum.valueOf(organization.getStatus()));
organizationDTO.setRef(buildURIForBody(organization.getId()));
organizationDTO.setRef(buildOrganizationURL(organization.getId()).toString());
organizationDTOs.add(organizationDTO);
}
organizationsResponse.setOrganizations(organizationDTOs);
Expand Down Expand Up @@ -800,7 +802,7 @@ private SharedOrganizationsResponse createSharedOrgResponse(List<BasicOrganizati
for (BasicOrganization org : organizations) {
BasicOrganizationResponse basicOrganizationResponse =
new BasicOrganizationResponse().id(org.getId()).name(org.getName())
.ref(buildURIForBody(org.getId()));
.ref(buildOrganizationURL(org.getId()).toString());
response.addOrganizationsItem(basicOrganizationResponse);
}
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,22 @@
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.api.server.organization.management.v1.exceptions.OrganizationManagementEndpointException;
import org.wso2.carbon.identity.api.server.organization.management.v1.model.Error;
import org.wso2.carbon.identity.core.ServiceURLBuilder;
import org.wso2.carbon.identity.core.URLBuilderException;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementClientException;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;

import java.net.URI;

import javax.ws.rs.core.Response;

import static org.wso2.carbon.identity.api.server.common.ContextLoader.buildURIForBody;
import static org.wso2.carbon.identity.api.server.common.ContextLoader.buildURIForHeader;
import static org.wso2.carbon.identity.api.server.organization.management.v1.constants.OrganizationManagementEndpointConstants.DISCOVERY_PATH;
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_ERROR_BUILDING_RESPONSE_HEADER_URL;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_APPLICATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_USER_NOT_AUTHORIZED_TO_CREATE_ORGANIZATION;
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;
import static org.wso2.carbon.identity.organization.management.service.util.Utils.getContext;

/**
* This class provides util functions to the organization management endpoint.
Expand Down Expand Up @@ -150,6 +147,18 @@ public static URI getResourceLocation(String organizationId) {
+ organizationId);
}

/**
* 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);
}

/**
* Get location of the created organization discovery attributes.
*
Expand All @@ -162,34 +171,10 @@ public static URI getDiscoveryResourceLocation(String organizationId) {
+ organizationId + PATH_SEPARATOR + DISCOVERY_PATH);
}

private static URI buildURIForHeader(String endpoint) {

String context = getContext(endpoint);
try {
String url = ServiceURLBuilder.create().addPath(context).build().getAbsolutePublicURL();
return URI.create(url);
} catch (URLBuilderException e) {
LOG.error("Server encountered an error while building URL for response header.");
Error error = getError(ERROR_CODE_ERROR_BUILDING_RESPONSE_HEADER_URL.getCode(),
ERROR_CODE_ERROR_BUILDING_RESPONSE_HEADER_URL.getMessage(),
ERROR_CODE_ERROR_BUILDING_RESPONSE_HEADER_URL.getDescription());
throw new OrganizationManagementEndpointException(Response.Status.INTERNAL_SERVER_ERROR, error);
}
}

public static String buildURIForPagination(String paginationURL) {

String context = getContext(V1_API_PATH_COMPONENT + PATH_SEPARATOR + ORGANIZATION_PATH + paginationURL);

try {
return ServiceURLBuilder.create().addPath(context).build().getRelativePublicURL();
} catch (URLBuilderException e) {
LOG.error("Server encountered an error while building paginated URL for the response.", e);
Error error = getError(ERROR_CODE_ERROR_BUILDING_PAGINATED_RESPONSE_URL.getCode(),
ERROR_CODE_ERROR_BUILDING_PAGINATED_RESPONSE_URL.getMessage(),
ERROR_CODE_ERROR_BUILDING_PAGINATED_RESPONSE_URL.getDescription());
throw new OrganizationManagementEndpointException(Response.Status.INTERNAL_SERVER_ERROR, error);
}
return buildURIForBody(V1_API_PATH_COMPONENT + PATH_SEPARATOR + ORGANIZATION_PATH + paginationURL)
.toString();
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@
<identity.verification.version>1.0.3</identity.verification.version>

<!-- Organization management core Version -->
<org.wso2.carbon.identity.organization.management.core.version>1.0.64
<org.wso2.carbon.identity.organization.management.core.version>1.0.85
</org.wso2.carbon.identity.organization.management.core.version>
<org.wso2.carbon.identity.organization.management.core.version.range>[1.0.0, 2.0.0)
</org.wso2.carbon.identity.organization.management.core.version.range>
Expand Down
Loading