use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.
the class BackupInputStream method getInputStream.
/**
* Opens a stream on the right file.
* <p>
* If a backup file is found, and is bigger than the target file, then it will be used.
* </p>
* @param file file on which to open an input stream.
* @return a stream on the right file.
* @exception IOException thrown if any IO related error occurs.
*/
private static InputStream getInputStream(AbstractFile file) throws IOException {
AbstractFile backup;
FileURL test;
test = (FileURL) file.getURL().clone();
test.setPath(test.getPath() + BACKUP_SUFFIX);
// Checks whether the backup file is a better choice than the target one.
backup = FileFactory.getFile(test);
if (backup != null && backup.exists() && (file.getSize() < backup.getSize()))
return backup.getInputStream();
// Opens a stream on the target file.
return file.getInputStream();
}
use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.
the class CredentialsManager method findMatches.
/**
* Looks for credentials matching the specified location in the given credentials Vector and adds them to the given
* matches Vector.
*
* @param location the location to find matching credentials for
* @param credentials the Vector containing the CredentialsMapping instances to compare to the given location
* @param matches the Vector where matching CredentialsMapping instances will be added
*/
private static void findMatches(FileURL location, List<CredentialsMapping> credentials, List<CredentialsMapping> matches) {
FileURL tempRealm;
int nbEntries = credentials.size();
for (CredentialsMapping tempCredentialsMapping : credentials) {
tempRealm = tempCredentialsMapping.getRealm();
if (location.schemeEquals(tempRealm) && location.portEquals(tempRealm) && location.hostEquals(tempRealm))
matches.add(tempCredentialsMapping);
}
LOGGER.trace("returning matches=" + matches);
}
use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.
the class CredentialsManager method authenticate.
/**
* 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));
}
}
use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.
the class CredentialsWriter method write.
/**
* Writes the credentials XML file in the user's preferences folder.
* This method should only be called by {@link CredentialsManager}.
*/
static void write(OutputStream stream) throws IOException {
XmlWriter out = new XmlWriter(stream);
// Root element, add the encryption method used
XmlAttributes attributes = new XmlAttributes();
attributes.add(ATTRIBUTE_ENCRYPTION, WEAK_ENCRYPTION_METHOD);
// Version the file
attributes.add(ATTRIBUTE_VERSION, RuntimeConstants.VERSION);
out.startElement(ELEMENT_ROOT, attributes);
out.println();
Iterator<CredentialsMapping> iterator = CredentialsManager.getPersistentCredentialMappings().iterator();
CredentialsMapping credentialsMapping;
FileURL realm;
Enumeration<String> propertyKeys;
String name;
while (iterator.hasNext()) {
credentialsMapping = iterator.next();
realm = credentialsMapping.getRealm();
// Start credentials element
out.startElement(ELEMENT_CREDENTIALS);
out.println();
// Write URL
out.startElement(ELEMENT_URL);
out.writeCData(realm.toString(false));
out.endElement(ELEMENT_URL);
Credentials credentials = credentialsMapping.getCredentials();
// Write login
out.startElement(ELEMENT_LOGIN);
out.writeCData(credentials.getLogin());
out.endElement(ELEMENT_LOGIN);
// Write password (XOR encrypted)
out.startElement(ELEMENT_PASSWORD);
out.writeCData(XORCipher.encryptXORBase64(credentials.getPassword()));
out.endElement(ELEMENT_PASSWORD);
// Write properties, each property is stored in a separate 'property' element
propertyKeys = realm.getPropertyNames();
while (propertyKeys.hasMoreElements()) {
name = propertyKeys.nextElement();
attributes = new XmlAttributes();
attributes.add(ATTRIBUTE_NAME, name);
attributes.add(ATTRIBUTE_VALUE, realm.getProperty(name));
out.startElement(ELEMENT_PROPERTY, attributes);
out.endElement(ELEMENT_PROPERTY);
}
// End credentials element
out.endElement(ELEMENT_CREDENTIALS);
}
// End root element
out.endElement(ELEMENT_ROOT);
}
use of com.mucommander.commons.file.FileURL in project mucommander by mucommander.
the class ConfFileTableTab method getInitialAbstractPaths.
/**
* Returns a valid initial abstract path for the specified frame.
* <p>
* This method does its best to interpret <code>path</code> properly, or to fail
* politely if it can't. This means that:<br/>
* - we first try to see whether <code>path</code> is a legal, existing URI.<br/>
* - if it's not, we check whether it might be a legal local, existing file path.<br/>
* - if it's not, we'll just use the default initial path for the frame.<br/>
* - if <code>path</code> is browsable (eg directory, archive, ...), use it as is.<br/>
* - if it's not, use its parent.<br/>
* - if it does not have a parent, use the default initial path for the frame.<br/>
* </p>
*
* @param path path to the folder we want to open in <code>frame</code>.
* @param folderPanelType identifier of the panel we want to compute the path for (either {@link com.mucommander.ui.main.FolderPanel.FolderPanelType.LEFT} or
* {@link #@link com.mucommander.ui.main.FolderPanel.FolderPanelType.RIGHT}).
* @return our best shot at what was actually requested.
*/
private FileURL getInitialAbstractPaths(String path) {
// This is one of those cases where a null value actually has a proper meaning.
if (path == null)
return MainFrameBuilder.getHomeFolder().getURL();
// Tries the specified path as-is.
AbstractFile file;
CredentialsMapping newCredentialsMapping;
while (true) {
try {
file = FileFactory.getFile(path, true);
if (!file.exists())
file = null;
break;
}// If an AuthException occurred, gets login credential from the user.
catch (Exception e) {
if (e instanceof AuthException) {
// Prompts the user for a login and password.
AuthException authException = (AuthException) e;
FileURL url = authException.getURL();
AuthDialog authDialog = new AuthDialog(WindowManager.getCurrentMainFrame(), url, true, authException.getMessage());
authDialog.showDialog();
newCredentialsMapping = authDialog.getCredentialsMapping();
if (newCredentialsMapping != null) {
// Use the provided credentials
CredentialsManager.authenticate(url, newCredentialsMapping);
path = url.toString(true);
} else // If the user cancels, we fall back to the default path.
{
return MainFrameBuilder.getHomeFolder().getURL();
}
} else {
file = null;
break;
}
}
}
// If the specified path does not work out,
if (file == null)
// Tries the specified path as a relative path.
if ((file = FileFactory.getFile(new File(path).getAbsolutePath())) == null || !file.exists())
// Defaults to home.
return MainFrameBuilder.getHomeFolder().getURL();
// If the specified path is a non-browsable, uses its parent.
if (!file.isBrowsable()) {
fileToSelect = file;
// a file without a parent directory.
if ((file = file.getParent()) == null)
return MainFrameBuilder.getHomeFolder().getURL();
}
return file.getURL();
}
Aggregations