Example #1
0
 private boolean isUseList() {
   RemoteFileConfiguration config = (RemoteFileConfiguration) endpoint.getConfiguration();
   return config.isUseList();
 }
Example #2
0
 private boolean isStepwise() {
   RemoteFileConfiguration config = (RemoteFileConfiguration) endpoint.getConfiguration();
   return config.isStepwise();
 }
Example #3
0
  public boolean connect(RemoteFileConfiguration configuration)
      throws GenericFileOperationFailedException {
    if (isConnected()) {
      // already connected
      return true;
    }

    boolean connected = false;
    int attempt = 0;

    while (!connected) {
      try {
        if (LOG.isTraceEnabled() && attempt > 0) {
          LOG.trace(
              "Reconnect attempt #{} connecting to + {}",
              attempt,
              configuration.remoteServerInformation());
        }

        if (channel == null || !channel.isConnected()) {
          if (session == null || !session.isConnected()) {
            LOG.trace("Session isn't connected, trying to recreate and connect.");
            session = createSession(configuration);
            if (endpoint.getConfiguration().getConnectTimeout() > 0) {
              LOG.trace(
                  "Connecting use connectTimeout: "
                      + endpoint.getConfiguration().getConnectTimeout()
                      + " ...");
              session.connect(endpoint.getConfiguration().getConnectTimeout());
            } else {
              LOG.trace("Connecting ...");
              session.connect();
            }
          }

          LOG.trace("Channel isn't connected, trying to recreate and connect.");
          channel = (ChannelSftp) session.openChannel("sftp");

          if (endpoint.getConfiguration().getConnectTimeout() > 0) {
            LOG.trace(
                "Connecting use connectTimeout: "
                    + endpoint.getConfiguration().getConnectTimeout()
                    + " ...");
            channel.connect(endpoint.getConfiguration().getConnectTimeout());
          } else {
            LOG.trace("Connecting ...");
            channel.connect();
          }
          LOG.info("Connected to " + configuration.remoteServerInformation());
        }

        // yes we could connect
        connected = true;
      } catch (Exception e) {
        // check if we are interrupted so we can break out
        if (Thread.currentThread().isInterrupted()) {
          throw new GenericFileOperationFailedException(
              "Interrupted during connecting",
              new InterruptedException("Interrupted during connecting"));
        }

        GenericFileOperationFailedException failed =
            new GenericFileOperationFailedException(
                "Cannot connect to " + configuration.remoteServerInformation(), e);
        LOG.trace("Cannot connect due: {}", failed.getMessage());
        attempt++;
        if (attempt > endpoint.getMaximumReconnectAttempts()) {
          throw failed;
        }
        if (endpoint.getReconnectDelay() > 0) {
          try {
            Thread.sleep(endpoint.getReconnectDelay());
          } catch (InterruptedException ie) {
            // we could potentially also be interrupted during sleep
            Thread.currentThread().interrupt();
            throw new GenericFileOperationFailedException("Interrupted during sleeping", ie);
          }
        }
      }
    }

    return true;
  }
Example #4
0
 /**
  * Validates this endpoint if its configured properly.
  *
  * @throws Exception is thrown if endpoint is invalid configured for its mandatory options
  */
 protected void afterPropertiesSet() throws Exception {
   RemoteFileConfiguration config = getConfiguration();
   ObjectHelper.notEmpty(config.getHost(), "host");
   ObjectHelper.notEmpty(config.getProtocol(), "protocol");
 }
Example #5
0
  protected Session createSession(final RemoteFileConfiguration configuration)
      throws JSchException {
    final JSch jsch = new JSch();
    JSch.setLogger(new JSchLogger());

    SftpConfiguration sftpConfig = (SftpConfiguration) configuration;

    if (isNotEmpty(sftpConfig.getCiphers())) {
      LOG.debug("Using ciphers: {}", sftpConfig.getCiphers());
      Hashtable<String, String> ciphers = new Hashtable<String, String>();
      ciphers.put("cipher.s2c", sftpConfig.getCiphers());
      ciphers.put("cipher.c2s", sftpConfig.getCiphers());
      JSch.setConfig(ciphers);
    }

    if (isNotEmpty(sftpConfig.getPrivateKeyFile())) {
      LOG.debug("Using private keyfile: {}", sftpConfig.getPrivateKeyFile());
      if (isNotEmpty(sftpConfig.getPrivateKeyPassphrase())) {
        jsch.addIdentity(sftpConfig.getPrivateKeyFile(), sftpConfig.getPrivateKeyPassphrase());
      } else {
        jsch.addIdentity(sftpConfig.getPrivateKeyFile());
      }
    }

    if (sftpConfig.getPrivateKey() != null) {
      LOG.debug("Using private key information from byte array");
      byte[] passphrase = null;
      if (isNotEmpty(sftpConfig.getPrivateKeyPassphrase())) {
        try {
          passphrase = sftpConfig.getPrivateKeyPassphrase().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
          throw new JSchException("Cannot transform passphrase to byte[]", e);
        }
      }
      jsch.addIdentity("ID", sftpConfig.getPrivateKey(), null, passphrase);
    }

    if (sftpConfig.getPrivateKeyUri() != null) {
      LOG.debug("Using private key uri : {}", sftpConfig.getPrivateKeyUri());
      byte[] passphrase = null;
      if (isNotEmpty(sftpConfig.getPrivateKeyPassphrase())) {
        try {
          passphrase = sftpConfig.getPrivateKeyPassphrase().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
          throw new JSchException("Cannot transform passphrase to byte[]", e);
        }
      }
      try {
        InputStream is =
            ResourceHelper.resolveMandatoryResourceAsInputStream(
                endpoint.getCamelContext().getClassResolver(), sftpConfig.getPrivateKeyUri());
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOHelper.copyAndCloseInput(is, bos);
        jsch.addIdentity("ID", bos.toByteArray(), null, passphrase);
      } catch (IOException e) {
        throw new JSchException("Cannot read resource: " + sftpConfig.getPrivateKeyUri(), e);
      }
    }

    if (sftpConfig.getKeyPair() != null) {
      LOG.debug("Using private key information from key pair");
      KeyPair keyPair = sftpConfig.getKeyPair();
      if (keyPair.getPrivate() != null && keyPair.getPublic() != null) {
        if (keyPair.getPrivate() instanceof RSAPrivateKey
            && keyPair.getPublic() instanceof RSAPublicKey) {
          jsch.addIdentity(new RSAKeyPairIdentity("ID", keyPair), null);
        } else if (keyPair.getPrivate() instanceof DSAPrivateKey
            && keyPair.getPublic() instanceof DSAPublicKey) {
          jsch.addIdentity(new DSAKeyPairIdentity("ID", keyPair), null);
        } else {
          LOG.warn("Only RSA and DSA key pairs are supported");
        }
      } else {
        LOG.warn("PrivateKey and PublicKey in the KeyPair must be filled");
      }
    }

    if (isNotEmpty(sftpConfig.getKnownHostsFile())) {
      LOG.debug("Using knownhosts file: {}", sftpConfig.getKnownHostsFile());
      jsch.setKnownHosts(sftpConfig.getKnownHostsFile());
    }

    if (isNotEmpty(sftpConfig.getKnownHostsUri())) {
      LOG.debug("Using knownhosts uri: {}", sftpConfig.getKnownHostsUri());
      try {
        InputStream is =
            ResourceHelper.resolveMandatoryResourceAsInputStream(
                endpoint.getCamelContext().getClassResolver(), sftpConfig.getKnownHostsUri());
        jsch.setKnownHosts(is);
      } catch (IOException e) {
        throw new JSchException("Cannot read resource: " + sftpConfig.getKnownHostsUri(), e);
      }
    }

    if (sftpConfig.getKnownHosts() != null) {
      LOG.debug("Using knownhosts information from byte array");
      jsch.setKnownHosts(new ByteArrayInputStream(sftpConfig.getKnownHosts()));
    }

    final Session session =
        jsch.getSession(
            configuration.getUsername(), configuration.getHost(), configuration.getPort());

    if (isNotEmpty(sftpConfig.getStrictHostKeyChecking())) {
      LOG.debug("Using StrickHostKeyChecking: {}", sftpConfig.getStrictHostKeyChecking());
      session.setConfig("StrictHostKeyChecking", sftpConfig.getStrictHostKeyChecking());
    }

    session.setServerAliveInterval(sftpConfig.getServerAliveInterval());
    session.setServerAliveCountMax(sftpConfig.getServerAliveCountMax());

    // compression
    if (sftpConfig.getCompression() > 0) {
      LOG.debug("Using compression: {}", sftpConfig.getCompression());
      session.setConfig("compression.s2c", "[email protected],zlib,none");
      session.setConfig("compression.c2s", "[email protected],zlib,none");
      session.setConfig("compression_level", Integer.toString(sftpConfig.getCompression()));
    }

    // set the PreferredAuthentications
    if (sftpConfig.getPreferredAuthentications() != null) {
      LOG.debug("Using PreferredAuthentications: {}", sftpConfig.getPreferredAuthentications());
      session.setConfig("PreferredAuthentications", sftpConfig.getPreferredAuthentications());
    }

    // set user information
    session.setUserInfo(
        new ExtendedUserInfo() {
          public String getPassphrase() {
            return null;
          }

          public String getPassword() {
            return configuration.getPassword();
          }

          public boolean promptPassword(String s) {
            return true;
          }

          public boolean promptPassphrase(String s) {
            return true;
          }

          public boolean promptYesNo(String s) {
            LOG.warn("Server asks for confirmation (yes|no): " + s + ". Camel will answer no.");
            // Return 'false' indicating modification of the hosts file is disabled.
            return false;
          }

          public void showMessage(String s) {
            LOG.trace("Message received from Server: " + s);
          }

          public String[] promptKeyboardInteractive(
              String destination,
              String name,
              String instruction,
              String[] prompt,
              boolean[] echo) {
            // must return an empty array if password is null
            if (configuration.getPassword() == null) {
              return new String[0];
            } else {
              return new String[] {configuration.getPassword()};
            }
          }
        });

    // set the SO_TIMEOUT for the time after the connect phase
    if (configuration.getSoTimeout() > 0) {
      session.setTimeout(configuration.getSoTimeout());
    }

    // set proxy if configured
    if (proxy != null) {
      session.setProxy(proxy);
    }

    return session;
  }