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;
}
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());
}
}
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();
}
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;
}
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;
}
Aggregations