use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getRegistry.
protected RegistryHolder getRegistry(String requestedTenantDomain) throws APIPersistenceException {
String userTenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
log.debug("Accessing system registry in tenant domain " + userTenantDomain + ". Requested tenant domain: " + requestedTenantDomain);
boolean tenantFlowStarted = false;
Registry registry;
RegistryHolder holder = new RegistryHolder();
try {
if (requestedTenantDomain != null) {
int id = getTenantManager().getTenantId(requestedTenantDomain);
RegistryPersistenceUtil.startTenantFlow(requestedTenantDomain);
tenantFlowStarted = true;
if (userTenantDomain != null && !userTenantDomain.equals(requestedTenantDomain)) {
// cross tenant
log.debug("Cross tenant user from tenant " + userTenantDomain + " accessing " + requestedTenantDomain + " registry");
loadTenantRegistry(id);
registry = getRegistryService().getGovernanceSystemRegistry(id);
holder.setTenantId(id);
ServiceReferenceHolder.setUserRealm((ServiceReferenceHolder.getInstance().getRealmService().getBootstrapRealm()));
} else {
log.debug("Same tenant accessing registry of tenant " + userTenantDomain + ":" + tenantId);
loadTenantRegistry(tenantId);
registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
holder.setTenantId(tenantId);
ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
}
} else {
log.debug("Same tenant user accessing registry of tenant " + userTenantDomain + ":" + tenantId);
loadTenantRegistry(tenantId);
registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
holder.setTenantId(tenantId);
}
} catch (RegistryException | UserStoreException | APIManagementException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
}
holder.setRegistry(registry);
holder.setTenantFlowStarted(tenantFlowStarted);
return holder;
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveAPIStatus.
/**
* Persist API Status into a property of API Registry resource
*
* @param artifactId API artifact ID
* @param apiStatus Current status of the API
* @throws APIManagementException on error
*/
private void saveAPIStatus(Registry registry, String artifactId, String apiStatus) throws APIManagementException {
try {
Resource resource = registry.get(artifactId);
if (resource != null) {
String propValue = resource.getProperty(APIConstants.API_STATUS);
if (propValue == null) {
resource.addProperty(APIConstants.API_STATUS, apiStatus);
} else {
resource.setProperty(APIConstants.API_STATUS, apiStatus);
}
registry.put(artifactId, resource);
}
} catch (RegistryException e) {
handleException("Error while adding API", e);
}
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method changeAPILifeCycle.
@Override
public void changeAPILifeCycle(Organization org, String apiId, String status) throws APIPersistenceException {
GenericArtifactManager artifactManager = null;
boolean isTenantFlowStarted = false;
try {
RegistryHolder holder = getRegistry(org.getName());
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
if (GovernanceUtils.findGovernanceArtifactConfiguration(APIConstants.API_KEY, registry) != null) {
artifactManager = new GenericArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
String action = LCManagerFactory.getInstance().getLCManager().getTransitionAction(apiArtifact.getLifecycleState().toUpperCase(), status.toUpperCase());
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
} else {
log.warn("Couldn't find GovernanceArtifactConfiguration of RXT: " + APIConstants.API_KEY + ". Tenant id set in registry : " + ((UserRegistry) registry).getTenantId() + ", Tenant domain set in PrivilegedCarbonContext: " + PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
}
} catch (GovernanceException e) {
throw new APIPersistenceException("Error while changing the lifecycle. ", e);
} catch (RegistryException e) {
throw new APIPersistenceException("Error while accessing the registry. ", e);
} catch (PersistenceException e) {
throw new APIPersistenceException("Error while accessing the lifecycle. ", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method addMediationPolicy.
@Override
public Mediation addMediationPolicy(Organization org, String apiId, Mediation mediation) throws MediationPolicyPersistenceException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
BasicAPI api = getbasicAPIInfo(apiId, registry);
if (api == null) {
throw new MediationPolicyPersistenceException("API not foud ", ExceptionCodes.API_NOT_FOUND);
}
String resourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + api.apiProvider + RegistryConstants.PATH_SEPARATOR + api.apiName + RegistryConstants.PATH_SEPARATOR + api.apiVersion + RegistryConstants.PATH_SEPARATOR + mediation.getType() + RegistryConstants.PATH_SEPARATOR + mediation.getName();
if (registry.resourceExists(resourcePath)) {
throw new MediationPolicyPersistenceException("Mediation policy already exists for the given name " + mediation.getName(), ExceptionCodes.MEDIATION_POLICY_API_ALREADY_EXISTS);
}
Resource policy = registry.newResource();
policy.setContent(mediation.getConfig());
policy.setMediaType("application/xml");
registry.put(resourcePath, policy);
mediation.setId(policy.getUUID());
return mediation;
} catch (RegistryException | APIPersistenceException e) {
String msg = "Error while adding the mediation to the registry";
throw new MediationPolicyPersistenceException(msg, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getThumbnail.
@Override
public ResourceFile getThumbnail(Organization org, String apiId) throws ThumbnailPersistenceException {
Registry registry;
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
if (apiArtifact == null) {
return null;
}
String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
int prependIndex = apiPath.lastIndexOf("/api");
String artifactPath = apiPath.substring(0, prependIndex);
String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
if (registry.resourceExists(thumbPath)) {
Resource res = registry.get(thumbPath);
return new ResourceFile(res.getContentStream(), res.getMediaType());
} else if (registry.resourceExists(oldThumbPath)) {
Resource res = registry.get(oldThumbPath);
return new ResourceFile(res.getContentStream(), res.getMediaType());
}
} catch (RegistryException | APIPersistenceException e) {
String msg = "Error while loading API icon of API " + apiId + " from the registry";
throw new ThumbnailPersistenceException(msg, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return null;
}
Aggregations