// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.filedistribution;

import com.yahoo.config.FileReference;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Files;

/**
 * @author baldersheim
 */
public class ApplicationFileManager implements AddFileInterface {

    private final File applicationDir;
    private final FileDirectory fileDirectory;

    ApplicationFileManager(File applicationDir, FileDirectory fileDirectory) {
        this.applicationDir = applicationDir;
        this.fileDirectory = fileDirectory;
    }

    @Override
    public FileReference addFile(String relativePath) {
        return fileDirectory.addFile(new File(applicationDir, relativePath));
    }

    @Override
    public FileReference addUri(String uri, String relativePath) {
        download(uri, relativePath);
        return addFile(relativePath);
    }

    private void download(String uri, String relativePath) {
        File file = new File(applicationDir, relativePath);
        FileOutputStream fos = null;
        ReadableByteChannel rbc = null;
        try {
            Files.createDirectories(file.toPath().getParent());
            URL website = new URL(uri);
            rbc = Channels.newChannel(website.openStream());
            fos = new FileOutputStream(file.getAbsolutePath());
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (IOException e) {
            throw new IllegalArgumentException("Failed creating directory " + file.getParent(), e);
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                if (rbc != null) {
                    rbc.close();
                }
            } catch (IOException e) {
                throw new IllegalArgumentException("Failed closing down after downloading " + uri + " to " + file.getAbsolutePath());
            }
        }
    }
}

@@ -10,27 +10,21 @@
 ght 
-2017 Yahoo Holdings
+Verizon Media
 . Li
@@ -284,16 +284,56 @@
 eption;%0A
+import java.net.SocketTimeoutException;%0A
 import j
@@ -1642,16 +1642,159 @@
 VALUE);%0A
+        %7D catch (SocketTimeoutException e) %7B%0A            throw new IllegalArgumentException(%22Failed connecting to or reading from %22 + uri, e);%0A
         

Handle SocketTimeoutException better when downloading files