Search in sources :

Example 1 with FileApi

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

the class ApiFileDAOImpl method addAPI.

/**
 * @see ApiDAO#addAPI(API api)
 */
@Override
public void addAPI(API api) throws APIMgtDAOException {
    // Save API definition
    FileApi fileApi = new FileApi(api);
    String apiExportDirectory = APIFileUtils.getAPIBaseDirectory(storagePath, fileApi);
    APIFileUtils.createDirectory(apiExportDirectory);
    APIFileUtils.exportApiDefinitionToFileSystem(fileApi, apiExportDirectory);
    // Export gateway config to file system
    APIFileUtils.exportGatewayConfigToFileSystem(api.getGatewayConfig(), api, apiExportDirectory);
    // Export swagger definition to file system.
    APIFileUtils.exportSwaggerDefinitionToFileSystem(api.getApiDefinition(), api, apiExportDirectory);
}
Also used : FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 2 with FileApi

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

the class APIFileUtilsTestCase method testAPIFileUtils.

@Test
public void testAPIFileUtils() throws Exception {
    APIFileUtils.createDirectory(dirPath);
    Assert.assertTrue(Files.exists(Paths.get(dirPath)), "Directory doesn't exists");
    String filePath = dirPath + File.separatorChar + fileName;
    APIFileUtils.createFile(filePath);
    Assert.assertTrue(Files.exists(Paths.get(filePath)), "File doesn't exists");
    // write content to file
    APIFileUtils.writeToFile(dirPath + File.separatorChar + fileName, "This is some test content");
    // read content as text
    String contentRead = APIFileUtils.readFileContentAsText(dirPath + File.separatorChar + fileName);
    Assert.assertEquals(contentRead, "This is some test content");
    // read content as stream
    InputStream contentReadInputStream = APIFileUtils.readFileContentAsStream(dirPath + File.separatorChar + fileName);
    Assert.assertEquals(IOUtils.toString(contentReadInputStream), "This is some test content");
    // write stream to filesystem
    APIFileUtils.writeStreamToFile(dirPath + File.separatorChar + "write-stream-to-file", contentReadInputStream);
    Assert.assertTrue(Files.exists(Paths.get(dirPath + File.separatorChar + "write-stream-to-file")), "File doesn't exists");
    // create archive
    APIFileUtils.archiveDirectory(new File(dirPath).getAbsolutePath(), new File(parentDir + File.separatorChar).getAbsolutePath(), "my-test-archive");
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "my-test-archive.zip")), "File doesn't exists");
    // find in file system
    Assert.assertNotNull(APIFileUtils.findInFileSystem(new File(dirPath), fileName));
    // write object as json
    GatewayConfigDTO configDTO = new GatewayConfigDTO();
    configDTO.setApiName("api1");
    configDTO.setConfig("config");
    configDTO.setContext("context");
    APIFileUtils.writeObjectAsJsonToFile(configDTO, dirPath + File.separatorChar + "object.json");
    Assert.assertTrue(Files.exists(Paths.get(dirPath + File.separatorChar + "object.json")), "File doesn't exists");
    // write string as json
    APIFileUtils.writeStringAsJsonToFile("{\"config\":\"config\"}", dirPath + File.separatorChar + "object-2.json");
    Assert.assertTrue(Files.exists(Paths.get(dirPath + File.separatorChar + "object-2.json")), "File doesn't exists");
    // list directories
    Assert.assertTrue(APIFileUtils.getDirectoryList(parentDir).size() > 0);
    // export API Definition
    FileApi fileApi = new FileApi();
    fileApi.setApiDefinition("api-definition");
    fileApi.setName("test-api");
    fileApi.setVersion("1.0.0");
    fileApi.setProvider("p1");
    fileApi.setId("1x1x");
    APIFileUtils.exportApiDefinitionToFileSystem(fileApi, parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "api-1x1x.json")), "File doesn't exists");
    // export swagger definition
    String swaggerDefinition = "{\"swaggerVersion\": \"1.2\",\"apis\": [{\"path\": " + "\"http://localhost:8000/listings/greetings\"," + "\"description\": \"Generating greetings in our application.\"}]}";
    APIFileUtils.exportSwaggerDefinitionToFileSystem(swaggerDefinition, new API.APIBuilder("p", "name", "1.0").id("1x1x").build(), parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "swagger-1x1x.json")), "File doesn't exists");
    // export endpoint
    APIFileUtils.exportEndpointToFileSystem(SampleTestObjectCreator.createMockEndpoint(), parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "Endpoint1.json")), "Endpoint doesn't exists");
    // export gatewayconfig
    APIFileUtils.exportGatewayConfigToFileSystem("gateway-config-content-goes-here", new API.APIBuilder("p", "name-5", "1.0").id("5x5x").build(), parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "gateway-configuration")), "Gateway config doesn't exists");
    // delete file
    APIFileUtils.deleteFile(filePath);
    Assert.assertFalse(Files.exists(Paths.get(filePath)), "File has not deleted");
    // delete directory
    APIFileUtils.deleteDirectory(dirPath);
    Assert.assertFalse(Files.exists(Paths.get(dirPath)), "Directory has not deleted");
}
Also used : GatewayConfigDTO(org.wso2.carbon.apimgt.core.template.dto.GatewayConfigDTO) InputStream(java.io.InputStream) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) FileApi(org.wso2.carbon.apimgt.core.models.FileApi) Test(org.testng.annotations.Test)

Example 3 with FileApi

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

the class ApiFileDAOImpl method getAPISummary.

/**
 * @see ApiDAO#getAPISummary(String apiID)
 */
@Override
public API getAPISummary(String apiID) throws APIMgtDAOException {
    String apiFileName = APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX + apiID + APIMgtConstants.APIFileUtilConstants.JSON_EXTENSION;
    String apiFilePath = APIFileUtils.findInFileSystem(new File(storagePath), apiFileName);
    if (apiFilePath != null) {
        return new API.APIBuilder((FileApi) constructObjectSummaryFromFile(apiFilePath, FileApi.class)).build();
    }
    return null;
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 4 with FileApi

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

the class ApiFileDAOImpl method deleteAPI.

/**
 * @see ApiDAO#deleteAPI(String apiID)
 */
@Override
public void deleteAPI(String apiID) throws APIMgtDAOException {
    API api = getAPI(apiID);
    if (api == null) {
        String errorMsg = "API with Id " + apiID + " not found";
        log.error(errorMsg);
        throw new APIMgtDAOException(errorMsg, ExceptionCodes.API_NOT_FOUND);
    }
    APIFileUtils.deleteDirectory(APIFileUtils.getAPIBaseDirectory(storagePath, new FileApi(api)));
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 5 with FileApi

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

the class ApiFileDAOImpl method getAPI.

/**
 * @see ApiDAO#getAPI(String apiID)
 */
@Override
public API getAPI(String apiID) throws APIMgtDAOException {
    String apiFileName = APIMgtConstants.APIFileUtilConstants.API_DEFINITION_FILE_PREFIX + apiID + APIMgtConstants.APIFileUtilConstants.JSON_EXTENSION;
    String apiFilePath = APIFileUtils.findInFileSystem(new File(storagePath), apiFileName);
    if (apiFilePath != null) {
        return new API.APIBuilder((FileApi) constructObjectSummaryFromFile(apiFilePath, FileApi.class)).build();
    }
    return null;
}
Also used : CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Aggregations

FileApi (org.wso2.carbon.apimgt.core.models.FileApi)7 API (org.wso2.carbon.apimgt.core.models.API)6 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)4 File (java.io.File)3 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)3 InputStream (java.io.InputStream)1 Test (org.testng.annotations.Test)1 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)1 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)1 GatewayConfigDTO (org.wso2.carbon.apimgt.core.template.dto.GatewayConfigDTO)1