Search in sources :

Example 36 with FileURL

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();
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile)

Example 37 with FileURL

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);
}
Also used : FileURL(com.mucommander.commons.file.FileURL)

Example 38 with FileURL

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));
    }
}
Also used : FileURL(com.mucommander.commons.file.FileURL)

Example 39 with FileURL

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);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) XmlAttributes(com.mucommander.commons.util.xml.XmlAttributes) XmlWriter(com.mucommander.commons.util.xml.XmlWriter) Credentials(com.mucommander.commons.file.Credentials)

Example 40 with FileURL

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();
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AbstractFile(com.mucommander.commons.file.AbstractFile) AuthException(com.mucommander.commons.file.AuthException) AuthDialog(com.mucommander.ui.dialog.auth.AuthDialog) AbstractFile(com.mucommander.commons.file.AbstractFile) File(java.io.File) AuthException(com.mucommander.commons.file.AuthException) CredentialsMapping(com.mucommander.auth.CredentialsMapping)

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