blob: 3bcee7aeac4685bf8572105a03e97f2fdd2091b2 [file] [log] [blame]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// 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 com.cloud.utils;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
import com.cloud.utils.exception.CloudRuntimeException;
public class UriUtils {
public static String formNfsUri(String host, String path) {
try {
URI uri = new URI("nfs", host, path, null);
return uri.toString();
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Unable to form nfs URI: " + host + " - " + path);
}
}
public static String formIscsiUri(String host, String iqn, Integer lun) {
try {
String path = iqn;
if (lun != null) {
path += "/" + lun.toString();
}
URI uri = new URI("iscsi", host, path, null);
return uri.toString();
} catch (URISyntaxException e) {
throw new CloudRuntimeException("Unable to form iscsi URI: " + host + " - " + iqn + " - " + lun);
}
}
public static String formFileUri(String path) {
File file = new File(path);
return file.toURI().toString();
}
// a simple URI component helper (Note: it does not deal with URI paramemeter area)
public static String encodeURIComponent(String url) {
int schemeTail = url.indexOf("://");
int pathStart = 0;
if(schemeTail > 0)
pathStart = url.indexOf('/', schemeTail + 3);
else
pathStart = url.indexOf('/');
if(pathStart > 0) {
String[] tokens = url.substring(pathStart + 1).split("/");
if(tokens != null) {
StringBuffer sb = new StringBuffer();
sb.append(url.substring(0, pathStart));
for(String token : tokens) {
sb.append("/").append(URLEncoder.encode(token));
}
return sb.toString();
}
}
// no need to do URL component encoding
return url;
}
// Get the size of a file from URL response header.
public static Long getRemoteSize(String url) {
Long remoteSize = (long) 0;
HttpURLConnection httpConn = null;
HttpsURLConnection httpsConn = null;
try {
URI uri = new URI(url);
if(uri.getScheme().equalsIgnoreCase("http")) {
httpConn = (HttpURLConnection) uri.toURL().openConnection();
remoteSize = Long.parseLong(httpConn.getHeaderField("content-length"));
}
else if(uri.getScheme().equalsIgnoreCase("https")) {
httpsConn = (HttpsURLConnection) uri.toURL().openConnection();
remoteSize = Long.parseLong(httpsConn.getHeaderField("content-length"));
}
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URL " + url);
} catch (IOException e) {
throw new IllegalArgumentException("Unable to establish connection with URL " + url);
} finally {
if (httpConn != null) {
httpConn.disconnect();
} else if (httpsConn != null) {
httpsConn.disconnect();
}
}
return remoteSize;
}
}