use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPut.
@Override
public Response importApplicationsPut(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(RestApiUtil.getLoggedInUsername(request));
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
Application updatedApplication = importExportManager.updateApplication(applicationDetails, username);
return Response.status(Response.Status.OK).entity(updatedApplication).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class ImportApiServiceImpl method importApplicationsPost.
/**
* Import an Application which has been exported to a zip file
*
* @param fileInputStream content stream of the zip file which contains exported Application
* @param fileDetail meta information of the zip file
* @param request msf4j request object
* @return Application that was imported
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response importApplicationsPost(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
APIStore consumer = null;
String username = RestApiUtil.getLoggedInUsername(request);
try {
consumer = RestApiUtil.getConsumer(username);
FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
Application applicationDetails = importExportManager.importApplication(fileInputStream);
applicationDetails.setCreatedUser(username);
applicationDetails.setUpdatedUser(username);
ApplicationCreationResponse response = consumer.addApplication(applicationDetails);
return Response.status(Response.Status.OK).entity(response).build();
} catch (APIManagementException e) {
String errorMsg = "Error while importing the Applications";
log.error(errorMsg, e);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class APIPublisherImpl method addApiFromDefinition.
/**
* Create api from Definition
*
* @param apiDefinition API definition stream.
* @return UUID of the added API.
* @throws APIManagementException If failed to add the API.
*/
@Override
public String addApiFromDefinition(InputStream apiDefinition) throws APIManagementException {
try {
String apiDefinitionString = IOUtils.toString(apiDefinition);
validateScope(apiDefinitionString);
API.APIBuilder apiBuilder = apiDefinitionFromSwagger20.generateApiFromSwaggerResource(getUsername(), apiDefinitionString);
Map<String, String> scopes = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(apiDefinitionString);
for (Map.Entry<String, String> scopeEntry : scopes.entrySet()) {
getKeyManager().registerScope(new Scope(scopeEntry.getKey(), scopeEntry.getValue()));
}
apiBuilder.corsConfiguration(new CorsConfiguration());
apiBuilder.apiDefinition(apiDefinitionString);
addAPI(apiBuilder);
return apiBuilder.getId();
} catch (IOException e) {
throw new APIManagementException("Couldn't Generate ApiDefinition from file", ExceptionCodes.API_DEFINITION_MALFORMED);
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class FileEncryptionUtility method encryptFile.
/**
* Encrypts the contents of a file and stores it in a new file
*
* @param inputFilePath absolute path of the file to encrypt
* @param outputFilePath expected absolute path of the new encrypted file
* @throws APIManagementException if an error occurs encrypting the file
*/
public void encryptFile(String inputFilePath, String outputFilePath) throws APIManagementException {
InputStream inputStream = null;
CipherOutputStream cipherOutStream = null;
try {
Cipher aesCipher = Cipher.getInstance(EncryptionConstants.AES);
SecretKeySpec aesKeySpec = new SecretKeySpec(getAESKey(), EncryptionConstants.AES);
aesCipher.init(Cipher.ENCRYPT_MODE, aesKeySpec);
Files.deleteIfExists(Paths.get(outputFilePath));
inputStream = APIFileUtils.readFileContentAsStream(inputFilePath);
cipherOutStream = new CipherOutputStream(new FileOutputStream(outputFilePath), aesCipher);
IOUtils.copy(inputStream, cipherOutStream);
APIFileUtils.deleteFile(inputFilePath);
log.debug("Successfully encrypted file using stored AES key");
} catch (NoSuchPaddingException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
String msg = "Error while encrypting the file at " + inputFilePath;
throw new APIManagementException(msg, e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(cipherOutStream);
}
}
use of org.wso2.siddhi.query.api.execution.query.input.stream.InputStream in project carbon-apimgt by wso2.
the class APIPublisherImpl method extractAndValidateWSDLArchive.
@Override
public WSDLArchiveInfo extractAndValidateWSDLArchive(InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
String path = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
String archivePath = path + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
String extractedLocation = APIFileUtils.extractUploadedArchive(inputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, path);
if (log.isDebugEnabled()) {
log.debug("Successfully extracted WSDL archive. Location: " + extractedLocation);
}
WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
if (!processor.canProcess()) {
throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(path, APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME);
archiveInfo.setWsdlInfo(processor.getWsdlInfo());
return archiveInfo;
}
Aggregations