use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method unBlockSubscription.
/**
* Unblocks a subscription
*
* @param subscriptionId subscription identifier
* @param ifMatch If-Match header value
* @return 200 response and the updated subscription if subscription block is successful
*/
public Response unBlockSubscription(String subscriptionId, String ifMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
// validates the subscriptionId if it exists
SubscribedAPI currentSubscription = apiProvider.getSubscriptionByUUID(subscriptionId);
if (currentSubscription == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION, subscriptionId, log);
}
SubscribedAPI subscribedAPI = new SubscribedAPI(subscriptionId);
subscribedAPI.setSubStatus(APIConstants.SubscriptionStatus.UNBLOCKED);
apiProvider.updateSubscription(subscribedAPI);
SubscribedAPI updatedSubscribedAPI = apiProvider.getSubscriptionByUUID(subscriptionId);
SubscriptionDTO subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(updatedSubscribedAPI);
return Response.ok().entity(subscriptionDTO).build();
} catch (APIManagementException e) {
String msg = "Error while unblocking the subscription " + subscriptionId;
RestApiUtil.handleInternalServerError(msg, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method getSubscriptions.
/**
* Retrieves all subscriptions or retrieves subscriptions for a given API Id
*
* @param apiId API identifier
* @param limit max number of objects returns
* @param offset starting index
* @param ifNoneMatch If-None-Match header value
* @return Response object containing resulted subscriptions
*/
public Response getSubscriptions(String apiId, Integer limit, Integer offset, String ifNoneMatch, String query, MessageContext messageContext) {
// pre-processing
// setting default limit and offset if they are null
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIProvider apiProvider = RestApiCommonUtil.getProvider(username);
String organization = RestApiUtil.getValidatedOrganization(messageContext);
SubscriptionListDTO subscriptionListDTO;
List<SubscribedAPI> apiUsages;
if (apiId != null) {
String apiUuid = apiId;
APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId);
if (apiRevision != null && apiRevision.getApiUUID() != null) {
apiUuid = apiRevision.getApiUUID();
}
apiUsages = apiProvider.getAPIUsageByAPIId(apiUuid, organization);
} else {
UserApplicationAPIUsage[] allApiUsage = apiProvider.getAllAPIUsageByProvider(username);
apiUsages = SubscriptionMappingUtil.fromUserApplicationAPIUsageArrayToSubscribedAPIList(allApiUsage);
}
if (query != null && !query.isEmpty()) {
SubscriptionListDTO filteredSubscriptionList = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, query);
subscriptionListDTO = SubscriptionMappingUtil.getPaginatedSubscriptions(filteredSubscriptionList, limit, offset);
SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, filteredSubscriptionList.getCount());
} else {
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(apiUsages, limit, offset);
SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, apiUsages.size());
}
return Response.ok().entity(subscriptionListDTO).build();
} catch (APIManagementException e) {
// existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String msg = "Error while retrieving subscriptions of API " + apiId;
RestApiUtil.handleInternalServerError(msg, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SolaceApplicationNotifier method renameSolaceApplication.
/**
* Rename applications on the Solace broker
*
* @param event ApplicationEvent to rename Solace applications
* @throws NotifierException if error occurs when renaming applications on the Solace broker
*/
private void renameSolaceApplication(ApplicationEvent event) throws NotifierException {
// get list of subscribed APIs in the application
Subscriber subscriber = new Subscriber(event.getSubscriber());
try {
Application application = apiMgtDAO.getApplicationByUUID(event.getUuid());
Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
boolean isContainsSolaceApis = false;
String organizationNameOfSolaceDeployment = null;
labelOne: // Check whether the application needs to be updated has a Solace API subscription
for (SubscribedAPI api : subscriptions) {
List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getIdentifier().getUUID());
for (APIRevisionDeployment deployment : deployments) {
if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
isContainsSolaceApis = true;
organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
break labelOne;
}
}
}
}
// Renaming application using Solace Admin Apis
if (isContainsSolaceApis) {
SolaceNotifierUtils.renameSolaceApplication(organizationNameOfSolaceDeployment, application);
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class SolaceApplicationNotifier method removeSolaceApplication.
/**
* Remove applications from Solace broker
*
* @param event ApplicationEvent to remove Solace applications
* @throws NotifierException if error occurs when removing applications from Solace broker
*/
private void removeSolaceApplication(ApplicationEvent event) throws NotifierException {
// get list of subscribed APIs in the application
Subscriber subscriber = new Subscriber(event.getSubscriber());
try {
Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
List<SubscribedAPI> subscribedApiList = new ArrayList<>(subscriptions);
boolean hasSubscribedAPIDeployedInSolace = false;
String organizationNameOfSolaceDeployment = null;
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
labelOne: for (SubscribedAPI api : subscribedApiList) {
List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUUID());
for (APIRevisionDeployment deployment : deployments) {
if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
hasSubscribedAPIDeployedInSolace = true;
organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
break labelOne;
}
}
}
}
boolean applicationFoundInSolaceBroker = false;
if (hasSubscribedAPIDeployedInSolace) {
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
// check existence of application in Solace Broker
CloseableHttpResponse response1 = solaceAdminApis.applicationGet(organizationNameOfSolaceDeployment, event.getUuid(), "default");
if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
applicationFoundInSolaceBroker = true;
if (log.isDebugEnabled()) {
log.info("Found application '" + event.getApplicationName() + "' in Solace broker");
log.info("Waiting until application removing workflow gets finished");
}
} else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
throw new NotifierException("Application '" + event.getApplicationName() + "' cannot be found in " + "Solace Broker");
} else {
if (log.isDebugEnabled()) {
log.error("Error while searching for application '" + event.getApplicationName() + "'" + " in Solace Broker. : " + response1.getStatusLine().toString());
}
throw new NotifierException("Error while searching for application '" + event.getApplicationName() + "' in Solace Broker");
}
}
if (applicationFoundInSolaceBroker) {
log.info("Deleting application from Solace Broker");
// delete application from solace
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
CloseableHttpResponse response2 = solaceAdminApis.deleteApplication(organizationNameOfSolaceDeployment, event.getUuid());
if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
log.info("Successfully deleted application '" + event.getApplicationName() + "' " + "in Solace Broker");
} else {
if (log.isDebugEnabled()) {
log.error("Error while deleting application " + event.getApplicationName() + " in Solace. :" + response2.getStatusLine().toString());
}
throw new NotifierException("Error while deleting application '" + event.getApplicationName() + "' in Solace");
}
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.
the class AdditionalSubscriptionInfoMappingUtil method fromAdditionalSubscriptionInfoListToDTO.
/**
* Converts a List object of SubscribedAPIs into a DTO
*
* @param subscriptions a list of SubscribedAPI objects
* @param limit max number of objects returned
* @param offset starting index
* @param organization identifier of the organization
* @return SubscriptionListDTO object containing SubscriptionDTOs
* @throws APIManagementException if error occurred when creating AdditionalSubscriptionInfoListDTO
*/
public static AdditionalSubscriptionInfoListDTO fromAdditionalSubscriptionInfoListToDTO(List<SubscribedAPI> subscriptions, Integer limit, Integer offset, String organization) throws APIManagementException {
AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO = new AdditionalSubscriptionInfoListDTO();
List<AdditionalSubscriptionInfoDTO> additionalSubscriptionInfoDTOs = additionalSubscriptionInfoListDTO.getList();
if (additionalSubscriptionInfoDTOs == null) {
additionalSubscriptionInfoDTOs = new ArrayList<>();
additionalSubscriptionInfoListDTO.setList(additionalSubscriptionInfoDTOs);
}
// Identifying the proper start and end indexes
int size = subscriptions.size();
int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
for (int i = start; i <= end; i++) {
try {
SubscribedAPI subscription = subscriptions.get(i);
additionalSubscriptionInfoDTOs.add(fromAdditionalSubscriptionInfoToDTO(subscription, organization));
} catch (APIManagementException e) {
log.error("Error while obtaining additional info of subscriptions", e);
}
}
// Set count for list
additionalSubscriptionInfoListDTO.setCount(additionalSubscriptionInfoDTOs.size());
return additionalSubscriptionInfoListDTO;
}
Aggregations