org.jclouds.virtualbox.functions.admin.FileDownloadFromURI Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of virtualbox Show documentation
Show all versions of virtualbox Show documentation
jclouds components to access an implementation of virtualbox
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.virtualbox.functions.admin;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.ByteStreams.copy;
import static com.google.common.io.Closeables.closeQuietly;
import static org.jclouds.virtualbox.config.VirtualBoxConstants.VIRTUALBOX_WORKINGDIR;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.logging.Logger;
import org.jclouds.rest.HttpAsyncClient;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
/**
* @author Mattias Holmqvist
*/
public class FileDownloadFromURI implements Function {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private final HttpAsyncClient client;
private final String isosDir;
@Inject
public FileDownloadFromURI(HttpAsyncClient client, @Named(VIRTUALBOX_WORKINGDIR) String workingDir) {
this.client = client;
this.isosDir = workingDir + File.separator + "isos";
}
@Override
public File apply(@Nullable URI input) {
final File file = new File(isosDir, new File(input.getPath()).getName());
try {
if (!file.exists()) {
final InputStream inputStream = client.get(input).get();
checkNotNull(inputStream, "%s not found", input);
try {
copy(inputStream, new FileOutputStream(file));
return file;
} catch (FileNotFoundException e) {
logger.error(e, "File %s could not be found", file.getPath());
} catch (IOException e) {
logger.error(e, "Error when downloading file %s", input.toString());
} finally {
closeQuietly(inputStream);
}
return null;
} else {
logger.debug("File %s already exists. Skipping download", file.getPath());
return file;
}
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}