use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class APIProviderImpl method cloneOperationPoliciesToAPI.
private Map<String, String> cloneOperationPoliciesToAPI(String oldAPIUuid, API newAPI, Map<String, List<OperationPolicy>> operationPoliciesMap) throws APIManagementException {
Map<String, String> clonedPolicies = new HashMap<>();
for (Map.Entry<String, List<OperationPolicy>> operationPolicyEntry : operationPoliciesMap.entrySet()) {
List<OperationPolicy> operationPolicyList = operationPolicyEntry.getValue();
for (OperationPolicy operationPolicy : operationPolicyList) {
if (!clonedPolicies.containsKey(operationPolicy.getPolicyId())) {
OperationPolicyData apiSpecificOperationPolicy = apiMgtDAO.getAPISpecificOperationPolicyByPolicyID(operationPolicy.getPolicyId(), oldAPIUuid, newAPI.getOrganization(), true);
String policyUUID = apiMgtDAO.cloneOperationPolicy(newAPI.getUuid(), apiSpecificOperationPolicy);
clonedPolicies.put(operationPolicy.getPolicyId(), policyUUID);
}
}
}
return clonedPolicies;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class APIUtil method loadCommonOperationPolicies.
/**
* Load the common policies for the organization at the first startup. This will only copy the policies to the
* database if the total policies for this organization is zero.
*
* @param organization organization name
*/
public static void loadCommonOperationPolicies(String organization) throws APIManagementException {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
int policyCount = apiMgtDAO.getOperationPolicyCount(organization);
if (policyCount == 0) {
String policySpecLocation = CarbonUtils.getCarbonHome() + File.separator + APIConstants.COMMON_OPERATION_POLICY_SPECIFICATIONS_LOCATION;
String policyDefinitionLocation = CarbonUtils.getCarbonHome() + File.separator + APIConstants.COMMON_OPERATION_POLICY_DEFINITIONS_LOCATION;
File policiesDir = new File(policySpecLocation);
File[] files = policiesDir.listFiles();
if (files != null) {
for (File file : files) {
String jsonContent;
try {
jsonContent = FileUtils.readFileToString(file);
OperationPolicySpecification policySpec = getValidatedOperationPolicySpecification(jsonContent);
if (policySpec != null) {
OperationPolicyData policyData = new OperationPolicyData();
policyData.setSpecification(policySpec);
policyData.setOrganization(organization);
OperationPolicyDefinition synapsePolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policySpec.getName(), APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION);
if (synapsePolicyDefinition != null) {
synapsePolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.Synapse);
policyData.setSynapsePolicyDefinition(synapsePolicyDefinition);
}
OperationPolicyDefinition ccPolicyDefinition = getOperationPolicyDefinitionFromFile(policyDefinitionLocation, policySpec.getName(), APIConstants.CC_POLICY_DEFINITION_EXTENSION);
if (ccPolicyDefinition != null) {
ccPolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.ChoreoConnect);
policyData.setCcPolicyDefinition(ccPolicyDefinition);
}
policyData.setMd5Hash(getMd5OfOperationPolicy(policyData));
apiMgtDAO.addCommonOperationPolicy(policyData);
log.info("Common operation policy " + policySpec.getName() + "_" + policySpec.getVersion() + " was added to the organization " + organization + " successfully");
}
} catch (IOException | APIManagementException e) {
log.error("Invalid policy specification for file " + file.getName() + ".Hence skipped from importing as a common operation policy.", e);
}
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method deleteAPISpecificOperationPolicyByPolicyId.
/**
* Delete API specific operation policy by providing the policy ID
*
* @param apiId API UUID
* @param operationPolicyId UUID of the operation policy
* @param messageContext message context
* @return A zip file containing both (if exists) operation policy specification and policy definition
*/
@Override
public Response deleteAPISpecificOperationPolicyByPolicyId(String apiId, String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// validate if api exists
validateAPIExistence(apiId);
String organization = RestApiUtil.getValidatedOrganization(messageContext);
OperationPolicyData existingPolicy = apiProvider.getAPISpecificOperationPolicyByPolicyId(operationPolicyId, apiId, organization, false);
if (existingPolicy != null) {
apiProvider.deleteOperationPolicyById(operationPolicyId, organization);
if (log.isDebugEnabled()) {
log.debug("The operation policy " + operationPolicyId + " has been deleted from the the API " + apiId);
}
return Response.ok().build();
} else {
throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing operation policy with ID: " + operationPolicyId + " for API " + apiId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
} else {
String errorMessage = "Error while deleting the API specific operation policy with ID :" + operationPolicyId + " for API " + apiId + " " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An error has occurred while deleting the API specific " + " operation policy with ID" + operationPolicyId + " for API " + apiId, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class OperationPoliciesApiServiceImpl method getCommonOperationPolicyContentByPolicyId.
/**
* Download the operation policy specification and definition for a given common operation policy
*
* @param operationPolicyId UUID of the operation policy
* @param messageContext message context
* @return A zip file containing both (if exists) operation policy specification and policy definition
*/
@Override
public Response getCommonOperationPolicyContentByPolicyId(String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
OperationPolicyData policyData = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, true);
if (policyData != null) {
File file = RestApiPublisherUtils.exportOperationPolicyData(policyData);
return Response.ok(file).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"").build();
} else {
throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing common policy with ID: " + operationPolicyId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
} else {
String errorMessage = "Error while getting the content of common operation policy with ID :" + operationPolicyId + " " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An error has occurred while getting the content of the common operation " + " policy with ID " + operationPolicyId, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class OperationPoliciesApiServiceImpl method getCommonOperationPolicyByPolicyId.
/**
* Get the common operation policy by providing the policy ID
*
* @param operationPolicyId UUID of the operation policy
* @param messageContext message context
* @return Operation policy DTO as response
*/
@Override
public Response getCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, false);
if (existingPolicy != null) {
OperationPolicyDataDTO policyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(existingPolicy);
return Response.ok().entity(policyDataDTO).build();
} else {
throw new APIMgtResourceNotFoundException("Couldn't retrieve an existing common policy with ID: " + operationPolicyId, ExceptionCodes.from(ExceptionCodes.OPERATION_POLICY_NOT_FOUND, operationPolicyId));
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES, operationPolicyId, e, log);
} else {
String errorMessage = "Error while getting the common operation policy with ID :" + operationPolicyId + " " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An error has occurred while getting the common operation " + " policy with ID: " + operationPolicyId, e, log);
}
return null;
}
Aggregations