use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method changeAPILCCheckListItems.
@Override
public boolean changeAPILCCheckListItems(APIIdentifier apiIdentifier, int checkItem, boolean checkItemValue) throws APIManagementException {
String providerTenantMode = apiIdentifier.getProviderName();
boolean success = false;
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
GenericArtifact apiArtifact = getAPIArtifact(apiIdentifier);
String status = null;
try {
if (apiArtifact != null) {
if (checkItemValue && !apiArtifact.isLCItemChecked(checkItem, APIConstants.API_LIFE_CYCLE)) {
apiArtifact.checkLCItem(checkItem, APIConstants.API_LIFE_CYCLE);
} else if (!checkItemValue && apiArtifact.isLCItemChecked(checkItem, APIConstants.API_LIFE_CYCLE)) {
apiArtifact.uncheckLCItem(checkItem, APIConstants.API_LIFE_CYCLE);
}
success = true;
}
} catch (GovernanceException e) {
handleException("Error while setting registry lifecycle checklist items for the API: " + apiIdentifier.getApiName(), e);
}
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return success;
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method copyAllDocumentation.
/**
* Copies current Documentation into another version of the same API.
*
* @param toVersion Version to which Documentation should be copied.
* @param apiId id of the APIIdentifier
* @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to copy docs
*/
public void copyAllDocumentation(APIIdentifier apiId, String toVersion) throws APIManagementException {
String oldVersion = APIUtil.getAPIDocPath(apiId);
String newVersion = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiId.getProviderName() + RegistryConstants.PATH_SEPARATOR + apiId.getApiName() + RegistryConstants.PATH_SEPARATOR + toVersion + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR;
try {
Resource resource = registry.get(oldVersion);
if (resource instanceof org.wso2.carbon.registry.core.Collection) {
String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
for (String docPath : docsPaths) {
registry.copy(docPath, newVersion);
}
}
} catch (RegistryException e) {
handleException("Failed to copy docs to new version : " + newVersion, e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method getCustomSequence.
/**
* Get the resource which matches the user selected resource type and the name from the custom uploaded sequences.
*
* @param identifier : The API Identifier.
* @param type : The sequence type.
* @return : Resource object which matches the parameters. If no resource found, return null.
*/
private Resource getCustomSequence(APIIdentifier identifier, String type, String name) throws APIManagementException {
Resource customSequence = null;
boolean isTenantFlowStarted = false;
try {
String tenantDomain = null;
if (identifier.getProviderName().contains("-AT-")) {
String provider = identifier.getProviderName().replace("-AT-", "@");
tenantDomain = MultitenantUtils.getTenantDomain(provider);
}
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
PrivilegedCarbonContext.startTenantFlow();
isTenantFlowStarted = true;
}
if (!StringUtils.isEmpty(tenantDomain)) {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
} else {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(tenantId);
String customSeqFileLocation = "";
if (APIConstants.FAULT_SEQUENCE.equals(type)) {
customSeqFileLocation = APIUtil.getSequencePath(identifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_FAULT);
} else if (APIConstants.OUT_SEQUENCE.equals(type)) {
customSeqFileLocation = APIUtil.getSequencePath(identifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_OUT);
} else {
customSeqFileLocation = APIUtil.getSequencePath(identifier, APIConstants.API_CUSTOM_SEQUENCE_TYPE_IN);
}
if (registry.resourceExists(customSeqFileLocation)) {
org.wso2.carbon.registry.api.Collection customSeqCollection = (org.wso2.carbon.registry.api.Collection) registry.get(customSeqFileLocation);
if (customSeqCollection != null) {
String[] faultSeqChildPaths = customSeqCollection.getChildren();
for (String customSeqChildPath : faultSeqChildPaths) {
customSequence = registry.get(customSeqChildPath);
OMElement seqElement = APIUtil.buildOMElement(customSequence.getContentStream());
if (name.equals(seqElement.getAttributeValue(new QName("name")))) {
return customSequence;
}
}
}
}
} catch (RegistryException e) {
throw new APIManagementException("Error while retrieving registry for tenant " + tenantId, e);
} catch (org.wso2.carbon.registry.api.RegistryException e) {
throw new APIManagementException("Error while processing the " + type + " sequences of " + identifier + " in the registry", e);
} catch (Exception e) {
throw new APIManagementException("Error while building the OMElement from the sequence " + name, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPILifeCycleData.
@Override
public /*
* This method returns the lifecycle data for an API including current state,next states.
*
* @param apiId APIIdentifier
* @return Map<String,Object> a map with lifecycle data
*/
Map<String, Object> getAPILifeCycleData(APIIdentifier apiId) throws APIManagementException {
String path = APIUtil.getAPIPath(apiId);
Map<String, Object> lcData = new HashMap<String, Object>();
String providerTenantMode = apiId.getProviderName();
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(providerTenantMode));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
Resource apiSourceArtifact = registry.get(path);
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when getting lifecycle data for API " + apiId;
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifact artifact = artifactManager.getGenericArtifact(apiSourceArtifact.getUUID());
// Get all the actions corresponding to current state of the api artifact
String[] actions = artifact.getAllLifecycleActions(APIConstants.API_LIFE_CYCLE);
// Put next states into map
lcData.put(APIConstants.LC_NEXT_STATES, actions);
String lifeCycleState = artifact.getLifecycleState();
lcData.put(APIConstants.LC_STATUS, lifeCycleState);
LifecycleBean bean;
bean = LifecycleBeanPopulator.getLifecycleBean(path, (UserRegistry) registry, configRegistry);
if (bean != null) {
ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
ArrayList<String> permissionList = new ArrayList<String>();
// Get lc properties
Property[] lifecycleProps = bean.getLifecycleProperties();
// Get roles of the current session holder
String[] roleNames = bean.getRolesOfUser();
for (Property property : lifecycleProps) {
String propName = property.getKey();
String[] propValues = property.getValues();
// Check for permission properties if any exists
if (propValues != null && propValues.length != 0) {
if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
for (String role : roleNames) {
for (String propValue : propValues) {
String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX, "");
if (propValue.equals(role)) {
permissionList.add(key);
} else if (propValue.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propValue.endsWith(APIConstants.LC_PROPERTY_PERMISSION_SUFFIX)) {
permissionList.add(key);
}
}
}
}
}
}
// Check for lifecycle checklist item properties defined
for (Property property : lifecycleProps) {
String propName = property.getKey();
String[] propValues = property.getValues();
if (propValues != null && propValues.length != 0) {
CheckListItem checkListItem = new CheckListItem();
checkListItem.setVisible("false");
if (propName.startsWith(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX) && propName.endsWith(APIConstants.LC_PROPERTY_ITEM_SUFFIX) && propName.contains(APIConstants.API_LIFE_CYCLE)) {
if (propValues.length > 2) {
for (String param : propValues) {
if (param.startsWith(APIConstants.LC_STATUS)) {
checkListItem.setLifeCycleStatus(param.substring(7));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_NAME)) {
checkListItem.setName(param.substring(5));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_VALUE)) {
checkListItem.setValue(param.substring(6));
} else if (param.startsWith(APIConstants.LC_CHECK_ITEM_ORDER)) {
checkListItem.setOrder(param.substring(6));
}
}
}
String key = propName.replace(APIConstants.LC_PROPERTY_CHECKLIST_PREFIX, "").replace(APIConstants.LC_PROPERTY_ITEM_SUFFIX, "");
if (permissionList.contains(key)) {
// Set visible to true if the checklist item permits
checkListItem.setVisible("true");
}
}
if (checkListItem.matchLifeCycleStatus(lifeCycleState)) {
checkListItems.add(checkListItem);
}
}
}
lcData.put("items", checkListItems);
}
} catch (Exception e) {
handleException(e.getMessage(), e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return lcData;
}
use of org.wso2.carbon.apimgt.api.model.APIIdentifier in project carbon-apimgt by wso2.
the class APIProviderImpl method searchPaginatedContent.
@Override
public Map<String, Object> searchPaginatedContent(String searchQuery, String organization, int start, int end) throws APIManagementException {
ArrayList<Object> compoundResult = new ArrayList<Object>();
Map<Documentation, API> docMap = new HashMap<Documentation, API>();
Map<Documentation, APIProduct> productDocMap = new HashMap<Documentation, APIProduct>();
Map<String, Object> result = new HashMap<String, Object>();
SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
SortedSet<APIProduct> apiProductSet = new TreeSet<APIProduct>(new APIProductNameComparator());
String userame = userNameWithoutChange;
Organization org = new Organization(organization);
Map<String, Object> properties = APIUtil.getUserProperties(userame);
String[] roles = APIUtil.getFilteredUserRoles(userame);
UserContext ctx = new UserContext(userame, org, properties, roles);
try {
PublisherContentSearchResult results = apiPersistenceInstance.searchContentForPublisher(org, searchQuery, start, end, ctx);
if (results != null) {
List<SearchContent> resultList = results.getResults();
for (SearchContent item : resultList) {
if ("API".equals(item.getType())) {
PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
API api = new API(new APIIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
api.setUuid(publiserAPI.getId());
api.setContext(publiserAPI.getContext());
api.setContextTemplate(publiserAPI.getContext());
api.setStatus(publiserAPI.getStatus());
apiSet.add(api);
} else if ("APIProduct".equals(item.getType())) {
PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
APIProduct api = new APIProduct(new APIProductIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
api.setUuid(publiserAPI.getId());
api.setContextTemplate(publiserAPI.getContext());
api.setState(publiserAPI.getStatus());
apiProductSet.add(api);
} else if (item instanceof DocumentSearchContent) {
// doc item
DocumentSearchContent docItem = (DocumentSearchContent) item;
Documentation doc = new Documentation(DocumentationType.valueOf(docItem.getDocType().toString()), docItem.getName());
doc.setSourceType(DocumentSourceType.valueOf(docItem.getSourceType().toString()));
doc.setVisibility(DocumentVisibility.valueOf(docItem.getVisibility().toString()));
doc.setId(docItem.getId());
if ("API".equals(docItem.getAssociatedType())) {
API api = new API(new APIIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
api.setUuid(docItem.getApiUUID());
docMap.put(doc, api);
} else if ("APIProduct".equals(docItem.getAssociatedType())) {
APIProduct api = new APIProduct(new APIProductIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
api.setUuid(docItem.getApiUUID());
productDocMap.put(doc, api);
}
}
}
compoundResult.addAll(apiSet);
compoundResult.addAll(apiProductSet);
compoundResult.addAll(docMap.entrySet());
compoundResult.addAll(productDocMap.entrySet());
compoundResult.sort(new ContentSearchResultNameComparator());
result.put("length", results.getTotalCount());
} else {
result.put("length", compoundResult.size());
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while searching content ", e);
}
result.put("apis", compoundResult);
return result;
}
Aggregations