use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsPost.
/**
* Creates a new 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 subscriptionsPost(SubscriptionDTO body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String userOrganization = RestApiUtil.getValidatedSubjectOrganization(messageContext);
apiConsumer = RestApiCommonUtil.getConsumer(username, userOrganization);
String applicationId = body.getApplicationId();
// 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 application creation workflow status is pending or rejected, throw a Bad request exception
if (application.getStatus().equals(WorkflowStatus.REJECTED.toString()) || application.getStatus().equals(WorkflowStatus.CREATED.toString())) {
RestApiUtil.handleBadRequest("Workflow status is not Approved", 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.addSubscription(apiTypeWrapper, username, application);
SubscribedAPI addedSubscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionResponse.getSubscriptionUUID());
SubscriptionDTO addedSubscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(addedSubscribedAPI, apiTypeWrapper, organization);
WorkflowResponse workflowResponse = subscriptionResponse.getWorkflowResponse();
if (workflowResponse instanceof HttpWorkflowResponse) {
String payload = workflowResponse.getJSONPayload();
addedSubscriptionDTO.setRedirectionParams(payload);
}
return Response.created(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 (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.rest.api.store.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsMultiplePost.
/**
* Create multiple new subscriptions with the list of subscription details specified in the body parameter.
*
* @param body list of new subscription details
* @return list of newly added subscription as a SubscriptionDTO if successful
*/
@Override
public Response subscriptionsMultiplePost(List<SubscriptionDTO> body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
List<SubscriptionDTO> subscriptions = new ArrayList<>();
for (SubscriptionDTO subscriptionDTO : body) {
try {
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
String applicationId = subscriptionDTO.getApplicationId();
APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(subscriptionDTO.getApiId(), organization);
// this will throw a APIMgtResourceNotFoundException
if (!org.wso2.carbon.apimgt.rest.api.util.utils.RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(subscriptionDTO.getApiId(), organization)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, subscriptionDTO.getApiId(), log);
}
Application application = apiConsumer.getApplicationByUUID(applicationId);
if (application == null) {
// required application not found
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
// application access failure occurred
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(subscriptionDTO.getApiId(), organization);
apiTypeWrapper.setTier(subscriptionDTO.getThrottlingPolicy());
SubscriptionResponse subscriptionResponse = apiConsumer.addSubscription(apiTypeWrapper, username, application);
SubscribedAPI addedSubscribedAPI = apiConsumer.getSubscriptionByUUID(subscriptionResponse.getSubscriptionUUID());
SubscriptionDTO addedSubscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(addedSubscribedAPI, organization);
subscriptions.add(addedSubscriptionDTO);
} 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 " + subscriptionDTO.getApiId() + " for application " + subscriptionDTO.getApplicationId(), e, log);
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
// this happens when the specified API identifier does not exist
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, subscriptionDTO.getApiId(), e, log);
} else {
// unhandled exception
RestApiUtil.handleInternalServerError("Error while adding the subscription API:" + subscriptionDTO.getApiId() + ", application:" + subscriptionDTO.getApplicationId() + ", throttling policy:" + subscriptionDTO.getThrottlingPolicy(), e, log);
}
}
}
return Response.ok().entity(subscriptions).build();
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdGet.
/**
* Gets a subscription by identifier
*
* @param subscriptionId subscription identifier
* @param ifNoneMatch If-None-Match header value
* @return matched subscription as a SubscriptionDTO
*/
@Override
public Response subscriptionsSubscriptionIdGet(String subscriptionId, String ifNoneMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
apiConsumer = RestApiCommonUtil.getConsumer(username);
SubscribedAPI subscribedAPI = validateAndGetSubscription(subscriptionId, apiConsumer);
SubscriptionDTO subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscribedAPI, organization);
return Response.ok().entity(subscriptionDTO).build();
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error while getting subscription with id " + subscriptionId, e, log);
}
return null;
}
Aggregations