Search in sources :

Example 6 with AuthException

use of com.mucommander.commons.file.AuthException 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)

Example 7 with AuthException

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

the class DropboxConnectionHandler method startConnection.

@Override
public void startConnection() throws IOException, AuthException {
    FileURL credentialFileURL = getCredentialFileURL(fileURL.getHost());
    DbxCredential credential;
    try {
        credential = DbxCredential.Reader.readFromFile(credentialFileURL.getPath());
    } catch (JsonReader.FileLoadException e) {
        LOGGER.error("failed to load credentials to dropbox", e);
        throw new AuthException(fileURL, e.getMessage());
    }
    // Create a DbxClientV2, which is what you use to make API calls.
    DbxRequestConfig requestConfig = new DbxRequestConfig("examples-account-info");
    // Use DbxCredential to create dbx client.
    dbxClient = new DbxClientV2(requestConfig, credential);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) DbxClientV2(com.dropbox.core.v2.DbxClientV2) DbxRequestConfig(com.dropbox.core.DbxRequestConfig) DbxCredential(com.dropbox.core.oauth.DbxCredential) JsonReader(com.dropbox.core.json.JsonReader) AuthException(com.mucommander.commons.file.AuthException)

Aggregations

AuthException (com.mucommander.commons.file.AuthException)7 FileURL (com.mucommander.commons.file.FileURL)5 CredentialsMapping (com.mucommander.auth.CredentialsMapping)2 AbstractFile (com.mucommander.commons.file.AbstractFile)2 Credentials (com.mucommander.commons.file.Credentials)2 AuthDialog (com.mucommander.ui.dialog.auth.AuthDialog)2 DbxRequestConfig (com.dropbox.core.DbxRequestConfig)1 JsonReader (com.dropbox.core.json.JsonReader)1 DbxCredential (com.dropbox.core.oauth.DbxCredential)1 DbxClientV2 (com.dropbox.core.v2.DbxClientV2)1 Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleNetHttpTransport (com.google.api.client.googleapis.javanet.GoogleNetHttpTransport)1 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)1 AuthenticationType (com.mucommander.commons.file.AuthenticationType)1 DialogAction (com.mucommander.ui.dialog.DialogAction)1 QuestionDialog (com.mucommander.ui.dialog.QuestionDialog)1 Cursor (java.awt.Cursor)1 File (java.io.File)1 IOError (java.io.IOError)1 IOException (java.io.IOException)1