Search in sources :

Example 86 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class APIProviderImpl method addAPIRevision.

/**
 * Adds a new APIRevision to an existing API
 *
 * @param apiRevision APIRevision
 * @throws APIManagementException if failed to add APIRevision
 */
@Override
public String addAPIRevision(APIRevision apiRevision, String organization) throws APIManagementException {
    int revisionCountPerAPI = apiMgtDAO.getRevisionCountByAPI(apiRevision.getApiUUID());
    int maxRevisionCount = getMaxRevisionCount(organization);
    if (revisionCountPerAPI >= maxRevisionCount) {
        String errorMessage = "Maximum number of revisions per API has reached. " + "Need to remove stale revision to create a new Revision for API with API UUID:" + apiRevision.getApiUUID();
        throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.MAXIMUM_REVISIONS_REACHED, apiRevision.getApiUUID()));
    }
    int revisionId = apiMgtDAO.getMostRecentRevisionId(apiRevision.getApiUUID()) + 1;
    apiRevision.setId(revisionId);
    APIIdentifier apiId = APIUtil.getAPIIdentifierFromUUID(apiRevision.getApiUUID());
    if (apiId == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiRevision.getApiUUID(), ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiRevision.getApiUUID()));
    }
    apiId.setUuid(apiRevision.getApiUUID());
    String revisionUUID;
    try {
        revisionUUID = apiPersistenceInstance.addAPIRevision(new Organization(organization), apiId.getUUID(), revisionId);
    } catch (APIPersistenceException e) {
        String errorMessage = "Failed to add revision registry artifacts";
        throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_CREATING_API_REVISION, apiRevision.getApiUUID()));
    }
    if (StringUtils.isEmpty(revisionUUID)) {
        String errorMessage = "Failed to retrieve revision uuid";
        throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.API_REVISION_UUID_NOT_FOUND));
    }
    apiRevision.setRevisionUUID(revisionUUID);
    apiMgtDAO.addAPIRevision(apiRevision);
    if (importExportAPI != null) {
        try {
            File artifact = importExportAPI.exportAPI(apiRevision.getApiUUID(), revisionUUID, true, ExportFormat.JSON, false, true, organization);
            // Keeping the organization as tenant domain since MG does not support organization-wise deployment
            // Artifacts will be deployed in ST for all organizations
            gatewayArtifactsMgtDAO.addGatewayAPIArtifactAndMetaData(apiRevision.getApiUUID(), apiId.getApiName(), apiId.getVersion(), apiRevision.getRevisionUUID(), tenantDomain, APIConstants.HTTP_PROTOCOL, artifact);
            if (artifactSaver != null) {
                // Keeping the organization as tenant domain since MG does not support organization-wise deployment
                // Artifacts will be deployed in ST for all organizations
                artifactSaver.saveArtifact(apiRevision.getApiUUID(), apiId.getApiName(), apiId.getVersion(), apiRevision.getRevisionUUID(), tenantDomain, artifact);
            }
        } catch (APIImportExportException | ArtifactSynchronizerException e) {
            throw new APIManagementException("Error while Store the Revision Artifact", ExceptionCodes.from(ExceptionCodes.API_REVISION_UUID_NOT_FOUND));
        }
    }
    return revisionUUID;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 87 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class APIProviderImpl method getAPIbyUUID.

@Override
public API getAPIbyUUID(String uuid, String organization) throws APIManagementException {
    Organization org = new Organization(organization);
    try {
        PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, uuid);
        if (publisherAPI != null) {
            API api = APIMapper.INSTANCE.toApi(publisherAPI);
            APIIdentifier apiIdentifier = api.getId();
            apiIdentifier.setUuid(uuid);
            api.setId(apiIdentifier);
            checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
            // ///////////////// Do processing on the data object//////////
            populateRevisionInformation(api, uuid);
            populateAPIInformation(uuid, organization, api);
            loadMediationPoliciesToAPI(api, organization);
            populateAPIStatus(api);
            populateDefaultVersion(api);
            return api;
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Failed to get API", e);
    } catch (OASPersistenceException e) {
        throw new APIManagementException("Error while retrieving the OAS definition", e);
    } catch (ParseException e) {
        throw new APIManagementException("Error while parsing the OAS definition", e);
    } catch (AsyncSpecPersistenceException e) {
        throw new APIManagementException("Error while retrieving the Async API definition", e);
    }
}
Also used : AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) 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) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) ParseException(org.json.simple.parser.ParseException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 88 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class APIProviderImpl method undeployAPIRevisionDeployment.

/**
 * Remove a new APIRevisionDeployment to an existing API
 *
 * @param apiId API UUID
 * @param apiRevisionId API Revision UUID
 * @param apiRevisionDeployments List of APIRevisionDeployment objects
 * @param organization
 * @throws APIManagementException if failed to add APIRevision
 */
@Override
public void undeployAPIRevisionDeployment(String apiId, String apiRevisionId, List<APIRevisionDeployment> apiRevisionDeployments, String organization) throws APIManagementException {
    APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
    if (apiIdentifier == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
    }
    APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
    if (apiRevision == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
    }
    API api = getAPIbyUUID(apiId, apiRevision, organization);
    removeFromGateway(api, new HashSet<>(apiRevisionDeployments), Collections.emptySet());
    apiMgtDAO.removeAPIRevisionDeployment(apiRevisionId, apiRevisionDeployments);
    GatewayArtifactsMgtDAO.getInstance().removePublishedGatewayLabels(apiId, apiRevisionId);
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) 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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 89 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization 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;
}
Also used : Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) APINameComparator(org.wso2.carbon.apimgt.impl.utils.APINameComparator) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TreeSet(java.util.TreeSet) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) PublisherContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherContentSearchResult) JSONObject(org.json.simple.JSONObject) 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) ContentSearchResultNameComparator(org.wso2.carbon.apimgt.impl.utils.ContentSearchResultNameComparator) APIProductNameComparator(org.wso2.carbon.apimgt.impl.utils.APIProductNameComparator)

Example 90 with Organization

use of org.wso2.carbon.apimgt.persistence.dto.Organization in project carbon-apimgt by wso2.

the class APIProviderImpl method getLightweightAPIByUUID.

/**
 * Get minimal details of API by registry artifact id
 *
 * @param uuid Registry artifact id
 * @param organization identifier of the organization
 * @return API of the provided artifact id
 * @throws APIManagementException
 */
@Override
public API getLightweightAPIByUUID(String uuid, String organization) throws APIManagementException {
    try {
        Organization org = new Organization(organization);
        PublisherAPI publisherAPI = apiPersistenceInstance.getPublisherAPI(org, uuid);
        if (publisherAPI != null) {
            API api = APIMapper.INSTANCE.toApi(publisherAPI);
            checkAccessControlPermission(userNameWithoutChange, api.getAccessControl(), api.getAccessControlRoles());
            // / populate relavant external info
            // environment
            String environmentString = null;
            if (api.getEnvironments() != null) {
                environmentString = String.join(",", api.getEnvironments());
            }
            api.setEnvironments(APIUtil.extractEnvironmentsForAPI(environmentString, organization));
            // CORS . if null is returned, set default config from the configuration
            if (api.getCorsConfiguration() == null) {
                api.setCorsConfiguration(APIUtil.getDefaultCorsConfiguration());
            }
            api.setOrganization(organization);
            return api;
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException e) {
        String msg = "Failed to get API with uuid " + uuid;
        throw new APIManagementException(msg, e);
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) 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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)304 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)106 API (org.wso2.carbon.apimgt.api.model.API)100 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)89 ArrayList (java.util.ArrayList)79 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)72 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)70 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)65 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)64 IOException (java.io.IOException)61 Registry (org.wso2.carbon.registry.core.Registry)58 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)57 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)56 HashMap (java.util.HashMap)54 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)53 Resource (org.wso2.carbon.registry.core.Resource)51 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)49 JSONObject (org.json.simple.JSONObject)45 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 URISyntaxException (java.net.URISyntaxException)42