use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Get all subscriptions that are of user or shared subscriptions of the user's group.
* <p/>
* If apiId is specified this will return the subscribed applications of that api
* If application id is specified this will return the api subscriptions of that application
*
* @param apiId api identifier
* @param applicationId application identifier
* @param offset starting index of the subscription list
* @param limit max num of subscriptions returned
* @param ifNoneMatch If-None-Match header value
* @return matched subscriptions as a list of SubscriptionDTOs
*/
@Override
public Response subscriptionsGet(String apiId, String applicationId, String groupId, String xWSO2Tenant, Integer offset, Integer limit, String ifNoneMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
Subscriber subscriber = new Subscriber(username);
Set<SubscribedAPI> subscriptions;
List<SubscribedAPI> subscribedAPIList = new ArrayList<>();
// pre-processing
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
// currently groupId is taken from the user so that groupId coming as a query parameter is not honored.
// As a improvement, we can check admin privileges of the user and honor groupId.
groupId = RestApiUtil.getLoggedInUserGroupId();
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
SubscriptionListDTO subscriptionListDTO;
if (!StringUtils.isEmpty(apiId)) {
// todo : FIX properly, need to done properly with backend side pagination.
// todo : getSubscribedIdentifiers() method should NOT be used. Appears to be too slow.
// This will fail with an authorization failed exception if user does not have permission to access the API
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
if (apiTypeWrapper.isAPIProduct()) {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApiProduct().getId(), groupId, organization);
} else {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApi().getId(), groupId, organization);
}
// sort by application name
subscribedAPIList.addAll(subscriptions);
subscribedAPIList.sort(Comparator.comparing(o -> o.getApplication().getName()));
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedAPIList, limit, offset, organization);
SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, subscribedAPIList.size());
return Response.ok().entity(subscriptionListDTO).build();
} else if (!StringUtils.isEmpty(applicationId)) {
Application application = apiConsumer.getApplicationByUUID(applicationId);
if (application == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
return null;
}
if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
subscriptions = apiConsumer.getPaginatedSubscribedAPIsByApplication(application, offset, limit, organization);
subscribedAPIList.addAll(subscriptions);
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedAPIList, limit, offset, organization);
return Response.ok().entity(subscriptionListDTO).build();
} else {
// neither apiId nor applicationId is given
RestApiUtil.handleBadRequest("Either applicationId or apiId should be available", log);
return null;
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while getting subscriptions of the user " + username, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class APIMappingUtil method setEndpointURLsForAwsAPIs.
private static List<APIEndpointURLsDTO> setEndpointURLsForAwsAPIs(ApiTypeWrapper model, String organization) throws APIManagementException {
APIDTO apidto;
apidto = fromAPItoDTO(model.getApi(), organization);
JsonElement configElement = new JsonParser().parse(apidto.getApiDefinition());
// swaggerDefinition as a json object
JsonObject configObject = configElement.getAsJsonObject();
JsonArray servers = configObject.getAsJsonArray("servers");
JsonObject server = servers.get(0).getAsJsonObject();
String url = server.get("url").getAsString();
JsonObject variables = server.getAsJsonObject("variables");
JsonObject basePath = variables.getAsJsonObject("basePath");
String stageName = basePath.get("default").getAsString();
String serverUrl = url.replace("/{basePath}", stageName);
if (serverUrl == null) {
serverUrl = "Could not find server URL";
}
APIEndpointURLsDTO apiEndpointURLsDTO = new APIEndpointURLsDTO();
List<APIEndpointURLsDTO> endpointUrls = new ArrayList<>();
APIURLsDTO apiurLsDTO = new APIURLsDTO();
apiurLsDTO.setHttps(serverUrl);
apiEndpointURLsDTO.setUrLs(apiurLsDTO);
endpointUrls.add(apiEndpointURLsDTO);
return endpointUrls;
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdTopicsGet.
@Override
public Response apisApiIdTopicsGet(String apiId, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
if (org.apache.commons.lang.StringUtils.isNotEmpty(apiId)) {
String username = RestApiCommonUtil.getLoggedInUsername();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
Set<Topic> topics;
try {
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
TopicListDTO topicListDTO;
if (apiTypeWrapper.isAPIProduct()) {
topics = apiConsumer.getTopics(apiTypeWrapper.getApiProduct().getUuid());
} else {
topics = apiConsumer.getTopics(apiTypeWrapper.getApi().getUuid());
}
topicListDTO = AsyncAPIMappingUtil.fromTopicListToDTO(topics);
return Response.ok().entity(topicListDTO).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 topics of Async API " + apiId, e, log);
}
}
} else {
RestApiUtil.handleBadRequest("API Id is missing in request", log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class ImportUtils method isTierAvailable.
/**
* Check whether a target Tier is available to subscribe
*
* @param targetTierName Target Tier Name
* @param apiTypeWrapper - {@link ApiTypeWrapper}
* @return true, if the target tier is available
*/
private static boolean isTierAvailable(String targetTierName, ApiTypeWrapper apiTypeWrapper) {
Set<Tier> availableTiers;
API api = null;
APIProduct apiProduct = null;
if (!apiTypeWrapper.isAPIProduct()) {
api = apiTypeWrapper.getApi();
availableTiers = api.getAvailableTiers();
} else {
apiProduct = apiTypeWrapper.getApiProduct();
availableTiers = apiProduct.getAvailableTiers();
}
for (Tier tier : availableTiers) {
if (StringUtils.equals(tier.getName(), targetTierName)) {
return true;
}
}
if (!apiTypeWrapper.isAPIProduct()) {
log.error("Tier:" + targetTierName + " is not available for API " + api.getId().getApiName() + "-" + api.getId().getVersion());
} else {
log.error("Tier:" + targetTierName + " is not available for API Product " + apiProduct.getId().getName() + "-" + apiProduct.getId().getVersion());
}
return false;
}
Aggregations