Search in sources :

Example 1 with DocumentContent

use of org.wso2.carbon.apimgt.core.models.DocumentContent 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 DocumentContent

use of org.wso2.carbon.apimgt.core.models.DocumentContent 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 DocumentContent

use of org.wso2.carbon.apimgt.core.models.DocumentContent 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 DocumentContent

use of org.wso2.carbon.apimgt.core.models.DocumentContent 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 DocumentContent

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

the class ApiImportExportManager method addAPIDetails.

/**
 * Adds the API details
 *
 * @param apiDetails {@link APIDetails} instance
 * @throws APIManagementException if an error occurs while adding API details
 */
public void addAPIDetails(APIDetails apiDetails) throws APIManagementException {
    // update everything
    String swaggerDefinition = apiDetails.getSwaggerDefinition();
    String gatewayConfig = apiDetails.getGatewayConfiguration();
    Map<String, Endpoint> endpointTypeToIdMap = apiDetails.getApi().getEndpoint();
    Map<String, UriTemplate> uriTemplateMap = apiDetails.getApi().getUriTemplates();
    // endpoints
    for (Endpoint endpoint : apiDetails.getEndpoints()) {
        try {
            Endpoint existingEndpoint = apiPublisher.getEndpointByName(endpoint.getName());
            String endpointId;
            if (existingEndpoint == null) {
                // no endpoint by that name, add it
                endpointId = apiPublisher.addEndpoint(endpoint);
            } else {
                endpointId = existingEndpoint.getId();
                if (log.isDebugEnabled()) {
                    log.debug("Endpoint with id " + endpoint.getId() + " already exists, not adding again");
                }
            // endpoint with same name exists, add to endpointTypeToIdMap
            // endpointTypeToIdMap.put(endpoint.getType(), existingEndpoint.getId());
            }
            endpointTypeToIdMap.forEach((String k, Endpoint v) -> {
                if (endpoint.getName().equals(v.getName())) {
                    Endpoint replacedEndpoint = new Endpoint.Builder(v).id(endpointId).build();
                    endpointTypeToIdMap.replace(k, replacedEndpoint);
                }
            });
            uriTemplateMap.forEach(((String templateId, UriTemplate uriTemplate) -> {
                UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
                Map<String, Endpoint> uriEndpointMap = uriTemplateBuilder.getEndpoint();
                uriEndpointMap.forEach((String type, Endpoint endpoint1) -> {
                    if (endpoint.getName().equals(endpoint1.getName())) {
                        Endpoint replacedEndpoint = new Endpoint.Builder(endpoint1).id(endpointId).build();
                        uriEndpointMap.replace(type, replacedEndpoint);
                    }
                });
                uriTemplateMap.replace(templateId, uriTemplateBuilder.endpoint(uriEndpointMap).build());
            }));
        } catch (APIManagementException e) {
            // skip adding this API; log and continue
            log.error("Error while adding the endpoint with id: " + endpoint.getId() + ", type: " + endpoint.getType() + " for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
        }
    }
    API.APIBuilder apiBuilder = new API.APIBuilder(apiDetails.getApi());
    apiPublisher.addAPI(apiBuilder.apiDefinition(swaggerDefinition).gatewayConfig(gatewayConfig).endpoint(endpointTypeToIdMap).uriTemplates(uriTemplateMap));
    // docs
    try {
        Set<DocumentInfo> documentInfo = apiDetails.getAllDocumentInformation();
        for (DocumentInfo aDocInfo : documentInfo) {
            apiPublisher.addDocumentationInfo(apiDetails.getApi().getId(), aDocInfo);
        }
        for (DocumentContent aDocContent : apiDetails.getDocumentContents()) {
            // add documentation
            if (aDocContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.FILE)) {
                apiPublisher.uploadDocumentationFile(aDocContent.getDocumentInfo().getId(), aDocContent.getFileContent(), URLConnection.guessContentTypeFromStream(aDocContent.getFileContent()));
            } else if (aDocContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                apiPublisher.addDocumentationContent(aDocContent.getDocumentInfo().getId(), aDocContent.getInlineContent());
            }
        }
    } catch (APIManagementException e) {
        // no need to throw, log and continue
        log.error("Error while adding Document details for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    } catch (IOException e) {
        // no need to throw, log and continue
        log.error("Error while retrieving content type of the File documentation of API : " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    }
    // add thumbnail
    try {
        apiPublisher.saveThumbnailImage(apiDetails.getApi().getId(), apiDetails.getThumbnailStream(), "thumbnail");
    } catch (APIManagementException e) {
        // no need to throw, log and continue
        log.error("Error while adding thumbnail for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    }
}
Also used : IOException(java.io.IOException) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) 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) HashMap(java.util.HashMap) Map(java.util.Map) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Aggregations

DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)16 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)16 API (org.wso2.carbon.apimgt.core.models.API)9 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)9 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)7 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 Test (org.testng.annotations.Test)4 InputStream (java.io.InputStream)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Map (java.util.Map)2