Search in sources :

Example 81 with APIConsumer

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

the class TagsApiServiceImpl method tagsGet.

@Override
public Response tagsGet(Integer limit, Integer offset, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    // pre-processing
    limit = limit != null ? limit : RestApiConstants.TAG_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.TAG_OFFSET_DEFAULT;
    Set<Tag> tagSet;
    List<Tag> tagList = new ArrayList<>();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        tagSet = apiConsumer.getAllTags(organization);
        if (tagSet != null) {
            tagList.addAll(tagSet);
        }
        TagListDTO tagListDTO = TagMappingUtil.fromTagListToDTO(tagList, limit, offset);
        TagMappingUtil.setPaginationParams(tagListDTO, limit, offset, tagList.size());
        return Response.ok().entity(tagListDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving tags", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TagListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO) ArrayList(java.util.ArrayList) Tag(org.wso2.carbon.apimgt.api.model.Tag) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 82 with APIConsumer

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

the class ThrottlingPoliciesApiServiceImpl method getThrottlingPolicyList.

/**
 * Returns the throttling policies which belongs to the given policy level
 * @param policyLevel
 * @return list of throttling policies
 */
public List<Tier> getThrottlingPolicyList(String policyLevel, String organization) {
    List<Tier> throttlingPolicyList = new ArrayList<>();
    int tierLevel = -1;
    try {
        if (StringUtils.isBlank(policyLevel)) {
            RestApiUtil.handleBadRequest("tierLevel cannot be empty", log);
        }
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // retrieves the tier based on the given tier-level
        if (ThrottlingPolicyDTO.PolicyLevelEnum.SUBSCRIPTION.toString().equals(policyLevel)) {
            tierLevel = APIConstants.TIER_API_TYPE;
        } else if (ThrottlingPolicyDTO.PolicyLevelEnum.APPLICATION.toString().equals(policyLevel)) {
            tierLevel = APIConstants.TIER_APPLICATION_TYPE;
        } else {
            RestApiUtil.handleResourceNotFoundError("tierLevel should be one of " + Arrays.toString(ThrottlingPolicyDTO.PolicyLevelEnum.values()), log);
        }
        throttlingPolicyList = apiConsumer.getThrottlePolicies(tierLevel, organization);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tiers";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return throttlingPolicyList;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) ArrayList(java.util.ArrayList) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 83 with APIConsumer

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdOauthKeysKeyMappingIdRegenerateSecretPost.

@Override
public Response applicationsApplicationIdOauthKeysKeyMappingIdRegenerateSecretPost(String applicationId, String keyMappingId, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    Set<APIKey> applicationKeys = getApplicationKeys(applicationId);
    if (applicationKeys == null) {
        return null;
    }
    ApplicationKeyDTO applicationKeyDTO = getApplicationKeyByAppIDAndKeyMapping(applicationId, keyMappingId);
    if (applicationKeyDTO != null) {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        String clientId = applicationKeyDTO.getConsumerKey();
        String clientSecret = apiConsumer.renewConsumerSecret(clientId, applicationKeyDTO.getKeyManager());
        ApplicationKeyDTO retrievedApplicationKey = new ApplicationKeyDTO();
        retrievedApplicationKey.setConsumerKey(clientId);
        retrievedApplicationKey.setConsumerSecret(clientSecret);
        return Response.ok().entity(retrievedApplicationKey).build();
    }
    return null;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 84 with APIConsumer

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

the class ApplicationsApiServiceImpl method getApplicationKeys.

/**
 * Used to get all keys of an application
 *
 * @param applicationUUID Id of the application
 * @return List of application keys
 */
private Set<APIKey> getApplicationKeys(String applicationUUID, String tenantDomain) {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        Application application = apiConsumer.getLightweightApplicationByUUID(applicationUUID);
        if (application != null) {
            if (RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
                return apiConsumer.getApplicationKeysOfApplication(application.getId(), tenantDomain);
            } else {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationUUID, log);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationUUID, log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving application " + applicationUUID, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 85 with APIConsumer

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

the class ApplicationsApiServiceImpl method applicationsApplicationIdGenerateKeysPost.

/**
 * Generate keys for a application
 *
 * @param applicationId     application identifier
 * @param body              request body
 * @return A response object containing application keys
 */
@Override
public Response applicationsApplicationIdGenerateKeysPost(String applicationId, ApplicationKeyGenerateRequestDTO body, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
    String username = RestApiCommonUtil.getLoggedInUsername();
    try {
        APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username);
        Application application = apiConsumer.getApplicationByUUID(applicationId);
        if (application != null) {
            if (RestAPIStoreUtils.isUserOwnerOfApplication(application)) {
                String[] accessAllowDomainsArray = { "ALL" };
                JSONObject jsonParamObj = new JSONObject();
                jsonParamObj.put(ApplicationConstants.OAUTH_CLIENT_USERNAME, username);
                String grantTypes = StringUtils.join(body.getGrantTypesToBeSupported(), ',');
                if (!StringUtils.isEmpty(grantTypes)) {
                    jsonParamObj.put(APIConstants.JSON_GRANT_TYPES, grantTypes);
                }
                /* Read clientId & clientSecret from ApplicationKeyGenerateRequestDTO object.
                       User can provide clientId only or both clientId and clientSecret
                       User cannot provide clientSecret only */
                if (!StringUtils.isEmpty(body.getClientId())) {
                    jsonParamObj.put(APIConstants.JSON_CLIENT_ID, body.getClientId());
                    if (!StringUtils.isEmpty(body.getClientSecret())) {
                        jsonParamObj.put(APIConstants.JSON_CLIENT_SECRET, body.getClientSecret());
                    }
                }
                if (body.getAdditionalProperties() != null) {
                    if (body.getAdditionalProperties() instanceof String && StringUtils.isNotEmpty((String) body.getAdditionalProperties())) {
                        jsonParamObj.put(APIConstants.JSON_ADDITIONAL_PROPERTIES, body.getAdditionalProperties());
                    } else if (body.getAdditionalProperties() instanceof Map) {
                        String jsonContent = new Gson().toJson(body.getAdditionalProperties());
                        jsonParamObj.put(APIConstants.JSON_ADDITIONAL_PROPERTIES, jsonContent);
                    }
                }
                String jsonParams = jsonParamObj.toString();
                String tokenScopes = StringUtils.join(body.getScopes(), " ");
                String keyManagerName = APIConstants.KeyManager.DEFAULT_KEY_MANAGER;
                if (StringUtils.isNotEmpty(body.getKeyManager())) {
                    keyManagerName = body.getKeyManager();
                }
                String organization = RestApiUtil.getValidatedOrganization(messageContext);
                Map<String, Object> keyDetails = apiConsumer.requestApprovalForApplicationRegistration(username, application, body.getKeyType().toString(), body.getCallbackUrl(), accessAllowDomainsArray, body.getValidityTime(), tokenScopes, jsonParams, keyManagerName, organization, false);
                ApplicationKeyDTO applicationKeyDTO = ApplicationKeyMappingUtil.fromApplicationKeyToDTO(keyDetails, body.getKeyType().toString());
                applicationKeyDTO.setKeyManager(keyManagerName);
                return Response.ok().entity(applicationKeyDTO).build();
            } else {
                RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
            }
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
        }
    } catch (EmptyCallbackURLForCodeGrantsException e) {
        RestApiUtil.handleBadRequest(e.getMessage(), log);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) ApplicationKeyDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) ExportedApplication(org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication) Application(org.wso2.carbon.apimgt.api.model.Application) Map(java.util.Map) HashMap(java.util.HashMap) EmptyCallbackURLForCodeGrantsException(org.wso2.carbon.apimgt.api.EmptyCallbackURLForCodeGrantsException)

Aggregations

APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 Application (org.wso2.carbon.apimgt.api.model.Application)50 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMap (java.util.HashMap)32 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)29 ArrayList (java.util.ArrayList)28 API (org.wso2.carbon.apimgt.api.model.API)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 JSONObject (org.json.simple.JSONObject)23 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)23 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)20 Map (java.util.Map)19 Matchers.anyString (org.mockito.Matchers.anyString)19 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)18 Tier (org.wso2.carbon.apimgt.api.model.Tier)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)15 URI (java.net.URI)13 URISyntaxException (java.net.URISyntaxException)13