use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPISpecificOperationPolicyContentByPolicyId.
/**
* Download the operation policy specification and definition for a given API specific policy
*
* @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 getAPISpecificOperationPolicyContentByPolicyId(String apiId, String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
validateAPIExistence(apiId);
OperationPolicyData policyData = apiProvider.getAPISpecificOperationPolicyByPolicyId(operationPolicyId, apiId, 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 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 getting an 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 exporting 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 ApisApiServiceImpl method addAPISpecificOperationPolicy.
/**
* Add an API specific operation policy
*
* @param apiId UUID of the API
* @param policySpecFileInputStream Input stream of the policy specification file
* @param policySpecFileDetail Operation policy specification
* @param synapsePolicyDefinitionFileInputStream Input stream of the synapse policy definition file
* @param synapsePolicyDefinitionFileDetail Synapse definition of the operation policy
* @param ccPolicyDefinitionFileInputStream Input stream of the choreo connect policy definition file
* @param ccPolicyDefinitionFileDetail Choreo connect definition of the operation policy
* @param messageContext message context
* @return Added Operation operation policy DTO as response
*/
@Override
public Response addAPISpecificOperationPolicy(String apiId, InputStream policySpecFileInputStream, Attachment policySpecFileDetail, InputStream synapsePolicyDefinitionFileInputStream, Attachment synapsePolicyDefinitionFileDetail, InputStream ccPolicyDefinitionFileInputStream, Attachment ccPolicyDefinitionFileDetail, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
validateAPIExistence(apiId);
String jsonContent = "";
OperationPolicyDefinition synapseDefinition = null;
OperationPolicyDefinition ccPolicyDefinition = null;
OperationPolicySpecification policySpecification;
if (policySpecFileInputStream != null) {
jsonContent = RestApiPublisherUtils.readInputStream(policySpecFileInputStream, policySpecFileDetail);
String fileName = policySpecFileDetail.getDataHandler().getName();
String fileContentType = URLConnection.guessContentTypeFromName(fileName);
if (org.apache.commons.lang3.StringUtils.isBlank(fileContentType)) {
fileContentType = policySpecFileDetail.getContentType().toString();
}
if (APIConstants.YAML_CONTENT_TYPE.equals(fileContentType)) {
jsonContent = CommonUtil.yamlToJson(jsonContent);
}
policySpecification = APIUtil.getValidatedOperationPolicySpecification(jsonContent);
OperationPolicyData operationPolicyData = new OperationPolicyData();
operationPolicyData.setOrganization(organization);
operationPolicyData.setApiUUID(apiId);
operationPolicyData.setSpecification(policySpecification);
if (synapsePolicyDefinitionFileInputStream != null) {
String synapsePolicyDefinition = RestApiPublisherUtils.readInputStream(synapsePolicyDefinitionFileInputStream, synapsePolicyDefinitionFileDetail);
synapseDefinition = new OperationPolicyDefinition();
synapseDefinition.setContent(synapsePolicyDefinition);
synapseDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.Synapse);
synapseDefinition.setMd5Hash(APIUtil.getMd5OfOperationPolicyDefinition(synapseDefinition));
operationPolicyData.setSynapsePolicyDefinition(synapseDefinition);
}
if (ccPolicyDefinitionFileInputStream != null) {
String choreoConnectPolicyDefinition = RestApiPublisherUtils.readInputStream(ccPolicyDefinitionFileInputStream, ccPolicyDefinitionFileDetail);
ccPolicyDefinition = new OperationPolicyDefinition();
ccPolicyDefinition.setContent(choreoConnectPolicyDefinition);
ccPolicyDefinition.setGatewayType(OperationPolicyDefinition.GatewayType.ChoreoConnect);
ccPolicyDefinition.setMd5Hash(APIUtil.getMd5OfOperationPolicyDefinition(ccPolicyDefinition));
operationPolicyData.setCcPolicyDefinition(ccPolicyDefinition);
}
operationPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(operationPolicyData));
OperationPolicyData existingPolicy = apiProvider.getAPISpecificOperationPolicyByPolicyName(policySpecification.getName(), apiId, null, organization, false);
String policyID;
if (existingPolicy == null) {
policyID = apiProvider.addAPISpecificOperationPolicy(apiId, operationPolicyData, organization);
if (log.isDebugEnabled()) {
log.debug("An API specific operation policy has been added for the API " + apiId + " with id " + policyID);
}
} else {
throw new APIManagementException("An API specific operation policy found for the same name.");
}
operationPolicyData.setPolicyId(policyID);
OperationPolicyDataDTO operationPolicyDataDTO = OperationPolicyMappingUtil.fromOperationPolicyDataToDTO(operationPolicyData);
URI createdPolicyUri = new URI(RestApiConstants.REST_API_PUBLISHER_VERSION + RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + "/" + RestApiConstants.RESOURCE_PATH_OPERATION_POLICIES + "/" + policyID);
return Response.created(createdPolicyUri).entity(operationPolicyDataDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding an API specific operation policy." + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An error has occurred while adding an API specific " + "operation policy", e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAllAPISpecificOperationPolicies.
/**
* Get the list of all API specific operation policies for a given API
*
* @param apiId API UUID
* @param limit max number of records returned
* @param offset starting index
* @param messageContext message context
* @return A list of operation policies available for the API
*/
@Override
public Response getAllAPISpecificOperationPolicies(String apiId, Integer limit, Integer offset, String query, MessageContext messageContext) {
try {
validateAPIExistence(apiId);
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// Lightweight API specific operation policy includes the policy ID and the policy specification.
// Since policy definition is bit bulky, we don't query the definition unnecessarily.
List<OperationPolicyData> sharedOperationPolicyLIst = apiProvider.getAllAPISpecificOperationPolicies(apiId, organization);
OperationPolicyDataListDTO policyListDTO = OperationPolicyMappingUtil.fromOperationPolicyDataListToDTO(sharedOperationPolicyLIst, offset, limit);
return Response.ok().entity(policyListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving the list of all API specific operation policies." + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An error has occurred while getting the list of API specific " + " operation policies", e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class OperationPoliciesApiServiceImpl method deleteCommonOperationPolicyByPolicyId.
/**
* Delete a common operation policy by providing the policy ID
*
* @param operationPolicyId UUID of the operation policy
* @param messageContext message context
* @return ok if deleted successfully
*/
@Override
public Response deleteCommonOperationPolicyByPolicyId(String operationPolicyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
OperationPolicyData existingPolicy = apiProvider.getCommonOperationPolicyByPolicyId(operationPolicyId, organization, false);
if (existingPolicy != null) {
apiProvider.deleteOperationPolicyById(operationPolicyId, organization);
if (log.isDebugEnabled()) {
log.debug("The common operation policy " + operationPolicyId + " has been deleted");
}
return Response.ok().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 deleting the common operation policy with ID: " + operationPolicyId + " " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An Error has occurred while deleting the common operation policy + " + operationPolicyId, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.OperationPolicyData in project carbon-apimgt by wso2.
the class OperationPoliciesApiServiceImpl method getAllCommonOperationPolicies.
/**
* Get the list of all common operation policies for a given organization
*
* @param limit max number of records returned
* @param offset starting index
* @param messageContext message context
* @return A list of operation policies available for the API
*/
@Override
public Response getAllCommonOperationPolicies(Integer limit, Integer offset, String query, MessageContext messageContext) throws APIManagementException {
try {
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// Since policy definition is bit bulky, we don't query the definition unnecessarily.
List<OperationPolicyData> commonOperationPolicyLIst = apiProvider.getAllCommonOperationPolicies(organization);
OperationPolicyDataListDTO policyListDTO = OperationPolicyMappingUtil.fromOperationPolicyDataListToDTO(commonOperationPolicyLIst, offset, limit);
return Response.ok().entity(policyListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving the list of all common operation policies." + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (Exception e) {
RestApiUtil.handleInternalServerError("An error has occurred while getting the list of all common " + " operation policies", e, log);
}
return null;
}
Aggregations