org.apache.commons.vfs2.FileContent Java Examples

The following examples show how to use org.apache.commons.vfs2.FileContent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OldVFSNotebookRepo.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
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");
  }
  
  FileContent content = noteJson.getContent();
  InputStream ins = content.getInputStream();
  String json = IOUtils.toString(ins, conf.getString(ConfVars.ZEPPELIN_ENCODING));
  ins.close();

  return Note.fromJson(json);
}
 
Example #2
Source File: WebdavFileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    final WebdavFileObject file = (WebdavFileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile());

    String contentType = null;
    String contentEncoding = null;

    final DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(DavPropertyName.GETCONTENTTYPE);
    final DavPropertySet propertySet = file.getProperties((URLFileName) file.getName(), nameSet, true);

    DavProperty property = propertySet.get(DavPropertyName.GETCONTENTTYPE);
    if (property != null) {
        contentType = (String) property.getValue();
    }
    property = propertySet.get(WebdavFileObject.RESPONSE_CHARSET);
    if (property != null) {
        contentEncoding = (String) property.getValue();
    }

    return new DefaultFileContentInfo(contentType, contentEncoding);
}
 
Example #3
Source File: Http4FileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    String contentMimeType = null;
    String contentCharset = null;

    try (final Http4FileObject<Http4FileSystem> http4File = (Http4FileObject<Http4FileSystem>) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile())) {
        final HttpResponse lastHeadResponse = http4File.getLastHeadResponse();

        final Header header = lastHeadResponse.getFirstHeader(HTTP.CONTENT_TYPE);

        if (header != null) {
            final ContentType contentType = ContentType.parse(header.getValue());
            contentMimeType = contentType.getMimeType();

            if (contentType.getCharset() != null) {
                contentCharset = contentType.getCharset().name();
            }
        }

        return new DefaultFileContentInfo(contentMimeType, contentCharset);
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
}
 
Example #4
Source File: TextFileInputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@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() );
}
 
Example #5
Source File: Webdav4FileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    final Webdav4FileObject file = (Webdav4FileObject) FileObjectUtils.getAbstractFileObject(fileContent.getFile());

    String contentType = null;
    String contentEncoding = null;

    final DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(DavPropertyName.GETCONTENTTYPE);
    final DavPropertySet propertySet = file.getProperties((GenericURLFileName) file.getName(), nameSet, true);

    DavProperty property = propertySet.get(DavPropertyName.GETCONTENTTYPE);
    if (property != null) {
        contentType = (String) property.getValue();
    }
    property = propertySet.get(Webdav4FileObject.RESPONSE_CHARSET);
    if (property != null) {
        contentEncoding = (String) property.getValue();
    }

    return new DefaultFileContentInfo(contentType, contentEncoding);
}
 
Example #6
Source File: HttpFileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {

    String contentType = null;
    String contentEncoding = null;

    HeadMethod headMethod;
    try (final HttpFileObject<HttpFileSystem> httpFile = (HttpFileObject<HttpFileSystem>) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile())) {
        headMethod = httpFile.getHeadMethod();
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
    final Header header = headMethod.getResponseHeader("content-type");
    if (header != null) {
        final HeaderElement[] element = header.getElements();
        if (element != null && element.length > 0) {
            contentType = element[0].getName();
        }
    }

    contentEncoding = headMethod.getResponseCharSet();

    return new DefaultFileContentInfo(contentType, contentEncoding);
}
 
Example #7
Source File: Http5FileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    String contentMimeType = null;
    String contentCharset = null;

    try (final Http5FileObject<Http5FileSystem> http4File = (Http5FileObject<Http5FileSystem>) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile())) {
        final HttpResponse lastHeadResponse = http4File.getLastHeadResponse();

        final Header header = lastHeadResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE);

        if (header != null) {
            final ContentType contentType = ContentType.parse(header.getValue());
            contentMimeType = contentType.getMimeType();

            if (contentType.getCharset() != null) {
                contentCharset = contentType.getCharset().name();
            }
        }

        return new DefaultFileContentInfo(contentMimeType, contentCharset);
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
}
 
Example #8
Source File: FileContentInfoFilenameFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileContentInfo create(final FileContent fileContent) {
    String contentType = null;

    final String name = fileContent.getFile().getName().getBaseName();
    if (name != null) {
        final FileNameMap fileNameMap = URLConnection.getFileNameMap();
        contentType = fileNameMap.getContentTypeFor(name);
    }

    // optimize object creation for common case
    if (contentType == null) {
        return NULL_INFO;
    } else {
        return new DefaultFileContentInfo(contentType, null);
    }
}
 
Example #9
Source File: VfsRepository.java    From ant-ivy with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }
}
 
Example #10
Source File: FileTypeMap.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Find the scheme for the provider of a layered file system.
 * <p>
 * This will check the FileContentInfo or file extension.
 * </p>
 *
 * @return Scheme supporting the file type or null (if unknonw).
 */
public String getScheme(final FileObject file) throws FileSystemException {
    // Check the file's mime type for a match
    final FileContent content = file.getContent();
    final String mimeType = content.getContentInfo().getContentType();
    if (mimeType != null) {
        return mimeTypeMap.get(mimeType);
    }

    // no specific mime-type - if it is a file also check the extension
    if (!file.isFile()) {
        return null; // VFS-490 folders don't use extensions for mime-type
    }
    final String extension = file.getName().getExtension();
    return extensionMap.get(extension);
}
 
Example #11
Source File: EmptyFileFilter.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
/**
 * Checks to see if the file is empty. A non-existing file is also considered empty.
 *
 * @param fileInfo the file or directory to check
 *
 * @return {@code true} if the file or directory is <i>empty</i>, otherwise {@code false}.
 * @throws FileSystemException Thrown for file system errors.
 */
@Override
public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
    try (final FileObject file = fileInfo.getFile()) {
        if (!file.exists()) {
            return true;
        }
        if (file.getType() == FileType.FOLDER) {
            final FileObject[] files = file.getChildren();
            return files == null || files.length == 0;
        }
        try (final FileContent content = file.getContent();) {
            return content.isEmpty();
        }
    }
}
 
Example #12
Source File: TextFileInputTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@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( IBaseFileInputTransformControl.class ),
    mockTFIM, mockTFID, mockFO, mock( ILogChannel.class ) );

  assertEquals( 3, mockTFID.lineBuffer.size() );
  tFIR.close();
  // After closing the file, the buffer must be empty!
  assertEquals( 0, mockTFID.lineBuffer.size() );
}
 
Example #13
Source File: Bzip2TestCase.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Test
public void testBZip2() throws IOException {
    final File testResource = getTestResource("bla.txt.bz2");
    try (final FileObject bz2FileObject = VFS.getManager().resolveFile("bz2://" + testResource)) {
        Assert.assertTrue(bz2FileObject.exists());
        Assert.assertTrue(bz2FileObject.isFolder());
        try (final FileObject fileObjectDir = bz2FileObject.resolveFile("bla.txt")) {
            Assert.assertTrue(fileObjectDir.exists());
            Assert.assertTrue(bz2FileObject.isFolder());
            try (final FileObject fileObject = fileObjectDir.resolveFile("bla.txt")) {
                Assert.assertTrue(fileObject.exists());
                Assert.assertFalse(fileObject.isFolder());
                Assert.assertTrue(fileObject.isFile());
                try (final FileContent content = fileObject.getContent()) {
                    Assert.assertEquals(CompressedFileFileObject.SIZE_UNDEFINED, content.getSize());
                    // blows up, Commons Compress?
                    final String string = content.getString(StandardCharsets.UTF_8);
                    Assert.assertEquals(26, string.length());
                    Assert.assertEquals("Hallo, dies ist ein Test.\n", string);
                }
            }
        }
    }
}
 
Example #14
Source File: TextFileInputTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@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( IBaseFileInputTransformControl.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() );
}
 
Example #15
Source File: AbstractProviderTestCase.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the content of a file is the same as expected. Checks the length reported by getSize() is correct,
 * then reads the content as a byte stream and compares the result with the expected content. Assumes files are
 * encoded using UTF-8.
 */
protected void assertSameContent(final String expected, final FileObject file) throws Exception {
    // Check the file exists, and is a file
    assertTrue(file.exists());
    assertSame(FileType.FILE, file.getType());
    assertTrue(file.isFile());

    // Get file content as a binary stream
    final byte[] expectedBin = expected.getBytes("utf-8");

    // Check lengths
    final FileContent content = file.getContent();
    assertEquals("same content length", expectedBin.length, content.getSize());

    // Read content into byte array
    final InputStream instr = content.getInputStream();
    final ByteArrayOutputStream outstr;
    try {
        outstr = new ByteArrayOutputStream(expectedBin.length);
        final byte[] buffer = new byte[256];
        int nread = 0;
        while (nread >= 0) {
            outstr.write(buffer, 0, nread);
            nread = instr.read(buffer);
        }
    } finally {
        instr.close();
    }

    // Compare
    assertArrayEquals("same binary content", expectedBin, outstr.toByteArray());
}
 
Example #16
Source File: FileObject.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public FileContent getContent() {
    try {
        return this.fileObject.getContent();
    } catch (FileSystemException e) {
        throw new VFSFileSystemException(e);
    }
}
 
Example #17
Source File: PubmedCentralCollectionReader.java    From bluima with Apache License 2.0 5 votes vote down vote up
private NextArticle getNextArticle() throws IOException {

        while (directoryIterator.hasNext()) {
            File f = directoryIterator.next();
            // LOG.debug("extracting " + f.getAbsolutePath());

            try {
                FileObject archive = fsManager.resolveFile("tgz:file://"
                        + f.getAbsolutePath());

                // List the children of the archive file
                FileObject[] children = archive.getChildren()[0].getChildren();
                for (int i = 0; i < children.length; i++) {
                    FileObject fo = children[i];
                    if (fo.isReadable() && fo.getType() == FileType.FILE
                            && fo.getName().getExtension().equals("nxml")) {
                        FileContent fc = fo.getContent();
                        Article article = archiveArticleParser.parse(fc
                                .getInputStream());

                        NextArticle nextArticle = new NextArticle();
                        nextArticle.article = article;
                        nextArticle.file = f.getAbsolutePath();

                        return nextArticle;
                    }
                }
            } catch (Exception e) {
                LOG.error("Error extracting " + f.getAbsolutePath() + ", " + e);
            }
        }
        return null;
    }
 
Example #18
Source File: KettleVFS.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
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 ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example #19
Source File: TestFTPRemoteFile.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateAndCommitOutputStream() throws Exception {
  String name = "file.txt";
  String filePath = "/some/path/";
  FileObject fileObject = Mockito.mock(FileObject.class);
  FileName fileName = Mockito.mock(FileName.class);
  Mockito.when(fileObject.getName()).thenReturn(fileName);
  Mockito.when(fileName.getBaseName()).thenReturn(name);
  FileObject parentFileObject = Mockito.mock(FileObject.class);
  FileObject tempFileObject = Mockito.mock(FileObject.class);
  Mockito.when(fileObject.getParent()).thenReturn(parentFileObject);
  Mockito.when(parentFileObject.resolveFile(Mockito.any())).thenReturn(tempFileObject);
  FileContent tempFileContent = Mockito.mock(FileContent.class);
  Mockito.when(tempFileObject.getContent()).thenReturn(tempFileContent);

  FTPRemoteFile file = new FTPRemoteFile(filePath + name, 0L, fileObject);

  try {
    file.commitOutputStream();
    Assert.fail("Expected IOException because called commitOutputStream before createOutputStream");
  } catch (IOException ioe) {
    Assert.assertEquals("Cannot commit " + filePath + name + " - it must be written first", ioe.getMessage());
  }

  file.createOutputStream();
  Mockito.verify(parentFileObject).resolveFile("_tmp_" + name);
  Mockito.verify(tempFileContent).getOutputStream();

  file.commitOutputStream();
  Mockito.verify(tempFileObject).moveTo(fileObject);
}
 
Example #20
Source File: HopVfs.java    From hop with Apache License 2.0 5 votes vote down vote up
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, "HopVFS.Exception.ParentDirectoryDoesNotExist", getFriendlyURI( parent ) ) );
    }
  }
  try {
    fileObject.createFile();
    FileContent content = fileObject.getContent();
    return content.getOutputStream( append );
  } catch ( FileSystemException e ) {
    // Perhaps if it's a local file, we can retry using the standard
    // File object. This is because on Windows there is a bug in VFS.
    //
    if ( fileObject instanceof LocalFile ) {
      try {
        String filename = getFilename( fileObject );
        return new FileOutputStream( new File( filename ), append );
      } catch ( Exception e2 ) {
        throw e; // throw the original exception: hide the retry.
      }
    } else {
      throw e;
    }
  }
}
 
Example #21
Source File: AbstractFileObject.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the file's content.
 *
 * @return the FileContent for this FileObject.
 * @throws FileSystemException if an error occurs.
 */
@Override
public FileContent getContent() throws FileSystemException {
    synchronized (fileSystem) {
        attach();
        if (content == null) {
            content = doCreateFileContent();
        }
        return content;
    }
}
 
Example #22
Source File: VFSFileSystem.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
@Override
public OutputStream getOutputStream(final URL url) throws ConfigurationException
{
    try
    {
        final FileSystemOptions opts = getOptions(url.getProtocol());
        final FileSystemManager fsManager = VFS.getManager();
        final FileObject file = opts == null ? fsManager.resolveFile(url.toString())
                : fsManager.resolveFile(url.toString(), opts);
        // throw an exception if the target URL is a directory
        if (file == null || file.getType() == FileType.FOLDER)
        {
            throw new ConfigurationException("Cannot save a configuration to a directory");
        }
        final FileContent content = file.getContent();

        if (content == null)
        {
            throw new ConfigurationException("Cannot access content of " + url);
        }
        return content.getOutputStream();
    }
    catch (final FileSystemException fse)
    {
        throw new ConfigurationException("Unable to access " + url, fse);
    }
}
 
Example #23
Source File: Shell.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Does an 'ls' command.
 */
private void ls(final String[] cmd) throws FileSystemException {
    int pos = 1;
    final boolean recursive;
    if (cmd.length > pos && cmd[pos].equals("-R")) {
        recursive = true;
        pos++;
    } else {
        recursive = false;
    }

    final FileObject file;
    if (cmd.length > pos) {
        file = mgr.resolveFile(cwd, cmd[pos]);
    } else {
        file = cwd;
    }

    if (file.getType() == FileType.FOLDER) {
        // List the contents
        System.out.println("Contents of " + file.getName());
        listChildren(file, recursive, "");
    } else {
        // Stat the file
        System.out.println(file.getName());
        final FileContent content = file.getContent();
        System.out.println("Size: " + content.getSize() + " bytes.");
        final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
        final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
        System.out.println("Last modified: " + lastMod);
    }
}
 
Example #24
Source File: DefaultFileContent.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Writes this content to another FileContent.
 *
 * @param fileContent The target FileContent.
 * @return the total number of bytes written
 * @throws IOException if an error occurs writing the content.
 * @since 2.1
 */
@Override
public long write(final FileContent fileContent) throws IOException {
    final OutputStream output = fileContent.getOutputStream();
    try {
        return this.write(output);
    } finally {
        output.close();
    }
}
 
Example #25
Source File: ProviderReadTests.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
public void testGetContentInfo() throws Exception {
    final FileObject file = resolveFile1Txt();
    assertTrue(file.exists());
    final FileContent content = file.getContent();
    assertNotNull(content);
    final FileContentInfo contentInfo = content.getContentInfo();
    assertNotNull(contentInfo);
}
 
Example #26
Source File: Http4GetContentInfoTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo().
 *
 * @throws FileSystemException thrown when the getContentInfo API fails.
 */
@Test
public void testGetContentInfo() throws FileSystemException {
    final FileSystemManager fsManager = VFS.getManager();
    final FileObject fo = fsManager.resolveFile("http4://www.apache.org/licenses/LICENSE-2.0.txt");
    final FileContent content = fo.getContent();
    Assert.assertNotNull(content);
    // Used to NPE before fix:
    content.getContentInfo();
}
 
Example #27
Source File: DefaultFileContentTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Test VFS-724 should be done on a website which render a page with no content size. Note the getSize() is
 * currently the value sent back by the server then zero usually means no content length attached.
 */
@Test
public void testGetZeroContents() throws IOException {
    final FileSystemManager fsManager = VFS.getManager();
    try (final FileObject fo = fsManager.resolveFile(new File("."), "src/test/resources/test-data/size-0-file.bin");
            final FileContent content = fo.getContent()) {
        Assert.assertEquals(0, content.getSize());
        Assert.assertTrue(content.isEmpty());
        Assert.assertEquals(StringUtils.EMPTY, content.getString(StandardCharsets.UTF_8));
        Assert.assertEquals(StringUtils.EMPTY, content.getString(StandardCharsets.UTF_8.name()));
        Assert.assertArrayEquals(ArrayUtils.EMPTY_BYTE_ARRAY, content.getByteArray());
    }
}
 
Example #28
Source File: CustomRamProviderTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
private InputStream createNonEmptyFile() throws FileSystemException, IOException {
    final FileObject root = manager.resolveFile("ram://file");
    root.createFile();

    final FileContent content = root.getContent();
    final OutputStream output = this.closeOnTearDown(content.getOutputStream());
    output.write(1);
    output.write(2);
    output.write(3);
    output.flush();
    output.close();

    return this.closeOnTearDown(content.getInputStream());
}
 
Example #29
Source File: Http5GetContentInfoTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo().
 *
 * @throws FileSystemException thrown when the getContentInfo API fails.
 */
@Test
public void testGetContentInfo() throws FileSystemException {
    final FileSystemManager fsManager = VFS.getManager();
    final FileObject fo = fsManager.resolveFile("http5://www.apache.org/licenses/LICENSE-2.0.txt");
    final FileContent content = fo.getContent();
    Assert.assertNotNull(content);
    // Used to NPE before fix:
    content.getContentInfo();
}
 
Example #30
Source File: GetContentInfoFunctionalTest.java    From commons-vfs with Apache License 2.0 5 votes vote down vote up
/**
 * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo().
 *
 * @throws FileSystemException thrown when the getContentInfo API fails.
 */
@Test
public void testGetContentInfo() throws FileSystemException {
    final FileSystemManager fsManager = VFS.getManager();
    try (final FileObject fo = fsManager.resolveFile("http://www.apache.org/licenses/LICENSE-2.0.txt");
            final FileContent content = fo.getContent();) {
        Assert.assertNotNull(content);
        // Used to NPE before fix:
        content.getContentInfo();
    }
}