Search in sources :

Example 36 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultAPI.

public static API.APIBuilder createDefaultAPI() {
    Set<String> transport = new HashSet<>();
    transport.add(HTTP);
    transport.add(HTTPS);
    Set<String> tags = new HashSet<>();
    tags.add(TAG_CLIMATE);
    Set<Policy> policies = new HashSet<>();
    policies.add(goldSubscriptionPolicy);
    policies.add(silverSubscriptionPolicy);
    policies.add(bronzeSubscriptionPolicy);
    BusinessInformation businessInformation = new BusinessInformation();
    businessInformation.setBusinessOwner(NAME_BUSINESS_OWNER_1);
    businessInformation.setBusinessOwnerEmail(EMAIL_BUSINESS_OWNER_1);
    businessInformation.setTechnicalOwner(NAME_TECHNICAL_OWNER_1);
    businessInformation.setTechnicalOwnerEmail(EMAIL_TECHNICAL_OWNER_1);
    String permissionJson = "[{\"groupId\" : \"developer\", \"permission\" : " + "[\"READ\",\"UPDATE\"]},{\"groupId\" : \"admin\", \"permission\" : [\"READ\",\"UPDATE\"," + "\"DELETE\", \"MANAGE_SUBSCRIPTION\"]}]";
    Set<String> visibleRoles = new HashSet<>();
    visibleRoles.add("testRple");
    List<String> labels = new ArrayList<>();
    labels.add("testLabel");
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setEnabled(true);
    corsConfiguration.setAllowMethods(Arrays.asList(APIMgtConstants.FunctionsConstants.GET, APIMgtConstants.FunctionsConstants.POST, APIMgtConstants.FunctionsConstants.DELETE));
    corsConfiguration.setAllowHeaders(Arrays.asList(ALLOWED_HEADER_AUTHORIZATION, ALLOWED_HEADER_CUSTOM));
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowOrigins(Arrays.asList("*"));
    Map<String, Endpoint> endpointMap = new HashMap<>();
    endpointMap.put("TestEndpoint", createMockEndpoint());
    API.APIBuilder apiBuilder = new API.APIBuilder(ADMIN, "WeatherAPI", API_VERSION).id(UUID.randomUUID().toString()).context("weather").description("Get Weather Info").lifeCycleStatus(APIStatus.CREATED.getStatus()).lifecycleInstanceId(UUID.randomUUID().toString()).endpoint(Collections.emptyMap()).wsdlUri("http://localhost:9443/echo?wsdl").isResponseCachingEnabled(false).cacheTimeout(60).isDefaultVersion(false).apiPolicy(unlimitedApiPolicy).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.PUBLIC).visibleRoles(visibleRoles).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy(ADMIN).updatedBy(ADMIN).lastUpdatedTime(LocalDateTime.now()).apiPermission(permissionJson).uriTemplates(getMockUriTemplates()).apiDefinition(apiDefinition).workflowStatus(WORKFLOW_STATUS).labels(labels).endpoint(endpointMap);
    Map map = new HashMap();
    map.put(DEVELOPER_ROLE_ID, 6);
    map.put(ADMIN_ROLE_ID, 15);
    apiBuilder.permissionMap(map);
    return apiBuilder;
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 37 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class ThreatProtectionPoliciesApiServiceImplTestCase method createThreatProtectionPolicyDTO.

private ThreatProtectionPolicyDTO createThreatProtectionPolicyDTO() {
    ThreatProtectionPolicyDTO policyDTO = new ThreatProtectionPolicyDTO();
    policyDTO.setName("TEST-POLICY");
    policyDTO.setType("XML");
    policyDTO.setUuid(UUID.randomUUID().toString());
    return policyDTO;
}
Also used : ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)

Example 38 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingApplicationIdPut.

/**
 * Updates/adds a new Application throttle policy to the system
 *
 * @param id          Uuid of the policy.
 * @param body              DTO object including the Policy meta information
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Response object  response object with the updated application throttle policy resource
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingApplicationIdPut(String id, ApplicationThrottlePolicyDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.application;
    if (log.isDebugEnabled()) {
        log.info("Received Application Policy PUT request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        ApplicationPolicy applicationPolicy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(body);
        applicationPolicy.setUuid(id);
        apiMgtAdminService.updateApplicationPolicy(applicationPolicy);
        return Response.status(Response.Status.OK).entity(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(apiMgtAdminService.getApplicationPolicyByUuid(id))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while updating Application Policy. policy uuid: " + id;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 39 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingCustomGet.

/**
 * Retrieves all custom policies.
 *
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return All matched Global Throttle policies to the given request
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingCustomGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Custom Policy GET request.");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<CustomPolicy> policies = apiMgtAdminService.getCustomRules();
        CustomRuleListDTO customRuleListDTO = CustomPolicyMappingUtil.fromCustomPolicyArrayToListDTO(policies);
        return Response.ok().entity(customRuleListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving custom policies";
        org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 40 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAPIPolicyArrayToListDTO.

/**
 * Converts an array of Advanced Policy objects into a List DTO
 *
 * @param policies Array of Advanced Policies
 * @return A List DTO of converted Advanced Policies
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static AdvancedThrottlePolicyListDTO fromAPIPolicyArrayToListDTO(List<APIPolicy> policies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyListDTO listDTO = new AdvancedThrottlePolicyListDTO();
    List<AdvancedThrottlePolicyDTO> advancedPolicyDTOs = new ArrayList<>();
    if (policies != null) {
        for (APIPolicy policy : policies) {
            advancedPolicyDTOs.add(fromAdvancedPolicyToDTO(policy));
        }
    }
    listDTO.setList(advancedPolicyDTOs);
    listDTO.setCount(advancedPolicyDTOs.size());
    return listDTO;
}
Also used : AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)114 Test (org.testng.annotations.Test)106 ArrayList (java.util.ArrayList)96 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)79 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)73 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)73 Test (org.junit.Test)72 PreparedStatement (java.sql.PreparedStatement)68 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)64 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)63 SQLException (java.sql.SQLException)62 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)62 Connection (java.sql.Connection)58 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)58 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)54 ResultSet (java.sql.ResultSet)49 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)43 HashMap (java.util.HashMap)40 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)40 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)40