-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
524 additions
and
11 deletions.
There are no files selected for viewing
173 changes: 173 additions & 0 deletions
173
...so2.carbon.identity.core/src/main/java/org/wso2/carbon/identity/core/dao/RegistryDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* 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.core.dao; | ||
|
||
import org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement; | ||
import org.wso2.carbon.identity.base.IdentityException; | ||
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; | ||
import org.wso2.carbon.identity.core.util.IdentityTenantUtil; | ||
|
||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RegistryDAO { | ||
|
||
private static final String PATH = "PATH"; | ||
private static final String TENANT_ID = "TENANT_ID"; | ||
private static final String REG_NAME = "REG_NAME"; | ||
|
||
public String getPathId(String path, String tenantDomain) throws IdentityException { | ||
|
||
String pathId = null; | ||
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) { | ||
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, | ||
RegistrySQLQueries.GET_REG_PATH_ID)) { | ||
statement.setString(PATH, path); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
if (resultSet.next()) { | ||
pathId = resultSet.getString(1); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new IdentityException("Error while retrieving registry path id.", e); | ||
} | ||
} catch (SQLException | IdentityException e) { | ||
throw new IdentityException("Error while retrieving registry path id.", e); | ||
} | ||
return pathId; | ||
} | ||
|
||
public Map<String, String> getCollectionProperties(int pathId, String tenantDomain) throws IdentityException { | ||
|
||
Map<String, String> properties = new HashMap<>(); | ||
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) { | ||
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, | ||
RegistrySQLQueries.GET_REG_RESOURCE_PROPERTY_COLLECTION)) { | ||
statement.setInt(PATH, pathId); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
while (resultSet.next()) { | ||
String key = resultSet.getString(1); | ||
String value = resultSet.getString(2); | ||
properties.put(key, value); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new IdentityException("Error while retrieving registry resource collection properties.", e); | ||
} | ||
} catch (SQLException | IdentityException e) { | ||
throw new IdentityException("Error while retrieving registry resource collection properties.", e); | ||
} | ||
return properties; | ||
} | ||
|
||
public String[] getCollectionChildren(String path, int pathId, String tenantDomain) throws IdentityException { | ||
|
||
List<String> children = new ArrayList<>(); | ||
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) { | ||
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, | ||
RegistrySQLQueries.GET_REG_NAME_COLLECTION)) { | ||
statement.setInt(PATH, pathId); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
while (resultSet.next()) { | ||
children.add(path + "/" + resultSet.getString(1)); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new IdentityException("Error while retrieving registry resource collection sub resource paths.", | ||
e); | ||
} | ||
|
||
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, | ||
RegistrySQLQueries.GET_REG_PATH_COLLECTION)) { | ||
statement.setInt(PATH, pathId); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
while (resultSet.next()) { | ||
String childPathName = resultSet.getString(2).split("/(?!.*\\/)")[1]; | ||
children.add(path + "/" + childPathName); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new IdentityException("Error while retrieving registry resource collection sub paths.", e); | ||
} | ||
} catch (SQLException | IdentityException e) { | ||
throw new IdentityException("Error while retrieving registry resource collection children.", e); | ||
} | ||
return children.toArray(new String[0]); | ||
} | ||
|
||
public Map<String, String> getResourceProperties(int pathId, String resourceName, String tenantDomain) | ||
throws IdentityException { | ||
|
||
Map<String, String> properties = new HashMap<>(); | ||
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) { | ||
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, | ||
RegistrySQLQueries.GET_REG_RESOURCE_PROPERTY_RESOURCE)) { | ||
statement.setInt(PATH, pathId); | ||
statement.setString(REG_NAME, resourceName); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
while (resultSet.next()) { | ||
String key = resultSet.getString(1); | ||
String value = resultSet.getString(2); | ||
properties.put(key, value); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new IdentityException("Error while retrieving registry resource properties.", e); | ||
} | ||
} catch (SQLException | IdentityException e) { | ||
throw new IdentityException("Error while retrieving registry resource properties.", e); | ||
} | ||
return properties; | ||
} | ||
|
||
public byte[] getResourceContent(int pathId, String resourceName, String tenantDomain) | ||
throws IdentityException { | ||
|
||
byte[] content = null; | ||
try (Connection connection = IdentityDatabaseUtil.getGovernanceDBConnection(false)) { | ||
try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, | ||
RegistrySQLQueries.GET_REG_CONTENT)) { | ||
statement.setInt(PATH, pathId); | ||
statement.setString(REG_NAME, resourceName); | ||
statement.setInt(TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain)); | ||
try (ResultSet resultSet = statement.executeQuery()) { | ||
if (resultSet.next()) { | ||
content = resultSet.getBytes(1); | ||
} | ||
} | ||
} catch (SQLException e) { | ||
throw new IdentityException("Error while retrieving registry resource content.", e); | ||
} | ||
} catch (SQLException | IdentityException e) { | ||
throw new IdentityException("Error while retrieving registry resource content.", e); | ||
} | ||
return content; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...bon.identity.core/src/main/java/org/wso2/carbon/identity/core/dao/RegistrySQLQueries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.core.dao; | ||
|
||
public class RegistrySQLQueries { | ||
|
||
private RegistrySQLQueries() { | ||
|
||
} | ||
|
||
public static final String GET_REG_PATH_ID = | ||
"SELECT REG_PATH_ID FROM REG_PATH WHERE REG_PATH_VALUE = :PATH; AND REG_TENANT_ID = :TENANT_ID;"; | ||
public static final String GET_REG_RESOURCE_PROPERTY_COLLECTION = | ||
"SELECT REG_NAME, REG_VALUE FROM REG_PROPERTY P, REG_RESOURCE_PROPERTY RP WHERE P.REG_ID=RP.REG_PROPERTY_ID AND RP.REG_PATH_ID = :PATH; AND RP.REG_RESOURCE_NAME IS NULL AND P.REG_TENANT_ID=RP.REG_TENANT_ID AND RP.REG_TENANT_ID = :TENANT_ID; ORDER BY P.REG_ID"; | ||
public static final String GET_REG_RESOURCE_PROPERTY_RESOURCE = | ||
"SELECT REG_NAME, REG_VALUE FROM REG_PROPERTY P, REG_RESOURCE_PROPERTY RP WHERE P.REG_ID=RP.REG_PROPERTY_ID AND RP.REG_PATH_ID = :PATH; AND RP.REG_RESOURCE_NAME = :REG_NAME; AND P.REG_TENANT_ID=RP.REG_TENANT_ID AND RP.REG_TENANT_ID = :TENANT_ID; ORDER BY P.REG_ID"; | ||
public static final String GET_REG_NAME_COLLECTION = | ||
"SELECT REG_NAME FROM REG_RESOURCE WHERE REG_PATH_ID = :PATH; AND REG_TENANT_ID = :TENANT_ID; AND REG_NAME IS NOT NULL;"; | ||
public static final String GET_REG_PATH_COLLECTION = | ||
"SELECT P.REG_PATH_ID, P.REG_PATH_VALUE FROM REG_PATH P, REG_RESOURCE R WHERE P.REG_PATH_PARENT_ID = :PATH; AND P.REG_TENANT_ID = :TENANT_ID; AND R.REG_PATH_ID=P.REG_PATH_ID AND R.REG_NAME IS NULL AND R.REG_TENANT_ID = :TENANT_ID;"; | ||
public static final String GET_REG_CONTENT = | ||
"SELECT REG_CONTENT_DATA FROM REG_CONTENT WHERE REG_CONTENT_ID = (SELECT REG_CONTENT_ID FROM REG_RESOURCE WHERE REG_PATH_ID = :PATH; AND REG_NAME = :REG_NAME; AND REG_TENANT_ID = :TENANT_ID; );"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.