Java Code Examples for org.wso2.carbon.apimgt.api.APIProvider#getSubscriptionPolicy()

The following examples show how to use org.wso2.carbon.apimgt.api.APIProvider#getSubscriptionPolicy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Updates a given Subscription level policy specified by uuid
 *
 * @param policyId          u
 * @param body              DTO of policy to be updated
 * @param contentType       Content-Type header
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdPut(String policyId, SubscriptionThrottlePolicyDTO body,
                                                          String contentType, String ifMatch,
                                                          String ifUnmodifiedSince, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String username = RestApiUtil.getLoggedInUsername();

        //will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy existingPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }

        //overridden properties
        body.setPolicyId(policyId);
        body.setPolicyName(existingPolicy.getPolicyName());

        //update the policy
        SubscriptionPolicy subscriptionPolicy =
                SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        apiProvider.updatePolicy(subscriptionPolicy);

        //retrieve the new policy and send back as the response
        SubscriptionPolicy newSubscriptionPolicy = apiProvider.getSubscriptionPolicy(username,
                body.getPolicyName());
        SubscriptionThrottlePolicyDTO policyDTO =
                SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(newSubscriptionPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e,
                    log);
        } else {
            String errorMessage = "Error while updating Subscription level policy: " + body.getPolicyName();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
 
Example 2
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Updates a given Subscription level policy specified by uuid
 *
 * @param policyId        u
 * @param body              DTO of policy to be updated
 * @param contentType       Content-Type header
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdPut(String policyId,
        SubscriptionThrottlePolicyDTO body, String contentType, String ifMatch, String ifUnmodifiedSince) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String username = RestApiUtil.getLoggedInUsername();

        //will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy existingPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }

        //overridden properties
        body.setPolicyId(policyId);
        body.setPolicyName(existingPolicy.getPolicyName());

        //update the policy
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil
                .fromSubscriptionThrottlePolicyDTOToModel(
                        body);
        apiProvider.updatePolicy(subscriptionPolicy);

        //retrieve the new policy and send back as the response
        SubscriptionPolicy newSubscriptionPolicy = apiProvider
                .getSubscriptionPolicy(username, body.getPolicyName());
        SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil
                .fromSubscriptionThrottlePolicyToDTO(newSubscriptionPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil
                    .handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while updating Subscription level policy: " + body.getPolicyName();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}