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