use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class DefaultGroupIDExtractorImpl method getGroupingIdentifiers.
public String getGroupingIdentifiers(String loginResponse) {
JSONObject obj;
String username = null;
Boolean isSuperTenant;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
String tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
String claim = config.getFirstProperty(APIConstants.API_STORE_GROUP_EXTRACTOR_CLAIM_URI);
if (StringUtils.isBlank(claim)) {
claim = "http://wso2.org/claims/organization";
}
String organization = null;
try {
obj = new JSONObject(loginResponse);
username = (String) obj.get("user");
isSuperTenant = (Boolean) obj.get("isSuperTenant");
RealmService realmService = ServiceReferenceHolder.getInstance().getRealmService();
// if the user is not in the super tenant domain then find the domain name and tenant id.
if (!isSuperTenant) {
tenantDomain = MultitenantUtils.getTenantDomain(username);
tenantId = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager().getTenantId(tenantDomain);
}
UserRealm realm = (UserRealm) realmService.getTenantUserRealm(tenantId);
UserStoreManager manager = realm.getUserStoreManager();
organization = manager.getUserClaimValue(MultitenantUtils.getTenantAwareUsername(username), claim, null);
if (organization != null) {
organization = tenantDomain + "/" + organization.trim();
}
} catch (JSONException e) {
log.error("Exception occured while trying to get group Identifier from login response", e);
} catch (org.wso2.carbon.user.api.UserStoreException e) {
log.error("Error while checking user existence for " + username, e);
}
return organization;
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class UserAwareAPIConsumer method getLightweightAPI.
@Override
public API getLightweightAPI(APIIdentifier identifier, String orgId) throws APIManagementException {
API api = super.getLightweightAPI(identifier, orgId);
checkVisibilityPermission(userNameWithoutChange, api.getVisibility(), api.getVisibleRoles());
return api;
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class APIUtil method getMediationPolicyAttributes.
/**
* Returns attributes correspond to the given mediation policy name and direction
*
* @param policyName name of the sequence
* @param tenantId logged in user's tenantId
* @param direction in/out/fault
* @param identifier API identifier
* @return attributes(path, uuid) of the given mediation sequence or null
* @throws APIManagementException If failed to get the uuid of the mediation sequence
*/
public static Map<String, String> getMediationPolicyAttributes(String policyName, int tenantId, String direction, APIIdentifier identifier) throws APIManagementException {
org.wso2.carbon.registry.api.Collection seqCollection = null;
String seqCollectionPath = "";
Map<String, String> mediationPolicyAttributes = new HashMap<>(3);
try {
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
} else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
} else if (APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT.equals(direction)) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(APIConstants.API_CUSTOM_SEQUENCE_LOCATION + "/" + APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
}
if (seqCollection == null) {
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, direction));
}
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource mediationPolicy = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(mediationPolicy.getContentStream());
String seqElmentName = seqElment.getAttributeValue(new QName("name"));
if (policyName.equals(seqElmentName)) {
mediationPolicyAttributes.put("path", childPath);
mediationPolicyAttributes.put("uuid", mediationPolicy.getUUID());
mediationPolicyAttributes.put("name", policyName);
return mediationPolicyAttributes;
}
}
}
// If the sequence not found the default sequences, check in custom sequences
seqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(getSequencePath(identifier, direction));
if (seqCollection != null) {
String[] childPaths = seqCollection.getChildren();
for (String childPath : childPaths) {
Resource mediationPolicy = registry.get(childPath);
OMElement seqElment = APIUtil.buildOMElement(mediationPolicy.getContentStream());
if (policyName.equals(seqElment.getAttributeValue(new QName("name")))) {
mediationPolicyAttributes.put("path", childPath);
mediationPolicyAttributes.put("uuid", mediationPolicy.getUUID());
mediationPolicyAttributes.put("name", policyName);
return mediationPolicyAttributes;
}
}
}
} catch (Exception e) {
String msg = "Issue is in accessing the Registry";
log.error(msg);
throw new APIManagementException(msg, e);
}
return mediationPolicyAttributes;
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method deleteAPIProduct.
@Override
public void deleteAPIProduct(Organization org, String apiId) throws APIPersistenceException {
boolean tenantFlowStarted = false;
try {
RegistryHolder holder = getRegistry(org.getName());
tenantFlowStarted = holder.isTenantFlowStarted();
Registry registry = holder.getRegistry();
GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when deleting API Product" + apiId;
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifact apiProductArtifact = artifactManager.getGenericArtifact(apiId);
APIProductIdentifier identifier = new APIProductIdentifier(apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
// this is the product resource collection path
String productResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
// this is the product rxt instance path
String apiProductArtifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
Resource apiProductResource = registry.get(productResourcePath);
String productResourceUUID = apiProductResource.getUUID();
if (productResourceUUID == null) {
throw new APIManagementException("artifact id is null for : " + productResourcePath);
}
Resource apiArtifactResource = registry.get(apiProductArtifactPath);
String apiArtifactResourceUUID = apiArtifactResource.getUUID();
if (apiArtifactResourceUUID == null) {
throw new APIManagementException("artifact id is null for : " + apiProductArtifactPath);
}
// Delete the dependencies associated with the api product artifact
GovernanceArtifact[] dependenciesArray = apiProductArtifact.getDependencies();
if (dependenciesArray.length > 0) {
for (GovernanceArtifact artifact : dependenciesArray) {
registry.delete(artifact.getPath());
}
}
// delete registry resources
artifactManager.removeGenericArtifact(apiProductArtifact);
artifactManager.removeGenericArtifact(productResourceUUID);
/* remove empty directories */
String apiProductCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
if (registry.resourceExists(apiProductCollectionPath)) {
// at the moment product versioning is not supported so we are directly deleting this collection as
// this is known to be empty
registry.delete(apiProductCollectionPath);
}
String productProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
if (registry.resourceExists(productProviderPath)) {
Resource providerCollection = registry.get(productProviderPath);
CollectionImpl collection = (CollectionImpl) providerCollection;
// if there is no api product for given provider delete the provider directory
if (collection.getChildCount() == 0) {
if (log.isDebugEnabled()) {
log.debug("No more API Products from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
}
registry.delete(productProviderPath);
}
}
} catch (RegistryException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
} catch (APIManagementException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.Identifier in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method deleteAPI.
@Override
public void deleteAPI(Organization org, String apiId) throws APIPersistenceException {
boolean transactionCommitted = false;
boolean tenantFlowStarted = false;
Registry registry = null;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
registry = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
registry.beginTransaction();
GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when deleting API " + apiId;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
APIIdentifier identifier = new APIIdentifier(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
// Delete the dependencies associated with the api artifact
GovernanceArtifact[] dependenciesArray = apiArtifact.getDependencies();
if (dependenciesArray.length > 0) {
for (GovernanceArtifact artifact : dependenciesArray) {
registry.delete(artifact.getPath());
}
}
artifactManager.removeGenericArtifact(apiArtifact);
String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
Resource apiResource = registry.get(path);
String artifactId = apiResource.getUUID();
artifactManager.removeGenericArtifact(artifactId);
String thumbPath = RegistryPersistenceUtil.getIconPath(identifier);
if (registry.resourceExists(thumbPath)) {
registry.delete(thumbPath);
}
String wsdlArchivePath = RegistryPersistenceUtil.getWsdlArchivePath(identifier);
if (registry.resourceExists(wsdlArchivePath)) {
registry.delete(wsdlArchivePath);
}
/*Remove API Definition Resource - swagger*/
String apiDefinitionFilePath = APIConstants.API_DOC_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + '-' + identifier.getVersion() + '-' + identifier.getProviderName();
if (registry.resourceExists(apiDefinitionFilePath)) {
registry.delete(apiDefinitionFilePath);
}
/*remove empty directories*/
String apiCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName();
if (registry.resourceExists(apiCollectionPath)) {
Resource apiCollection = registry.get(apiCollectionPath);
CollectionImpl collection = (CollectionImpl) apiCollection;
// if there is no other versions of apis delete the directory of the api
if (collection.getChildCount() == 0) {
if (log.isDebugEnabled()) {
log.debug("No more versions of the API found, removing API collection from registry");
}
registry.delete(apiCollectionPath);
}
}
String apiProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName();
if (registry.resourceExists(apiProviderPath)) {
Resource providerCollection = registry.get(apiProviderPath);
CollectionImpl collection = (CollectionImpl) providerCollection;
// if there is no api for given provider delete the provider directory
if (collection.getChildCount() == 0) {
if (log.isDebugEnabled()) {
log.debug("No more APIs from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
}
registry.delete(apiProviderPath);
}
}
registry.commitTransaction();
transactionCommitted = true;
} catch (RegistryException e) {
throw new APIPersistenceException("Failed to remove the API : " + apiId, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
try {
if (!transactionCommitted) {
registry.rollbackTransaction();
}
} catch (RegistryException ex) {
throw new APIPersistenceException("Error occurred while rolling back the transaction. ", ex);
}
}
}
Aggregations