Search in sources :

Example 1 with UserApplicationAPIUsage

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);
    }
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 2 with UserApplicationAPIUsage

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;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI)

Example 3 with UserApplicationAPIUsage

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;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)

Example 4 with UserApplicationAPIUsage

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());
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with UserApplicationAPIUsage

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[] {};
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Application(org.wso2.carbon.apimgt.api.model.Application)

Aggregations

UserApplicationAPIUsage (org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage)7 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)7 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 TreeMap (java.util.TreeMap)3 Application (org.wso2.carbon.apimgt.api.model.Application)3 Tier (org.wso2.carbon.apimgt.api.model.Tier)3 ArrayList (java.util.ArrayList)2 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)1 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)1