Search in sources :

Example 1 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method fromSubscriptionListToDTO.

/**
 * 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
 * @return SubscriptionListDTO object containing SubscriptionDTOs
 */
public static SubscriptionListDTO fromSubscriptionListToDTO(List<Subscription> subscriptions, Integer limit, Integer offset) {
    SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
    List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
    if (subscriptionDTOs == null) {
        subscriptionDTOs = new ArrayList<>();
        subscriptionListDTO.setList(subscriptionDTOs);
    }
    // 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++) {
        Subscription subscription = subscriptions.get(i);
        subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
    }
    subscriptionListDTO.setCount(subscriptionDTOs.size());
    return subscriptionListDTO;
}
Also used : Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO)

Example 2 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method fromSubscriptionToDTO.

/**
 * Converts a SubscribedAPI object into SubscriptionDTO
 *
 * @param subscription SubscribedAPI object
 * @return SubscriptionDTO corresponds to SubscribedAPI object
 */
public static SubscriptionDTO fromSubscriptionToDTO(Subscription subscription) {
    SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
    subscriptionDTO.setSubscriptionId(subscription.getId());
    if (subscription.getApi() != null) {
        subscriptionDTO.setApiIdentifier(subscription.getApi().getId());
        subscriptionDTO.setApiName(subscription.getApi().getName());
        subscriptionDTO.setApiVersion(subscription.getApi().getVersion());
    }
    if (subscription.getApplication() != null) {
        subscriptionDTO.setApplicationId(subscription.getApplication().getId());
    }
    subscriptionDTO.setPolicy(subscription.getPolicy().getPolicyName());
    subscriptionDTO.setLifeCycleStatus(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription.getStatus().toString()));
    return subscriptionDTO;
}
Also used : SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO)

Example 3 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateSubscriptionStatus.

/**
 * Update the subscription status
 *
 * @param subId     Subscription ID
 * @param subStatus Subscription Status
 * @throws APIManagementException If failed to update subscription status
 */
@Override
public void updateSubscriptionStatus(String subId, APIMgtConstants.SubscriptionStatus subStatus) throws APIManagementException {
    try {
        getApiSubscriptionDAO().updateSubscriptionStatus(subId, subStatus);
        Subscription subscription = getApiSubscriptionDAO().getAPISubscription(subId);
        if (subscription != null) {
            API subscribedApi = subscription.getApi();
            List<SubscriptionValidationData> subscriptionValidationDataList = getApiSubscriptionDAO().getAPISubscriptionsOfAPIForValidation(subscribedApi.getContext(), subscribedApi.getVersion(), subscription.getApplication().getId());
            getApiGateway().updateAPISubscriptionStatus(subscriptionValidationDataList);
        }
    } catch (APIMgtDAOException e) {
        throw new APIManagementException(e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 4 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class GatewayUtils method validateAPISubscription.

/**
 * Validate whether the user is subscribed to the invoked API. If subscribed, return a JSON object containing
 * the API information.
 *
 * @param apiContext API context
 * @param apiVersion API version
 * @param payload    The payload of the JWT token
 * @return an JSON object containing subscribed API information retrieved from token payload.
 * If the subscription information is not found, return a null object.
 * @throws APISecurityException if the user is not subscribed to the API
 */
public static JSONObject validateAPISubscription(String apiContext, String apiVersion, JWTClaimsSet payload, String[] splitToken, boolean isOauth) throws APISecurityException {
    JSONObject api = null;
    APIKeyValidator apiKeyValidator = new APIKeyValidator();
    APIKeyValidationInfoDTO apiKeyValidationInfoDTO = null;
    boolean apiKeySubValidationEnabled = isAPIKeySubscriptionValidationEnabled();
    JSONObject application;
    int appId = 0;
    if (payload.getClaim(APIConstants.JwtTokenConstants.APPLICATION) != null) {
        application = (JSONObject) payload.getClaim(APIConstants.JwtTokenConstants.APPLICATION);
        appId = Integer.parseInt(application.getAsString(APIConstants.JwtTokenConstants.APPLICATION_ID));
    }
    // if the appId is equal to 0 then it's a internal key
    if (apiKeySubValidationEnabled && appId != 0) {
        apiKeyValidationInfoDTO = apiKeyValidator.validateSubscription(apiContext, apiVersion, appId, getTenantDomain());
    }
    if (payload.getClaim(APIConstants.JwtTokenConstants.SUBSCRIBED_APIS) != null) {
        // Subscription validation
        JSONArray subscribedAPIs = (JSONArray) payload.getClaim(APIConstants.JwtTokenConstants.SUBSCRIBED_APIS);
        for (Object subscribedAPI : subscribedAPIs) {
            JSONObject subscribedAPIsJSONObject = (JSONObject) subscribedAPI;
            if (apiContext.equals(subscribedAPIsJSONObject.getAsString(APIConstants.JwtTokenConstants.API_CONTEXT)) && apiVersion.equals(subscribedAPIsJSONObject.getAsString(APIConstants.JwtTokenConstants.API_VERSION))) {
                // check whether the subscription is authorized
                if (apiKeySubValidationEnabled && appId != 0) {
                    if (apiKeyValidationInfoDTO.isAuthorized()) {
                        api = subscribedAPIsJSONObject;
                        if (log.isDebugEnabled()) {
                            log.debug("User is subscribed to the API: " + apiContext + ", " + "version: " + apiVersion + ". Token: " + getMaskedToken(splitToken[0]));
                        }
                    }
                } else {
                    api = subscribedAPIsJSONObject;
                    if (log.isDebugEnabled()) {
                        log.debug("User is subscribed to the API: " + apiContext + ", " + "version: " + apiVersion + ". Token: " + getMaskedToken(splitToken[0]));
                    }
                }
                break;
            }
        }
        if (api == null) {
            if (log.isDebugEnabled()) {
                log.debug("User is not subscribed to access the API: " + apiContext + ", version: " + apiVersion + ". Token: " + getMaskedToken(splitToken[0]));
            }
            log.error("User is not subscribed to access the API.");
            throw new APISecurityException(APISecurityConstants.API_AUTH_FORBIDDEN, APISecurityConstants.API_AUTH_FORBIDDEN_MESSAGE);
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("No subscription information found in the token.");
        }
        // we perform mandatory authentication for Api Keys
        if (!isOauth) {
            log.error("User is not subscribed to access the API.");
            throw new APISecurityException(APISecurityConstants.API_AUTH_FORBIDDEN, APISecurityConstants.API_AUTH_FORBIDDEN_MESSAGE);
        }
    }
    return api;
}
Also used : APISecurityException(org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException) JSONObject(net.minidev.json.JSONObject) APIKeyValidator(org.wso2.carbon.apimgt.gateway.handlers.security.APIKeyValidator) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)

Example 5 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class ApiMgtDAO method initSubscribedAPIDetailed.

private void initSubscribedAPIDetailed(Connection connection, SubscribedAPI subscribedAPI, Subscriber subscriber, ResultSet result) throws SQLException, APIManagementException {
    subscribedAPI.setSubscriptionId(result.getInt("SUBS_ID"));
    subscribedAPI.setSubStatus(result.getString("SUB_STATUS"));
    subscribedAPI.setSubCreatedStatus(result.getString("SUBS_CREATE_STATE"));
    String tierName = result.getString(APIConstants.SUBSCRIPTION_FIELD_TIER_ID);
    String requestedTierName = result.getString(APIConstants.SUBSCRIPTION_FIELD_TIER_ID_PENDING);
    subscribedAPI.setTier(new Tier(tierName));
    subscribedAPI.setRequestedTier(new Tier(requestedTierName));
    subscribedAPI.setUUID(result.getString("SUB_UUID"));
    // setting NULL for subscriber. If needed, Subscriber object should be constructed &
    // passed in
    int applicationId = result.getInt("APP_ID");
    Application application = new Application(result.getString("APP_NAME"), subscriber);
    application.setId(result.getInt("APP_ID"));
    application.setTokenType(result.getString("APP_TOKEN_TYPE"));
    application.setCallbackUrl(result.getString("CALLBACK_URL"));
    application.setUUID(result.getString("APP_UUID"));
    if (multiGroupAppSharingEnabled) {
        application.setGroupId(getGroupId(connection, application.getId()));
        application.setOwner(result.getString("OWNER"));
    }
    subscribedAPI.setApplication(application);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) Application(org.wso2.carbon.apimgt.api.model.Application)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 Application (org.wso2.carbon.apimgt.api.model.Application)28 Tier (org.wso2.carbon.apimgt.api.model.Tier)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)16 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)14 ArrayList (java.util.ArrayList)12 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)12 HashSet (java.util.HashSet)11 TreeMap (java.util.TreeMap)11 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)11 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 ResultSet (java.sql.ResultSet)9 JSONObject (net.minidev.json.JSONObject)9 API (org.wso2.carbon.apimgt.api.model.API)9