Consolidate code to use UriUtils.validateUrl instead of repeating code

several places.
This commit is contained in:
Min Chen 2013-05-21 16:50:02 -07:00
parent 2aab3c8849
commit f5732fe3bf
5 changed files with 13 additions and 158 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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));

View File

@ -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)) {