Example #1
0
  /** 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 #2
0
  private void changeCurrentNode() {

    boolean hasMatchSelectedName = false;
    FileObject[] children = null;
    try {
      children = currentFileObject.getChildren();
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (children == null) return;

    sortFiles(children);

    String selectedName = historyManager.getSelectedItem(currentFileObject.getName().getPath());
    table.removeAll();
    TableItem item;

    for (int i = 0; i < children.length; i++) {
      FileName fileName = children[i].getName();

      if (fileName.getBaseName().equals(selectedName)) {
        currentRow = i;
        hasMatchSelectedName = true;
      }

      item = new TableItem(table, SWT.NONE);
      item.setData("fileObject", children[i]);
      item.setText(fileName.getBaseName());

      try {
        FileType fileType = children[i].getType();
        FileContent fileContent = children[i].getContent();

        if (fileType.equals(FileType.FOLDER)) {
          item.setImage(folderImage);
          item.setText(1, "--");
          item.setText(2, StringUtil.formatDate(fileContent.getLastModifiedTime()));
        } else {
          item.setImage(fileImage);
          item.setText(1, StringUtil.formatSize(fileContent.getSize()));
          item.setText(2, StringUtil.formatDate(fileContent.getLastModifiedTime()));
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    if (!hasMatchSelectedName) currentRow = 0;
    table.setSelection(currentRow);
    table.setFocus();
  }
  public FileContentInfo create(FileContent fileContent) {
    String contentType = null;

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

    return new DefaultFileContentInfo(contentType, null);
  }
  public FileContentInfo create(FileContent fileContent) throws FileSystemException {
    WebdavFileObject file =
        (WebdavFileObject) (FileObjectUtils.getAbstractFileObject(fileContent.getFile()));

    String contentType = null;
    String contentEncoding = null;

    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(DavPropertyName.GETCONTENTTYPE);
    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 #5
0
  public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
      FileContent content = file.getContent();
      FileContentInfo contentInfo = content.getContentInfo();
      io.println("Content type: {}", contentInfo.getContentType());
      io.println("Content encoding: {}", contentInfo.getContentEncoding());

      try {
        // noinspection unchecked
        Map<String, Object> attrs = content.getAttributes();
        if (attrs != null && !attrs.isEmpty()) {
          io.println("Attributes:");
          for (Map.Entry<String, Object> entry : attrs.entrySet()) {
            io.println("    {}='{}'", entry.getKey(), entry.getValue());
          }
        }
      } catch (FileSystemException e) {
        io.println("File attributes are NOT supported");
      }

      try {
        Certificate[] certs = content.getCertificates();
        if (certs != null && certs.length != 0) {
          io.println("Certificate:");
          for (Certificate cert : certs) {
            io.println("    {}", cert);
          }
        }
      } catch (FileSystemException e) {
        io.println("File certificates are NOT supported");
      }

      if (file.getType().equals(FileType.FILE)) {
        io.println("Size: {} bytes", content.getSize());
      } else if (file.getType().hasChildren() && file.isReadable()) {
        FileObject[] children = file.getChildren();
        io.println("Directory with {} files", children.length);

        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
          io.println("#{}:{}", iterChildren, children[iterChildren].getName());
          if (iterChildren > 5) {
            break;
          }
        }
      }

      io.println(
          "Last modified: {}",
          DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
      io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
  }
Example #6
0
  public String getFileContent(String fileName) throws Exception {
    FileSystemManager fsManager = null;
    FileObject sftpFile = null;
    FileObject src = null; // used for cleanup in release()
    String fileContent = null;

    try {
      System.out.println("SFTP download");
      FileSystemOptions opts = null;
      // app.initialize();
      try {
        fsManager = VFS.getManager();
      } catch (FileSystemException ex) {
        throw new RuntimeException("failed to get fsManager from VFS", ex);
      }

      UserAuthenticator auth = new StaticUserAuthenticator(null, this.user, this.password);
      opts = new FileSystemOptions();
      try {
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
      } catch (FileSystemException ex) {
        throw new RuntimeException("setUserAuthenticator failed", ex);
      }

      // app.process();
      String startPath =
          "sftp://"
              + this.user
              + ":"
              + this.password
              + "@"
              + this.host
              + ":22"
              + this.remoteDir
              + fileName;

      // Set starting path on remote SFTP server.
      try {
        sftpFile = fsManager.resolveFile(startPath, opts);

        System.out.println("SFTP connection successfully established to " + startPath);
      } catch (FileSystemException ex) {
        throw new RuntimeException("SFTP error parsing path " + this.remoteDir, ex);
      }

      if (sftpFile.exists()) {
        FileContent fc = sftpFile.getContent();
        StringWriter writer = new StringWriter();
        IOUtils.copy(fc.getInputStream(), writer, encoding);
        String theString = writer.toString();
        //	         	System.out.println(theString);
        fileContent = theString;
      }

    } finally {
      // app.release();
      /** Release system resources, close connection to the filesystem. */
      try {
        FileSystem fs = null;
        if (sftpFile != null) {
          fs = sftpFile.getFileSystem(); // This works even if the src is closed.
          fsManager.closeFileSystem(fs);
        } // TODO if sftpFile != null
      } catch (Exception e) {
        System.out.println(e);
      }
    }
    return fileContent;
  }