Search in sources :

Example 1 with APISubscriptionInfoDTO

use of org.wso2.carbon.apimgt.impl.dto.APISubscriptionInfoDTO in project carbon-apimgt by wso2.

the class ApiMgtDAO method getSubscribedAPIsForAnApp.

/**
 * Get Subscribed APIs for an App.
 *
 * @param applicationID id of the application name
 * @return APISubscriptionInfoDTO[]
 * @throws APIManagementException if failed to get Subscribed APIs
 */
public APISubscriptionInfoDTO[] getSubscribedAPIsForAnApp(String userId, int applicationID) throws APIManagementException {
    List<APISubscriptionInfoDTO> apiSubscriptionInfoDTOS = new ArrayList<APISubscriptionInfoDTO>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    // identify logged in user
    String loginUserName = getLoginUserName(userId);
    int tenantId = APIUtil.getTenantId(loginUserName);
    String sqlQuery = SQLConstants.GET_SUBSCRIBED_APIS_OF_USER_BY_APP_SQL;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.GET_SUBSCRIBED_APIS_OF_USER_BY_APP_CASE_INSENSITIVE_SQL;
    }
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, tenantId);
        ps.setInt(2, applicationID);
        rs = ps.executeQuery();
        while (rs.next()) {
            APISubscriptionInfoDTO infoDTO = new APISubscriptionInfoDTO();
            infoDTO.setProviderId(APIUtil.replaceEmailDomain(rs.getString("API_PROVIDER")));
            infoDTO.setApiName(rs.getString("API_NAME"));
            infoDTO.setContext(rs.getString("API_CONTEXT"));
            infoDTO.setVersion(rs.getString("API_VERSION"));
            infoDTO.setSubscriptionTier(rs.getString("SP_TIER_ID"));
            apiSubscriptionInfoDTOS.add(infoDTO);
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return apiSubscriptionInfoDTOS.toArray(new APISubscriptionInfoDTO[apiSubscriptionInfoDTOS.size()]);
}
Also used : SQLException(java.sql.SQLException) APISubscriptionInfoDTO(org.wso2.carbon.apimgt.impl.dto.APISubscriptionInfoDTO) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with APISubscriptionInfoDTO

use of org.wso2.carbon.apimgt.impl.dto.APISubscriptionInfoDTO in project carbon-apimgt by wso2.

the class APIUtil method getJwtTokenInfoDTO.

public static JwtTokenInfoDTO getJwtTokenInfoDTO(Application application, String userName, String tenantDomain) throws APIManagementException {
    int applicationId = application.getId();
    String appOwner = application.getOwner();
    APISubscriptionInfoDTO[] apis = ApiMgtDAO.getInstance().getSubscribedAPIsForAnApp(appOwner, applicationId);
    JwtTokenInfoDTO jwtTokenInfoDTO = new JwtTokenInfoDTO();
    jwtTokenInfoDTO.setSubscriber("sub");
    jwtTokenInfoDTO.setEndUserName(userName);
    jwtTokenInfoDTO.setContentAware(true);
    Set<String> subscriptionTiers = new HashSet<>();
    List<SubscribedApiDTO> subscribedApiDTOList = new ArrayList<SubscribedApiDTO>();
    for (APISubscriptionInfoDTO api : apis) {
        subscriptionTiers.add(api.getSubscriptionTier());
        SubscribedApiDTO subscribedApiDTO = new SubscribedApiDTO();
        subscribedApiDTO.setName(api.getApiName());
        subscribedApiDTO.setContext(api.getContext());
        subscribedApiDTO.setVersion(api.getVersion());
        subscribedApiDTO.setPublisher(APIUtil.replaceEmailDomainBack(api.getProviderId()));
        subscribedApiDTO.setSubscriptionTier(api.getSubscriptionTier());
        subscribedApiDTO.setSubscriberTenantDomain(tenantDomain);
        subscribedApiDTOList.add(subscribedApiDTO);
    }
    jwtTokenInfoDTO.setSubscribedApiDTOList(subscribedApiDTOList);
    if (subscriptionTiers.size() > 0) {
        SubscriptionPolicy[] subscriptionPolicies = ApiMgtDAO.getInstance().getSubscriptionPolicies(subscriptionTiers.toArray(new String[0]), APIUtil.getTenantId(appOwner));
        Map<String, SubscriptionPolicyDTO> subscriptionPolicyDTOList = new HashMap<>();
        for (SubscriptionPolicy subscriptionPolicy : subscriptionPolicies) {
            SubscriptionPolicyDTO subscriptionPolicyDTO = new SubscriptionPolicyDTO();
            subscriptionPolicyDTO.setSpikeArrestLimit(subscriptionPolicy.getRateLimitCount());
            subscriptionPolicyDTO.setSpikeArrestUnit(subscriptionPolicy.getRateLimitTimeUnit());
            subscriptionPolicyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
            subscriptionPolicyDTO.setTierQuotaType(subscriptionPolicy.getTierQuotaType());
            subscriptionPolicyDTO.setGraphQLMaxDepth(subscriptionPolicy.getGraphQLMaxDepth());
            subscriptionPolicyDTO.setGraphQLMaxComplexity(subscriptionPolicy.getGraphQLMaxComplexity());
            subscriptionPolicyDTOList.put(subscriptionPolicy.getPolicyName(), subscriptionPolicyDTO);
        }
        jwtTokenInfoDTO.setSubscriptionPolicyDTOList(subscriptionPolicyDTOList);
    }
    return jwtTokenInfoDTO;
}
Also used : SubscribedApiDTO(org.wso2.carbon.apimgt.impl.dto.SubscribedApiDTO) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) JwtTokenInfoDTO(org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO) ArrayList(java.util.ArrayList) Endpoint(org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) APISubscriptionInfoDTO(org.wso2.carbon.apimgt.impl.dto.APISubscriptionInfoDTO) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) SubscriptionPolicyDTO(org.wso2.carbon.apimgt.impl.dto.SubscriptionPolicyDTO)

Aggregations

ArrayList (java.util.ArrayList)2 APISubscriptionInfoDTO (org.wso2.carbon.apimgt.impl.dto.APISubscriptionInfoDTO)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)1 JwtTokenInfoDTO (org.wso2.carbon.apimgt.impl.dto.JwtTokenInfoDTO)1 SubscribedApiDTO (org.wso2.carbon.apimgt.impl.dto.SubscribedApiDTO)1 SubscriptionPolicyDTO (org.wso2.carbon.apimgt.impl.dto.SubscriptionPolicyDTO)1 Endpoint (org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint)1