use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class RegistryPersistenceDocUtil method createDocArtifactContent.
public static GenericArtifact createDocArtifactContent(GenericArtifact artifact, String apiName, String apiVersion, String apiProvider, Documentation documentation) throws DocumentationPersistenceException {
try {
artifact.setAttribute(APIConstants.DOC_NAME, documentation.getName());
artifact.setAttribute(APIConstants.DOC_SUMMARY, documentation.getSummary());
artifact.setAttribute(APIConstants.DOC_TYPE, documentation.getType().getType());
artifact.setAttribute(APIConstants.DOC_VISIBILITY, documentation.getVisibility().name());
Documentation.DocumentSourceType sourceType = documentation.getSourceType();
switch(sourceType) {
case INLINE:
sourceType = Documentation.DocumentSourceType.INLINE;
break;
case MARKDOWN:
sourceType = Documentation.DocumentSourceType.MARKDOWN;
break;
case URL:
sourceType = Documentation.DocumentSourceType.URL;
break;
case FILE:
{
sourceType = Documentation.DocumentSourceType.FILE;
}
break;
default:
throw new DocumentationPersistenceException("Unknown sourceType " + sourceType + " provided for documentation");
}
// Therefore setting a default value if it is not set.
if (documentation.getSourceUrl() == null) {
documentation.setSourceUrl(" ");
}
artifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, sourceType.name());
artifact.setAttribute(APIConstants.DOC_SOURCE_URL, documentation.getSourceUrl());
artifact.setAttribute(APIConstants.DOC_FILE_PATH, documentation.getFilePath());
artifact.setAttribute(APIConstants.DOC_OTHER_TYPE_NAME, documentation.getOtherTypeName());
String basePath = RegistryPersistenceUtil.replaceEmailDomain(apiProvider) + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
artifact.setAttribute(APIConstants.DOC_API_BASE_PATH, basePath);
} catch (GovernanceException e) {
String msg = "Failed to create doc artifact content from :" + documentation.getName();
log.error(msg, e);
throw new DocumentationPersistenceException(msg, e);
}
return artifact;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesMediationMediationPolicyIdGet.
/**
* Returns a specific global mediation policy by identifier
*
* @param mediationPolicyId Mediation policy uuid
* @param accept Accept header value
* @return returns the matched mediation
*/
@Override
public Response policiesMediationMediationPolicyIdGet(String mediationPolicyId, String accept, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// Get given global mediation policy
Mediation mediation = apiProvider.getGlobalMediationPolicy(mediationPolicyId);
if (mediation != null) {
MediationDTO mediationDTO = MediationMappingUtil.fromMediationToDTO(mediation);
return Response.ok().entity(mediationDTO).build();
} else {
// If global mediation policy not exists
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_POLICY, mediationPolicyId, log);
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving the global mediation policy with id " + mediationPolicyId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesApplicationPolicyIdDelete.
/**
* Delete an Application level policy specified by uuid
*
* @param policyId uuid of the policy
* @return 200 OK response if successfully deleted the policy
*/
@Override
public Response throttlingPoliciesApplicationPolicyIdDelete(String policyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// This will give PolicyNotFoundException if there's no policy exists with UUID
ApplicationPolicy existingPolicy = apiProvider.getApplicationPolicyByUUID(policyId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APP_POLICY, policyId, log);
}
if (apiProvider.hasAttachments(username, existingPolicy.getPolicyName(), PolicyConstants.POLICY_LEVEL_APP)) {
String message = "Policy " + policyId + " already attached to an application";
log.error(message);
throw new APIManagementException(message);
}
apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_APP, existingPolicy.getPolicyName());
return Response.ok().build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APP_POLICY, policyId, e, log);
} else {
String errorMessage = "Error while deleting Application level policy : " + policyId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdGet.
/**
* Get a specific Subscription Policy by its uuid
*
* @param policyId uuid of the policy
* @return Matched Subscription Throttle Policy by the given name
*/
@Override
public Response throttlingPoliciesSubscriptionPolicyIdGet(String policyId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// This will give PolicyNotFoundException if there's no policy exists with UUID
SubscriptionPolicy subscriptionPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, subscriptionPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
}
SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(subscriptionPolicy);
// setting policy permissions
setPolicyPermissionsToDTO(policyDTO);
return Response.ok().entity(policyDTO).build();
} catch (APIManagementException | ParseException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
} else {
String errorMessage = "Error while retrieving Subscription level policy: " + policyId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesCustomRuleIdGet.
/**
* Get a specific custom rule by its name
*
* @param ruleId uuid of the policy
* @return Matched Global Throttle Policy by the given name
*/
@Override
public Response throttlingPoliciesCustomRuleIdGet(String ruleId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// only super tenant is allowed to access global policies/custom rules
checkTenantDomainForCustomRules();
// This will give PolicyNotFoundException if there's no policy exists with UUID
GlobalPolicy globalPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, globalPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
}
CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(globalPolicy);
return Response.ok().entity(policyDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
} else {
String errorMessage = "Error while retrieving Custom Rule: " + ruleId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations