Search in sources :

Example 1 with APIDetails

use of org.wso2.carbon.apimgt.core.models.APIDetails in project carbon-apimgt by wso2.

the class APIImportExportTestCase method testApiExport.

private void testApiExport(String exportDir) throws Exception {
    String api1Id = UUID.randomUUID().toString();
    Endpoint api1SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api1ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api1 = createApi("provider1", api1Id, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", createEndpointTypeToIdMap(api1SandBoxEndpointId, api1ProdEndpointId)).build();
    String api1Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api1Doc1Info = createAPIDoc(api1Doc1Id, "api1doc1", "", "API 1 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api1Doc2Id = UUID.randomUUID().toString();
    DocumentInfo api1Doc2Info = createAPIDoc(api1Doc2Id, "api1doc2.pdf", "api1doc2.pdf", "API 1 DOC 2", DocumentInfo.DocType.PUBLIC_FORUM, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.API_LEVEL);
    String api1Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api1Doc3Info = createAPIDoc(api1Doc3Id, "api1doc3", "", "API 1 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    Set<DocumentInfo> api1DocumentInfo = new HashSet<>();
    api1DocumentInfo.add(api1Doc1Info);
    api1DocumentInfo.add(api1Doc2Info);
    api1DocumentInfo.add(api1Doc3Info);
    // contents for documents
    DocumentContent api1Doc1Content = createDocContent(api1Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api1Doc2Content = createDocContent(api1Doc2Info, "", api1Doc2Stream);
    DocumentContent api1Doc3Content = createDocContent(api1Doc3Info, "", null);
    Set<DocumentContent> api1DocContent = new HashSet<>();
    api1DocContent.add(api1Doc1Content);
    api1DocContent.add(api1Doc2Content);
    api1DocContent.add(api1Doc3Content);
    APIDetails api1Details = new APIDetails(api1, api1Definition);
    api1Details.setGatewayConfiguration(api1GatewayConfig);
    api1Details.addDocumentInformation(api1DocumentInfo);
    api1Details.addDocumentContents(api1DocContent);
    api1Details.setThumbnailStream(getClass().getClassLoader().getResourceAsStream("api1_thumbnail.png"));
    Endpoint api1SandboxEndpoint = createEndpoint(api1SandBoxEndpointId.getId(), "api1SandBoxEndpoint", "SANDBOX", "{'type':'http','url':'http://localhost:8280'}", "{'enabled':'true','type':'basic','properties':{'username':'admin','password':'admin'}}", 10l);
    Endpoint api1ProdEndpoint = createEndpoint(api1ProdEndpointId.getId(), "api1ProdEndpoint", "PRODUCTION", "{'type':'http','url':'http://localhost:8280'}", "{'enabled':'true','type':'basic','properties':{'username':'admin','password':'admin'}}", 10l);
    api1Details.addEndpoint(api1SandboxEndpoint);
    api1Details.addEndpoint(api1ProdEndpoint);
    String api2Id = UUID.randomUUID().toString();
    Endpoint api2SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api2ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api2 = createApi("provider2", api2Id, "testapi2", "3.0.0", "Test API 2 - version 3.0.0", createEndpointTypeToIdMap(api2SandBoxEndpointId, api2ProdEndpointId)).build();
    List<API> apis = new ArrayList<>();
    apis.add(api1);
    apis.add(api2);
    String api2Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api2Doc1Info = createAPIDoc(api2Doc1Id, "api2doc1", "", "API 2 DOC 1", DocumentInfo.DocType.API_MESSAGE_FORMAT, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.API_LEVEL);
    String api2Doc2Id = UUID.randomUUID().toString();
    DocumentInfo api2Doc2Info = createAPIDoc(api2Doc2Id, "api2doc2", "", "API 2 DOC 2", DocumentInfo.DocType.PUBLIC_FORUM, "other type", DocumentInfo.SourceType.URL, "http://api2.org/documentation/1", DocumentInfo.Visibility.PRIVATE);
    Set<DocumentInfo> api2DocumentInfo = new HashSet<>();
    api2DocumentInfo.add(api2Doc1Info);
    api2DocumentInfo.add(api2Doc2Info);
    DocumentContent api2Doc1Content = createDocContent(api2Doc1Info, "Sample inline content for API2 DOC 1", null);
    DocumentContent api2Doc2Content = createDocContent(api2Doc2Info, "", null);
    Set<DocumentContent> api2DocContent = new HashSet<>();
    api2DocContent.add(api2Doc1Content);
    api2DocContent.add(api2Doc2Content);
    APIDetails api2Details = new APIDetails(api2, api2Definition);
    api2Details.setGatewayConfiguration(api2GatewayConfig);
    api2Details.addDocumentInformation(api2DocumentInfo);
    api2Details.addDocumentContents(api2DocContent);
    api2Details.setThumbnailStream(getClass().getClassLoader().getResourceAsStream("api2_thumbnail.jpg"));
    Endpoint api2SandboxEndpoint = createEndpoint(api2SandBoxEndpointId.getId(), "api2SandBoxEndpoint", "SANDBOX", "{'type':'http','url':'http://localhost:8280'}", "{'enabled':'true','type':'basic','properties':{'username':'admin','password':'admin'}}", 20l);
    Endpoint api2ProdEndpoint = createEndpoint(api2ProdEndpointId.getId(), "api2ProdEndpoint", "PRODUCTION", "{'type':'http','url':'http://localhost:8280'}", "{'enabled':'true','type':'basic','properties':{'username':'admin','password':'admin'}}", 20l);
    api2Details.addEndpoint(api2SandboxEndpoint);
    api2Details.addEndpoint(api2ProdEndpoint);
    Set<APIDetails> apiDetailsSet = new HashSet<>();
    apiDetailsSet.add(api1Details);
    apiDetailsSet.add(api2Details);
    // mock the method calls for retrieving APIs
    Mockito.when(apiPublisher.getAPIbyUUID(api1Id)).thenReturn(api1);
    Mockito.when(apiPublisher.getAPIbyUUID(api2Id)).thenReturn(api2);
    // export
    FileBasedApiImportExportManager importExportManager = new FileBasedApiImportExportManager(apiPublisher, exportDir);
    String exportedApiDirName = "exported-apis";
    String exportedApiDirPath = importExportManager.exportAPIs(apiDetailsSet, exportedApiDirName);
    String exportedApiArchiveFilePath = importExportManager.createArchiveFromExportedApiArtifacts(exportedApiDirPath, exportDir, exportedApiDirName);
    // check if two APIs are written to the file system
    String unzipPath = importExportRootDirectory + File.separator + "unzipped-export-archive";
    APIFileUtils.extractArchive(exportedApiArchiveFilePath, unzipPath);
    Assert.assertEquals(APIFileUtils.getDirectoryList(unzipPath).size() == 2, true, "Exported API count is not equal to 2");
    Mockito.when(apiPublisher.isAPIExists(api2Id)).thenReturn(true);
}
Also used : APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) HashSet(java.util.HashSet)

Example 2 with APIDetails

use of org.wso2.carbon.apimgt.core.models.APIDetails in project carbon-apimgt by wso2.

the class APIImportExportTestCase method testGetMultipleApiDetailsWithFatalErrors.

@Test(description = "Test getAPIDetails - multiple APIs with critical error in retrieving information of one API")
void testGetMultipleApiDetailsWithFatalErrors() throws APIManagementException {
    printTestMethodName();
    apiPublisher = Mockito.mock(APIPublisher.class);
    String api6Id = UUID.randomUUID().toString();
    Endpoint api6SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api6ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api6 = createApi("provider4", api6Id, "testapi6", "1.0.0", "Test API 6 - version 1.0.0", createEndpointTypeToIdMap(api6SandBoxEndpointId, api6ProdEndpointId)).build();
    String api6Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api6Doc1Info = createAPIDoc(api6Doc1Id, "api1doc1", "", "API 6 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api6Doc2Id = UUID.randomUUID().toString();
    DocumentInfo api6Doc2Info = createAPIDoc(api6Doc2Id, "api1doc2.pdf", "api1doc2.pdf", "API 4 DOC 2", DocumentInfo.DocType.PUBLIC_FORUM, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.API_LEVEL);
    String api6Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api6Doc3Info = createAPIDoc(api6Doc3Id, "api1doc3", "", "API 6 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    List<DocumentInfo> api1DocumentInfo = new ArrayList<>();
    api1DocumentInfo.add(api6Doc1Info);
    api1DocumentInfo.add(api6Doc2Info);
    api1DocumentInfo.add(api6Doc3Info);
    // contents for documents
    DocumentContent api6Doc1Content = createDocContent(api6Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api6Doc2Content = createDocContent(api6Doc2Info, "", api1Doc2Stream);
    DocumentContent api6Doc3Content = createDocContent(api6Doc3Info, "", null);
    Mockito.when(apiPublisher.getAPIbyUUID(api6Id)).thenReturn(api6);
    Mockito.when(apiPublisher.getApiSwaggerDefinition(api6Id)).thenReturn(api1Definition);
    Mockito.when(apiPublisher.getApiGatewayConfig(api6Id)).thenReturn(api1GatewayConfig);
    Mockito.when(apiPublisher.getAllDocumentation(api6Id, 0, Integer.MAX_VALUE)).thenReturn(api1DocumentInfo);
    Mockito.when(apiPublisher.getDocumentationContent(api6Doc1Id)).thenReturn(api6Doc1Content);
    Mockito.when(apiPublisher.getDocumentationContent(api6Doc2Id)).thenReturn(api6Doc2Content);
    Mockito.when(apiPublisher.getDocumentationContent(api6Doc3Id)).thenReturn(api6Doc3Content);
    Mockito.when(apiPublisher.getThumbnailImage(api6Id)).thenReturn(getClass().getClassLoader().getResourceAsStream("api1_thumbnail.png"));
    String api7Id = UUID.randomUUID().toString();
    Endpoint api7SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api7ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api7 = createApi("provider5", api7Id, "testapi4", "1.0.0", "Test API 7 - version 1.0.0", createEndpointTypeToIdMap(api7SandBoxEndpointId, api7ProdEndpointId)).build();
    String api7Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api7Doc1Info = createAPIDoc(api7Doc1Id, "api1doc1", "", "API 7 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api7Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api7Doc3Info = createAPIDoc(api7Doc3Id, "api1doc3", "", "API 7 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    List<DocumentInfo> api7DocumentInfo = new ArrayList<>();
    api7DocumentInfo.add(api7Doc1Info);
    api7DocumentInfo.add(api7Doc3Info);
    // contents for documents
    DocumentContent api7Doc1Content = createDocContent(api7Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api7Doc3Content = createDocContent(api7Doc3Info, "", null);
    Mockito.when(apiPublisher.getAPIbyUUID(api7Id)).thenReturn(api7);
    Mockito.when(apiPublisher.getApiSwaggerDefinition(api7Id)).thenReturn(api1Definition);
    Mockito.when(apiPublisher.getApiGatewayConfig(api7Id)).thenReturn(api1GatewayConfig);
    Mockito.when(apiPublisher.getAllDocumentation(api7Id, 0, Integer.MAX_VALUE)).thenReturn(api7DocumentInfo);
    Mockito.when(apiPublisher.getDocumentationContent(api7Doc1Id)).thenReturn(api7Doc1Content);
    Mockito.when(apiPublisher.getDocumentationContent(api7Doc3Id)).thenReturn(api7Doc3Content);
    Mockito.when(apiPublisher.getThumbnailImage(api7Id)).thenReturn(getClass().getClassLoader().getResourceAsStream("api1_thumbnail.png"));
    Mockito.when(apiPublisher.getApiSwaggerDefinition(api7Id)).thenThrow(APIManagementException.class);
    List<API> apis = new ArrayList<>();
    apis.add(api6);
    apis.add(api7);
    Mockito.when(apiPublisher.searchAPIs(Integer.MAX_VALUE, 0, "*")).thenReturn(apis);
    ApiImportExportManager importExportManager = new ApiImportExportManager(apiPublisher);
    Set<APIDetails> apiDetailsSet = importExportManager.getAPIDetails(Integer.MAX_VALUE, 0, "*");
    Assert.assertEquals(apiDetailsSet.size() == 1, true, "Error getting API details");
}
Also used : APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) API(org.wso2.carbon.apimgt.core.models.API) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) Test(org.testng.annotations.Test)

Example 3 with APIDetails

use of org.wso2.carbon.apimgt.core.models.APIDetails in project carbon-apimgt by wso2.

the class APIImportExportTestCase method testUpdateApiDetails.

@Test(description = "Test updateAPIDetails")
void testUpdateApiDetails() throws APIManagementException {
    printTestMethodName();
    apiPublisher = Mockito.mock(APIPublisher.class);
    String api2Id = UUID.randomUUID().toString();
    Endpoint api2SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
    Endpoint api2ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
    API api2 = createApi("provider1", api2Id, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", createEndpointTypeToIdMap(api2SandBoxEndpointId, api2ProdEndpointId)).build();
    String api2Doc1Id = UUID.randomUUID().toString();
    DocumentInfo api2Doc1Info = createAPIDoc(api2Doc1Id, "api1doc1", "", "API 2 DOC 1", DocumentInfo.DocType.HOWTO, "other type", DocumentInfo.SourceType.INLINE, "", DocumentInfo.Visibility.PRIVATE);
    String api2Doc2Id = UUID.randomUUID().toString();
    DocumentInfo api2Doc2Info = createAPIDoc(api2Doc2Id, "api1doc2.pdf", "api1doc2.pdf", "API 2 DOC 2", DocumentInfo.DocType.PUBLIC_FORUM, "other type", DocumentInfo.SourceType.FILE, "", DocumentInfo.Visibility.API_LEVEL);
    String api2Doc3Id = UUID.randomUUID().toString();
    DocumentInfo api2Doc3Info = createAPIDoc(api2Doc3Id, "api1doc3", "", "API 2 DOC 3", DocumentInfo.DocType.OTHER, "other type", DocumentInfo.SourceType.OTHER, "", DocumentInfo.Visibility.API_LEVEL);
    Set<DocumentInfo> api2DocumentInfo = new HashSet<>();
    api2DocumentInfo.add(api2Doc1Info);
    api2DocumentInfo.add(api2Doc2Info);
    api2DocumentInfo.add(api2Doc3Info);
    // contents for documents
    DocumentContent api2Doc1Content = createDocContent(api2Doc1Info, "Sample inline content for API1 DOC 1", null);
    DocumentContent api2Doc2Content = createDocContent(api2Doc2Info, "", api1Doc2Stream);
    DocumentContent api2Doc3Content = createDocContent(api2Doc3Info, "", null);
    Set<DocumentContent> api2DocContents = new HashSet<>();
    api2DocContents.add(api2Doc1Content);
    api2DocContents.add(api2Doc2Content);
    api2DocContents.add(api2Doc3Content);
    APIDetails api2Details = new APIDetails(api2, api2Definition);
    api2Details.setGatewayConfiguration(api2GatewayConfig);
    api2Details.addDocumentInformation(api2DocumentInfo);
    api2Details.addDocumentContents(api2DocContents);
    api2Details.setThumbnailStream(getClass().getClassLoader().getResourceAsStream("api2_thumbnail.jpg"));
    ApiImportExportManager importExportManager = new ApiImportExportManager(apiPublisher);
    importExportManager.addAPIDetails(api2Details);
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) API(org.wso2.carbon.apimgt.core.models.API) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with APIDetails

use of org.wso2.carbon.apimgt.core.models.APIDetails in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method decodeApiInformationFromDirectoryStructure.

/**
 * Reads and decodes APIs and relevant information from the given set of paths
 *
 * @param apiArtifactsBasePath path to the directory with API related artifacts
 * @param newApiProvider       API newApiProvider to be updated
 * @return Set of {@link APIDetails} objects
 * @throws APIMgtEntityImportExportException if any error occurs while decoding the APIs
 */
public Set<APIDetails> decodeApiInformationFromDirectoryStructure(String apiArtifactsBasePath, String newApiProvider) throws APIMgtEntityImportExportException {
    Set<String> apiDefinitionsRootDirectoryPaths = null;
    try {
        apiDefinitionsRootDirectoryPaths = APIFileUtils.getDirectoryList(apiArtifactsBasePath);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    if (apiDefinitionsRootDirectoryPaths.isEmpty()) {
        try {
            APIFileUtils.deleteDirectory(path);
        } catch (APIMgtDAOException e) {
            log.warn("Unable to remove directory " + path);
        }
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBasePath;
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    Set<APIDetails> apiDetailsSet = new HashSet<>();
    for (String apiDefinitionDirectoryPath : apiDefinitionsRootDirectoryPaths) {
        File apiDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX);
        File swaggerDefinitionFile = getFileFromPrefix(apiDefinitionDirectoryPath, APIMgtConstants.APIFileUtilConstants.SWAGGER_DEFINITION_FILE_PREFIX);
        API api;
        String swaggerDefinition, gatewayConfiguration;
        Set<Endpoint> endpoints;
        try {
            api = getApiDefinitionFromExtractedArchive(apiDefinitionFile.getPath());
            swaggerDefinition = getSwaggerDefinitionFromExtractedArchive(swaggerDefinitionFile.getPath());
            gatewayConfiguration = getGatewayConfigurationFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.GATEWAY_CONFIGURATION_DEFINITION_FILE);
            endpoints = getEndpointsFromExtractedArchive(apiDefinitionDirectoryPath + File.separator + ENDPOINTS_ROOT_DIRECTORY, api.getName(), api.getVersion());
        } catch (APIManagementException e) {
            log.error("Error occurred while importing api from path: " + apiDefinitionDirectoryPath, e);
            // skip this API
            continue;
        }
        if (newApiProvider != null && !newApiProvider.isEmpty()) {
            // update the newApiProvider
            api = new API.APIBuilder(api).provider(newApiProvider).build();
        }
        String documentsRootDirectory = apiDefinitionDirectoryPath + File.separator + DOCUMENTS_ROOT_DIRECTORY;
        Set<DocumentInfo> documentInfoSet = getDocumentInfoFromExtractedArchive(documentsRootDirectory, api.getName(), api.getVersion());
        Set<DocumentContent> documentContents = new HashSet<>();
        for (DocumentInfo aDocumentInfo : documentInfoSet) {
            DocumentContent aDocumentContent = getDocumentContentFromExtractedArchive(aDocumentInfo, documentsRootDirectory + File.separator + aDocumentInfo.getId());
            if (aDocumentContent != null) {
                documentContents.add(aDocumentContent);
            }
        }
        InputStream thumbnailStream = null;
        try {
            thumbnailStream = APIFileUtils.getThumbnailImage(apiDefinitionDirectoryPath + File.separator + APIMgtConstants.APIFileUtilConstants.THUMBNAIL_FILE_NAME);
        } catch (APIMgtDAOException e) {
            // log and ignore
            log.error("Error occurred while reading thumbnail image.", e);
        }
        APIDetails apiDetails = new APIDetails(api, swaggerDefinition);
        apiDetails.setGatewayConfiguration(gatewayConfiguration);
        apiDetails.setEndpoints(endpoints);
        if (!documentInfoSet.isEmpty()) {
            apiDetails.addDocumentInformation(documentInfoSet);
        }
        if (!documentContents.isEmpty()) {
            apiDetails.addDocumentContents(documentContents);
        }
        if (thumbnailStream != null) {
            apiDetails.setThumbnailStream(thumbnailStream);
        }
        apiDetailsSet.add(apiDetails);
    }
    return apiDetailsSet;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) InputStream(java.io.InputStream) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) HashSet(java.util.HashSet) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 5 with APIDetails

use of org.wso2.carbon.apimgt.core.models.APIDetails in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method importAndCreateAPIs.

/**
 * Imports and creates a set of new APIs to API Manager by reading and decoding the
 * input stream. Will fail if the APIs already exists
 *
 * @param uploadedApiArchiveInputStream InputStream to be read ana decoded to a set of APIs
 * @param provider                      API provider, if needs to be updated
 * @return {@link APIListDTO} object comprising of successfully imported APIs
 * @throws APIMgtEntityImportExportException if any error occurs while importing or no APIs are imported successfully
 */
public APIListDTO importAndCreateAPIs(InputStream uploadedApiArchiveInputStream, String provider) throws APIMgtEntityImportExportException {
    String apiArchiveLocation = path + File.separator + IMPORTED_APIS_DIRECTORY_NAME + ".zip";
    String archiveExtractLocation = null;
    try {
        archiveExtractLocation = APIFileUtils.extractUploadedArchive(uploadedApiArchiveInputStream, IMPORTED_APIS_DIRECTORY_NAME, apiArchiveLocation, path);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error in accessing uploaded API archive" + apiArchiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.API_IMPORT_ERROR);
    }
    // List to contain newly created/updated APIs
    Set<APIDetails> apiDetailsSet = decodeApiInformationFromDirectoryStructure(archiveExtractLocation, provider);
    List<API> apis = new ArrayList<>();
    for (APIDetails apiDetails : apiDetailsSet) {
        try {
            apis.add(importAndCreateApi(apiDetails));
        } catch (APIManagementException e) {
            log.error("Error while importing API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
            // skip importing the API
            continue;
        }
        log.info("Successfully imported API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
    }
    try {
        APIFileUtils.deleteDirectory(path);
    } catch (APIMgtDAOException e) {
        log.warn("Unable to remove directory " + path);
    }
    // if no APIs are corrected exported, throw an error
    if (apis.isEmpty()) {
        String errorMsg = "No APIs imported successfully";
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    return MappingUtil.toAPIListDTO(apis);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Aggregations

API (org.wso2.carbon.apimgt.core.models.API)12 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)11 DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)10 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)10 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 ArrayList (java.util.ArrayList)6 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)5 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Test (org.testng.annotations.Test)4 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)4 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Map (java.util.Map)2 UriTemplate (org.wso2.carbon.apimgt.core.models.UriTemplate)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1