use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class APIMWSDLUtils method getWSDL.
/**
* Retrieves the WSDL located in the provided URI ({@code wsdlUrl}
*
* @param wsdlUrl URL of the WSDL file
* @return Content bytes of the WSDL file
* @throws APIMgtWSDLException If an error occurred while retrieving the WSDL file
*/
public static byte[] getWSDL(String wsdlUrl) throws APIMgtWSDLException {
ByteArrayOutputStream outputStream = null;
InputStream inputStream = null;
URLConnection conn;
try {
URL url = new URL(wsdlUrl);
conn = url.openConnection();
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setReadTimeout(READ_TIMEOUT);
conn.connect();
outputStream = new ByteArrayOutputStream();
inputStream = conn.getInputStream();
IOUtils.copy(inputStream, outputStream);
return outputStream.toByteArray();
} catch (IOException e) {
throw new APIMgtWSDLException("Error while reading content from " + wsdlUrl, e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
} finally {
if (outputStream != null) {
IOUtils.closeQuietly(outputStream);
}
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testSaveThumbnailImage.
@Test(description = "Save thumbnail image for API")
public void testSaveThumbnailImage() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO);
InputStream image = SampleTestObjectCreator.createDefaultThumbnailImage();
apiPublisher.saveThumbnailImage(API_ID, image, "png");
Mockito.verify(apiDAO, Mockito.times(1)).updateImage(API_ID, image, "png", USER);
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream 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");
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream 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;
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream 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);
}
Aggregations