Search in sources :

Example 1 with AuthException

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

the class S3ProtocolProvider method getFile.

public AbstractFile getFile(FileURL url, Map<String, Object> instantiationParams) throws IOException {
    Credentials credentials = url.getCredentials();
    if (credentials == null || credentials.getLogin().equals("") || credentials.getPassword().equals(""))
        throw new AuthException(url);
    S3Service service;
    String bucketName;
    if (instantiationParams.isEmpty()) {
        Jets3tProperties props = new Jets3tProperties();
        props.setProperty("s3service.s3-endpoint", url.getHost());
        boolean secure = Boolean.parseBoolean(url.getProperty(S3File.SECUTRE_HTTP));
        if (url.getPort() > 0)
            props.setProperty(secure ? "s3service.s3-endpoint-https-port" : "s3service.s3-endpoint-http-port", String.valueOf(url.getPort()));
        props.setProperty("s3service.https-only", String.valueOf(secure));
        props.setProperty("s3service.disable-dns-buckets", url.getProperty(S3File.DISABLE_DNS_BUCKETS));
        props.setProperty("s3service.default-bucket-location", url.getProperty(S3File.DEFAULT_BUCKET_LOCATION));
        service = new RestS3Service(getProviderCredentials(url), null, null, props);
    } else {
        service = (S3Service) instantiationParams.get("service");
    }
    String path = url.getPath();
    // Root resource
    if (("/").equals(path))
        return new S3Root(url, service);
    // Fetch the bucket name from the URL
    StringTokenizer st = new StringTokenizer(path, "/");
    bucketName = st.nextToken();
    // Object resource
    if (st.hasMoreTokens()) {
        org.jets3t.service.model.S3Object obj = (org.jets3t.service.model.S3Object) instantiationParams.get("object");
        if (obj != null)
            return new S3Object(url, service, bucketName, obj);
        return new S3Object(url, service, bucketName);
    }
    // Bucket resource
    org.jets3t.service.model.S3Bucket bucket = (org.jets3t.service.model.S3Bucket) instantiationParams.get("bucket");
    if (bucket != null)
        return new S3Bucket(url, service, bucket);
    return new S3Bucket(url, service, bucketName);
}
Also used : AuthException(com.mucommander.commons.file.AuthException) StringTokenizer(java.util.StringTokenizer) Jets3tProperties(org.jets3t.service.Jets3tProperties) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Service(org.jets3t.service.S3Service) AWSCredentials(org.jets3t.service.security.AWSCredentials) GSCredentials(org.jets3t.service.security.GSCredentials) Credentials(com.mucommander.commons.file.Credentials) ProviderCredentials(org.jets3t.service.security.ProviderCredentials)

Example 2 with AuthException

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

the class S3File method getHadoopFileSystem.

// /////////////////////////////
// HadoopFile implementation //
// /////////////////////////////
@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);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AuthException(com.mucommander.commons.file.AuthException) Credentials(com.mucommander.commons.file.Credentials)

Example 3 with AuthException

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

the class ConnectionHandler method throwAuthException.

/**
 * Throws an {@link AuthException} using this connection handler's realm, credentials and the message passed as
 * an argument (can be <code>null</code>). The FileURL instance representing the realm that is used to create
 * the <code>AuthException</code> is a clone of this realm, making it safe for modification.
 *
 * @param message the message to pass to AuthException's constructor, can be <code>null</code>
 * @throws AuthException always throws the created AuthException
 */
public void throwAuthException(String message) throws AuthException {
    FileURL clonedRealm = (FileURL) realm.clone();
    clonedRealm.setCredentials(credentials);
    throw new AuthException(clonedRealm, message);
}
Also used : FileURL(com.mucommander.commons.file.FileURL) AuthException(com.mucommander.commons.file.AuthException)

Example 4 with AuthException

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

the class BrowseLocationThread method run.

@Override
public void run() {
    LOGGER.debug("starting folder change...");
    boolean folderChangedSuccessfully = false;
    // Show some progress in the progress bar to give hope
    folderPanel.setProgressValue(10);
    boolean userCancelled = false;
    CredentialsMapping newCredentialsMapping = null;
    // True if Guest authentication was selected in the authentication dialog (guest credentials must not be
    // added to CredentialsManager)
    boolean guestCredentialsSelected = false;
    AuthenticationType authenticationType = folderURL.getAuthenticationType();
    if (credentialsMapping != null) {
        newCredentialsMapping = credentialsMapping;
        CredentialsManager.authenticate(folderURL, newCredentialsMapping);
    } else // avoid waiting for an AuthException to be thrown.
    if (!folderURL.containsCredentials() && ((authenticationType == AuthenticationType.AUTHENTICATION_REQUIRED) || (authenticationType == AuthenticationType.AUTHENTICATION_OPTIONAL && CredentialsManager.getMatchingCredentials(folderURL).length > 0))) {
        AuthDialog authDialog = popAuthDialog(folderURL, false, null);
        newCredentialsMapping = authDialog.getCredentialsMapping();
        guestCredentialsSelected = authDialog.guestCredentialsSelected();
        // User cancelled the authentication dialog, stop
        if (newCredentialsMapping == null)
            userCancelled = true;
        else // Use the provided credentials and invalidate the folder AbstractFile instance (if any) so that
        // it gets recreated with the new credentials
        {
            CredentialsManager.authenticate(folderURL, newCredentialsMapping);
            folder = null;
        }
    }
    if (!userCancelled) {
        boolean canonicalPathFollowed = false;
        do {
            // Set cursor to hourglass/wait
            mainFrame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            // Render all actions inactive while changing folder
            mainFrame.setNoEventsMode(true);
            try {
                // Thread was created using a FileURL
                if (folder == null) {
                    AbstractFile file = FileFactory.getFile(folderURL, true);
                    synchronized (KILL_LOCK) {
                        if (killed) {
                            LOGGER.debug("this thread has been killed, returning");
                            break;
                        }
                    }
                    // File resolved -> 25% complete
                    folderPanel.setProgressValue(25);
                    // or doesn't exist
                    if (file == null || !file.exists()) {
                        // Restore default cursor
                        mainFrame.setCursor(Cursor.getDefaultCursor());
                        locationChanger.showFolderDoesNotExistDialog();
                        break;
                    }
                    if (!file.canRead()) {
                        // Restore default cursor
                        mainFrame.setCursor(Cursor.getDefaultCursor());
                        showFailedToReadFolderDialog();
                        break;
                    }
                    // File is a regular directory, all good
                    if (file.isDirectory()) {
                    // Just continue
                    } else // File is a browsable file (Zip archive for instance) but not a directory : Browse or Download ? => ask the user
                    if (file.isBrowsable()) {
                        // of the OpenAction (enter pressed on the file). This works well enough in practice.
                        if (!globalHistory.contains(folderURL) && !file.equals(folderPanel.getFileTable().getSelectedFile())) {
                            // Restore default cursor
                            mainFrame.setCursor(Cursor.getDefaultCursor());
                            // Download or browse file ?
                            QuestionDialog dialog = new QuestionDialog(mainFrame, null, Translator.get("table.download_or_browse"), mainFrame, Arrays.asList(BrowseLocationThreadAction.BROWSE, BrowseLocationThreadAction.DOWNLOAD, BrowseLocationThreadAction.CANCEL), 0);
                            DialogAction ret = dialog.getActionValue();
                            if (ret == DIALOG_DISPOSED_ACTION || ret == BrowseLocationThreadAction.CANCEL)
                                break;
                            // Download file
                            if (ret == BrowseLocationThreadAction.DOWNLOAD) {
                                showDownloadDialog(file);
                                break;
                            }
                            // Continue if BROWSE
                            // Set cursor to hourglass/wait
                            mainFrame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
                        }
                    // else just continue and browse file's contents
                    } else // File is a regular file: show download dialog which allows to download (copy) the file
                    // to a directory specified by the user
                    {
                        showDownloadDialog(file);
                        break;
                    }
                    this.folder = file;
                } else // Thread was created using an AbstractFile instance, check file existence
                if (!folder.exists()) {
                    // Find a 'workable' folder if the requested folder doesn't exist anymore
                    if (findWorkableFolder) {
                        AbstractFile newFolder = locationChanger.getWorkableFolder(folder);
                        if (newFolder.equals(folder)) {
                            // If we've already tried the returned folder, give up (avoids a potentially endless loop)
                            locationChanger.showFolderDoesNotExistDialog();
                            break;
                        }
                        // Try again with the new folder
                        folder = newFolder;
                        folderURL = folder.getURL();
                        // Discard the file to select, if any
                        fileToSelect = null;
                        continue;
                    } else {
                        locationChanger.showFolderDoesNotExistDialog();
                        break;
                    }
                } else if (!folder.canRead()) {
                    showFailedToReadFolderDialog();
                    break;
                }
                // resolved again.
                if (!canonicalPathFollowed && followCanonicalPath(folder)) {
                    try {
                        // Recreate the FileURL using the file's canonical path
                        FileURL newURL = FileURL.getFileURL(folder.getCanonicalPath());
                        // Keep the credentials and properties (if any)
                        newURL.setCredentials(folderURL.getCredentials());
                        newURL.importProperties(folderURL);
                        this.folderURL = newURL;
                        // Invalidate the AbstractFile instance
                        this.folder = null;
                        // There won't be any further attempts after this one
                        canonicalPathFollowed = true;
                        // Loop the resolve the file
                        continue;
                    } catch (MalformedURLException e) {
                    // In the unlikely event of the canonical path being malformed, the AbstractFile
                    // and FileURL instances are left untouched
                    }
                }
                synchronized (KILL_LOCK) {
                    if (killed) {
                        LOGGER.debug("this thread has been killed, returning");
                        break;
                    }
                }
                // File tested -> 50% complete
                folderPanel.setProgressValue(50);
                synchronized (KILL_LOCK) {
                    if (killed) {
                        LOGGER.debug("this thread has been killed, returning");
                        break;
                    }
                    // From now on, thread cannot be killed (would comprise table integrity)
                    doNotKill = true;
                }
                // files listed -> 75% complete
                folderPanel.setProgressValue(75);
                LOGGER.trace("calling setCurrentFolder");
                // Change the file table's current folder and select the specified file (if any)
                locationChanger.setCurrentFolder(folder, fileToSelect, changeLockedTab, true);
                // folder set -> 95% complete
                folderPanel.setProgressValue(95);
                // Do not add the credentials if guest credentials were selected by the user.
                if (newCredentialsMapping != null && !guestCredentialsSelected)
                    CredentialsManager.addCredentials(newCredentialsMapping);
                // All good !
                folderChangedSuccessfully = true;
                break;
            } catch (Exception e) {
                LOGGER.debug("Caught exception", e);
                if (killed) {
                    // If #tryKill() called #interrupt(), the exception we just caught was most likely
                    // thrown as a result of the thread being interrupted.
                    // 
                    // The exception can be a java.lang.InterruptedException (Thread throws those),
                    // a java.nio.channels.ClosedByInterruptException (InterruptibleChannel throws those)
                    // or any other exception thrown by some code that swallowed the original exception
                    // and threw a new one.
                    LOGGER.debug("Thread was interrupted, ignoring exception");
                    break;
                }
                // Restore default cursor
                mainFrame.setCursor(Cursor.getDefaultCursor());
                if (e instanceof AuthException) {
                    AuthException authException = (AuthException) e;
                    // Retry (loop) if user provided new credentials, if not stop
                    AuthDialog authDialog = popAuthDialog(authException.getURL(), true, authException.getMessage());
                    newCredentialsMapping = authDialog.getCredentialsMapping();
                    guestCredentialsSelected = authDialog.guestCredentialsSelected();
                    if (newCredentialsMapping != null) {
                        // Invalidate the existing AbstractFile instance
                        folder = null;
                        // Use the provided credentials
                        CredentialsManager.authenticate(folderURL, newCredentialsMapping);
                        continue;
                    }
                } else {
                    // Find a 'workable' folder if the requested folder doesn't exist anymore
                    if (findWorkableFolder) {
                        AbstractFile newFolder = locationChanger.getWorkableFolder(folder);
                        if (newFolder.equals(folder)) {
                            // If we've already tried the returned folder, give up (avoids a potentially endless loop)
                            locationChanger.showFolderDoesNotExistDialog();
                            break;
                        }
                        // Try again with the new folder
                        folder = newFolder;
                        folderURL = folder.getURL();
                        // Discard the file to select, if any
                        fileToSelect = null;
                        continue;
                    }
                    showAccessErrorDialog(e);
                }
                // Stop looping!
                break;
            }
        } while (true);
    }
    synchronized (KILL_LOCK) {
        // Clean things up
        cleanup(folderChangedSuccessfully);
    }
}
Also used : FileURL(com.mucommander.commons.file.FileURL) MalformedURLException(java.net.MalformedURLException) AbstractFile(com.mucommander.commons.file.AbstractFile) QuestionDialog(com.mucommander.ui.dialog.QuestionDialog) DialogAction(com.mucommander.ui.dialog.DialogAction) AuthException(com.mucommander.commons.file.AuthException) AuthDialog(com.mucommander.ui.dialog.auth.AuthDialog) Cursor(java.awt.Cursor) MalformedURLException(java.net.MalformedURLException) AuthException(com.mucommander.commons.file.AuthException) CredentialsMapping(com.mucommander.auth.CredentialsMapping) AuthenticationType(com.mucommander.commons.file.AuthenticationType)

Example 5 with AuthException

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

the class GoogleDriveClient method connect.

public void connect() throws AuthException {
    try {
        NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Credential credential = this.credential != null ? this.credential : getCredentials(HTTP_TRANSPORT, fileUrl.getHost(), null);
        drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
    } catch (GeneralSecurityException e) {
        throw new AuthException(fileUrl);
    } catch (IOException e) {
        throw new IOError(e);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOError(java.io.IOError) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleNetHttpTransport(com.google.api.client.googleapis.javanet.GoogleNetHttpTransport) GeneralSecurityException(java.security.GeneralSecurityException) AuthException(com.mucommander.commons.file.AuthException) IOException(java.io.IOException)

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