Search in sources :

Example 11 with StorageException

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

the class UploadInterruptedTest method testUnreliableUpload_Test4_1_FailsAtSecondMultiChunkUpload.

@Test
public void testUnreliableUpload_Test4_1_FailsAtSecondMultiChunkUpload() throws Exception {
    /*
		 * This test fails when trying to upload the second multichunk, but succeeds on retry
		 *
		 * 1. upload(action-up-987, actions/action-up-987)
		 * 2. upload(transaction-123, transactions/transaction-123)
		 * 3. upload(multichunk-1, temp-1)
		 * 4. upload(multichunk-2, temp-2) <<< FAILS HERE
		 * 5. upload(database-123, temp-3) 6. move(temp-1, multichunks/multichunk-1)
		 * 7. move(temp-2, multichunks/multichunk-2)
		 * 8. move(temp-3, databases/database-123)
		 */
    // Setup
    UnreliableLocalTransferSettings testConnection = TestConfigUtil.createTestUnreliableLocalConnection(Arrays.asList(new String[] { "rel=[456].+upload.+multichunk" }));
    TestClient clientA = new TestClient("A", testConnection);
    // << larger than one multichunk!
    clientA.createNewFile("A-original", 5 * 1024 * 1024);
    boolean upFailed = false;
    try {
        clientA.up();
    } catch (StorageException e) {
        upFailed = true;
        logger.log(Level.INFO, e.getMessage());
    }
    assertTrue(upFailed);
    assertEquals(0, new File(testConnection.getPath() + "/databases/").listFiles().length);
    assertEquals(0, new File(testConnection.getPath() + "/multichunks/").listFiles().length);
    assertEquals(1, new File(testConnection.getPath() + "/actions/").listFiles().length);
    assertEquals(1, new File(testConnection.getPath() + "/transactions/").listFiles().length);
    File[] tempFiles = new File(testConnection.getPath() + "/temporary/").listFiles();
    assertEquals(1, tempFiles.length);
    // 1 MC with 1 MB, 1 with 4 MB; must be larger than 500 KB
    assertTrue(tempFiles[0].length() > 500 * 1024);
    File transactionFile = new File(testConnection.getPath() + "/transactions/").listFiles()[0];
    TransactionTO transactionTO = new Persister().read(TransactionTO.class, transactionFile);
    assertEquals(3, transactionTO.getActions().size());
    assertTrue(transactionTO.getActions().get(0).getRemoteFile().getName().contains("multichunk-"));
    assertTrue(transactionTO.getActions().get(1).getRemoteFile().getName().contains("multichunk-"));
    assertTrue(transactionTO.getActions().get(2).getRemoteFile().getName().contains("database-"));
    // 2. Second try succeeds and must clean up the transactions
    clientA.up();
    assertEquals(1, new File(testConnection.getPath() + "/databases/").listFiles().length);
    assertEquals(2, new File(testConnection.getPath() + "/multichunks/").listFiles().length);
    assertEquals(0, new File(testConnection.getPath() + "/actions/").listFiles().length);
    assertEquals(0, new File(testConnection.getPath() + "/transactions/").listFiles().length);
    assertEquals(0, new File(testConnection.getPath() + "/temporary/").listFiles().length);
    // Tear down
    clientA.deleteTestData();
}
Also used : UnreliableLocalTransferSettings(org.syncany.plugins.unreliable_local.UnreliableLocalTransferSettings) TestClient(org.syncany.tests.util.TestClient) Persister(org.simpleframework.xml.core.Persister) StorageException(org.syncany.plugins.transfer.StorageException) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) TransactionTO(org.syncany.plugins.transfer.to.TransactionTO) Test(org.junit.Test)

Example 12 with StorageException

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

the class ApplicationLink method createTransferSettings.

private TransferSettings createTransferSettings(byte[] plaintextPluginSettingsBytes) throws StorageException, IOException {
    // Find plugin ID and settings XML
    int pluginIdentifierLength = Ints.fromByteArray(Arrays.copyOfRange(plaintextPluginSettingsBytes, 0, INTEGER_BYTES));
    String pluginId = new String(Arrays.copyOfRange(plaintextPluginSettingsBytes, INTEGER_BYTES, INTEGER_BYTES + pluginIdentifierLength));
    byte[] gzippedPluginSettingsByteArray = Arrays.copyOfRange(plaintextPluginSettingsBytes, INTEGER_BYTES + pluginIdentifierLength, plaintextPluginSettingsBytes.length);
    String pluginSettings = IOUtils.toString(new GZIPInputStream(new ByteArrayInputStream(gzippedPluginSettingsByteArray)));
    // Create transfer settings object
    try {
        TransferPlugin plugin = Plugins.get(pluginId, TransferPlugin.class);
        if (plugin == null) {
            throw new StorageException("Link contains unknown connection type '" + pluginId + "'. Corresponding plugin not found.");
        }
        Class<? extends TransferSettings> pluginTransferSettingsClass = TransferPluginUtil.getTransferSettingsClass(plugin.getClass());
        TransferSettings transferSettings = new Persister().read(pluginTransferSettingsClass, pluginSettings);
        logger.log(Level.INFO, "(Decrypted) link contains: " + pluginId + " -- " + pluginSettings);
        return transferSettings;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) TransferPlugin(org.syncany.plugins.transfer.TransferPlugin) ByteArrayInputStream(java.io.ByteArrayInputStream) TransferSettings(org.syncany.plugins.transfer.TransferSettings) Persister(org.simpleframework.xml.core.Persister) StorageException(org.syncany.plugins.transfer.StorageException) StorageException(org.syncany.plugins.transfer.StorageException) IOException(java.io.IOException)

Example 13 with StorageException

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

the class ApplicationLink method resolveLink.

private String resolveLink(String httpApplicationLink, int redirectCount) throws IllegalArgumentException, StorageException {
    if (redirectCount >= LINK_HTTP_MAX_REDIRECT_COUNT) {
        throw new IllegalArgumentException("Max. redirect count of " + LINK_HTTP_MAX_REDIRECT_COUNT + " for URL reached. Cannot find syncany:// link.");
    }
    try {
        logger.log(Level.INFO, "- Retrieving HTTP HEAD for " + httpApplicationLink + " ...");
        HttpHead headMethod = new HttpHead(httpApplicationLink);
        HttpResponse httpResponse = createHttpClient().execute(headMethod);
        // Find syncany:// link
        Header locationHeader = httpResponse.getLastHeader("Location");
        if (locationHeader == null) {
            throw new Exception("Link does not redirect to a syncany:// link.");
        }
        String locationHeaderUrl = locationHeader.getValue();
        Matcher locationHeaderMatcher = LINK_PATTERN.matcher(locationHeaderUrl);
        boolean isApplicationLink = locationHeaderMatcher.find();
        if (isApplicationLink) {
            String applicationLink = locationHeaderMatcher.group(0);
            logger.log(Level.INFO, "Resolved application link is: " + applicationLink);
            return applicationLink;
        } else {
            return resolveLink(locationHeaderUrl, ++redirectCount);
        }
    } catch (StorageException | IllegalArgumentException e) {
        throw e;
    } catch (Exception e) {
        throw new StorageException(e.getMessage(), e);
    }
}
Also used : Header(org.apache.http.Header) Matcher(java.util.regex.Matcher) HttpResponse(org.apache.http.HttpResponse) StorageException(org.syncany.plugins.transfer.StorageException) HttpHead(org.apache.http.client.methods.HttpHead) StorageException(org.syncany.plugins.transfer.StorageException) IOException(java.io.IOException)

Example 14 with StorageException

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

the class AbstractInitCommand method askGenericChildPluginSettings.

/**
 * Queries the user for a plugin (which plugin to use?) and then
 * asks for all of the plugin's settings.
 *
 * <p>This case is triggered by a field looking like this:
 * <tt>private TransferSettings childPluginSettings;</tt>
 */
private void askGenericChildPluginSettings(TransferSettings settings, TransferPluginOption option, Map<String, String> knownPluginSettings, String nestPrefix) throws StorageException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    TransferPluginOptionCallback optionCallback = createOptionCallback(settings, option.getCallback());
    if (isInteractive) {
        callAndPrintPreQueryCallback(optionCallback);
        out.println();
        out.println(option.getDescription() + ":");
    }
    TransferPlugin childPlugin = null;
    Class<? extends TransferPlugin> pluginClass = TransferPluginUtil.getTransferPluginClass(settings.getClass());
    // Non-interactive: Plugin settings might be given via command line
    try {
        childPlugin = initPlugin(knownPluginSettings.get(nestPrefix + option.getName() + GENERIC_PLUGIN_TYPE_IDENTIFIER));
    } catch (Exception e) {
        if (!isInteractive) {
            throw new IllegalArgumentException("Missing nested plugin type (" + nestPrefix + option.getName() + GENERIC_PLUGIN_TYPE_IDENTIFIER + ") in non-interactive mode.");
        }
    }
    // Interactive mode: Ask for sub-plugin
    while (childPlugin == null) {
        childPlugin = askPlugin(pluginClass);
    }
    if (isInteractive) {
        out.println();
    }
    // Create nested/child settings
    TransferSettings childSettings = childPlugin.createEmptySettings();
    settings.setField(option.getField().getName(), childSettings);
    nestPrefix = nestPrefix + option.getName() + NESTED_OPTIONS_SEPARATOR;
    for (TransferPluginOption nestedOption : TransferPluginOptions.getOrderedOptions(childSettings.getClass())) {
        askPluginSettings(childSettings, nestedOption, knownPluginSettings, nestPrefix);
    }
    if (isInteractive) {
        callAndPrintPostQueryCallback(optionCallback, null);
    }
}
Also used : TransferPlugin(org.syncany.plugins.transfer.TransferPlugin) TransferPluginOptionCallback(org.syncany.plugins.transfer.TransferPluginOptionCallback) TransferPluginOption(org.syncany.plugins.transfer.TransferPluginOption) NestedTransferPluginOption(org.syncany.plugins.transfer.NestedTransferPluginOption) TransferSettings(org.syncany.plugins.transfer.TransferSettings) TimeoutException(java.util.concurrent.TimeoutException) StorageException(org.syncany.plugins.transfer.StorageException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException)

Example 15 with StorageException

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

the class LocalTransferManager method move.

@Override
public void move(RemoteFile sourceFile, RemoteFile targetFile) throws StorageException {
    connect();
    File sourceRemoteFile = getRemoteFile(sourceFile);
    File targetRemoteFile = getRemoteFile(targetFile);
    if (!sourceRemoteFile.exists()) {
        throw new StorageMoveException("Unable to move file " + sourceFile + " because it does not exist.");
    }
    try {
        FileUtils.moveFile(sourceRemoteFile, targetRemoteFile);
    } catch (IOException ex) {
        throw new StorageException("Unable to move file " + sourceRemoteFile + " to destination " + targetRemoteFile, ex);
    }
}
Also used : StorageMoveException(org.syncany.plugins.transfer.StorageMoveException) IOException(java.io.IOException) RemoteFile(org.syncany.plugins.transfer.files.RemoteFile) DatabaseRemoteFile(org.syncany.plugins.transfer.files.DatabaseRemoteFile) SyncanyRemoteFile(org.syncany.plugins.transfer.files.SyncanyRemoteFile) TempRemoteFile(org.syncany.plugins.transfer.files.TempRemoteFile) ActionRemoteFile(org.syncany.plugins.transfer.files.ActionRemoteFile) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) CleanupRemoteFile(org.syncany.plugins.transfer.files.CleanupRemoteFile) File(java.io.File) MultichunkRemoteFile(org.syncany.plugins.transfer.files.MultichunkRemoteFile) 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