use of org.apache.commons.vfs2.FileContent in project pentaho-kettle by pentaho.
the class PropertyInputMetaTest method testOpenNextFile.
@Test
public void testOpenNextFile() throws Exception {
PropertyInputMeta propertyInputMeta = Mockito.mock(PropertyInputMeta.class);
PropertyInputData propertyInputData = new PropertyInputData();
FileInputList fileInputList = new FileInputList();
FileObject fileObject = Mockito.mock(FileObject.class);
FileName fileName = Mockito.mock(FileName.class);
Mockito.when(fileName.getRootURI()).thenReturn("testFolder");
Mockito.when(fileName.getURI()).thenReturn("testFileName.ini");
String header = "test ini data with umlauts";
String key = "key";
String testValue = "value-with-äöü";
String testData = "[" + header + "]\r\n" + key + "=" + testValue;
String charsetEncode = "Windows-1252";
InputStream inputStream = new ByteArrayInputStream(testData.getBytes(Charset.forName(charsetEncode)));
FileContent fileContent = Mockito.mock(FileContent.class);
Mockito.when(fileObject.getContent()).thenReturn(fileContent);
Mockito.when(fileContent.getInputStream()).thenReturn(inputStream);
Mockito.when(fileObject.getName()).thenReturn(fileName);
fileInputList.addFile(fileObject);
propertyInputData.files = fileInputList;
propertyInputData.propfiles = false;
propertyInputData.realEncoding = charsetEncode;
PropertyInput propertyInput = Mockito.mock(PropertyInput.class);
Field logField = BaseStep.class.getDeclaredField("log");
logField.setAccessible(true);
logField.set(propertyInput, Mockito.mock(LogChannelInterface.class));
Mockito.doCallRealMethod().when(propertyInput).dispose(propertyInputMeta, propertyInputData);
propertyInput.dispose(propertyInputMeta, propertyInputData);
Method method = PropertyInput.class.getDeclaredMethod("openNextFile");
method.setAccessible(true);
method.invoke(propertyInput);
Assert.assertEquals(testValue, propertyInputData.wini.get(header).get(key));
}
use of org.apache.commons.vfs2.FileContent in project pentaho-kettle by pentaho.
the class SSHData method OpenConnection.
public static Connection OpenConnection(String serveur, int port, String username, String password, boolean useKey, String keyFilename, String passPhrase, int timeOut, VariableSpace space, String proxyhost, int proxyport, String proxyusername, String proxypassword) throws KettleException {
Connection conn = null;
char[] content = null;
boolean isAuthenticated = false;
try {
// perform some checks
if (useKey) {
if (Utils.isEmpty(keyFilename)) {
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyFileMissing"));
}
FileObject keyFileObject = KettleVFS.getFileObject(keyFilename);
if (!keyFileObject.exists()) {
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.PrivateKeyNotExist", keyFilename));
}
FileContent keyFileContent = keyFileObject.getContent();
CharArrayWriter charArrayWriter = new CharArrayWriter((int) keyFileContent.getSize());
try (InputStream in = keyFileContent.getInputStream()) {
IOUtils.copy(in, charArrayWriter);
}
content = charArrayWriter.toCharArray();
}
// Create a new connection
conn = createConnection(serveur, port);
/* We want to connect through a HTTP proxy */
if (!Utils.isEmpty(proxyhost)) {
// if the proxy requires basic authentication:
if (!Utils.isEmpty(proxyusername)) {
conn.setProxyData(new HTTPProxyData(proxyhost, proxyport, proxyusername, proxypassword));
} else {
conn.setProxyData(new HTTPProxyData(proxyhost, proxyport));
}
}
// and connect
if (timeOut == 0) {
conn.connect();
} else {
conn.connect(null, 0, timeOut * 1000);
}
// authenticate
if (useKey) {
isAuthenticated = conn.authenticateWithPublicKey(username, content, space.environmentSubstitute(passPhrase));
} else {
isAuthenticated = conn.authenticateWithPassword(username, password);
}
if (isAuthenticated == false) {
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.AuthenticationFailed", username));
}
} catch (Exception e) {
// do not forget to disconnect if connected
if (conn != null) {
conn.close();
}
throw new KettleException(BaseMessages.getString(SSHMeta.PKG, "SSH.Error.ErrorConnecting", serveur, username), e);
}
return conn;
}
use of org.apache.commons.vfs2.FileContent in project pentaho-metaverse by pentaho.
the class VfsLineageWriter method createOutputStream.
protected OutputStream createOutputStream(LineageHolder holder, String extension) {
if (holder != null) {
try {
IExecutionProfile profile = holder.getExecutionProfile();
String timestampString = Long.toString(profile.getExecutionData().getStartTime().getTime());
FileObject destFolder = getOutputDirectoryAsFile(holder);
String name = Const.NVL(profile.getName(), "unknown");
FileObject file = destFolder.resolveFile(timestampString + "_" + name + extension);
FileContent content = file.getContent();
return content.getOutputStream();
} catch (Exception e) {
log.error(Messages.getErrorString("ERROR.CantCreateOutputStream"), e);
return null;
}
} else {
return null;
}
}
use of org.apache.commons.vfs2.FileContent in project mondrian by pentaho.
the class Util method readVirtualFile.
/**
* Gets content via Apache VFS. File must exist and have content
*
* @param url String
* @return Apache VFS FileContent for further processing
* @throws FileSystemException on error
*/
public static InputStream readVirtualFile(String url) throws FileSystemException {
// Treat catalogUrl as an Apache VFS (Virtual File System) URL.
// VFS handles all of the usual protocols (http:, file:)
// and then some.
FileSystemManager fsManager = VFS.getManager();
if (fsManager == null) {
throw newError("Cannot get virtual file system manager");
}
// Workaround VFS bug.
if (url.startsWith("file://localhost")) {
url = url.substring("file://localhost".length());
}
if (url.startsWith("file:")) {
url = url.substring("file:".length());
}
// (Mondrian-585)
if (url.startsWith("http")) {
try {
return new URL(url).openStream();
} catch (IOException e) {
throw newError("Could not read URL: " + url);
}
}
File userDir = new File("").getAbsoluteFile();
FileObject file = fsManager.resolveFile(userDir, url);
FileContent fileContent = null;
try {
// Because of VFS caching, make sure we refresh to get the latest
// file content. This refresh may possibly solve the following
// workaround for defect MONDRIAN-508, but cannot be tested, so we
// will leave the work around for now.
file.refresh();
// http://blah.com?param=B)
if (file instanceof HttpFileObject && !file.getName().getURI().equals(url)) {
fsManager.getFilesCache().removeFile(file.getFileSystem(), file.getName());
file = fsManager.resolveFile(userDir, url);
}
if (!file.isReadable()) {
throw newError("Virtual file is not readable: " + url);
}
fileContent = file.getContent();
} finally {
file.close();
}
if (fileContent == null) {
throw newError("Cannot get virtual file content: " + url);
}
return fileContent.getInputStream();
}
use of org.apache.commons.vfs2.FileContent in project scheduling by ow2-proactive.
the class SchedulerStateRest method pushFile.
/**
* Pushes a file from the local file system into the given DataSpace
*
* @param sessionId
* a valid session id
* @param spaceName
* the name of the DataSpace
* @param filePath
* the path inside the DataSpace where to put the file e.g.
* "/myfolder"
* @param multipart
* the form data containing : - fileName the name of the file
* that will be created on the DataSpace - fileContent the
* content of the file
* @return true if the transfer succeeded
* @see org.ow2.proactive.scheduler.common.SchedulerConstants for spaces
* names
*/
@Override
public boolean pushFile(@HeaderParam("sessionid") String sessionId, @PathParam("spaceName") String spaceName, @PathParam("filePath") String filePath, MultipartFormDataInput multipart) throws IOException, NotConnectedRestException, PermissionRestException {
checkAccess(sessionId, "pushFile");
Session session = dataspaceRestApi.checkSessionValidity(sessionId);
Map<String, List<InputPart>> formDataMap = multipart.getFormDataMap();
List<InputPart> fNL = formDataMap.get("fileName");
if ((fNL == null) || (fNL.size() == 0)) {
throw new IllegalArgumentException("Illegal multipart argument definition (fileName), received " + fNL);
}
String fileName = fNL.get(0).getBody(String.class, null);
List<InputPart> fCL = formDataMap.get("fileContent");
if ((fCL == null) || (fCL.size() == 0)) {
throw new IllegalArgumentException("Illegal multipart argument definition (fileContent), received " + fCL);
}
InputStream fileContent = fCL.get(0).getBody(InputStream.class, null);
if (fileName == null) {
throw new IllegalArgumentException("Wrong file name : " + fileName);
}
filePath = normalizeFilePath(filePath, fileName);
FileObject destfo = dataspaceRestApi.resolveFile(session, spaceName, filePath);
URL targetUrl = destfo.getURL();
logger.info("[pushFile] pushing file to " + targetUrl);
if (!destfo.isWriteable()) {
RuntimeException ex = new IllegalArgumentException("File " + filePath + " is not writable in space " + spaceName);
logger.error(ex);
throw ex;
}
if (destfo.exists()) {
destfo.delete();
}
// used to create the necessary directories if needed
destfo.createFile();
dataspaceRestApi.writeFile(fileContent, destfo, null);
return true;
}
Aggregations