Search in sources :

Example 11 with FileContent

use of org.apache.commons.vfs2.FileContent in project pentaho-platform by pentaho.

the class FileObjectTestHelper method mockFile.

public static FileObject mockFile(final String contents, final boolean exists) throws FileSystemException {
    FileObject fileObject = mock(FileObject.class);
    when(fileObject.exists()).thenReturn(exists);
    FileContent fileContent = mock(FileContent.class);
    when(fileObject.getContent()).thenReturn(fileContent);
    when(fileContent.getInputStream()).thenReturn(IOUtils.toInputStream(contents));
    final FileObject parent = mock(FileObject.class);
    when(fileObject.getParent()).thenReturn(parent);
    final FileName fileName = mock(FileName.class);
    when(parent.getName()).thenReturn(fileName);
    when(fileName.getURI()).thenReturn("mondrian:/catalog");
    return fileObject;
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) FileName(org.apache.commons.vfs2.FileName) FileObject(org.apache.commons.vfs2.FileObject)

Example 12 with FileContent

use of org.apache.commons.vfs2.FileContent in project zeppelin by apache.

the class VFSNotebookRepo method getNote.

private Note getNote(FileObject noteDir) throws IOException {
    if (!isDirectory(noteDir)) {
        throw new IOException(noteDir.getName().toString() + " is not a directory");
    }
    FileObject noteJson = noteDir.resolveFile("note.json", NameScope.CHILD);
    if (!noteJson.exists()) {
        throw new IOException(noteJson.getName().toString() + " not found");
    }
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    FileContent content = noteJson.getContent();
    InputStream ins = content.getInputStream();
    String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
    ins.close();
    Note note = gson.fromJson(json, Note.class);
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
        List<ApplicationState> appStates = p.getAllApplicationStates();
        if (appStates != null) {
            for (ApplicationState app : appStates) {
                if (app.getStatus() != ApplicationState.Status.ERROR) {
                    app.setStatus(ApplicationState.Status.UNLOADED);
                }
            }
        }
    }
    return note;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) ApplicationState(org.apache.zeppelin.notebook.ApplicationState) Gson(com.google.gson.Gson) IOException(java.io.IOException) NotebookImportDeserializer(org.apache.zeppelin.notebook.NotebookImportDeserializer) Date(java.util.Date) Paragraph(org.apache.zeppelin.notebook.Paragraph) FileContent(org.apache.commons.vfs2.FileContent) Note(org.apache.zeppelin.notebook.Note) FileObject(org.apache.commons.vfs2.FileObject)

Example 13 with FileContent

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

the class Log4jFileAppenderTest method before.

@Before
public void before() throws IOException {
    outputStream = mock(OutputStream.class);
    FileContent fileContent = mock(FileContent.class);
    when(fileContent.getOutputStream(anyBoolean())).thenReturn(outputStream);
    FileObject file = mock(FileObject.class);
    when(file.getContent()).thenReturn(fileContent);
    log4jFileAppender = new Log4jFileAppender(file);
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) OutputStream(java.io.OutputStream) FileObject(org.apache.commons.vfs2.FileObject) Before(org.junit.Before)

Example 14 with FileContent

use of org.apache.commons.vfs2.FileContent in project pentaho-platform by pentaho.

the class SolutionRepositoryVfsFileObjectTest method testContentRelatedMethods.

@Test
@SuppressWarnings({ "checkstyle:onestatementperline", "multiple statements help understand the mock definition" })
public void testContentRelatedMethods(@mockit.Mocked final RepositoryFile mockRepoFile, @mockit.Mocked final IAclNodeHelper mockAclHelper, @mockit.Mocked final IUnifiedRepository mockUnifiedRepository, @mockit.Mocked final SimpleRepositoryFileData mockFileData) throws FileSystemException {
    String fileRef = "/etc/mondrian/SteelWheels/schema.xml";
    new mockit.NonStrictExpectations() {

        // CHECKSTYLE IGNORE check FOR NEXT 6 LINES
        {
            mockUnifiedRepository.getFile(anyString);
            result = mockRepoFile;
            mockUnifiedRepository.getDataForRead((Serializable) any, (Class) any);
            result = mockFileData;
            mockAclHelper.canAccess(mockRepoFile, EnumSet.of(RepositoryFilePermission.READ));
            result = true;
            mockFileData.getStream();
            result = new ByteArrayInputStream("some string".getBytes());
        }
    };
    Deencapsulation.setField(SolutionRepositoryVfsFileObject.class, "repository", mockUnifiedRepository);
    SolutionRepositoryVfsFileObject solutionRepositoryVfsFileObject = new SolutionRepositoryVfsFileObject(fileRef);
    Deencapsulation.setField(solutionRepositoryVfsFileObject, "aclHelper", mockAclHelper);
    FileContent someFileContent = solutionRepositoryVfsFileObject.getContent();
    assertThat(someFileContent, is(notNullValue()));
    assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(false));
    someFileContent.getInputStream();
    assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(true));
    someFileContent.close();
    assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(false));
    someFileContent = solutionRepositoryVfsFileObject.getContent();
    someFileContent.getInputStream();
    assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(true));
    solutionRepositoryVfsFileObject.close();
    assertThat(solutionRepositoryVfsFileObject.isContentOpen(), is(false));
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 15 with FileContent

use of org.apache.commons.vfs2.FileContent in project javautils by jiadongpo.

the class FtpVFS method testftp2.

public void testftp2() throws Exception {
    FileSystemManager fsManager = VFS.getManager();
    FileObject fo = fsManager.resolveFile("ftp://ci:Zj4xyBkgjd@10.151.30.10:21/apps/tomcat7-40-tomcat-air-ticket-merchant/logs");
    // 得到远程文件列表
    FileObject[] children = fo.getChildren();
    for (int i = 0; i < children.length; i++) {
        FileObject f = children[i];
        FileContent c = f.getContent();
        File localFile = new File(f.getName().getBaseName());
        FileOutputStream out = new FileOutputStream(localFile);
        // 写入本地
        org.apache.commons.io.IOUtils.copy(c.getInputStream(), out);
        // 或使用写入
        FileObject obj = fsManager.resolveFile(this.getTargetResourceURL() + f.getName().getBaseName());
        if (!obj.exists()) {
            obj.createFile();
            obj.copyFrom(f, Selectors.SELECT_SELF);
        }
        final long size = (f.getType() == FileType.FILE) ? c.getSize() : -1;
        final long date = (f.getType() == FileType.FILE) ? c.getLastModifiedTime() : -1;
        System.out.println(f.getName().getPath() + " date:" + date + " Size:" + size);
    }
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) FileOutputStream(java.io.FileOutputStream) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager) File(java.io.File)

Aggregations

FileContent (org.apache.commons.vfs2.FileContent)20 FileObject (org.apache.commons.vfs2.FileObject)16 IOException (java.io.IOException)6 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 File (java.io.File)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)4 FileSystemManager (org.apache.commons.vfs2.FileSystemManager)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 URL (java.net.URL)2 FileName (org.apache.commons.vfs2.FileName)2 KettleFileException (org.pentaho.di.core.exception.KettleFileException)2 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 Connection (com.trilead.ssh2.Connection)1 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 CharArrayWriter (java.io.CharArrayWriter)1