Search in sources :

Example 6 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class S3Panel method getServerURL.

// //////////////////////////////
// ServerPanel implementation //
// //////////////////////////////
@Override
public FileURL getServerURL() throws MalformedURLException {
    updateValues();
    if (!lastInitialDir.startsWith("/"))
        lastInitialDir = "/" + lastInitialDir;
    FileURL url = FileURL.getFileURL(FileProtocols.S3 + "://" + lastServer + lastInitialDir);
    // Set credentials
    url.setCredentials(new Credentials(lastUsername, lastPassword));
    // Set port
    url.setPort(lastPort);
    url.setProperty(S3File.STORAGE_TYPE, lastStorageType);
    url.setProperty(S3File.DISABLE_DNS_BUCKETS, String.valueOf(lastDisableDnsBuckets));
    url.setProperty(S3File.SECUTRE_HTTP, String.valueOf(lastSecureHttp));
    url.setProperty(S3File.DEFAULT_BUCKET_LOCATION, lastLocation);
    return url;
}
Also used : FileURL(com.mucommander.commons.file.FileURL) Credentials(com.mucommander.commons.file.Credentials)

Example 7 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class SFTPConnectionHandler method startConnection.

// ////////////////////////////////////
// ConnectionHandler implementation //
// ////////////////////////////////////
@Override
public void startConnection() throws IOException {
    LOGGER.info("starting connection to {}", realm);
    try {
        FileURL realm = getRealm();
        // Retrieve credentials to be used to authenticate
        final Credentials credentials = getCredentials();
        // Throw an AuthException if no auth information, required for SSH
        if (credentials == null)
            // Todo: localize this entry
            throwAuthException("Login and password required");
        LOGGER.trace("creating SshClient");
        JSch jsch = new JSch();
        // Override default port (22) if a custom port was specified in the URL
        int port = realm.getPort();
        if (port == -1)
            port = 22;
        String privateKeyPath = realm.getProperty(SFTPFile.PRIVATE_KEY_PATH_PROPERTY_NAME);
        if (privateKeyPath != null) {
            LOGGER.info("Using {} authentication method", PUBLIC_KEY_AUTH_METHOD);
            jsch.addIdentity(privateKeyPath);
        }
        session = jsch.getSession(credentials.getLogin(), realm.getHost(), port);
        session.setUserInfo(new PasswordAuthentication());
        session.connect(5 * 1000);
        // Init SFTP connections
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect(5 * 1000);
        LOGGER.info("authentication complete");
    } catch (IOException e) {
        LOGGER.info("IOException thrown while starting connection", e);
        // Disconnect if something went wrong
        if (session != null && session.isConnected())
            session.disconnect();
        ;
        session = null;
        channelSftp = null;
        // Re-throw exception
        throw e;
    } catch (JSchException e) {
        LOGGER.info("Caught exception while authenticating: {}", e.getMessage());
        LOGGER.debug("Exception:", e);
        throwAuthException(e.getMessage());
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) FileURL(com.mucommander.commons.file.FileURL) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Credentials(com.mucommander.commons.file.Credentials)

Example 8 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class SFTPFile method getCanonicalPath.

@Override
public String getCanonicalPath() {
    if (isSymlink()) {
        // Check if there is a previous value that hasn't expired yet
        if (canonicalPath != null && (System.currentTimeMillis() - canonicalPathFetchedTime < attributeCachingPeriod))
            return canonicalPath;
        try (SFTPConnectionHandler connHandler = (SFTPConnectionHandler) ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true)) {
            // Makes sure the connection is started, if not starts it
            connHandler.checkConnection();
            // getSymbolicLinkTarget returns the raw symlink target which can either be an absolute path or a
            // relative path. If the path is relative preprend the absolute path of the symlink's parent folder.
            String symlinkTargetPath = connHandler.channelSftp.readlink(fileURL.getPath());
            if (!symlinkTargetPath.startsWith("/")) {
                String parentPath = fileURL.getParent().getPath();
                if (!parentPath.endsWith("/"))
                    parentPath += "/";
                symlinkTargetPath = parentPath + symlinkTargetPath;
            }
            FileURL canonicalURL = (FileURL) fileURL.clone();
            canonicalURL.setPath(symlinkTargetPath);
            // Cache the value and return it until it expires
            canonicalPath = canonicalURL.toString(false);
            canonicalPathFetchedTime = System.currentTimeMillis();
            return canonicalPath;
        } catch (Exception e) {
            LOGGER.warn("failed to get canonical path of symlink %s", getURL());
        }
    }
    // If this file is not a symlink, or the symlink target path could not be retrieved, return the absolute path
    return getAbsolutePath();
}
Also used : FileURL(com.mucommander.commons.file.FileURL) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) UnsupportedFileOperationException(com.mucommander.commons.file.UnsupportedFileOperationException) AuthException(com.mucommander.commons.file.AuthException)

Example 9 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class SFTPFile method getParent.

@Override
public AbstractFile getParent() {
    if (!parentValSet) {
        FileURL parentFileURL = this.fileURL.getParent();
        if (parentFileURL != null) {
            parent = FileFactory.getFile(parentFileURL);
        // Note: parent may be null if it can't be resolved
        }
        parentValSet = true;
    }
    return parent;
}
Also used : FileURL(com.mucommander.commons.file.FileURL)

Example 10 with FileURL

use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.

the class SFTPFile method ls.

@SuppressWarnings("unchecked")
@Override
public AbstractFile[] ls() throws IOException {
    List<LsEntry> files = new ArrayList<LsEntry>();
    try (SFTPConnectionHandler connHandler = (SFTPConnectionHandler) ConnectionPool.getConnectionHandler(connHandlerFactory, fileURL, true)) {
        // Makes sure the connection is started, if not starts it
        connHandler.checkConnection();
        files = connHandler.channelSftp.ls(absPath);
    } catch (Exception e) {
        LOGGER.error("failed to ls %s", getURL());
    }
    int nbFiles = files.size();
    // File doesn't exist, return an empty file array
    if (nbFiles == 0)
        return new AbstractFile[] {};
    AbstractFile[] children = new AbstractFile[nbFiles];
    FileURL childURL;
    String filename;
    int fileCount = 0;
    String parentPath = fileURL.getPath();
    if (!parentPath.endsWith(SEPARATOR))
        parentPath += SEPARATOR;
    // Fill AbstractFile array and discard '.' and '..' files
    for (LsEntry file : files) {
        filename = file.getFilename();
        // Discard '.' and '..' files, dunno why these are returned
        if (filename.equals(".") || filename.equals(".."))
            continue;
        childURL = (FileURL) fileURL.clone();
        childURL.setPath(parentPath + filename);
        children[fileCount++] = FileFactory.getFile(childURL, this, Collections.singletonMap("attributes", new SFTPFileAttributes(childURL, file.getAttrs())));
    }
    // Create new array of the exact file count
    if (fileCount < nbFiles) {
        AbstractFile[] newChildren = new AbstractFile[fileCount];
        System.arraycopy(children, 0, newChildren, 0, fileCount);
        return newChildren;
    }
    return children;
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile) ArrayList(java.util.ArrayList) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) UnsupportedFileOperationException(com.mucommander.commons.file.UnsupportedFileOperationException) AuthException(com.mucommander.commons.file.AuthException)

Aggregations

FileURL (com.mucommander.commons.file.FileURL)60 AbstractFile (com.mucommander.commons.file.AbstractFile)18 Credentials (com.mucommander.commons.file.Credentials)16 IOException (java.io.IOException)11 AuthException (com.mucommander.commons.file.AuthException)7 MalformedURLException (java.net.MalformedURLException)5 CredentialsMapping (com.mucommander.auth.CredentialsMapping)3 UnsupportedFileOperationException (com.mucommander.commons.file.UnsupportedFileOperationException)3 ProtocolFile (com.mucommander.commons.file.protocol.ProtocolFile)3 File (java.io.File)3 SftpException (com.jcraft.jsch.SftpException)2 AuthDialog (com.mucommander.ui.dialog.auth.AuthDialog)2 RandomAccessFile (java.io.RandomAccessFile)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 DbxException (com.dropbox.core.DbxException)1 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 JsonReader (com.dropbox.core.json.JsonReader)1 DbxCredential (com.dropbox.core.oauth.DbxCredential)1