Search in sources :

Example 16 with FileContent

use of org.apache.commons.vfs2.FileContent in project mycore by MyCoRe-Org.

the class MCRCStoreVFS method doStoreContent.

@Override
protected String doStoreContent(MCRFile file, MCRContentInputStream source) throws Exception {
    StringBuilder storageId = new StringBuilder();
    String[] slots = buildSlotPath();
    // Recursively create directory name
    for (String slot : slots) {
        storageId.append(slot).append("/");
    }
    String fileId = buildNextID(file);
    storageId.append(fileId);
    FileObject targetObject = fsManager.resolveFile(getBase(), storageId.toString());
    FileContent targetContent = targetObject.getContent();
    try (OutputStream out = targetContent.getOutputStream()) {
        IOUtils.copy(source, out);
    } finally {
        targetContent.close();
    }
    return storageId.toString();
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) OutputStream(java.io.OutputStream) FileObject(org.apache.commons.vfs2.FileObject)

Example 17 with FileContent

use of org.apache.commons.vfs2.FileContent in project ant-ivy by apache.

the class VfsRepository method get.

/**
 * Transfer a VFS Resource from the repository to the local file system.
 *
 * @param srcVfsURI
 *            a <code>String</code> identifying the VFS resource to be fetched
 * @param destination
 *            a <code>File</code> identifying the destination file
 * @throws IOException on failure
 * @see "Supported File Systems in the jakarta-commons-vfs documentation"
 */
public void get(String srcVfsURI, File destination) throws IOException {
    VfsResource src = new VfsResource(srcVfsURI, getVFSManager());
    fireTransferInitiated(src, TransferEvent.REQUEST_GET);
    try {
        FileContent content = src.getContent();
        if (content == null) {
            throw new IllegalArgumentException("invalid vfs uri " + srcVfsURI + ": no content found");
        }
        FileUtil.copy(content.getInputStream(), destination, progress);
    } catch (IOException | RuntimeException ex) {
        fireTransferError(ex);
        throw ex;
    }
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) IOException(java.io.IOException)

Example 18 with FileContent

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

the class TextFileInputTest method testClose.

@Test
public void testClose() throws Exception {
    TextFileInputMeta mockTFIM = createMetaObject(null);
    String virtualFile = createVirtualFile("pdi-17267.txt", null);
    TextFileInputData mockTFID = createDataObject(virtualFile, ";", null);
    mockTFID.lineBuffer = new ArrayList<>();
    mockTFID.lineBuffer.add(new TextFileLine(null, 0l, null));
    mockTFID.lineBuffer.add(new TextFileLine(null, 0l, null));
    mockTFID.lineBuffer.add(new TextFileLine(null, 0l, null));
    mockTFID.filename = "";
    FileContent mockFileContent = mock(FileContent.class);
    InputStream mockInputStream = mock(InputStream.class);
    when(mockFileContent.getInputStream()).thenReturn(mockInputStream);
    FileObject mockFO = mock(FileObject.class);
    when(mockFO.getContent()).thenReturn(mockFileContent);
    TextFileInputReader tFIR = new TextFileInputReader(mock(IBaseFileInputStepControl.class), mockTFIM, mockTFID, mockFO, mock(LogChannelInterface.class));
    assertEquals(3, mockTFID.lineBuffer.size());
    tFIR.close();
    // After closing the file, the buffer must be empty!
    assertEquals(0, mockTFID.lineBuffer.size());
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) IBaseFileInputStepControl(org.pentaho.di.trans.steps.file.IBaseFileInputStepControl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Matchers.anyString(org.mockito.Matchers.anyString) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) FileObject(org.apache.commons.vfs2.FileObject) LogChannelInterface(org.pentaho.di.core.logging.LogChannelInterface) Test(org.junit.Test)

Example 19 with FileContent

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

the class KettleVFS method getOutputStream.

public static OutputStream getOutputStream(FileObject fileObject, boolean append) throws IOException {
    FileObject parent = fileObject.getParent();
    if (parent != null) {
        if (!parent.exists()) {
            throw new IOException(BaseMessages.getString(PKG, "KettleVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI(parent)));
        }
    }
    try {
        fileObject.createFile();
        FileContent content = fileObject.getContent();
        return content.getOutputStream(append);
    } catch (FileSystemException e) {
        // 
        if (fileObject instanceof LocalFile) {
            try {
                String filename = getFilename(fileObject);
                return new FileOutputStream(new File(filename), append);
            } catch (Exception e2) {
                // throw the original exception: hide the retry.
                throw e;
            }
        } else {
            throw e;
        }
    }
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) FileSystemException(org.apache.commons.vfs2.FileSystemException) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) FileOutputStream(java.io.FileOutputStream) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) LocalFile(org.apache.commons.vfs2.provider.local.LocalFile) File(java.io.File) KettleFileException(org.pentaho.di.core.exception.KettleFileException) FileSystemException(org.apache.commons.vfs2.FileSystemException) IOException(java.io.IOException)

Example 20 with FileContent

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

the class ApacheVFSOutputHandler method getFileOutputContentItem.

@Override
public IContentItem getFileOutputContentItem() {
    String contentRef = getContentRef();
    try {
        // $NON-NLS-1$
        String contentName = getHandlerId().substring(4) + ":" + contentRef;
        FileSystemManager fsManager = getFileSystemManager();
        if (fsManager == null) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0001_CANNOT_GET_VFSMGR"));
            return null;
        }
        FileObject file = fsManager.resolveFile(contentName);
        if (file == null) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0002_CANNOT_GET_VF", contentName));
            return null;
        }
        if (!file.isWriteable()) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0003_CANNOT_WRITE", contentName));
            return null;
        }
        FileContent fileContent = file.getContent();
        if (fileContent == null) {
            logError(Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0004_CANNOT_GET_CTX", contentName));
            return null;
        }
        OutputStream outputStream = fileContent.getOutputStream();
        SimpleContentItem content = new SimpleContentItem(outputStream);
        return content;
    } catch (Throwable t) {
        Logger.error(ApacheVFSOutputHandler.class.getName(), Messages.getInstance().getString("ApacheVFSOutputHandler.ERROR_0005_CANNOT_GET_HANDLER", contentRef), // $NON-NLS-1$
        t);
    }
    return null;
}
Also used : FileContent(org.apache.commons.vfs2.FileContent) SimpleContentItem(org.pentaho.platform.engine.core.output.SimpleContentItem) OutputStream(java.io.OutputStream) FileObject(org.apache.commons.vfs2.FileObject) FileSystemManager(org.apache.commons.vfs2.FileSystemManager)

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