Skip to content

Change table API_AM_POLICY_MAPPING and AM_API_OPERATION_POLICY_MAPPING #13152

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

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -16459,12 +16459,13 @@ public void addAPIProductResourceMappings(List<APIProductResource> productResour

Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

insertOperationPolicyMappingStatement.setInt(1, rs.getInt(1));
insertOperationPolicyMappingStatement
.setString(2, clonedPoliciesMap.get(policy.getPolicyId()));
insertOperationPolicyMappingStatement.setString(3, policy.getDirection());
insertOperationPolicyMappingStatement.setString(4, paramJSON);
insertOperationPolicyMappingStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
insertOperationPolicyMappingStatement.setInt(5, policy.getOrder());
insertOperationPolicyMappingStatement.addBatch();
}
Expand Down Expand Up @@ -20451,11 +20452,12 @@ public void addAPIProductRevision(APIRevision apiRevision) throws APIManagementE
apiRevision.getRevisionUUID(), clonedPoliciesMap, toBeClonedPolicyDetails);
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

insertOperationPolicyMappingStatement.setInt(1, rs.getInt(1));
insertOperationPolicyMappingStatement.setString(2, clonedPoliciesMap.get(policy.getPolicyId()));
insertOperationPolicyMappingStatement.setString(3, policy.getDirection());
insertOperationPolicyMappingStatement.setString(4, paramJSON);
insertOperationPolicyMappingStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
insertOperationPolicyMappingStatement.setInt(5, policy.getOrder());
insertOperationPolicyMappingStatement.addBatch();
}
Expand Down Expand Up @@ -20699,11 +20701,12 @@ public void restoreAPIProductRevision(APIRevision apiRevision, String organizati

Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

addOperationPolicyStatement.setInt(1, rs.getInt(1));
addOperationPolicyStatement.setString(2, clonedPoliciesMap.get(policy.getPolicyName()));
addOperationPolicyStatement.setString(3, policy.getDirection());
addOperationPolicyStatement.setString(4, paramJSON);
addOperationPolicyStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
addOperationPolicyStatement.setInt(5, policy.getOrder());
addOperationPolicyStatement.executeUpdate();
}
Expand Down Expand Up @@ -21260,10 +21263,11 @@ public void addOperationPolicyMapping(Set<URITemplate> uriTemplates, Connection
for (OperationPolicy operationPolicy : operationPolicies){
Gson gson = new Gson();
String paramJSON = gson.toJson(operationPolicy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));
preparedStatement.setInt(1, uriTemplate.getId());
preparedStatement.setString(2,operationPolicy.getPolicyId());
preparedStatement.setString(3, operationPolicy.getDirection());
preparedStatement.setString(4, paramJSON);
preparedStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
preparedStatement.setInt(5, operationPolicy.getOrder());
preparedStatement.addBatch();
}
Expand Down Expand Up @@ -22915,7 +22919,7 @@ private OperationPolicy populateOperationPolicyWithRS(ResultSet rs) throws SQLEx
operationPolicy.setPolicyId(rs.getString("POLICY_UUID"));
operationPolicy.setOrder(rs.getInt("POLICY_ORDER"));
operationPolicy.setDirection(rs.getString("DIRECTION"));
operationPolicy.setParameters(APIMgtDBUtil.convertJSONStringToMap(rs.getString("PARAMETERS")));
operationPolicy.setParameters(APIMgtDBUtil.convertJSONStringToMap(APIMgtDBUtil.getStringFromInputStream(rs.getBinaryStream("PARAMETERS"))));
return operationPolicy;
}

Expand Down Expand Up @@ -23033,6 +23037,7 @@ private void addAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplate,
usedClonedPolicies, toBeClonedPolicyDetails);
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));
if (log.isDebugEnabled()) {
log.debug("Adding operation policy " + policy.getPolicyName() + " for API "
+ apiUUID + " to URL mapping Id " + template.getId());
Expand All @@ -23041,7 +23046,7 @@ private void addAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplate,
operationPolicyMappingStatement.setInt(1, template.getId());
operationPolicyMappingStatement.setString(2, updatedPoliciesMap.get(policy.getPolicyId()));
operationPolicyMappingStatement.setString(3, policy.getDirection());
operationPolicyMappingStatement.setString(4, paramJSON);
operationPolicyMappingStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
operationPolicyMappingStatement.setInt(5, policy.getOrder());
operationPolicyMappingStatement.addBatch();
}
Expand All @@ -23055,6 +23060,7 @@ private void addAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplate,
usedClonedPolicies, toBeClonedPolicyDetails);
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

if (log.isDebugEnabled()) {
log.debug("Adding API level policy " + policy.getPolicyName() + ":"
Expand All @@ -23065,7 +23071,7 @@ private void addAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplate,
apiLevelPolicyMappingStatement.setString(2, null);
apiLevelPolicyMappingStatement.setString(3, updatedPoliciesMap.get(policy.getPolicyId()));
apiLevelPolicyMappingStatement.setString(4, policy.getDirection());
apiLevelPolicyMappingStatement.setString(5, paramJSON);
apiLevelPolicyMappingStatement.setBinaryStream(5, paramInputStream, paramJSON.length());
apiLevelPolicyMappingStatement.setInt(6, policy.getOrder());
apiLevelPolicyMappingStatement.addBatch();
}
Expand Down Expand Up @@ -23269,6 +23275,7 @@ private void addAPILevelPolicies(List<OperationPolicy> policies, String apiUUID,

Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

if (log.isDebugEnabled()) {
log.debug("Adding API level policy " + policy.getPolicyName() + ":"
Expand All @@ -23279,7 +23286,7 @@ private void addAPILevelPolicies(List<OperationPolicy> policies, String apiUUID,
statement.setString(2, revisionUUID);
statement.setString(3, updatedPoliciesMap.get(policy.getPolicyId()));
statement.setString(4, policy.getDirection());
statement.setString(5, paramJSON);
statement.setBinaryStream(5, paramInputStream, paramJSON.length());
statement.setInt(6, policy.getOrder());
statement.addBatch();
}
Expand Down Expand Up @@ -23465,6 +23472,7 @@ private void revisionAPIPolicies(APIRevision apiRevision, String tenantDomain,
apiRevision.getRevisionUUID(), clonedPolicyMap, toBeClonedPolicyDetails);
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

if (log.isDebugEnabled()) {
log.debug("Adding operation policy " + policy.getPolicyName() + ":"
Expand All @@ -23474,7 +23482,7 @@ private void revisionAPIPolicies(APIRevision apiRevision, String tenantDomain,
operationPolicyMappingStatement.setInt(1, urlMapping.getId());
operationPolicyMappingStatement.setString(2, clonedPolicyMap.get(policy.getPolicyId()));
operationPolicyMappingStatement.setString(3, policy.getDirection());
operationPolicyMappingStatement.setString(4, paramJSON);
operationPolicyMappingStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
operationPolicyMappingStatement.setInt(5, policy.getOrder());
operationPolicyMappingStatement.addBatch();
}
Expand All @@ -23488,6 +23496,7 @@ private void revisionAPIPolicies(APIRevision apiRevision, String tenantDomain,
clonedPolicyMap, toBeClonedPolicyDetails);
Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));

if (log.isDebugEnabled()) {
log.debug("Adding API level policy " + policy.getPolicyName() + ":"
Expand All @@ -23498,7 +23507,7 @@ private void revisionAPIPolicies(APIRevision apiRevision, String tenantDomain,
apiLevelPolicyMappingStatement.setString(2, apiRevision.getRevisionUUID());
apiLevelPolicyMappingStatement.setString(3, clonedPolicyMap.get(policy.getPolicyId()));
apiLevelPolicyMappingStatement.setString(4, policy.getDirection());
apiLevelPolicyMappingStatement.setString(5, paramJSON);
apiLevelPolicyMappingStatement.setBinaryStream(5, paramInputStream, paramJSON.length());
apiLevelPolicyMappingStatement.setInt(6, policy.getOrder());
apiLevelPolicyMappingStatement.addBatch();
}
Expand Down Expand Up @@ -23613,6 +23622,7 @@ private void restoreAPIPolicies(APIRevision apiRevision, String tenantDomain, Ma

Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());
InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));
if (log.isDebugEnabled()) {
log.debug("Restored operation policy " + policy.getPolicyName() + ":"
+ policy.getPolicyVersion() + " from API revision " + apiRevision.getRevisionUUID());
Expand All @@ -23621,7 +23631,7 @@ private void restoreAPIPolicies(APIRevision apiRevision, String tenantDomain, Ma
operationPolicyMappingStatement.setInt(1, urlMapping.getId());
operationPolicyMappingStatement.setString(2, restoredPolicyMap.get(policy.getPolicyName()));
operationPolicyMappingStatement.setString(3, policy.getDirection());
operationPolicyMappingStatement.setString(4, paramJSON);
operationPolicyMappingStatement.setBinaryStream(4, paramInputStream, paramJSON.length());
operationPolicyMappingStatement.setInt(5, policy.getOrder());
operationPolicyMappingStatement.addBatch();
}
Expand All @@ -23644,7 +23654,7 @@ private void restoreAPIPolicies(APIRevision apiRevision, String tenantDomain, Ma

Gson gson = new Gson();
String paramJSON = gson.toJson(policy.getParameters());

InputStream paramInputStream = new ByteArrayInputStream(paramJSON.getBytes(StandardCharsets.UTF_8));
if (log.isDebugEnabled()) {
log.debug("Restored API level policy " + policy.getPolicyName() + ":"
+ policy.getPolicyVersion() + " from API revision " + apiRevision.getRevisionUUID());
Expand All @@ -23654,7 +23664,7 @@ private void restoreAPIPolicies(APIRevision apiRevision, String tenantDomain, Ma
apiLevelPolicyMappingStatement.setString(2, null);
apiLevelPolicyMappingStatement.setString(3, restoredPolicyMap.get(policy.getPolicyName()));
apiLevelPolicyMappingStatement.setString(4, policy.getDirection());
apiLevelPolicyMappingStatement.setString(5, paramJSON);
apiLevelPolicyMappingStatement.setBinaryStream(5, paramInputStream, paramJSON.length());
apiLevelPolicyMappingStatement.setInt(6, policy.getOrder());
apiLevelPolicyMappingStatement.addBatch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -1357,7 +1359,8 @@ private void attachPolicies(Connection connection, String revisionId, API api) t
String policyVersion = resultSet.getString("POLICY_VERSION");
String operationPolicyDirection = resultSet.getString("OPERATION_POLICY_DIRECTION");
String operationPolicyID = resultSet.getString("OPERATION_POLICY_UUID");
String parameters = resultSet.getString("OPERATION_PARAMS");
String parameters = APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("OPERATION_PARAMS"));

URLMapping urlMapping = null;
if (StringUtils.isNotEmpty(httpMethod) && StringUtils.isNotEmpty(urlPattern)) {
urlMapping = api.getResource(urlPattern, httpMethod);
Expand Down Expand Up @@ -1401,8 +1404,9 @@ private void attachPolicies(Connection connection, String revisionId, API api) t
String apiPolicyUUID = resultSet.getString("API_POLICY_UUID");

// We get parameters of the policies separately. However, this can be retrieved from the AM_API_OPERATION_POLICY_MAPPING as it contains both API and Operation Policies
String operationParameters = resultSet.getString("OPERATION_PARAMS");
String apiParams = resultSet.getString("API_PARAMS");
String operationParameters = APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("OPERATION_PARAMS"));
String apiParams = APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("API_PARAMS"));

URLMapping urlMapping = null;
if (StringUtils.isNotEmpty(httpMethod) && StringUtils.isNotEmpty(urlPattern)) {
urlMapping = api.getResource(urlPattern, httpMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@ CREATE TABLE AM_API_OPERATION_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (URL_MAPPING_ID) REFERENCES AM_API_URL_MAPPING(URL_MAPPING_ID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(OPERATION_POLICY_MAPPING_ID)
Expand All @@ -2311,7 +2311,7 @@ CREATE TABLE AM_API_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(API_POLICY_MAPPING_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,7 @@ CREATE TABLE AM_API_OPERATION_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (URL_MAPPING_ID) REFERENCES AM_API_URL_MAPPING(URL_MAPPING_ID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(OPERATION_POLICY_MAPPING_ID)
Expand All @@ -2311,7 +2311,7 @@ CREATE TABLE AM_API_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(API_POLICY_MAPPING_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,7 @@ CREATE TABLE IF NOT EXISTS AM_API_OPERATION_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (URL_MAPPING_ID) REFERENCES AM_API_URL_MAPPING(URL_MAPPING_ID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(OPERATION_POLICY_MAPPING_ID)
Expand All @@ -2835,7 +2835,7 @@ CREATE TABLE IF NOT EXISTS AM_API_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(API_POLICY_MAPPING_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2700,7 +2700,7 @@ CREATE TABLE AM_API_OPERATION_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (URL_MAPPING_ID) REFERENCES AM_API_URL_MAPPING(URL_MAPPING_ID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(OPERATION_POLICY_MAPPING_ID)
Expand All @@ -2714,7 +2714,7 @@ CREATE TABLE AM_API_POLICY_MAPPING (
POLICY_UUID VARCHAR(45) NOT NULL,
POLICY_ORDER INTEGER NOT NULL,
DIRECTION VARCHAR(10) NOT NULL,
PARAMETERS VARCHAR(2048) NOT NULL,
PARAMETERS BLOB NOT NULL,
FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE,
FOREIGN KEY (POLICY_UUID) REFERENCES AM_OPERATION_POLICY(POLICY_UUID) ON DELETE CASCADE,
PRIMARY KEY(API_POLICY_MAPPING_ID)
Expand Down
Loading
Loading