use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdPut.
/**
* Update already created subscriptions with the details specified in the body parameter
*
* @param body new subscription details
* @return newly added subscription as a SubscriptionDTO if successful
*/
@Override
public Response subscriptionsSubscriptionIdPut(String subscriptionId, SubscriptionDTO body, String xWSO2Tenant, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
apiConsumer = RestApiCommonUtil.getConsumer(username);
String applicationId = body.getApplicationId();
String currentThrottlingPolicy = body.getThrottlingPolicy();
String requestedThrottlingPolicy = body.getRequestedThrottlingPolicy();
SubscribedAPI subscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionId);
// Check whether the subscription status is not empty and also not blocked
if (body.getStatus() != null && subscribedAPI != null) {
if ("BLOCKED".equals(body.getStatus().value()) || "ON_HOLD".equals(body.getStatus().value()) || "REJECTED".equals(body.getStatus().value()) || "BLOCKED".equals(subscribedAPI.getSubStatus()) || "ON_HOLD".equals(subscribedAPI.getSubStatus()) || "REJECTED".equals(subscribedAPI.getSubStatus())) {
RestApiUtil.handleBadRequest("Cannot update subscriptions with provided or existing status", log);
return null;
}
} else {
RestApiUtil.handleBadRequest("Request must contain status of the subscription", log);
return null;
}
// this will throw a APIMgtResourceNotFoundException
if (body.getApiId() != null) {
if (!RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(body.getApiId(), organization)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, body.getApiId(), log);
}
} else {
RestApiUtil.handleBadRequest("Request must contain either apiIdentifier or apiProductIdentifier and the relevant type", log);
return null;
}
Application application = apiConsumer.getApplicationByUUID(applicationId);
if (application == null) {
// required application not found
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
return null;
}
if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
// application access failure occurred
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(body.getApiId(), organization);
apiTypeWrapper.setTier(body.getThrottlingPolicy());
SubscriptionResponse subscriptionResponse = apiConsumer.updateSubscription(apiTypeWrapper, username, application, subscriptionId, currentThrottlingPolicy, requestedThrottlingPolicy);
SubscribedAPI addedSubscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionResponse.getSubscriptionUUID());
SubscriptionDTO addedSubscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(addedSubscribedAPI, organization);
WorkflowResponse workflowResponse = subscriptionResponse.getWorkflowResponse();
if (workflowResponse instanceof HttpWorkflowResponse) {
String payload = workflowResponse.getJSONPayload();
addedSubscriptionDTO.setRedirectionParams(payload);
}
return Response.ok(new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTIONS + "/" + addedSubscribedAPI.getUUID())).entity(addedSubscriptionDTO).build();
} catch (APIMgtAuthorizationFailedException e) {
// this occurs when the api:application:tier mapping is not allowed. The reason for the message is taken from
// the message of the exception e
RestApiUtil.handleAuthorizationFailure(e.getMessage(), e, log);
} catch (SubscriptionAlreadyExistingException e) {
RestApiUtil.handleResourceAlreadyExistsError("Specified subscription already exists for API " + body.getApiId() + ", for application " + body.getApplicationId(), e, log);
} catch (APIManagementException | URISyntaxException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
// this happens when the specified API identifier does not exist
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, body.getApiId(), e, log);
} else {
// unhandled exception
RestApiUtil.handleInternalServerError("Error while adding the subscription API:" + body.getApiId() + ", application:" + body.getApplicationId() + ", tier:" + body.getThrottlingPolicy(), e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class WebhooksApiServiceImpl method webhooksSubscriptionsGet.
public Response webhooksSubscriptionsGet(String applicationId, String apiId, String xWSO2Tenant, MessageContext messageContext) {
if (StringUtils.isNotEmpty(applicationId)) {
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
WebhookSubscriptionListDTO WebhookSubscriptionListDTO;
Set<Subscription> subscriptionSet = apiConsumer.getTopicSubscriptions(applicationId, apiId);
WebhookSubscriptionListDTO = AsyncAPIMappingUtil.fromSubscriptionListToDTO(subscriptionSet);
return Response.ok().entity(WebhookSubscriptionListDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Failed to get topic subscriptions of Async API " + apiId, e, log);
}
}
} else {
RestApiUtil.handleBadRequest("Application Id cannot be empty", log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method fromSubscriptionToDTO.
/**
* Converts a SubscribedAPI object into SubscriptionDTO
*
* @param subscription SubscribedAPI object
* @param organization Identifier of the organization
* @return SubscriptionDTO corresponds to SubscribedAPI object
*/
public static SubscriptionDTO fromSubscriptionToDTO(SubscribedAPI subscription, String organization) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
subscriptionDTO.setSubscriptionId(subscription.getUUID());
APIInfoDTO apiInfo;
Identifier apiId = subscription.getIdentifier();
ApiTypeWrapper apiTypeWrapper;
try {
apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(subscription.getIdentifier().getUUID(), organization);
subscriptionDTO.setApiId(subscription.getIdentifier().getUUID());
Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
Map<String, Tier> tierMap = APIUtil.getTiers(organization);
if (apiTypeWrapper.isAPIProduct()) {
apiInfo = APIMappingUtil.fromAPIToInfoDTO(apiTypeWrapper.getApiProduct(), organization);
APIMappingUtil.setThrottlePoliciesAndMonetization(apiTypeWrapper.getApiProduct(), apiInfo, deniedTiers, tierMap);
} else {
apiInfo = APIMappingUtil.fromAPIToInfoDTO(apiTypeWrapper.getApi());
APIMappingUtil.setThrottlePoliciesAndMonetization(apiTypeWrapper.getApi(), apiInfo, deniedTiers, tierMap);
}
subscriptionDTO.setApiInfo(apiInfo);
} catch (APIManagementException e) {
if (log.isDebugEnabled()) {
log.debug("User :" + username + " does not have access to the API " + apiId);
}
apiInfo = new APIInfoDTO();
apiInfo.setName(apiId.getName());
apiInfo.setVersion(apiId.getVersion());
subscriptionDTO.setApiInfo(apiInfo);
}
Application application = subscription.getApplication();
application = apiConsumer.getLightweightApplicationByUUID(application.getUUID());
subscriptionDTO.setApplicationId(subscription.getApplication().getUUID());
subscriptionDTO.setStatus(SubscriptionDTO.StatusEnum.valueOf(subscription.getSubStatus()));
subscriptionDTO.setThrottlingPolicy(subscription.getTier().getName());
subscriptionDTO.setRequestedThrottlingPolicy(subscription.getRequestedTier().getName());
ApplicationInfoDTO applicationInfoDTO = ApplicationMappingUtil.fromApplicationToInfoDTO(application);
subscriptionDTO.setApplicationInfo(applicationInfoDTO);
return subscriptionDTO;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class SubscriptionMappingUtil method fromSubscriptionToDTO.
public static SubscriptionDTO fromSubscriptionToDTO(SubscribedAPI subscription, ApiTypeWrapper apiTypeWrapper, String organization) throws APIManagementException {
SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
subscriptionDTO.setSubscriptionId(subscription.getUUID());
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
Map<String, Tier> tierMap = APIUtil.getTiers(organization);
if (apiTypeWrapper != null && !apiTypeWrapper.isAPIProduct()) {
API api = apiTypeWrapper.getApi();
subscriptionDTO.setApiId(api.getUUID());
APIInfoDTO apiInfo = APIMappingUtil.fromAPIToInfoDTO(api);
APIMappingUtil.setThrottlePoliciesAndMonetization(api, apiInfo, deniedTiers, tierMap);
subscriptionDTO.setApiInfo(apiInfo);
} else {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
subscriptionDTO.setApiId(apiProduct.getUuid());
APIInfoDTO apiInfo = APIMappingUtil.fromAPIToInfoDTO(apiProduct, organization);
APIMappingUtil.setThrottlePoliciesAndMonetization(apiProduct, apiInfo, deniedTiers, tierMap);
subscriptionDTO.setApiInfo(apiInfo);
}
Application application = subscription.getApplication();
subscriptionDTO.setApplicationId(subscription.getApplication().getUUID());
subscriptionDTO.setStatus(SubscriptionDTO.StatusEnum.valueOf(subscription.getSubStatus()));
subscriptionDTO.setThrottlingPolicy(subscription.getTier().getName());
subscriptionDTO.setRequestedThrottlingPolicy(subscription.getRequestedTier().getName());
ApplicationInfoDTO applicationInfoDTO = ApplicationMappingUtil.fromApplicationToInfoDTO(application);
subscriptionDTO.setApplicationInfo(applicationInfoDTO);
return subscriptionDTO;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class SettingsApiServiceImpl method settingsApplicationAttributesGet.
@Override
public Response settingsApplicationAttributesGet(String ifNoneMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
List<ApplicationAttributeDTO> applicationAttributeDTOList = new ArrayList<>();
JSONArray attributeArray = apiConsumer.getAppAttributesFromConfig(username);
for (int i = 0; i < attributeArray.size(); i++) {
JSONObject obj = (JSONObject) attributeArray.get(i);
ApplicationAttributeDTO applicationAttributeDTO = ApplicationMappingUtil.fromApplicationAttributeJsonToDTO(obj);
applicationAttributeDTOList.add(applicationAttributeDTO);
}
ApplicationAttributeListDTO applicationAttributeListDTO = ApplicationMappingUtil.fromApplicationAttributeListToDTO(applicationAttributeDTOList);
return Response.ok().entity(applicationAttributeListDTO).build();
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error occurred in reading application attributes from config", e, log);
}
return null;
}
Aggregations