Search in sources :

Example 61 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class ScriptAddedFunctions method getFileSize.

public static double getFileSize(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList, Object FunctionContext) {
    try {
        if (ArgList.length == 1 && !isNull(ArgList[0]) && !isUndefined(ArgList[0])) {
            if (ArgList[0].equals(null)) {
                return 0;
            }
            FileObject file = null;
            try {
                // Source file
                file = KettleVFS.getFileObject((String) ArgList[0]);
                long filesize = 0;
                if (file.exists()) {
                    if (file.getType().equals(FileType.FILE)) {
                        filesize = file.getContent().getSize();
                    } else {
                        throw new RuntimeException("[" + ArgList[0] + "] is not a file!");
                    }
                } else {
                    throw new RuntimeException("file [" + ArgList[0] + "] can not be found!");
                }
                return filesize;
            } catch (IOException e) {
                throw new RuntimeException("The function call getFileSize throw an error : " + e.toString());
            } finally {
                if (file != null) {
                    try {
                        file.close();
                    } catch (Exception e) {
                    // Ignore errors
                    }
                }
            }
        } else {
            throw new RuntimeException("The function call getFileSize is not valid.");
        }
    } catch (Exception e) {
        throw new RuntimeException(e.toString());
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Example 62 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class FTPSConnectionTest method testEnforceProtPOnPut.

@Test
public void testEnforceProtPOnPut() throws Exception {
    FileObject file = KettleVFS.createTempFile("FTPSConnectionTest_testEnforceProtPOnPut", KettleVFS.Suffix.TMP);
    file.createFile();
    try {
        FTPSTestConnection connection = spy(new FTPSTestConnection(FTPSConnection.CONNECTION_TYPE_FTP_IMPLICIT_TLS_WITH_CRYPTED, "the.perfect.host", 2010, "warwickw", "julia", null));
        connection.replies.put("PWD", new Reply(Arrays.asList("257 \"/la\" is current directory")));
        connection.connect();
        connection.uploadFile(file.getPublicURIString(), "uploaded-file");
        assertEquals("buffer not set", "PBSZ 0\r\n", connection.commands.get(0).toString());
        assertEquals("data privacy not set", "PROT P\r\n", connection.commands.get(1).toString());
    } finally {
        file.delete();
    }
}
Also used : Reply(org.ftp4che.reply.Reply) FileObject(org.apache.commons.vfs2.FileObject) Test(org.junit.Test)

Example 63 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class TextFileOutputTest method testsIterate.

@Test
public void testsIterate() {
    FileObject resultFile = null;
    FileObject contentFile;
    String content = null;
    int i = 0;
    for (Boolean fileExists : BOOL_VALUE_LIST) {
        for (Boolean dataReceived : BOOL_VALUE_LIST) {
            for (Boolean isDoNotOpenNewFileInit : BOOL_VALUE_LIST) {
                for (Boolean endLineExists : BOOL_VALUE_LIST) {
                    for (Boolean append : BOOL_VALUE_LIST) {
                        try {
                            resultFile = helpTestInit(fileExists, dataReceived, isDoNotOpenNewFileInit, endLineExists, append);
                            content = (String) contents.toArray()[i++];
                            contentFile = createTemplateFile(content);
                            if (resultFile.exists()) {
                                assertTrue(IOUtils.contentEquals(resultFile.getContent().getInputStream(), contentFile.getContent().getInputStream()));
                            } else {
                                assertFalse(contentFile.exists());
                            }
                        } catch (Exception e) {
                            fail(e.getMessage() + "\n FileExists = " + fileExists + "\n DataReceived = " + dataReceived + "\n isDoNotOpenNewFileInit = " + isDoNotOpenNewFileInit + "\n EndLineExists = " + endLineExists + "\n Append = " + append + "\n Content = " + (content != null ? content : "<null>") + "\n resultFile = " + (resultFile != null ? resultFile : "<null>"));
                        }
                    }
                }
            }
        }
    }
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) KettleStepException(org.pentaho.di.core.exception.KettleStepException) KettleException(org.pentaho.di.core.exception.KettleException) IOException(java.io.IOException) Test(org.junit.Test)

Example 64 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class TextFileOutputTest method helpTestInit.

private FileObject helpTestInit(Boolean fileExists, Boolean dataReceived, Boolean isDoNotOpenNewFileInit, Boolean endLineExists, Boolean append) throws Exception {
    FileObject f = createTemplateFile(fileExists ? TEST_PREVIOUS_DATA : null);
    List<Object[]> rows = dataReceived ? this.rows : this.emptyRows;
    String endLine = endLineExists ? END_LINE : null;
    List<Throwable> errors = doOutput(textFileFields, rows, f.getName().getURI(), endLine, false, isDoNotOpenNewFileInit, append);
    if (!errors.isEmpty()) {
        StringBuilder str = new StringBuilder();
        for (Throwable thr : errors) {
            str.append(thr);
        }
        fail(str.toString());
    }
    return f;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString)

Example 65 with FileObject

use of org.apache.commons.vfs2.FileObject in project pentaho-kettle by pentaho.

the class VFSDirectory method create.

public static VFSDirectory create(String parent, FileObject fileObject, String connection, String domain) {
    VFSDirectory vfsDirectory = new VFSDirectory();
    vfsDirectory.setName(fileObject.getName().getBaseName());
    vfsDirectory.setPath(fileObject.getName().getURI());
    vfsDirectory.setParent(parent);
    if (connection != null) {
        vfsDirectory.setConnection(connection);
        vfsDirectory.setRoot(VFSFileProvider.NAME);
    }
    vfsDirectory.setDomain(domain != null ? domain : "");
    vfsDirectory.setCanEdit(true);
    vfsDirectory.setHasChildren(true);
    vfsDirectory.setCanAddChildren(true);
    try {
        vfsDirectory.setDate(new Date(fileObject.getContent().getLastModifiedTime()));
    } catch (FileSystemException e) {
        vfsDirectory.setDate(new Date());
    }
    return vfsDirectory;
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) Date(java.util.Date)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)646 KettleException (org.pentaho.di.core.exception.KettleException)206 IOException (java.io.IOException)203 FileSystemException (org.apache.commons.vfs2.FileSystemException)173 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)104 KettleFileException (org.pentaho.di.core.exception.KettleFileException)97 Test (org.junit.Test)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)68 File (java.io.File)60 InputStream (java.io.InputStream)48 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)37 KettleStepException (org.pentaho.di.core.exception.KettleStepException)36 ArrayList (java.util.ArrayList)35 ResultFile (org.pentaho.di.core.ResultFile)33 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)32 Result (org.pentaho.di.core.Result)32 OutputStream (java.io.OutputStream)29 FileName (org.apache.commons.vfs2.FileName)29 KettleValueException (org.pentaho.di.core.exception.KettleValueException)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)28