forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUriUtils.java
More file actions
114 lines (101 loc) · 4.07 KB
/
Copy pathUriUtils.java
File metadata and controls
114 lines (101 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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;
}
}