Updated Branches: refs/heads/object_store 2aab3c884 -> 235825dc3
Consolidate code to use UriUtils.validateUrl instead of repeating code several places. Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/f5732fe3 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/f5732fe3 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/f5732fe3 Branch: refs/heads/object_store Commit: f5732fe3bff9970eb913bfbd9a8912dbb8d8592a Parents: 2aab3c8 Author: Min Chen <min.c...@citrix.com> Authored: Tue May 21 16:50:02 2013 -0700 Committer: Min Chen <min.c...@citrix.com> Committed: Tue May 21 16:50:02 2013 -0700 ---------------------------------------------------------------------- .../storage/template/HttpTemplateDownloader.java | 42 +------------- .../storage/template/S3TemplateDownloader.java | 46 +-------------- .../src/com/cloud/storage/VolumeManagerImpl.java | 42 +------------- .../cloud/template/HypervisorTemplateAdapter.java | 36 +---------- utils/src/com/cloud/utils/UriUtils.java | 5 +- 5 files changed, 13 insertions(+), 158 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f5732fe3/core/src/com/cloud/storage/template/HttpTemplateDownloader.java ---------------------------------------------------------------------- diff --git a/core/src/com/cloud/storage/template/HttpTemplateDownloader.java b/core/src/com/cloud/storage/template/HttpTemplateDownloader.java index e832c02..4c0e145 100644 --- a/core/src/com/cloud/storage/template/HttpTemplateDownloader.java +++ b/core/src/com/cloud/storage/template/HttpTemplateDownloader.java @@ -50,6 +50,7 @@ import com.cloud.agent.api.storage.Proxy; import com.cloud.storage.StorageLayer; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.Pair; +import com.cloud.utils.UriUtils; /** * Download a template file using HTTP @@ -128,7 +129,7 @@ public class HttpTemplateDownloader implements TemplateDownloader { } toFile = f.getAbsolutePath(); - Pair<String, Integer> hostAndPort = validateUrl(downloadUrl); + Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl); if (proxy != null) { client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); @@ -159,45 +160,6 @@ public class HttpTemplateDownloader implements TemplateDownloader { } - private Pair<String, Integer> validateUrl(String url) throws IllegalArgumentException { - try { - URI uri = new URI(url); - if (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") ) { - throw new IllegalArgumentException("Unsupported scheme for url"); - } - int port = uri.getPort(); - if (!(port == 80 || port == 8080 || port == 443 || port == -1)) { - throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed"); - } - - if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) { - port = 443; - } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) { - port = 80; - } - - String host = uri.getHost(); - try { - InetAddress hostAddr = InetAddress.getByName(host); - if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { - throw new IllegalArgumentException("Illegal host specified in url"); - } - if (hostAddr instanceof Inet6Address) { - throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); - } - return new Pair<String, Integer>(host, port); - } catch (UnknownHostException uhe) { - throw new IllegalArgumentException("Unable to resolve " + host); - } - } catch (IllegalArgumentException iae) { - s_logger.warn("Failed uri validation check: " + iae.getMessage()); - throw iae; - } catch (URISyntaxException use) { - s_logger.warn("Failed uri syntax check: " + use.getMessage()); - throw new IllegalArgumentException(use.getMessage()); - } - } - @Override public long download(boolean resume, DownloadCompleteCallback callback) { switch (status) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f5732fe3/core/src/com/cloud/storage/template/S3TemplateDownloader.java ---------------------------------------------------------------------- diff --git a/core/src/com/cloud/storage/template/S3TemplateDownloader.java b/core/src/com/cloud/storage/template/S3TemplateDownloader.java index 43b582c..5ca6d33 100644 --- a/core/src/com/cloud/storage/template/S3TemplateDownloader.java +++ b/core/src/com/cloud/storage/template/S3TemplateDownloader.java @@ -61,6 +61,7 @@ import com.cloud.agent.api.storage.Proxy; import com.cloud.agent.api.to.S3TO; import com.cloud.utils.Pair; import com.cloud.utils.S3Utils; +import com.cloud.utils.UriUtils; /** * Download a template file using HTTP @@ -132,7 +133,8 @@ public class S3TemplateDownloader implements TemplateDownloader { this.request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler); this.completionCallback = callback; - Pair<String, Integer> hostAndPort = validateUrl(downloadUrl); + Pair<String, Integer> hostAndPort = UriUtils.validateUrl(downloadUrl); + this.fileName = StringUtils.substringAfterLast(downloadUrl, "/"); if (proxy != null) { client.getHostConfiguration().setProxy(proxy.getHost(), proxy.getPort()); @@ -163,48 +165,6 @@ public class S3TemplateDownloader implements TemplateDownloader { } - private Pair<String, Integer> validateUrl(String url) throws IllegalArgumentException { - try { - URI uri = new URI(url); - if (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") ) { - throw new IllegalArgumentException("Unsupported scheme for url"); - } - int port = uri.getPort(); - if (!(port == 80 || port == 8080 || port == 443 || port == -1)) { - throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed"); - } - - if (port == -1 && uri.getScheme().equalsIgnoreCase("https")) { - port = 443; - } else if (port == -1 && uri.getScheme().equalsIgnoreCase("http")) { - port = 80; - } - - this.fileName = StringUtils.substringAfterLast(url, "/"); - - String host = uri.getHost(); - try { - InetAddress hostAddr = InetAddress.getByName(host); - if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { - throw new IllegalArgumentException("Illegal host specified in url"); - } - if (hostAddr instanceof Inet6Address) { - throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); - } - return new Pair<String, Integer>(host, port); - } catch (UnknownHostException uhe) { - throw new IllegalArgumentException("Unable to resolve " + host); - } - - } catch (IllegalArgumentException iae) { - s_logger.warn("Failed uri validation check: " + iae.getMessage()); - throw iae; - } catch (URISyntaxException use) { - s_logger.warn("Failed uri syntax check: " + use.getMessage()); - throw new IllegalArgumentException(use.getMessage()); - } - } - @Override public long download(boolean resume, DownloadCompleteCallback callback) { switch (status) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f5732fe3/server/src/com/cloud/storage/VolumeManagerImpl.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/storage/VolumeManagerImpl.java b/server/src/com/cloud/storage/VolumeManagerImpl.java index 2aa4192..38f9959 100644 --- a/server/src/com/cloud/storage/VolumeManagerImpl.java +++ b/server/src/com/cloud/storage/VolumeManagerImpl.java @@ -481,7 +481,7 @@ public class VolumeManagerImpl extends ManagerBase implements VolumeManager { + " is an invalid for the format " + format.toLowerCase()); } - validateUrl(url); + UriUtils.validateUrl(url); // Check that the resource limit for secondary storage won't be exceeded _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(ownerId), ResourceType.secondary_storage, @@ -2478,47 +2478,7 @@ public class VolumeManagerImpl extends ManagerBase implements VolumeManager { } - private String validateUrl(String url) { - try { - URI uri = new URI(url); - if ((uri.getScheme() == null) - || (!uri.getScheme().equalsIgnoreCase("http") - && !uri.getScheme().equalsIgnoreCase("https") && !uri - .getScheme().equalsIgnoreCase("file"))) { - throw new IllegalArgumentException( - "Unsupported scheme for url: " + url); - } - int port = uri.getPort(); - if (!(port == 80 || port == 8080 || port == 443 || port == -1)) { - throw new IllegalArgumentException( - "Only ports 80, 8080 and 443 are allowed"); - } - String host = uri.getHost(); - try { - InetAddress hostAddr = InetAddress.getByName(host); - if (hostAddr.isAnyLocalAddress() - || hostAddr.isLinkLocalAddress() - || hostAddr.isLoopbackAddress() - || hostAddr.isMulticastAddress()) { - throw new IllegalArgumentException( - "Illegal host specified in url"); - } - if (hostAddr instanceof Inet6Address) { - throw new IllegalArgumentException( - "IPV6 addresses not supported (" - + hostAddr.getHostAddress() + ")"); - } - } catch (UnknownHostException uhe) { - throw new IllegalArgumentException("Unable to resolve " + host); - } - - return uri.toString(); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Invalid URL " + url); - } - - } @Override public boolean canVmRestartOnAnotherServer(long vmId) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f5732fe3/server/src/com/cloud/template/HypervisorTemplateAdapter.java ---------------------------------------------------------------------- diff --git a/server/src/com/cloud/template/HypervisorTemplateAdapter.java b/server/src/com/cloud/template/HypervisorTemplateAdapter.java index 74bae35..e1d535b 100755 --- a/server/src/com/cloud/template/HypervisorTemplateAdapter.java +++ b/server/src/com/cloud/template/HypervisorTemplateAdapter.java @@ -87,36 +87,6 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { return TemplateAdapterType.Hypervisor.getName(); } - private String validateUrl(String url) { - try { - URI uri = new URI(url); - if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") - && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) { - throw new IllegalArgumentException("Unsupported scheme for url: " + url); - } - - int port = uri.getPort(); - if (!(port == 80 || port == 8080 || port == 443 || port == -1)) { - throw new IllegalArgumentException("Only ports 80, 8080 and 443 are allowed"); - } - String host = uri.getHost(); - try { - InetAddress hostAddr = InetAddress.getByName(host); - if (hostAddr.isAnyLocalAddress() || hostAddr.isLinkLocalAddress() || hostAddr.isLoopbackAddress() || hostAddr.isMulticastAddress()) { - throw new IllegalArgumentException("Illegal host specified in url"); - } - if (hostAddr instanceof Inet6Address) { - throw new IllegalArgumentException("IPV6 addresses not supported (" + hostAddr.getHostAddress() + ")"); - } - } catch (UnknownHostException uhe) { - throw new IllegalArgumentException("Unable to resolve " + host); - } - - return uri.toString(); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Invalid URL " + url); - } - } @Override public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException { @@ -128,7 +98,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { throw new InvalidParameterValueException("Please specify a valid iso"); } - profile.setUrl(validateUrl(url)); + UriUtils.validateUrl(url); + profile.setUrl(url); // Check that the resource limit for secondary storage won't be exceeded _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(cmd.getEntityOwnerId()), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); @@ -160,7 +131,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase { throw new InvalidParameterValueException("Please specify a valid URL. URL:" + url + " is an invalid for the format " + cmd.getFormat().toLowerCase()); } - profile.setUrl(validateUrl(url)); + UriUtils.validateUrl(url); + profile.setUrl(url); // Check that the resource limit for secondary storage won't be exceeded _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(cmd.getEntityOwnerId()), ResourceType.secondary_storage, UriUtils.getRemoteSize(url)); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f5732fe3/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 2673177..b9d54d5 100644 --- a/utils/src/com/cloud/utils/UriUtils.java +++ b/utils/src/com/cloud/utils/UriUtils.java @@ -133,8 +133,9 @@ public class UriUtils { public static Pair<String, Integer> validateUrl(String url) throws IllegalArgumentException { try { URI uri = new URI(url); - if (!uri.getScheme().equalsIgnoreCase("http") && !uri.getScheme().equalsIgnoreCase("https") ) { - throw new IllegalArgumentException("Unsupported scheme for url"); + if ((uri.getScheme() == null) || (!uri.getScheme().equalsIgnoreCase("http") + && !uri.getScheme().equalsIgnoreCase("https") && !uri.getScheme().equalsIgnoreCase("file"))) { + throw new IllegalArgumentException("Unsupported scheme for url: " + url); } int port = uri.getPort(); if (!(port == 80 || port == 8080 || port == 443 || port == -1)) {