diff --git a/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java b/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java index edb50b2833f..4f8255aa9fd 100644 --- a/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java +++ b/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java @@ -1020,7 +1020,7 @@ public class VolumeServiceImpl implements VolumeService { } srcVolume.processEvent(Event.OperationSuccessed); destVolume.processEvent(Event.MigrationCopySucceeded, result.getAnswer()); - _volumeDao.updateUuid(srcVolume.getId(), destVolume.getId()); + volDao.updateUuid(srcVolume.getId(), destVolume.getId()); try { destroyVolume(srcVolume.getId()); srcVolume = volFactory.getVolume(srcVolume.getId()); @@ -1225,10 +1225,16 @@ public class VolumeServiceImpl implements VolumeService { protected Void registerVolumeCallback(AsyncCallbackDispatcher callback, CreateVolumeContext context) { CreateCmdResult result = callback.getResult(); + VolumeObject vo = (VolumeObject)context.volume; try { - VolumeObject vo = (VolumeObject)context.volume; if (result.isFailed()) { vo.processEvent(Event.OperationFailed); + // delete the volume entry from volumes table in case of failure + VolumeVO vol = volDao.findById(vo.getId()); + if (vol != null) { + volDao.remove(vo.getId()); + } + } else { vo.processEvent(Event.OperationSuccessed, result.getAnswer()); @@ -1267,6 +1273,11 @@ public class VolumeServiceImpl implements VolumeService { } catch (Exception e) { s_logger.error("register volume failed: ", e); + // delete the volume entry from volumes table in case of failure + VolumeVO vol = volDao.findById(vo.getId()); + if (vol != null) { + volDao.remove(vo.getId()); + } VolumeApiResult res = new VolumeApiResult(null); context.future.complete(res); return null; @@ -1302,7 +1313,7 @@ public class VolumeServiceImpl implements VolumeService { EndPoint ep = RemoteHostEndPoint.getHypervisorHostEndPoint(destHost); if (ep != null) { - VolumeVO volume = _volumeDao.findById(volumeId); + VolumeVO volume = volDao.findById(volumeId); PrimaryDataStore primaryDataStore = this.dataStoreMgr.getPrimaryDataStore(volume.getPoolId()); ResizeVolumeCommand resizeCmd = new ResizeVolumeCommand(volume.getPath(), new StorageFilerTO(primaryDataStore), volume.getSize(), newSize, true, instanceName); @@ -1374,7 +1385,7 @@ public class VolumeServiceImpl implements VolumeService { List dbVolumes = _volumeStoreDao.listUploadedVolumesByStoreId(storeId); List toBeDownloaded = new ArrayList(dbVolumes); for (VolumeDataStoreVO volumeStore : dbVolumes) { - VolumeVO volume = _volumeDao.findById(volumeStore.getVolumeId()); + VolumeVO volume = volDao.findById(volumeStore.getVolumeId()); if (volume == null) { s_logger.warn("Volume_store_ref shows that volume " + volumeStore.getVolumeId() + " is on image store " + storeId + ", but the volume is not found in volumes table, potentially some bugs in deleteVolume, so we just treat this volume to be deleted and mark it as destroyed"); @@ -1419,7 +1430,7 @@ public class VolumeServiceImpl implements VolumeService { if (volume.getSize() == 0) { // Set volume size in volumes table volume.setSize(volInfo.getSize()); - _volumeDao.update(volumeStore.getVolumeId(), volume); + volDao.update(volumeStore.getVolumeId(), volume); } if (volInfo.getSize() > 0) { diff --git a/server/src/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/com/cloud/storage/VolumeApiServiceImpl.java index 928c63b4d32..785a95e00b3 100644 --- a/server/src/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/com/cloud/storage/VolumeApiServiceImpl.java @@ -298,6 +298,8 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic UriUtils.validateUrl(format, url); + // check URL existence + UriUtils.checkUrlExistence(url); // Check that the resource limit for secondary storage won't be exceeded _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); diff --git a/utils/src/com/cloud/utils/UriUtils.java b/utils/src/com/cloud/utils/UriUtils.java index 9f6f0377688..631c629aed3 100644 --- a/utils/src/com/cloud/utils/UriUtils.java +++ b/utils/src/com/cloud/utils/UriUtils.java @@ -38,15 +38,18 @@ import javax.net.ssl.HttpsURLConnection; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.HeadMethod; import org.apache.commons.httpclient.util.URIUtil; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URLEncodedUtils; + import org.apache.http.message.BasicNameValuePair; import org.apache.log4j.Logger; @@ -273,12 +276,28 @@ public class UriUtils { checkFormat(format, uripath); } return new Pair(host, port); - } catch (URISyntaxException use) { throw new IllegalArgumentException("Invalid URL: " + url); } } + // use http HEAD method to validate url + public static void checkUrlExistence(String url) { + if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) { + HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); + HeadMethod httphead = new HeadMethod(url); + try { + if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) { + throw new IllegalArgumentException("Invalid URL: " + url); + } + } catch (HttpException hte) { + throw new IllegalArgumentException("Cannot reach URL: " + url); + } catch (IOException ioe) { + throw new IllegalArgumentException("Cannot reach URL: " + url); + } + } + } + // verify if a URI path is compliance with the file format given private static void checkFormat(String format, String uripath) { if ((!uripath.toLowerCase().endsWith("vhd")) && (!uripath.toLowerCase().endsWith("vhd.zip")) && (!uripath.toLowerCase().endsWith("vhd.bz2")) &&