use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.
the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToInfoDTO.
/**
* Converts a single Advanced Policy model into REST API DTO
*
* @param apiPolicy Advanced Policy model object
* @return Converted Advanced policy REST API DTO object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
* @throws UnsupportedThrottleConditionTypeException - If error occurs
*/
public static AdvancedThrottlePolicyDTO fromAdvancedPolicyToInfoDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
if (apiPolicy.getDefaultQuotaPolicy() != null) {
policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
}
return policyDTO;
}
use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingAdvancedIdGet.
/**
* Get policy by id
* @param Id Uuid of the Advanced policy.
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingAdvancedIdGet(String Id, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.info("Received Advanced Policy Get request. Policy uuid: " + Id);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
APIPolicy policy = apiMgtAdminService.getApiPolicyByUuid(Id);
return Response.status(Response.Status.OK).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(policy)).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while getting Advanced Policy. policy uuid: " + Id;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingAdvancedPost.
/**
* Create Policy
*
* @param body DTO object including the Policy meta information
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingAdvancedPost(AdvancedThrottlePolicyDTO body, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
if (log.isDebugEnabled()) {
log.info("Received Advance Policy POST request " + body + " with tierLevel = " + tierLevel);
}
if (log.isDebugEnabled()) {
log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
String policyId = apiMgtAdminService.addApiPolicy(apiPolicy);
return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(policyId))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding Advanced Throttle Policy, policy name: " + body.getPolicyName();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.
the class APIImportExportTestCase method createApi.
private static API.APIBuilder createApi(String provider, String apiId, String name, String version, String description, Map<String, Endpoint> endpointTypeToIdMap) throws APIManagementException {
Set<String> transport = new HashSet<>();
transport.add("http");
Set<Policy> policies = new HashSet<>();
policies.add(new SubscriptionPolicy("Silver"));
policies.add(new SubscriptionPolicy("Bronze"));
Set<String> tags = new HashSet<>();
tags.add("food");
tags.add("beverage");
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner("John Doe");
businessInformation.setBusinessOwnerEmail("john.doe@annonymous.com");
businessInformation.setTechnicalOwner("Jane Doe");
businessInformation.setBusinessOwnerEmail("jane.doe@annonymous.com");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList("GET", "POST", "DELETE"));
corsConfiguration.setAllowHeaders(Arrays.asList("Authorization", "X-Custom"));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Collections.singletonList("*"));
API.APIBuilder apiBuilder = new API.APIBuilder(provider, name, version).id(apiId).context(UUID.randomUUID().toString()).description(description).lifeCycleStatus("CREATED").apiDefinition(api1Definition).wsdlUri("http://www.webservicex.net/globalweather.asmx?op=GetWeather?wsdl").isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(new APIPolicy("Gold")).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList("customer", "manager", "employee"))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy("Adam Doe").lastUpdatedTime(LocalDateTime.now()).endpoint(endpointTypeToIdMap);
apiBuilder.uriTemplates(Collections.emptyMap());
return apiBuilder;
}
use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.
the class MappingUtil method toAPI.
/**
* This method converts the API model object from the DTO object.
*
* @param apidto APIDTO object with API data
* @return APIBuilder object
*/
public static API.APIBuilder toAPI(APIDTO apidto) throws JsonProcessingException {
BusinessInformation businessInformation = new BusinessInformation();
API_businessInformationDTO apiBusinessInformationDTO = apidto.getBusinessInformation();
if (apiBusinessInformationDTO != null) {
businessInformation.setBusinessOwner(apiBusinessInformationDTO.getBusinessOwner());
businessInformation.setBusinessOwnerEmail(apiBusinessInformationDTO.getBusinessOwnerEmail());
businessInformation.setTechnicalOwner(apiBusinessInformationDTO.getTechnicalOwner());
businessInformation.setTechnicalOwnerEmail(apiBusinessInformationDTO.getTechnicalOwnerEmail());
}
API_corsConfigurationDTO apiCorsConfigurationDTO = apidto.getCorsConfiguration();
CorsConfiguration corsConfiguration = new CorsConfiguration();
if (apiCorsConfigurationDTO != null) {
corsConfiguration.setAllowCredentials(apiCorsConfigurationDTO.getAccessControlAllowCredentials());
corsConfiguration.setAllowHeaders(apiCorsConfigurationDTO.getAccessControlAllowHeaders());
corsConfiguration.setAllowMethods(apiCorsConfigurationDTO.getAccessControlAllowMethods());
corsConfiguration.setAllowOrigins(apiCorsConfigurationDTO.getAccessControlAllowOrigins());
corsConfiguration.setEnabled(apiCorsConfigurationDTO.getCorsConfigurationEnabled());
}
List<API_operationsDTO> operationList = apidto.getOperations();
Map<String, UriTemplate> uriTemplateList = new HashMap<>();
for (API_operationsDTO operationsDTO : operationList) {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
uriTemplateBuilder.uriTemplate(operationsDTO.getUritemplate());
uriTemplateBuilder.authType(operationsDTO.getAuthType());
uriTemplateBuilder.httpVerb(operationsDTO.getHttpVerb());
uriTemplateBuilder.policy(new APIPolicy(operationsDTO.getPolicy()));
uriTemplateBuilder.scopes(operationsDTO.getScopes());
if (operationsDTO.getEndpoint() != null && !operationsDTO.getEndpoint().isEmpty()) {
uriTemplateBuilder.endpoint(fromEndpointListToMap(operationsDTO.getEndpoint()));
}
if (operationsDTO.getId() != null) {
uriTemplateBuilder.templateId(operationsDTO.getId());
} else {
uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(operationsDTO.getUritemplate(), operationsDTO.getHttpVerb()));
}
uriTemplateList.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
}
Set<Policy> subscriptionPolicies = new HashSet<>();
apidto.getPolicies().forEach(v -> subscriptionPolicies.add(new SubscriptionPolicy(v)));
API.APIBuilder apiBuilder = new API.APIBuilder(apidto.getProvider(), apidto.getName(), apidto.getVersion()).id(apidto.getId()).context(apidto.getContext()).description(apidto.getDescription()).lifeCycleStatus(apidto.getLifeCycleStatus()).endpoint(fromEndpointListToMap(apidto.getEndpoint())).visibleRoles(new HashSet<>(apidto.getVisibleRoles())).policies(subscriptionPolicies).apiPermission(apidto.getPermission()).tags(new HashSet<>(apidto.getTags())).labels(new ArrayList<>(apidto.getLabels())).transport(new HashSet<>(apidto.getTransport())).isResponseCachingEnabled(Boolean.valueOf(apidto.getResponseCaching())).businessInformation(businessInformation).uriTemplates(uriTemplateList).corsConfiguration(corsConfiguration).wsdlUri(apidto.getWsdlUri()).scopes(apidto.getScopes()).securityScheme(mapSecuritySchemeListToInt(apidto.getSecurityScheme()));
if (apidto.getIsDefaultVersion() != null) {
apiBuilder.isDefaultVersion(apidto.getIsDefaultVersion());
}
if (apidto.getVisibility() != null) {
apiBuilder.visibility(API.Visibility.valueOf(apidto.getVisibility().toString()));
}
if (apidto.getCacheTimeout() != null) {
apiBuilder.cacheTimeout(apidto.getCacheTimeout());
}
if (apidto.getApiPolicy() != null) {
Policy policy = new APIPolicy(apidto.getApiPolicy());
apiBuilder.apiPolicy(policy);
}
if (apidto.getHasOwnGateway() != null) {
apiBuilder.hasOwnGateway(apidto.getHasOwnGateway());
}
if (apidto.getThreatProtectionPolicies() != null) {
API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = apidto.getThreatProtectionPolicies();
List<API_threatProtectionPolicies_listDTO> threatProtectionPolicies_listDTO = threatProtectionPoliciesDTO.getList();
Set<String> policyIdSet = new HashSet<>();
for (API_threatProtectionPolicies_listDTO listDTO : threatProtectionPolicies_listDTO) {
policyIdSet.add(listDTO.getPolicyId());
}
apiBuilder.threatProtectionPolicies(policyIdSet);
}
return apiBuilder;
}
Aggregations