use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchAPIProductsForPublisher.
@Override
public PublisherAPIProductSearchResult searchAPIProductsForPublisher(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
String requestedTenantDomain = org.getName();
boolean isTenantFlowStarted = false;
PublisherAPIProductSearchResult result = new PublisherAPIProductSearchResult();
try {
RegistryHolder holder = getRegistry(ctx.getUserame(), requestedTenantDomain);
Registry userRegistry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
log.debug("Requested query for publisher product search: " + searchQuery);
String modifiedQuery = RegistrySearchUtil.getPublisherProductSearchQuery(searchQuery, ctx);
log.debug("Modified query for publisher product search: " + modifiedQuery);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ctx.getUserame());
final int maxPaginationLimit = getMaxPaginationLimit();
PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
List<GovernanceArtifact> governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(modifiedQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE, true);
int totalLength = PaginationContext.getInstance().getLength();
// Check to see if we can speculate that there are more APIs to be loaded
if (maxPaginationLimit == totalLength) {
// Remove the additional 1 added earlier when setting max pagination limit
--totalLength;
}
int tempLength = 0;
List<PublisherAPIProductInfo> publisherAPIProductInfoList = new ArrayList<PublisherAPIProductInfo>();
for (GovernanceArtifact artifact : governanceArtifacts) {
PublisherAPIProductInfo info = new PublisherAPIProductInfo();
info.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
info.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT));
info.setId(artifact.getId());
info.setApiProductName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
info.setState(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
info.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
info.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
info.setApiSecurity(artifact.getAttribute(APIConstants.API_OVERVIEW_API_SECURITY));
publisherAPIProductInfoList.add(info);
// Ensure the APIs returned matches the length, there could be an additional API
// returned due incrementing the pagination limit when getting from registry
tempLength++;
if (tempLength >= totalLength) {
break;
}
}
result.setPublisherAPIProductInfoList(publisherAPIProductInfoList);
result.setReturnedAPIsCount(publisherAPIProductInfoList.size());
result.setTotalAPIsCount(totalLength);
} catch (GovernanceException e) {
throw new APIPersistenceException("Error while searching APIs ", e);
} finally {
PaginationContext.destroy();
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return result;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException 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.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getbasicAPIInfo.
private BasicAPI getbasicAPIInfo(String uuid, Registry registry) throws APIPersistenceException, GovernanceException {
BasicAPI api = new BasicAPI();
GenericArtifact apiArtifact = getAPIArtifact(uuid, registry);
if (apiArtifact == null) {
return null;
}
String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
api.apiProvider = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
api.apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
api.apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
if (visibleRolesList != null) {
api.visibleRoles = visibleRolesList.split(",");
}
api.visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
return api;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException 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.apimgt.persistence.exceptions.APIPersistenceException 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();
}
}
}
Aggregations