Search in sources :

Example 1 with PersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException 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();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with PersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException in project carbon-apimgt by wso2.

the class LCManagerFactory method getLCManager.

public RegistryLCManager getLCManager() throws PersistenceException {
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    String cacheName = tenantDomain + "_" + APIConstants.LC_CACHE_NAME;
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    Cache lcCache = Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.LC_CACHE_NAME);
    RegistryLCManager lcManager = (RegistryLCManager) lcCache.get(cacheName);
    if (lcManager != null) {
        log.debug("Lifecycle info servered from Cache.");
        return lcManager;
    } else {
        try {
            log.debug("Lifecycle info not found in Cache.");
            lcManager = new RegistryLCManager(tenantId);
            lcCache.put(cacheName, lcManager);
            return lcManager;
        } catch (RegistryException | XMLStreamException | ParserConfigurationException | SAXException | IOException e) {
            throw new PersistenceException("Error while accessing the lifecycle resource ", e);
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) RegistryLCManager(org.wso2.carbon.apimgt.persistence.utils.RegistryLCManager) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Cache(javax.cache.Cache) SAXException(org.xml.sax.SAXException)

Example 3 with PersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method getApiOrApiProductLifecycleData.

private Map<String, Object> getApiOrApiProductLifecycleData(String status) throws APIManagementException {
    Map<String, Object> lcData = new HashMap<String, Object>();
    List<String> actionsList;
    try {
        actionsList = LCManagerFactory.getInstance().getLCManager().getAllowedActionsForState(status);
        if (actionsList != null) {
            String[] actionsArray = new String[actionsList.size()];
            actionsArray = actionsList.toArray(actionsArray);
            lcData.put(APIConstants.LC_NEXT_STATES, actionsArray);
        }
        ArrayList<CheckListItem> checkListItems = new ArrayList<CheckListItem>();
        List<String> checklistItemsList = LCManagerFactory.getInstance().getLCManager().getCheckListItemsForState(status);
        if (checklistItemsList != null) {
            for (String name : checklistItemsList) {
                CheckListItem item = new CheckListItem();
                item.setName(name);
                item.setValue("false");
                checkListItems.add(item);
            }
        }
        lcData.put("items", checkListItems);
    } catch (PersistenceException e) {
        throw new APIManagementException("Error while parsing the lifecycle ", e);
    }
    // First letter capital
    status = status.substring(0, 1).toUpperCase() + status.substring(1).toLowerCase();
    lcData.put(APIConstants.LC_STATUS, status);
    return lcData;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CheckListItem(org.wso2.carbon.governance.custom.lifecycles.checklist.util.CheckListItem) ArrayList(java.util.ArrayList) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) JSONObject(org.json.simple.JSONObject)

Example 4 with PersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method changeLifeCycleStatus.

/**
 * This method is to change registry lifecycle states for an API or API Product artifact
 *
 * @param orgId          UUID of the organization
 * @param apiTypeWrapper API Type Wrapper
 * @param action         Action which need to execute from registry lifecycle
 * @param checklist      checklist items
 * @return APIStateChangeResponse API workflow state and WorkflowResponse
 */
@Override
public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTypeWrapper, String action, Map<String, Boolean> checklist) throws APIManagementException, FaultGatewaysException {
    APIStateChangeResponse response = new APIStateChangeResponse();
    try {
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
        String targetStatus;
        String providerName;
        String apiName;
        String apiContext;
        String apiType;
        String apiVersion;
        String currentStatus;
        String uuid;
        int apiOrApiProductId;
        boolean isApiProduct = apiTypeWrapper.isAPIProduct();
        String workflowType;
        if (isApiProduct) {
            APIProduct apiProduct = apiTypeWrapper.getApiProduct();
            providerName = apiProduct.getId().getProviderName();
            apiName = apiProduct.getId().getName();
            apiContext = apiProduct.getContext();
            apiType = apiProduct.getType();
            apiVersion = apiProduct.getId().getVersion();
            currentStatus = apiProduct.getState();
            uuid = apiProduct.getUuid();
            apiOrApiProductId = apiMgtDAO.getAPIProductId(apiTypeWrapper.getApiProduct().getId());
            workflowType = WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE;
        } else {
            API api = apiTypeWrapper.getApi();
            providerName = api.getId().getProviderName();
            apiName = api.getId().getApiName();
            apiContext = api.getContext();
            apiType = api.getType();
            apiVersion = api.getId().getVersion();
            currentStatus = api.getStatus();
            uuid = api.getUuid();
            apiOrApiProductId = apiMgtDAO.getAPIID(uuid);
            workflowType = WorkflowConstants.WF_TYPE_AM_API_STATE;
        }
        String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
        WorkflowStatus apiWFState = null;
        WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
        if (wfDTO != null) {
            apiWFState = wfDTO.getStatus();
        }
        // if the workflow has started, then executor should not fire again
        if (!WorkflowStatus.CREATED.equals(apiWFState)) {
            response = executeStateChangeWorkflow(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType);
            // get the workflow state once the executor is executed.
            wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
            if (wfDTO != null) {
                apiWFState = wfDTO.getStatus();
                response.setStateChangeStatus(apiWFState.toString());
            } else {
                response.setStateChangeStatus(WorkflowStatus.APPROVED.toString());
            }
        }
        // apiWFState is null when simple wf executor is used because wf state is not stored in the db.
        if (WorkflowStatus.APPROVED.equals(apiWFState) || apiWFState == null) {
            targetStatus = LCManagerFactory.getInstance().getLCManager().getStateForTransition(action);
            apiPersistenceInstance.changeAPILifeCycle(new Organization(orgId), uuid, targetStatus);
            sendLCStateChangeNotification(apiName, apiType, apiContext, apiVersion, targetStatus, providerName, apiOrApiProductId, uuid);
            if (!isApiProduct) {
                API api = apiTypeWrapper.getApi();
                api.setOrganization(orgId);
                changeLifeCycle(api, currentStatus, targetStatus, checklist);
                // Sending Notifications to existing subscribers
                if (APIConstants.PUBLISHED.equals(targetStatus)) {
                    sendEmailNotification(api);
                }
            } else {
                APIProduct apiProduct = apiTypeWrapper.getApiProduct();
                apiProduct.setOrganization(orgId);
                changeLifecycle(apiProduct, currentStatus, targetStatus);
            }
            addLCStateChangeInDatabase(currentStatus, targetStatus, uuid);
            if (log.isDebugEnabled()) {
                String logMessage = "LC Status changed successfully for artifact with name: " + apiName + ", version " + apiVersion + ", New Status : " + targetStatus;
                log.debug(logMessage);
            }
            extractRecommendationDetails(apiTypeWrapper);
            return response;
        }
    } catch (APIPersistenceException e) {
        handleException("Error while accessing persistence layer", e);
    } catch (PersistenceException e) {
        handleException("Error while accessing lifecycle information ", e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
    return response;
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) APIStateWorkflowDTO(org.wso2.carbon.apimgt.impl.workflow.APIStateWorkflowDTO) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) WorkflowStatus(org.wso2.carbon.apimgt.impl.workflow.WorkflowStatus)

Example 5 with PersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method addResourceFile.

protected String addResourceFile(String resourcePath, ResourceFile resourceFile, Registry registry, String tenantDomain) throws PersistenceException {
    try {
        Resource thumb = registry.newResource();
        thumb.setContentStream(resourceFile.getContent());
        thumb.setMediaType(resourceFile.getContentType());
        registry.put(resourcePath, thumb);
        if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
            return RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
        } else {
            return "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + RegistryConstants.PATH_SEPARATOR + "_system" + RegistryConstants.PATH_SEPARATOR + "governance" + resourcePath;
        }
    } catch (RegistryException e) {
        String msg = "Error while adding the resource to the registry";
        throw new PersistenceException(msg, e);
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

PersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException)8 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)6 AsyncSpecPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException)6 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)6 GraphQLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException)6 MediationPolicyPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException)6 OASPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException)6 ThumbnailPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException)6 WSDLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException)6 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)3 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)3 Registry (org.wso2.carbon.registry.core.Registry)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 IOException (java.io.IOException)2 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)2 Resource (org.wso2.carbon.registry.core.Resource)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1