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

Introduce B2B Organization Resource Resolver #6139

Closed
Closed
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
204 changes: 204 additions & 0 deletions components/org-mgt/org.wso2.carbon.identity.org.resource.mgt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org-mgt</artifactId>
<version>7.5.119</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.identity.org.resource.mgt</artifactId>
<packaging>bundle</packaging>
<name>WSO2 Carbon - Organization Management BE</name>
<description>
org.wso2.carbon.identity.org.resource.mgt provides the management functionality for organizations.
</description>
<url>http://wso2.org</url>

<dependencies>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
</dependency>
<dependency>
<groupId>commons-collections.wso2</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>org.ops4j.pax.logging</groupId>
<artifactId>pax-logging-api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.base</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.organization.management.core</groupId>
<artifactId>org.wso2.carbon.identity.organization.management.service</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>

<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Private-Package>
org.wso2.carbon.identity.org.resource.mgt.internal
</Private-Package>
<Import-Package>
org.apache.commons.lang; version="${commons-lang.wso2.osgi.version.range}",
org.apache.commons.logging; version="${import.package.version.commons.logging}",
org.apache.commons.collections; version="${commons-collections.wso2.osgi.version.range}",

org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",

org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}",
org.wso2.carbon.identity.base;version="${carbon.identity.package.import.version.range}",

org.wso2.carbon.identity.application.mgt;
version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.application.common;
version="${carbon.identity.package.import.version.range}",

org.wso2.carbon.identity.organization.management.service;
version="${org.wso2.carbon.identity.organization.management.core.version.range}",
org.wso2.carbon.identity.organization.management.service.exception;
version="${org.wso2.carbon.identity.organization.management.core.version.range}",
org.wso2.carbon.identity.organization.management.service.util;
version="${org.wso2.carbon.identity.organization.management.core.version.range}"
</Import-Package>
<Export-Package>
!org.wso2.carbon.identity.org.resource.mgt.internal,
org.wso2.carbon.identity.org.resource.mgt.*;
version="${carbon.identity.package.export.version}"
</Export-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<!--suppress UnresolvedMavenProperty -->
<argLine>
${argLine}
--add-opens java.xml/jdk.xml.internal=ALL-UNNAMED
--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED
</argLine>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-prepare-agent-integration</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-report-integration</id>
<goals>
<goal>report-integration</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<!--<minimum>0.60</minimum>-->
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* 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.
*/

package org.wso2.carbon.identity.org.resource.mgt;

import org.wso2.carbon.identity.org.resource.mgt.exceptions.NotImplementedException;
import org.wso2.carbon.identity.org.resource.mgt.exceptions.OrgResourceManagementException;
import org.wso2.carbon.identity.org.resource.mgt.strategy.AggregationStrategy;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Service interface for organization resource management.
*/
public interface OrgResourceManagementService {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check service name


/**
* Get resources from the organization hierarchy.
*
* @param organizationId Organization ID.
* @param resourceRetriever Function to retrieve the resource.
* @param aggregationStrategy Aggregation strategy.
* @param <T> Type of the resource.
* @return Resolved resources.
* @throws OrgResourceManagementException If an error occurs while retrieving the resources.
*/
default <T> T getResourcesFromOrgHierarchy(String organizationId,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if we need default

Function<String, Optional<T>> resourceRetriever,
AggregationStrategy<T> aggregationStrategy)
throws OrgResourceManagementException {

throw new NotImplementedException("getResourcesFromOrgHierarchy method is not implemented in " +
this.getClass());
}

/**
* Get resources from the organization and application hierarchy.
*
* @param organizationId Organization ID.
* @param applicationId Application ID.
* @param resourceRetriever Function to retrieve the resource.
* @param aggregationStrategy Aggregation strategy.
* @param <T> Type of the resource.
* @return Resolved resources.
* @throws OrgResourceManagementException If an error occurs while retrieving the resources.
*/
default <T> T getResourcesFromOrgHierarchy(String organizationId, String applicationId,
BiFunction<String, String, Optional<T>> resourceRetriever,
AggregationStrategy<T> aggregationStrategy)
throws OrgResourceManagementException {

throw new NotImplementedException("getResourcesFromOrgAppHierarchy method is not implemented in " +
this.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* 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.
*/

package org.wso2.carbon.identity.org.resource.mgt;

import org.apache.commons.collections.CollectionUtils;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.org.resource.mgt.exceptions.OrgResourceManagementException;
import org.wso2.carbon.identity.org.resource.mgt.exceptions.OrgResourceManagementServerException;
import org.wso2.carbon.identity.org.resource.mgt.internal.OrgResourceManagementServiceDataHolder;
import org.wso2.carbon.identity.org.resource.mgt.strategy.AggregationStrategy;
import org.wso2.carbon.identity.org.resource.mgt.util.OrgResourceManagementUtil;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
* Implementation of the OrgResourceManagementService.
*/
public class OrgResourceManagementServiceImpl implements OrgResourceManagementService {

@Override
public <T> T getResourcesFromOrgHierarchy(String organizationId, Function<String, Optional<T>> resourceRetriever,
AggregationStrategy<T> aggregationStrategy)
throws OrgResourceManagementException {

try {
OrganizationManager organizationManager = OrgResourceManagementUtil.getOrganizationManager();
List<String> organizationIds = organizationManager.getAncestorOrganizationIds(organizationId);

if (CollectionUtils.isEmpty(organizationIds) || organizationIds.isEmpty()) {
return null;
}

return aggregationStrategy.aggregate(organizationIds, resourceRetriever);
} catch (OrganizationManagementException e) {
String errorMsg = String.format(
"Unexpected server error occurred while resolving resource for organization with id: %s",
organizationId);
throw new OrgResourceManagementServerException(errorMsg, e);
}
}

@Override
public <T> T getResourcesFromOrgHierarchy(String organizationId, String applicationId,
BiFunction<String, String, Optional<T>> resourceRetriever,
AggregationStrategy<T> aggregationStrategy)
throws OrgResourceManagementException {

try {
OrganizationManager organizationManager = OrgResourceManagementUtil.getOrganizationManager();
List<String> organizationIds = organizationManager.getAncestorOrganizationIds(organizationId);

if (CollectionUtils.isEmpty(organizationIds) || organizationIds.isEmpty()) {
return null;
}

ApplicationManagementService applicationManagementService = getApplicationManagementService();
Map<String, String> ancestorAppIds = Collections.emptyMap();
if (applicationId != null) {
ancestorAppIds = applicationManagementService.getAncestorAppIds(applicationId, organizationId);
}

return aggregationStrategy.aggregate(organizationIds, ancestorAppIds, resourceRetriever);
} catch (OrganizationManagementException | IdentityApplicationManagementException e) {
String errorMsg = String.format(
"Unexpected server error occurred while resolving resource for organization with id: %s and " +
"application id: %s.", organizationId, applicationId);
throw new OrgResourceManagementServerException(errorMsg, e);
}
}

private ApplicationManagementService getApplicationManagementService() {

return OrgResourceManagementServiceDataHolder.getInstance().getApplicationManagementService();
}
}
Loading
Loading