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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations