use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.
the class ApiMgtDAO method addAPISpecificOperationPolicy.
/**
* Add a new API specific operation policy to the database
*
* @param apiUUID Unique Identifier of API
* @param revisionUUID Unique Identifier of API revision
* @param policyData Unique Identifier of API
* @return UUID of the newly created shared policy
* @throws APIManagementException
*/
public String addAPISpecificOperationPolicy(String apiUUID, String revisionUUID, OperationPolicyData policyData) throws APIManagementException {
OperationPolicySpecification policySpecification = policyData.getSpecification();
try (Connection connection = APIMgtDBUtil.getConnection()) {
try {
connection.setAutoCommit(false);
String policyID = addAPISpecificOperationPolicy(connection, apiUUID, revisionUUID, policyData, null);
connection.commit();
return policyID;
} catch (SQLException e) {
connection.rollback();
handleException("Failed to add API specific operation policy " + policySpecification.getName() + " for API " + apiUUID, e);
}
} catch (SQLException e) {
handleException("Failed to add API specific operation policy " + policySpecification.getName() + " for API " + apiUUID, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.
the class ApiMgtDAO method updateOperationPolicy.
/**
* Update an existing operation policy
*
* @param connection DB connection
* @param policyId Shared policy UUID
* @param policyData Updated policy definition
* @throws SQLException
*/
private void updateOperationPolicy(Connection connection, String policyId, OperationPolicyData policyData) throws SQLException {
OperationPolicySpecification policySpecification = policyData.getSpecification();
PreparedStatement statement = connection.prepareStatement(SQLConstants.OperationPolicyConstants.UPDATE_OPERATION_POLICY_CONTENT);
statement.setString(1, policySpecification.getName());
statement.setString(2, policySpecification.getDisplayName());
statement.setString(3, policySpecification.getDescription());
statement.setString(4, policySpecification.getApplicableFlows().toString());
statement.setString(5, policySpecification.getSupportedGateways().toString());
statement.setString(6, policySpecification.getSupportedApiTypes().toString());
statement.setBinaryStream(7, new ByteArrayInputStream(APIUtil.getPolicyAttributesAsString(policySpecification).getBytes()));
statement.setString(8, policyData.getOrganization());
statement.setString(9, policySpecification.getCategory().toString());
statement.setBoolean(10, policySpecification.isMultipleAllowed());
statement.setString(11, policyData.getMd5Hash());
statement.setString(12, policyId);
statement.executeUpdate();
statement.close();
if (policyData.getSynapsePolicyDefinition() != null) {
updateOperationPolicyDefinition(connection, policyId, policyData.getSynapsePolicyDefinition());
}
if (policyData.getCcPolicyDefinition() != null) {
updateOperationPolicyDefinition(connection, policyId, policyData.getCcPolicyDefinition());
}
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.
the class ApiMgtDAO method populatePolicySpecificationFromRS.
/**
* This method will read the result set and populate OperationPolicySpecification object.
*
* @param rs Result set
* @return OperationPolicySpecification object
* @throws APIManagementException
* @throws SQLException
*/
private OperationPolicySpecification populatePolicySpecificationFromRS(ResultSet rs) throws SQLException {
OperationPolicySpecification policySpecification = new OperationPolicySpecification();
policySpecification.setName(rs.getString("POLICY_NAME"));
policySpecification.setDisplayName(rs.getString("DISPLAY_NAME"));
policySpecification.setDescription(rs.getString("POLICY_DESCRIPTION"));
policySpecification.setApplicableFlows(getListFromString(rs.getString("APPLICABLE_FLOWS")));
policySpecification.setSupportedApiTypes(getListFromString(rs.getString("API_TYPES")));
policySpecification.setSupportedGateways(getListFromString(rs.getString("GATEWAY_TYPES")));
policySpecification.setCategory(OperationPolicySpecification.PolicyCategory.valueOf(rs.getString("POLICY_CATEGORY")));
policySpecification.setMultipleAllowed(rs.getBoolean("MULTIPLE_ALLOWED"));
List<OperationPolicySpecAttribute> policySpecAttributes = null;
try (InputStream policyParametersStream = rs.getBinaryStream("POLICY_PARAMETERS")) {
String policyParametersString = IOUtils.toString(policyParametersStream);
policySpecAttributes = new Gson().fromJson(policyParametersString, new TypeToken<List<OperationPolicySpecAttribute>>() {
}.getType());
} catch (IOException e) {
log.error("Error while converting policy specification attributes for the policy " + policySpecification.getName(), e);
}
policySpecification.setPolicyAttributes(policySpecAttributes);
return policySpecification;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.
the class SynapsePolicyAggregator method populatePolicyCaseList.
public static List<Object> populatePolicyCaseList(URITemplate template, String pathToAchieve, String flow, List<Object> caseList) throws APIManagementException {
Map<String, Object> caseMap = new HashMap<>();
String uriTemplateString = template.getUriTemplate();
String method = template.getHTTPVerb();
String key = method + "_" + uriTemplateString.replaceAll("[\\W]", "\\\\$0");
// This will replace & with & for query params
key = StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(key));
List<String> caseBody = new ArrayList<>();
String policyDirectory = pathToAchieve + File.separator + ImportExportConstants.POLICIES_DIRECTORY;
List<OperationPolicy> operationPolicies = template.getOperationPolicies();
Collections.sort(operationPolicies, new OperationPolicyComparator());
for (OperationPolicy policy : operationPolicies) {
if (flow.equals(policy.getDirection())) {
Map<String, Object> policyParameters = policy.getParameters();
OperationPolicySpecification policySpecification = ImportUtils.getOperationPolicySpecificationFromFile(policyDirectory, policy.getPolicyName());
if (policySpecification.getSupportedGateways().contains(APIConstants.OPERATION_POLICY_SUPPORTED_GATEWAY_SYNAPSE)) {
OperationPolicyDefinition policyDefinition = APIUtil.getOperationPolicyDefinitionFromFile(policyDirectory, policy.getPolicyName(), APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION);
if (policyDefinition != null) {
String renderedTemplate = renderPolicyTemplate(policyDefinition.getContent(), policyParameters);
if (renderedTemplate != null && !renderedTemplate.isEmpty()) {
caseBody.add(renderedTemplate);
}
} else {
log.error("Policy definition for " + policy.getPolicyName() + " is not found in the artifact");
}
} else {
log.error("Policy " + policy.getPolicyName() + " does not support Synapse gateway. " + "Hence skipped");
}
}
}
if (caseBody.size() != 0) {
caseMap.put("case_regex", key);
caseMap.put("policy_sequence", caseBody);
caseList.add(caseMap);
}
return caseList;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicySpecification in project carbon-apimgt by wso2.
the class APIProviderImpl method importOperationPolicy.
/**
* This method will be used to import Operation policy. This will check existing API specific policy first and
* then common policy.
* If API specific policy exists and MD5 hash matches, it will not import and will return the existing API specific policy.
* If the existing API specific policy is different in md5, it will be updated the existing policy
* If a common policy exists and MD5 hash match, it will return the common policy's id. This policy will be imported at the API update.
* If the common policy is different then the imported policy, a new API specific policy will be created.
* If there aren't any existing policies, a new API specific policy will be created.
*
* @param importedPolicyData Imported policy
* @param organization Organization name
* @return corrosponding policy ID for imported data
* @throws APIManagementException if failed to delete APIRevision
*/
@Override
public String importOperationPolicy(OperationPolicyData importedPolicyData, String organization) throws APIManagementException {
OperationPolicySpecification importedSpec = importedPolicyData.getSpecification();
OperationPolicyData existingOperationPolicy = getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedPolicyData.getApiUUID(), null, organization, false);
String policyId = null;
if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching API specific policy found for imported policy and MD5 hashes match.");
}
} else {
if (log.isDebugEnabled()) {
log.debug("Even though existing API specific policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ".Therefore updating the existing policy");
}
updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization);
}
policyId = existingOperationPolicy.getPolicyId();
} else {
existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(), organization, false);
if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching common policy found for imported policy and Md5 hashes match.");
}
policyId = existingOperationPolicy.getPolicyId();
} else {
importedSpec.setName(importedSpec.getName() + "_imported");
importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
importedPolicyData.setSpecification(importedSpec);
importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData));
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("Even though existing common policy name match with imported policy, " + "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId() + ". A new policy created with ID " + policyId);
}
}
} else {
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("There aren't any existing policies for the imported policy. A new policy created with ID " + policyId);
}
}
}
return policyId;
}
Aggregations