use of org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAllAPIUsageByProvider.
/**
* @param providerName Name of the provider
* @return UserApplicationAPIUsage of given provider
* @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
* UserApplicationAPIUsage for given provider
*/
public UserApplicationAPIUsage[] getAllAPIUsageByProvider(String providerName) throws APIManagementException {
Connection connection = null;
PreparedStatement ps = null;
ResultSet result = null;
try {
String sqlQuery = SQLConstants.GET_APP_API_USAGE_BY_PROVIDER_SQL;
connection = APIMgtDBUtil.getConnection();
ps = connection.prepareStatement(sqlQuery);
ps.setString(1, APIUtil.replaceEmailDomainBack(providerName));
result = ps.executeQuery();
Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
while (result.next()) {
int subId = result.getInt("SUBSCRIPTION_ID");
String userId = result.getString("USER_ID");
String application = result.getString("APPNAME");
int appId = result.getInt("APPLICATION_ID");
String subStatus = result.getString("SUB_STATUS");
String subsCreateState = result.getString("SUBS_CREATE_STATE");
String key = userId + "::" + application;
UserApplicationAPIUsage usage = userApplicationUsages.get(key);
if (usage == null) {
usage = new UserApplicationAPIUsage();
usage.setUserId(userId);
usage.setApplicationName(application);
usage.setAppId(appId);
userApplicationUsages.put(key, usage);
}
APIIdentifier apiId = new APIIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiId);
apiSubscription.setSubStatus(subStatus);
apiSubscription.setSubCreatedStatus(subsCreateState);
apiSubscription.setUUID(result.getString("SUB_UUID"));
apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
Application applicationObj = new Application(result.getString("APP_UUID"));
apiSubscription.setApplication(applicationObj);
usage.addApiSubscriptions(apiSubscription);
}
return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
} catch (SQLException e) {
handleException("Failed to find API Usage for :" + providerName, e);
return null;
} finally {
APIMgtDBUtil.closeAllConnections(ps, connection, result);
}
}
use of org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPIProductUsageByAPIProductId.
/**
* Returns usage details of a particular API
*
* @param apiProductId API Product identifier
* @return UserApplicationAPIUsages for given provider
* @throws org.wso2.carbon.apimgt.api.APIManagementException If failed to get UserApplicationAPIUsage
*/
@Override
public List<SubscribedAPI> getAPIProductUsageByAPIProductId(APIProductIdentifier apiProductId) throws APIManagementException {
APIProductIdentifier apiIdEmailReplaced = new APIProductIdentifier(APIUtil.replaceEmailDomain(apiProductId.getProviderName()), apiProductId.getName(), apiProductId.getVersion());
UserApplicationAPIUsage[] allApiProductResult = apiMgtDAO.getAllAPIProductUsageByProvider(apiProductId.getProviderName());
List<SubscribedAPI> subscribedAPIs = new ArrayList<>();
for (UserApplicationAPIUsage usage : allApiProductResult) {
for (SubscribedAPI apiSubscription : usage.getApiSubscriptions()) {
APIProductIdentifier subsApiProductId = apiSubscription.getProductId();
APIProductIdentifier subsApiProductIdEmailReplaced = new APIProductIdentifier(APIUtil.replaceEmailDomain(subsApiProductId.getProviderName()), subsApiProductId.getName(), subsApiProductId.getVersion());
if (subsApiProductIdEmailReplaced.equals(apiIdEmailReplaced)) {
subscribedAPIs.add(apiSubscription);
}
}
}
return subscribedAPIs;
}
use of org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage 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.dto.UserApplicationAPIUsage in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetAPIUsageByAPIId.
@Test
public void testGetAPIUsageByAPIId() throws APIManagementException, RegistryException, UserStoreException {
String uuid = UUID.randomUUID().toString();
SubscribedAPI subscribedAPI1 = new SubscribedAPI(new Subscriber("user1"), new APIIdentifier("admin", "API1", "1.0.0"));
SubscribedAPI subscribedAPI2 = new SubscribedAPI(new Subscriber("user1"), new APIIdentifier("admin", "API2", "1.0.0"));
UserApplicationAPIUsage apiResult1 = new UserApplicationAPIUsage();
apiResult1.addApiSubscriptions(subscribedAPI1);
apiResult1.addApiSubscriptions(subscribedAPI2);
SubscribedAPI subscribedAPI3 = new SubscribedAPI(new Subscriber("user2"), new APIIdentifier("admin", "API1", "1.0.0"));
SubscribedAPI subscribedAPI4 = new SubscribedAPI(new Subscriber("user2"), new APIIdentifier("admin", "API2", "1.0.0"));
UserApplicationAPIUsage apiResult2 = new UserApplicationAPIUsage();
apiResult2.addApiSubscriptions(subscribedAPI3);
apiResult2.addApiSubscriptions(subscribedAPI4);
UserApplicationAPIUsage[] apiResults = { apiResult1, apiResult2 };
Mockito.when(apimgtDAO.getAllAPIUsageByProviderAndApiId(uuid, "org1")).thenReturn(apiResults);
Mockito.when(apimgtDAO.getAPIIdentifierFromUUID(uuid)).thenReturn(new APIIdentifier("admin", "API1", "1.0.0"));
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
List<SubscribedAPI> subscribedAPIs = apiProvider.getAPIUsageByAPIId(uuid, "org1");
Assert.assertEquals(2, subscribedAPIs.size());
Assert.assertEquals("user1", subscribedAPIs.get(0).getSubscriber().getName());
Assert.assertEquals("user2", subscribedAPIs.get(1).getSubscriber().getName());
}
use of org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAllAPIProductUsageByProvider.
/**
* @param providerName Name of the provider
* @return UserApplicationAPIUsage of given provider
* @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
* UserApplicationAPIUsage for given provider
*/
public UserApplicationAPIUsage[] getAllAPIProductUsageByProvider(String providerName) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement ps = connection.prepareStatement(SQLConstants.GET_APP_API_USAGE_BY_PROVIDER_SQL)) {
ps.setString(1, APIUtil.replaceEmailDomainBack(providerName));
try (ResultSet result = ps.executeQuery()) {
Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
while (result.next()) {
int subId = result.getInt("SUBSCRIPTION_ID");
String userId = result.getString("USER_ID");
String application = result.getString("APPNAME");
int appId = result.getInt("APPLICATION_ID");
String subStatus = result.getString("SUB_STATUS");
String subsCreateState = result.getString("SUBS_CREATE_STATE");
String key = userId + "::" + application;
UserApplicationAPIUsage usage = userApplicationUsages.get(key);
if (usage == null) {
usage = new UserApplicationAPIUsage();
usage.setUserId(userId);
usage.setApplicationName(application);
usage.setAppId(appId);
userApplicationUsages.put(key, usage);
}
APIProductIdentifier apiProductId = new APIProductIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiProductId);
apiSubscription.setSubStatus(subStatus);
apiSubscription.setSubCreatedStatus(subsCreateState);
apiSubscription.setUUID(result.getString("SUB_UUID"));
apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
Application applicationObj = new Application(result.getString("APP_UUID"));
apiSubscription.setApplication(applicationObj);
usage.addApiSubscriptions(apiSubscription);
}
return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
}
} catch (SQLException e) {
handleException("Failed to find API Product Usage for :" + providerName, e);
}
return new UserApplicationAPIUsage[] {};
}
Aggregations