Repository: cloudstack Updated Branches: refs/heads/4.5 93b2b3a4a -> b85183ede
CLOUDSTACK-7678:volumes are getting uploaded successfully with wrong url. Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/b85183ed Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/b85183ed Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/b85183ed Branch: refs/heads/4.5 Commit: b85183ede7c1a94b8d8fcdec744ac0c1b9b56df9 Parents: 93b2b3a Author: Min Chen <[email protected]> Authored: Fri Nov 21 15:18:47 2014 -0800 Committer: Min Chen <[email protected]> Committed: Fri Nov 21 15:18:47 2014 -0800 ---------------------------------------------------------------------- .../storage/volume/VolumeServiceImpl.java | 23 ++++++++++++++------ .../com/cloud/storage/VolumeApiServiceImpl.java | 2 ++ utils/src/com/cloud/utils/UriUtils.java | 21 +++++++++++++++++- 3 files changed, 38 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b85183ed/engine/storage/volume/src/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java ---------------------------------------------------------------------- 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 7e3239f..e91477e 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 @@ -125,8 +125,6 @@ public class VolumeServiceImpl implements VolumeService { @Inject VMTemplatePoolDao _tmpltPoolDao; @Inject - VolumeDao _volumeDao; - @Inject EndPointSelector _epSelector; @Inject HostDao _hostDao; @@ -1021,7 +1019,7 @@ public class VolumeServiceImpl implements VolumeService { } srcVolume.processEvent(Event.OperationSuccessed); destVolume.processEvent(Event.OperationSuccessed, result.getAnswer()); - _volumeDao.updateUuid(srcVolume.getId(), destVolume.getId()); + volDao.updateUuid(srcVolume.getId(), destVolume.getId()); try { destroyVolume(srcVolume.getId()); srcVolume = volFactory.getVolume(srcVolume.getId()); @@ -1226,10 +1224,16 @@ public class VolumeServiceImpl implements VolumeService { protected Void registerVolumeCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> callback, CreateVolumeContext<VolumeApiResult> 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()); @@ -1268,6 +1272,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; @@ -1303,7 +1312,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); @@ -1375,7 +1384,7 @@ public class VolumeServiceImpl implements VolumeService { List<VolumeDataStoreVO> dbVolumes = _volumeStoreDao.listByStoreId(storeId); List<VolumeDataStoreVO> toBeDownloaded = new ArrayList<VolumeDataStoreVO>(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"); @@ -1420,7 +1429,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) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b85183ed/server/src/com/cloud/storage/VolumeApiServiceImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/com/cloud/storage/VolumeApiServiceImpl.java index 26678e6..92d84d5 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)); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b85183ed/utils/src/com/cloud/utils/UriUtils.java ---------------------------------------------------------------------- diff --git a/utils/src/com/cloud/utils/UriUtils.java b/utils/src/com/cloud/utils/UriUtils.java index 9f6f037..631c629 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<String, Integer>(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")) &&
