@Override FileURL getServerURL() throws MalformedURLException { updateValues(); if(!lastInitialDir.startsWith("/")) lastInitialDir = "/"+lastInitialDir; FileURL url = FileURL.getFileURL(FileProtocols.SFTP+"://"+lastServer+lastInitialDir); // Set credentials url.setCredentials(new Credentials(lastUsername, lastPassword)); if(!"".equals(lastKeyPath.trim())) url.setProperty(SFTPFile.PRIVATE_KEY_PATH_PROPERTY_NAME, lastKeyPath); // Set port url.setPort(lastPort); return url; }
/** * Returns a child of this file, whose path is the concatenation of this file's path and the given relative path. * Although this method does not enforce it, the specified path should be relative, i.e. should not start with * a separator.<br/> * An <code>IOException</code> may be thrown if the child file could not be instantiated but the returned file * instance should never be <code>null</code>. * * @param relativePath the child's path, relative to this file's path * @return an AbstractFile representing the requested child file, never null * @throws IOException if the child file could not be instantiated */ public final AbstractFile getChild(String relativePath) throws IOException { FileURL childURL = (FileURL)getURL().clone(); childURL.setPath(addTrailingSeparator(childURL.getPath())+ relativePath); return FileFactory.getFile(childURL, true); }
@Override protected FileSystem getHadoopFileSystem(FileURL url) throws IOException { if(!url.containsCredentials()) throw new AuthException(url); // Note: getRealm returns a fresh instance every time FileURL realm = url.getRealm(); // Import credentials Credentials creds = url.getCredentials(); if(creds!=null) { // URL-encode secret as it may contain non URL-safe characters ('+' and '/') realm.setCredentials(new Credentials(creds.getLogin(), URLEncoder.encode(creds.getPassword(), "UTF-8"))); } // Change the scheme to the actual Hadoop fileystem (s3 -> s3n) realm.setScheme("s3n"); return FileSystem.get(URI.create(realm.toString(true, false)), DEFAULT_CONFIGURATION); }
/** * Creates a new ConnectionHandler for the given server URL using the Credentials included in the URL (potentially * <code>null</code>). * * @param serverURL URL of the server to connect to */ public ConnectionHandler(FileURL serverURL) { realm = serverURL.getRealm(); this.credentials = serverURL.getCredentials(); }
/** * Creates a new OpenLocationAction instance using the provided url's string representation * (with credentials stripped out) as label. */ public OpenLocationAction(MainFrame mainFrame, Map<String,Object> properties, FileURL url) { this(mainFrame, properties, url, url.getScheme().equals(FileProtocols.FILE)?url.getPath():url.toString(false)); }
@Override public FileURL getRealm(FileURL location) { FileURL realm = new FileURL(this); String newPath = location.getPath(); // Find first path token (share) int pos = newPath.indexOf('/', 1); newPath = newPath.substring(0, pos==-1?newPath.length():pos+1); realm.setPath(newPath); realm.setScheme(location.getScheme()); realm.setHost(location.getHost()); realm.setPort(location.getPort()); // Copy properties (if any) realm.importProperties(location); return realm; } });
url.setScheme(scheme); url.setCredentials(credentials); url.setHost(host); url.setPort(port); url.setPath(path); url.setQuery(query); url.setProperty("name", "value"); assert scheme.equals(url.getScheme()); assert credentials.equals(url.getCredentials(), true); assert host.equals(url.getHost()); assert port == url.getPort(); assert path.equals(url.getPath()); assert query.equals(url.getQuery()); assert "to".equals(url.getFilename()); assert "value".equals(url.getProperty("name")); url.setCredentials(null); url.setHost(null); url.setPort(-1); url.setPath("/"); url.setQuery(null); url.setProperty("name", null); assert scheme.equals(url.getScheme()); assert url.getCredentials() == null; assert !url.containsCredentials(); assert url.getHost() == null; assert -1 == url.getPort();
FileURL url = FileURL.getFileURL(sb.toString()); assert scheme.equals(url.getScheme()); assert login.equals(url.getLogin()); assert url.containsCredentials(); assert password.equals(url.getPassword()); assert new Credentials(login, password).equals(url.getCredentials(), true); assert host.equals(url.getHost()); assert port == url.getPort(); assert url.getQuery() == null; path = path+"?"+query; assert url.getQuery() == null; else assert query.equals(url.getQuery()); assert url.equals(FileURL.getFileURL(url.toString(true, false))); assert url.equals(FileURL.getFileURL(url.toString(false, false)), false, false);
public void testParent() throws MalformedURLException { FileURL url = getURL("login", "password", "host", 10000, "/path/to", "query¶m=value"); url.setProperty("key", "value"); FileURL parentURL = url.getParent(); assert "path".equals(parentURL.getFilename()); assert url.getScheme().equals(parentURL.getScheme()); assert url.getHost().equals(parentURL.getHost()); assert url.getPort() == parentURL.getPort(); assert url.getCredentials().equals(parentURL.getCredentials()); assert url.getLogin().equals(parentURL.getLogin()); assert url.getPassword().equals(parentURL.getPassword()); assert "value".equals(parentURL.getProperty("key")); assert url.getHandler().equals(parentURL.getHandler()); assert parentURL.getQuery() == null; parentURL = url.getParent(); assert parentURL.getFilename() == null; assert url.getParent() == null;
destURL = FileURL.getFileURL(destPath); destURL = (FileURL)baseFolderURL.clone(); String basePath = destURL.getPath(); if(!destPath.equals("")) destURL.setPath(basePath + (basePath.endsWith(separator)?"":separator) + destPath); destURL = FileURL.getFileURL(destURL.toString(false)); destURL.setCredentials(baseFolderURL.getCredentials()); destURL.importProperties(baseFolderURL);
url = FileURL.getFileURL("C:\\"); assert "file".equals(url.getScheme()); assert "localhost".equals(url.getHost()); assert "/C:\\".equals(url.getPath()); url = FileURL.getFileURL("C:\\dir\\file"); assert "file".equals(url.getScheme()); assert "localhost".equals(url.getHost()); assert "/C:\\dir\\file".equals(url.getPath()); assert "file".equals(url.getFilename()); url = url.getParent(); assert "file".equals(url.getScheme()); assert "localhost".equals(url.getHost()); assert "/C:\\dir\\".equals(url.getPath()); assert "dir".equals(url.getFilename()); url = FileURL.getFileURL("C:\\direc/tory"); assert "file".equals(url.getScheme()); assert "localhost".equals(url.getHost()); assert "/C:\\direc/tory".equals(url.getPath()); assert "direc/tory".equals(url.getFilename()); url = FileURL.getFileURL("C:/"); assert "file".equals(url.getScheme()); assert "localhost".equals(url.getHost()); assert "/C:\\".equals(url.getPath()); url = FileURL.getFileURL("C:/dir/file");
AuthenticationType authenticationType = folderURL.getAuthenticationType(); if(credentialsMapping!=null) { newCredentialsMapping = credentialsMapping; else if(!folderURL.containsCredentials() && ( (authenticationType==AuthenticationType.AUTHENTICATION_REQUIRED) || (authenticationType==AuthenticationType.AUTHENTICATION_OPTIONAL && CredentialsManager.getMatchingCredentials(folderURL).length>0))) { try { FileURL newURL = FileURL.getFileURL(folder.getCanonicalPath()); newURL.setCredentials(folderURL.getCredentials()); newURL.importProperties(folderURL); this.folderURL = newURL;
/** * Returns a URL with the same scheme, host and port (if any) as the specified URL, and a path set to * <code>"/"</code> or <code>"\"</code> depending on the URL format. * The login, password, query and fragment parts of the returned URL are always <code>null</code>. * For example, when called with <code>http://www.mucommander.com:8080/path/to/file?query¶m=value</code>, * this method returns <code>http://www.mucommander.com:8080/</code>. * * @param location the location for which to return the authentication realm * @return the authentication realm of the specified location */ public FileURL getRealm(FileURL location) { // Start by cloning the given URL and then modify the parts that need it FileURL realm = (FileURL)location.clone(); realm.setPath(location.getPathSeparator()); realm.setCredentials(null); realm.setQuery(null); // Todo // realm.setFragment(null) return realm; } }
/** * Ensure that non URL-safe characters in login and password parts are properly handled, both when parsing * and representing URLs as string. * * @throws MalformedURLException should not happen */ @Test public void testCredentialsURLEncoding() throws MalformedURLException { FileURL url = getRootURL(); String urlDecodedString = ":@&=+$,/?t%#[]"; String urlEncodedString = "%3A%40%26%3D%2B%24%2C%2F%3Ft%25%23%5B%5D"; url.setCredentials(new Credentials(urlDecodedString, urlDecodedString)); String urlRep = url.getScheme()+"://"+urlEncodedString+":"+urlEncodedString+"@"; assert urlRep.equals(url.toString(true, false)); url = FileURL.getFileURL(urlRep); Credentials credentials = url.getCredentials(); assert credentials.getLogin().equals(urlDecodedString); assert credentials.getPassword().equals(urlDecodedString); }
/** * Verifies the given path is not null, that it can be resolved by {@link FileFactory#getFile(String)} into * a file, and that this file is equal to the given one. If the given file is not a directory, the contents of both * file instances are compared to make sure they are equal. * * @param file the file instance that corresponds to the given path * @param path the path that should be resolved into the specified file * @throws IOException should not happen * @throws NoSuchAlgorithmException should not happen */ protected void testPathResolution(AbstractFile file, String path) throws IOException, NoSuchAlgorithmException { assert path != null; // If the file is authenticated, test if the given path contains credentials and if it does not, add the // credentials to it. if(file.getURL().containsCredentials()) { FileURL fileURL = FileURL.getFileURL(path); if(!fileURL.containsCredentials()) { fileURL.setCredentials(file.getURL().getCredentials()); path = fileURL.toString(true); } } // Assert that the file can be resolved again using the path, and that the resolved file is shallow-equal // and deep-equal AbstractFile resolvedFile = FileFactory.getFile(path); assert resolvedFile != null; assert resolvedFile.equals(file); // Shallow equals assert resolvedFile.isDirectory()==file.isDirectory(); if(!file.isDirectory()) assertContentsEquals(file, resolvedFile); // Deep equals (compares contents) }
protected HTTPFile(FileURL fileURL, URL url) throws IOException { super(fileURL); String scheme = fileURL.getScheme().toLowerCase(); if((!scheme.equals(FileProtocols.HTTP) && !scheme.equals(FileProtocols.HTTPS)) || fileURL.getHost()==null) throw new IOException(); this.url = url; attributes = getDefaultAttributes(); String mimeType; String filename = fileURL.getFilename(); // Simple/fuzzy heuristic to avoid file resolution (HEAD) in cases where we have good reasons to believe that // the URL denotes a HTML/XTHML document: // - URL's path has no filename (e.g. http://www.mucommander.com/) or path ends with '/' (e.g. http://www.mucommander.com/download/) // - URL has a query part (works most of the time, must not always) // - URL has an extension that registered with an HTML/XHTML mime type if((filename==null || fileURL.getPath().endsWith("/") || fileURL.getQuery()!=null || ((mimeType=MimeTypes.getMimeType(this))!=null && isParsableMimeType(mimeType)))) { attributes.setDirectory(true); resolve = false; } else { resolve = true; } }
String nfsVersion = fileURL.getProperty(NFS_VERSION_PROPERTY_NAME); if(nfsVersion==null) nfsVersion = DEFAULT_NFS_VERSION; String nfsProtocol = fileURL.getProperty(NFS_PROTOCOL_PROPERTY_NAME); nfsProtocol = NFS_PROTOCOL_TCP.equals(nfsProtocol)?"t":NFS_PROTOCOL_UDP.equals(nfsProtocol)?"u":""; int port = fileURL.getPort(); String portString = port==-1||port==2049?"":""+port; this.file = new XFile("nfs://"+fileURL.getHost()+":"+portString+nfsVersion+nfsProtocol+"m"+"/"+fileURL.getPath()); this.absPath = fileURL.toString();
/** * Tests the resolution of Windows UNC paths (e.g. \\host\\share). This test is system-dependant. * * @throws MalformedURLException should not happen */ @Test public void testUNCParsing() throws MalformedURLException { FileURL url = FileURL.getFileURL("\\\\host\\share"); // UNC path will be transformed into either a 'file' or a 'smb' URL, depending on the current OS assert (OsFamily.WINDOWS.isCurrent()?"file":"smb").equals(url.getScheme()); assert "host".equals(url.getHost()); assert "/share".equals(url.getPath()); }
if(folderURL.getScheme().equals(FileProtocols.FILE)) { if (FileURL.LOCALHOST.equals(folderURL.getHost())) { locationText = folderURL.getPath(); locationText = "\\\\" + folderURL.getHost() + folderURL.getPath().replace('/', '\\'); if(!locationText.endsWith(UNCFile.SEPARATOR)) locationText += UNCFile.SEPARATOR; locationText = folderURL.toString(false);
/** * Use the credentials and realm properties of the specified <code>CredentialsMapping</code> to authenticate the * given {@link FileURL}. * <p>Any credentials contained by the <code>FileURL</code> will be lost and replaced with the new ones. * If properties with the same key are defined both in the realm and the given FileURL, the ones from the FileURL * will be preserved.</p> * * @param location the FileURL to authenticate * @param credentialsMapping the credentials to use to authenticate the given FileURL */ public static void authenticate(FileURL location, CredentialsMapping credentialsMapping) { location.setCredentials(credentialsMapping.getCredentials()); FileURL realm = credentialsMapping.getRealm(); Enumeration<String> propertyKeys = realm.getPropertyNames(); String key; while(propertyKeys.hasMoreElements()) { key = propertyKeys.nextElement(); if(location.getProperty(key)==null) location.setProperty(key, realm.getProperty(key)); } }