Search in sources :

Example 1 with StorageException

use of org.syncany.plugins.transfer.StorageException in project syncany by syncany.

the class LocalTransferManager method list.

@Override
public <T extends RemoteFile> Map<String, T> list(Class<T> remoteFileClass) throws StorageException {
    connect();
    Path folder = Paths.get(getRemoteFilePath(remoteFileClass));
    Map<String, T> files = Maps.newHashMap();
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(folder)) {
        for (Path path : directoryStream) {
            try {
                T remoteFile = RemoteFile.createRemoteFile(path.getFileName().toString(), remoteFileClass);
                files.put(path.getFileName().toString(), remoteFile);
            } catch (StorageException e) {
                logger.log(Level.INFO, "Cannot create instance of " + remoteFileClass.getSimpleName() + " for file " + path + "; maybe invalid file name pattern. Ignoring file.");
            }
        }
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Unable to list directory", e);
    }
    return files;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) StorageException(org.syncany.plugins.transfer.StorageException)

Example 2 with StorageException

use of org.syncany.plugins.transfer.StorageException in project syncany by syncany.

the class PathAwareFeatureTransferManager method list.

private <T extends RemoteFile> void list(String remoteFilePath, Map<String, T> remoteFiles, Class<T> remoteFileClass) throws StorageException {
    logger.log(Level.INFO, "Listing folder for files matching " + remoteFileClass.getSimpleName() + ": " + remoteFilePath);
    Map<String, FileType> folderList = pathAwareFeatureExtension.listFolder(remoteFilePath);
    for (Map.Entry<String, FileType> folderListEntry : folderList.entrySet()) {
        String fileName = folderListEntry.getKey();
        FileType fileType = folderListEntry.getValue();
        if (fileType == FileType.FILE) {
            try {
                remoteFiles.put(fileName, RemoteFile.createRemoteFile(fileName, remoteFileClass));
                logger.log(Level.INFO, "- File: " + fileName);
            } catch (StorageException e) {
            // We don't care and ignore non-matching files!
            }
        } else if (fileType == FileType.FOLDER) {
            logger.log(Level.INFO, "- Folder: " + fileName);
            String newRemoteFilePath = remoteFilePath + folderSeparator + fileName;
            list(newRemoteFilePath, remoteFiles, remoteFileClass);
        }
    }
}
Also used : FileType(org.syncany.plugins.transfer.FileType) Map(java.util.Map) StorageException(org.syncany.plugins.transfer.StorageException)

Example 3 with StorageException

use of org.syncany.plugins.transfer.StorageException in project syncany by syncany.

the class PathAwareFeatureTransferManager method upload.

@Override
public void upload(final File localFile, final RemoteFile remoteFile) throws StorageException {
    final RemoteFile pathAwareRemoteFile = createPathAwareRemoteFile(remoteFile);
    if (!createFolder(pathAwareRemoteFile)) {
        throw new StorageException("Unable to create path for " + pathAwareRemoteFile);
    }
    underlyingTransferManager.upload(localFile, pathAwareRemoteFile);
}
Also used : StorageException(org.syncany.plugins.transfer.StorageException) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile)

Example 4 with StorageException

use of org.syncany.plugins.transfer.StorageException in project syncany by syncany.

the class AbstractInitCommand method askPluginSettings.

private TransferSettings askPluginSettings(TransferSettings settings, Map<String, String> knownPluginSettings) throws StorageException {
    if (isInteractive) {
        out.println();
        out.println("Connection details for " + settings.getType() + " connection:");
    } else {
        logger.log(Level.INFO, "Non interactive mode");
    }
    try {
        // Show OAuth output
        printOAuthInformation(settings);
        // Ask for plugin settings
        List<TransferPluginOption> pluginOptions = TransferPluginOptions.getOrderedOptions(settings.getClass());
        for (TransferPluginOption option : pluginOptions) {
            askPluginSettings(settings, option, knownPluginSettings, "");
        }
    } catch (NoSuchFieldException e) {
        logger.log(Level.SEVERE, "No token could be found, maybe user denied access", e);
        throw new StorageException("No token found. Did you accept the authorization?", e);
    } catch (TimeoutException e) {
        logger.log(Level.SEVERE, "No token was received in the given time interval", e);
        throw new StorageException("No token was received in the given time interval", e);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | IOException | InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, "Unable to execute option generator", e);
        throw new RuntimeException("Unable to execute option generator: " + e.getMessage());
    }
    if (!settings.isValid()) {
        if (askRetryInvalidSettings(settings.getReasonForLastValidationFail())) {
            return askPluginSettings(settings, knownPluginSettings);
        }
        throw new StorageException("Validation failed: " + settings.getReasonForLastValidationFail());
    }
    logger.log(Level.INFO, "Settings are " + settings.toString());
    return settings;
}
Also used : TransferPluginOption(org.syncany.plugins.transfer.TransferPluginOption) NestedTransferPluginOption(org.syncany.plugins.transfer.NestedTransferPluginOption) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException) StorageException(org.syncany.plugins.transfer.StorageException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with StorageException

use of org.syncany.plugins.transfer.StorageException in project syncany by syncany.

the class AbstractInitCommand method doOAuthInRedirectMode.

private void doOAuthInRedirectMode(OAuthGenerator generator, OAuth settings) throws IOException, InterruptedException, ExecutionException, TimeoutException, StorageException {
    OAuthTokenWebListener.Builder tokenListerBuilder = OAuthTokenWebListener.forMode(settings.mode());
    if (settings.callbackPort() != OAuth.RANDOM_PORT) {
        tokenListerBuilder.setPort(settings.callbackPort());
    }
    if (!settings.callbackId().equals(OAuth.PLUGIN_ID)) {
        tokenListerBuilder.setId(settings.callbackId());
    }
    // non standard plugin?
    if (generator instanceof OAuthGenerator.WithInterceptor) {
        tokenListerBuilder.setTokenInterceptor(((OAuthGenerator.WithInterceptor) generator).getInterceptor());
    }
    if (generator instanceof OAuthGenerator.WithExtractor) {
        tokenListerBuilder.setTokenExtractor(((OAuthGenerator.WithExtractor) generator).getExtractor());
    }
    OAuthTokenWebListener tokenListener = tokenListerBuilder.build();
    URI oAuthURL = generator.generateAuthUrl(tokenListener.start());
    Future<OAuthTokenFinish> futureTokenResponse = tokenListener.getToken();
    out.println();
    out.println("This plugin needs you to authenticate your account so that Syncany can access it.");
    out.printf("Please navigate to the URL below and accept the given permissions:\n\n  %s\n\n", oAuthURL.toString());
    out.print("Waiting for authorization...");
    OAuthTokenFinish tokenResponse = futureTokenResponse.get(OAUTH_TOKEN_WAIT_TIMEOUT, TimeUnit.SECONDS);
    if (tokenResponse != null) {
        out.printf(" received token '%s'\n\n", tokenResponse.getToken());
        generator.checkToken(tokenResponse.getToken(), tokenResponse.getCsrfState());
    } else {
        out.println(" canceled");
        throw new StorageException("Error while acquiring token, perhaps user denied authorization");
    }
}
Also used : OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) OAuthGenerator(org.syncany.plugins.transfer.oauth.OAuthGenerator) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException)

Aggregations

StorageException (org.syncany.plugins.transfer.StorageException)33 File (java.io.File)15 MultichunkRemoteFile (org.syncany.plugins.transfer.files.MultichunkRemoteFile)14 Test (org.junit.Test)12 UnreliableLocalTransferSettings (org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings)12 TestClient (org.syncany.tests.util.TestClient)11 RemoteFile (org.syncany.plugins.transfer.files.RemoteFile)10 IOException (java.io.IOException)9 Persister (org.simpleframework.xml.core.Persister)7 TempRemoteFile (org.syncany.plugins.transfer.files.TempRemoteFile)7 TransactionRemoteFile (org.syncany.plugins.transfer.files.TransactionRemoteFile)7 ActionRemoteFile (org.syncany.plugins.transfer.files.ActionRemoteFile)6 DatabaseRemoteFile (org.syncany.plugins.transfer.files.DatabaseRemoteFile)6 TransactionTO (org.syncany.plugins.transfer.to.TransactionTO)5 Matcher (java.util.regex.Matcher)4 UpOperationOptions (org.syncany.operations.up.UpOperationOptions)4 TransferManager (org.syncany.plugins.transfer.TransferManager)4 TransactionAware (org.syncany.plugins.transfer.features.TransactionAware)4 SyncanyRemoteFile (org.syncany.plugins.transfer.files.SyncanyRemoteFile)4 FilenameFilter (java.io.FilenameFilter)3