예제 #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);
    }
  }
예제 #2
0
 private FileObject createFile(FileObject directory, String fileName) throws Exception {
   FileObject f = VFS.getManager().resolveFile(directory, fileName);
   OutputStream os = f.getContent().getOutputStream();
   os.write(TestStringSpecifications.SimpleAlternateCalculatorTest.getBytes());
   os.close();
   return f;
 }
예제 #3
0
  public byte[] readFile(ServerDetailsDTO conDetails, String filePath) throws IOException {

    FileObject fileObject = null;
    StandardFileSystemManager manager = null;
    try {

      manager = new StandardFileSystemManager();
      manager.init();

      UserAuthenticator auth =
          new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword());
      FileSystemOptions opts = new FileSystemOptions();

      DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
      SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

      String fileUri = buildUri(conDetails, filePath);

      fileObject = manager.resolveFile(fileUri, opts);
      if (fileObject.isReadable()) {
        InputStream is = fileObject.getContent().getInputStream();
        int size = (int) fileObject.getContent().getSize();
        byte[] fileContent = new byte[size];
        for (int index = 0; size > 0; index += 4000, size -= 4000) {
          is.read(fileContent, index, size < 4000 ? size : 4000);
          if (Thread.currentThread().isInterrupted()) {
            return null;
          }
        }
        return fileContent;
      }
      return null;
    } finally {
      try {
        if (fileObject != null) {
          fileObject.getContent().close();
          ((SftpFileSystem) fileObject.getFileSystem()).closeCommunicationLink();
          FileSystem fs = null;
          fs = fileObject.getFileSystem();
          manager.closeFileSystem(fs);
        }
      } finally {
      }
    }
  }
예제 #4
0
  private Document loadAsDom(FileObject inFile) throws Exception {
    InputStream is = null;

    try {
      is = inFile.getContent().getInputStream();

      DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
      parserFactory.setValidating(false);
      parserFactory.setNamespaceAware(false); // this is the only diference from readContentAsDom
      parserFactory.setIgnoringElementContentWhitespace(false);
      parserFactory.setIgnoringComments(false);

      DocumentBuilder builder = parserFactory.newDocumentBuilder();

      boolean dtdNotFound = false;
      Document doc = null;
      try {
        doc = builder.parse(is);
      } catch (FileNotFoundException e) {
        dtdNotFound = true;
      }

      // if dtd doesn't exist parse the document again without trying to load dtd
      if (dtdNotFound) {
        is = inFile.getContent().getInputStream();
        // disable dtd loading
        parserFactory.setFeature(
            "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        builder = parserFactory.newDocumentBuilder();
        doc = builder.parse(is);
      }

      DocumentType docType = doc.getDoctype();

      return doc;

    } finally {
      if (is != null)
        try {
          is.close();
        } catch (IOException e) {
          /**/
        }
    }
  }
예제 #5
0
 /** Does a 'touch' command. */
 private void touch(final String[] cmd) throws Exception {
   if (cmd.length < 2) {
     throw new Exception("USAGE: touch <path>");
   }
   final FileObject file = mgr.resolveFile(cwd, cmd[1]);
   if (!file.exists()) {
     file.createFile();
   }
   file.getContent().setLastModifiedTime(System.currentTimeMillis());
 }
예제 #6
0
  public boolean zipFiles(
      ServerDetailsDTO conDetails, String outputFileName, List<String> fileNames, String filePath) {
    UserAuthenticator auth =
        new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword());
    FileSystemOptions opts = new FileSystemOptions();
    FileObject fileObject = null;
    StandardFileSystemManager manager = null;
    manager = new StandardFileSystemManager();
    try {
      manager.init();
    } catch (FileSystemException e1) {
      e1.printStackTrace();
      return false;
    }
    try {
      DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
      SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
    } catch (FileSystemException e) {
      e.printStackTrace();
      return false;
    }
    try {
      System.out.println("Output to Zip : " + outputFileName);
      String fileUri = buildUri(conDetails, filePath + outputFileName);
      fileObject = manager.resolveFile(fileUri, opts);
      OutputStream os = fileObject.getContent().getOutputStream();
      ZipOutputStream zos = new ZipOutputStream(os);
      for (String file : fileNames) {
        System.out.println("File Added : " + file);
        ZipEntry ze = new ZipEntry(file);
        zos.putNextEntry(ze);
        zos.write(readFile(conDetails, filePath + file));
      }
      zos.closeEntry();
      // remember close it
      zos.close();
      return true;
    } catch (IOException ex) {
      ex.printStackTrace();
      return false;
    } finally {
      try {

        FileSystem fs = null;

        if (fileObject != null) {
          fs = fileObject.getFileSystem();
          manager.closeFileSystem(fs);
        }
      } finally {
      }
    }
  }
예제 #7
0
  @Test
  public void testLocalFileProvider() throws Exception {
    FileObject fo = null;
    try {
      fo = manager.resolveFile(testFile.getCanonicalPath());
      assertTrue(fo.exists());

      final InputStream ios = fo.getContent().getInputStream();
      final BufferedReader reader = new BufferedReader(new InputStreamReader(ios));
      assertEquals("test", reader.readLine());
    } finally {
      if (fo != null) fo.close();
    }
  }
 public void testDefault() throws Exception {
   Heuristics heuristics = RuleBasedHeuristics.getDefaultInstance();
   FileSystemManager fsm = VFS.getManager();
   FileObject root =
       fsm.resolveFile(
               RuleBasedHeuristicsTest.class.getResource("/heuristics/.root").toExternalForm())
           .getParent();
   for (Reason reason : Reason.values()) {
     for (FileObject file : root.resolveFile(reason.toString().toLowerCase()).getChildren()) {
       InputStream is = file.getContent().getInputStream();
       DeliveryStatus ds = new DeliveryStatus(is);
       is.close();
       assertEquals(
           "Reason for " + file,
           reason,
           heuristics.getReason(ds.getPerRecipientParts()[0].getDiagnostic()));
     }
   }
 }
예제 #9
0
  private File extractToTemp(FileObject fileObject) {
    String basename = fileObject.getName().getBaseName();
    File tempFile = null;
    try {
      String tmpPath = System.getProperty("java.io.tmpdir");
      tempFile = new File(FilenameUtils.concat(tmpPath, basename));

      byte[] buf = new byte[4096];
      BufferedInputStream bin = new BufferedInputStream(fileObject.getContent().getInputStream());
      BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(tempFile));
      while (bin.read(buf, 0, 1) != -1) {
        bout.write(buf, 0, 1);
      }
      bout.close();
      bin.close(); // by barney
    } catch (Throwable e) {
      e.printStackTrace();
    }
    return tempFile;
  }
예제 #10
0
  /**
   * Save the contained Package as a Zip file in the file system
   *
   * @param docxFile A destination FileObject
   * @return true if successful; false, otherwise.
   * @throws Docx4JException if there is an error
   */
  public boolean save(FileObject docxFile) throws Docx4JException {
    log.info("Saving to" + docxFile);

    boolean success = false;

    try {
      OutputStream docxOut = docxFile.getContent().getOutputStream();
      success = _saveToZipFile.save(docxOut);
    } catch (FileSystemException exc) {
      exc.printStackTrace();
      throw new Docx4JException("Failed to save package", exc);
    }

    try {
      docxFile.close();
    } catch (FileSystemException exc) {; // ignore
    }

    return success;
  }
예제 #11
0
  public Long getfileSize(ServerDetailsDTO conDetails, String filePath) {

    FileObject fileObject = null;
    StandardFileSystemManager manager = null;
    try {

      manager = new StandardFileSystemManager();
      manager.init();

      UserAuthenticator auth =
          new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword());
      FileSystemOptions opts = new FileSystemOptions();

      DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
      SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

      String fileUri = buildUri(conDetails, filePath);
      fileObject = manager.resolveFile(fileUri, opts);
      if (fileObject.exists()) {
        return fileObject.getContent().getSize();
      }
    } catch (FileSystemException e) {
      log.error("cannot access the server", e);
    } finally {
      try {
        if (fileObject != null) {
          ((SftpFileSystem) fileObject.getFileSystem()).closeCommunicationLink();
          FileSystem fs = null;
          fs = fileObject.getFileSystem();
          manager.closeFileSystem(fs);
        }
      } finally {
      }
    }
    return null;
  }
 /**
  * Load the properties file located at {@code file}
  *
  * @param file Location of a properties file to load
  * @return Loaded properties file
  * @throws IOException Error loading properties from file
  * @throws FileSystemException Error locating input stream for file
  */
 protected Properties loadProperties(FileObject file) throws FileSystemException, IOException {
   Properties p = new Properties();
   p.load(file.getContent().getInputStream());
   return p;
 }
예제 #13
0
  private boolean putFile(FileObject localFile, String remotefilename, SFTPv3Client sftpClient) {
    long filesize = -1;
    InputStream in = null;
    BufferedInputStream inBuf = null;
    SFTPv3FileHandle sftpFileHandle = null;
    boolean retval = false;

    try {
      // Put file in the folder
      sftpFileHandle = sftpClient.createFileTruncate(remotefilename);

      // Associate a file input stream for the current local file
      in = KettleVFS.getInputStream(localFile);
      inBuf = new BufferedInputStream(in);
      byte[] buf = new byte[2048];
      long offset = 0;
      long length = localFile.getContent().getSize();

      if (log.isDetailed()) {
        logDetailed(
            BaseMessages.getString(
                PKG,
                "JobSSH2PUT.Log.SendingFile",
                localFile.toString(),
                "" + length,
                remotefilename));
      }

      // Write to remote file
      while (true) {
        int len = in.read(buf, 0, buf.length);
        if (len <= 0) {
          break;
        }
        sftpClient.write(sftpFileHandle, offset, buf, 0, len);
        offset += len;
      }

      // Get File size
      filesize = getFileSize(sftpClient, remotefilename);

      if (log.isDetailed()) {
        logDetailed(
            BaseMessages.getString(
                PKG, "JobSSH2PUT.Log.FileOnRemoteHost", remotefilename, "" + filesize));
      }

      retval = true;
    } catch (Exception e) {
      // We failed to put files
      logError(
          BaseMessages.getString(PKG, "JobSSH2PUT.Log.ErrorCopyingFile", localFile.toString())
              + ":"
              + e.getMessage());
    } finally {
      if (in != null) {
        try {
          in.close();
          in = null;
        } catch (Exception ex) {
          // Ignore errors
        }
      }

      if (inBuf != null) {
        try {
          inBuf.close();
          inBuf = null;
        } catch (Exception ex) {
          // Ignore errors
        }
      }
      if (sftpFileHandle != null) {
        try {
          sftpClient.closeFile(sftpFileHandle);
          sftpFileHandle = null;
        } catch (Exception ex) {
          // Ignore errors
        }
      }
    }
    return retval;
  }
예제 #14
0
  public boolean writeFile(ServerDetailsDTO conDetails, String filePath, byte[] fileContent)
      throws IOException {
    FileObject fileObject2 = null;
    FileObject fileObject = null;
    StandardFileSystemManager manager = null;
    try {

      manager = new StandardFileSystemManager();
      manager.init();
      UserAuthenticator auth =
          new StaticUserAuthenticator(null, conDetails.getUserName(), conDetails.getPassword());
      FileSystemOptions opts = new FileSystemOptions();
      DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);
      SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

      String fileUri = buildUri(conDetails, filePath);
      fileObject = manager.resolveFile(fileUri + ".tmp", opts);
      OutputStream os = fileObject.getContent().getOutputStream();
      int size = fileContent.length;
      try {
        for (int index = 0; index < fileContent.length; index += 4000, size -= 4000) {
          os.write(fileContent, index, size > 4000 ? 4000 : size);
          if (Thread.currentThread().isInterrupted()) {
            fileObject.getContent().close();
            fileObject.delete();
            return false;
          }
        }
        os.flush();
      } finally {
        fileObject.getContent().close();
      }

      fileObject2 = manager.resolveFile(fileUri, opts);
      fileObject.moveTo(fileObject2);
      ((SftpFileSystem) fileObject2.getFileSystem()).closeCommunicationLink();
      ((SftpFileSystem) fileObject.getFileSystem()).closeCommunicationLink();

    } catch (IOException e) {
      throw e;
    } finally {
      try {

        FileSystem fs = null;

        if (fileObject2 != null) {
          fs = fileObject2.getFileSystem();
          manager.closeFileSystem(fs);
        }

        fs = null;
        if (fileObject != null) {
          fs = fileObject.getFileSystem();
          manager.closeFileSystem(fs);
        }
      } finally {
        // Just ignore the exception
      }
    }
    return true;
  }
  public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);

    // starttime (in seconds)
    long timeStart = System.currentTimeMillis() / 1000;

    if (filename != null) {
      FileObject fileObject = null;
      String realFilename = getRealFilename();
      try {
        fileObject = KettleVFS.getFileObject(realFilename, this);

        long iMaximumTimeout =
            Const.toInt(getRealMaximumTimeout(), Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0));
        long iCycleTime =
            Const.toInt(getRealCheckCycleTime(), Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 0));

        //
        // Sanity check on some values, and complain on insanity
        //
        if (iMaximumTimeout < 0) {
          iMaximumTimeout = Const.toInt(DEFAULT_MAXIMUM_TIMEOUT, 0);
          if (log.isBasic()) logBasic("Maximum timeout invalid, reset to " + iMaximumTimeout);
        }

        if (iCycleTime < 1) {
          // If lower than 1 set to the default
          iCycleTime = Const.toInt(DEFAULT_CHECK_CYCLE_TIME, 1);
          if (log.isBasic()) logBasic("Check cycle time invalid, reset to " + iCycleTime);
        }

        if (iMaximumTimeout == 0) {
          if (log.isBasic()) logBasic("Waiting indefinitely for file [" + realFilename + "]");
        } else {
          if (log.isBasic())
            logBasic("Waiting " + iMaximumTimeout + " seconds for file [" + realFilename + "]");
        }

        boolean continueLoop = true;
        while (continueLoop && !parentJob.isStopped()) {
          fileObject = KettleVFS.getFileObject(realFilename, this);

          if (fileObject.exists()) {
            // file exists, we're happy to exit
            if (log.isBasic()) logBasic("Detected file [" + realFilename + "] within timeout");
            result.setResult(true);
            continueLoop = false;

            // add filename to result filenames
            if (addFilenameToResult && fileObject.getType() == FileType.FILE) {
              ResultFile resultFile =
                  new ResultFile(
                      ResultFile.FILE_TYPE_GENERAL, fileObject, parentJob.getJobname(), toString());
              resultFile.setComment(BaseMessages.getString(PKG, "JobWaitForFile.FilenameAdded"));
              result.getResultFiles().put(resultFile.getFile().toString(), resultFile);
            }
          } else {
            long now = System.currentTimeMillis() / 1000;

            if ((iMaximumTimeout > 0) && (now > (timeStart + iMaximumTimeout))) {
              continueLoop = false;

              // file doesn't exist after timeout, either true or false
              if (isSuccessOnTimeout()) {
                if (log.isBasic())
                  logBasic("Didn't detect file [" + realFilename + "] before timeout, success");
                result.setResult(true);
              } else {
                if (log.isBasic())
                  logBasic("Didn't detect file [" + realFilename + "] before timeout, failure");
                result.setResult(false);
              }
            }

            // sleep algorithm
            long sleepTime = 0;

            if (iMaximumTimeout == 0) {
              sleepTime = iCycleTime;
            } else {
              if ((now + iCycleTime) < (timeStart + iMaximumTimeout)) {
                sleepTime = iCycleTime;
              } else {
                sleepTime = iCycleTime - ((now + iCycleTime) - (timeStart + iMaximumTimeout));
              }
            }

            try {
              if (sleepTime > 0) {
                if (log.isDetailed()) {
                  logDetailed(
                      "Sleeping "
                          + sleepTime
                          + " seconds before next check for file ["
                          + realFilename
                          + "]");
                }
                Thread.sleep(sleepTime * 1000);
              }
            } catch (InterruptedException e) {
              // something strange happened
              result.setResult(false);
              continueLoop = false;
            }
          }
        }

        if (!parentJob.isStopped() && fileObject.exists() && isFileSizeCheck()) {
          long oldSize = -1;
          long newSize = fileObject.getContent().getSize();

          if (log.isDetailed())
            logDetailed("File [" + realFilename + "] is " + newSize + " bytes long");
          if (log.isBasic())
            logBasic(
                "Waiting until file ["
                    + realFilename
                    + "] stops growing for "
                    + iCycleTime
                    + " seconds");
          while (oldSize != newSize && !parentJob.isStopped()) {
            try {
              if (log.isDetailed()) {
                logDetailed(
                    "Sleeping "
                        + iCycleTime
                        + " seconds, waiting for file ["
                        + realFilename
                        + "] to stop growing");
              }
              Thread.sleep(iCycleTime * 1000);
            } catch (InterruptedException e) {
              // something strange happened
              result.setResult(false);
              continueLoop = false;
            }
            oldSize = newSize;
            newSize = fileObject.getContent().getSize();
            if (log.isDetailed()) {
              logDetailed("File [" + realFilename + "] is " + newSize + " bytes long");
            }
          }
          if (log.isBasic())
            logBasic("Stopped waiting for file [" + realFilename + "] to stop growing");
        }

        if (parentJob.isStopped()) {
          result.setResult(false);
        }
      } catch (Exception e) {
        logBasic("Exception while waiting for file [" + realFilename + "] to stop growing", e);
      } finally {
        if (fileObject != null) {
          try {
            fileObject.close();
          } catch (Exception e) {
          }
        }
      }
    } else {
      logError("No filename is defined.");
    }

    return result;
  }
예제 #16
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;
  }
예제 #17
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;
  }