|
10 | 10 | // limitations under the License. |
11 | 11 | // |
12 | 12 | // Automatically generated by addcopyright.py at 04/03/2012 |
13 | | -package com.cloud.sample; |
14 | | - |
15 | | -import java.io.FileInputStream; |
16 | | -import java.net.URLEncoder; |
17 | | -import java.util.ArrayList; |
18 | | -import java.util.Collections; |
19 | | -import java.util.List; |
20 | | -import java.util.Properties; |
21 | | -import java.util.StringTokenizer; |
22 | | - |
23 | | -import javax.crypto.Mac; |
24 | | -import javax.crypto.spec.SecretKeySpec; |
25 | | - |
26 | | -import org.apache.commons.httpclient.HttpClient; |
27 | | -import org.apache.commons.httpclient.HttpMethod; |
28 | | -import org.apache.commons.httpclient.methods.GetMethod; |
29 | | - |
30 | | -import com.cloud.utils.encoding.Base64; |
31 | | - |
32 | | -/** |
| 13 | +package com.cloud.sample; |
| 14 | + |
| 15 | +import java.io.FileInputStream; |
| 16 | +import java.net.URLEncoder; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Properties; |
| 21 | +import java.util.StringTokenizer; |
| 22 | + |
| 23 | +import javax.crypto.Mac; |
| 24 | +import javax.crypto.spec.SecretKeySpec; |
| 25 | + |
| 26 | +import org.apache.commons.httpclient.HttpClient; |
| 27 | +import org.apache.commons.httpclient.HttpMethod; |
| 28 | +import org.apache.commons.httpclient.methods.GetMethod; |
| 29 | + |
| 30 | +import com.cloud.utils.encoding.Base64; |
| 31 | + |
| 32 | +/** |
33 | 33 | * |
34 | 34 | * |
35 | 35 | * |
36 | 36 | * |
37 | | - * |
38 | | - * |
39 | | - * |
40 | | - * |
41 | | - * |
42 | | - */ |
43 | | - |
44 | | -/** |
45 | | - * Sample CloudStack Management User API Executor. |
46 | | - * |
47 | | - * Prerequisites: - Edit usercloud.properties to include your host, apiUrl, apiKey, and secretKey - Use ./executeUserAPI.sh to |
48 | | - * execute this test class |
49 | | - * |
50 | | - * @author will |
51 | | - * |
52 | | - */ |
53 | | -public class UserCloudAPIExecutor { |
54 | | - public static void main(String[] args) { |
55 | | - // Host |
56 | | - String host = null; |
57 | | - |
58 | | - // Fully qualified URL with http(s)://host:port |
59 | | - String apiUrl = null; |
60 | | - |
61 | | - // ApiKey and secretKey as given by your CloudStack vendor |
62 | | - String apiKey = null; |
63 | | - String secretKey = null; |
64 | | - |
65 | | - try { |
66 | | - Properties prop = new Properties(); |
67 | | - prop.load(new FileInputStream("usercloud.properties")); |
68 | | - |
69 | | - // host |
70 | | - host = prop.getProperty("host"); |
71 | | - if (host == null) { |
72 | | - System.out.println("Please specify a valid host in the format of http(s)://:/client/api in your usercloud.properties file."); |
73 | | - } |
74 | | - |
75 | | - // apiUrl |
76 | | - apiUrl = prop.getProperty("apiUrl"); |
77 | | - if (apiUrl == null) { |
78 | | - System.out.println("Please specify a valid API URL in the format of command=¶m1=¶m2=... in your usercloud.properties file."); |
79 | | - } |
80 | | - |
81 | | - // apiKey |
82 | | - apiKey = prop.getProperty("apiKey"); |
83 | | - if (apiKey == null) { |
84 | | - System.out.println("Please specify your API Key as provided by your CloudStack vendor in your usercloud.properties file."); |
85 | | - } |
86 | | - |
87 | | - // secretKey |
88 | | - secretKey = prop.getProperty("secretKey"); |
89 | | - if (secretKey == null) { |
90 | | - System.out.println("Please specify your secret Key as provided by your CloudStack vendor in your usercloud.properties file."); |
91 | | - } |
92 | | - |
93 | | - if (apiUrl == null || apiKey == null || secretKey == null) { |
94 | | - return; |
95 | | - } |
96 | | - |
97 | | - System.out.println("Constructing API call to host = '" + host + "' with API command = '" + apiUrl + "' using apiKey = '" + apiKey + "' and secretKey = '" + secretKey + "'"); |
98 | | - |
99 | | - // Step 1: Make sure your APIKey is URL encoded |
100 | | - String encodedApiKey = URLEncoder.encode(apiKey, "UTF-8"); |
101 | | - |
102 | | - // Step 2: URL encode each parameter value, then sort the parameters and apiKey in |
103 | | - // alphabetical order, and then toLowerCase all the parameters, parameter values and apiKey. |
104 | | - // Please note that if any parameters with a '&' as a value will cause this test client to fail since we are using |
105 | | - // '&' to delimit |
106 | | - // the string |
107 | | - List<String> sortedParams = new ArrayList<String>(); |
108 | | - sortedParams.add("apikey=" + encodedApiKey.toLowerCase()); |
109 | | - StringTokenizer st = new StringTokenizer(apiUrl, "&"); |
110 | | - String url = null; |
111 | | - boolean first = true; |
112 | | - while (st.hasMoreTokens()) { |
113 | | - String paramValue = st.nextToken(); |
114 | | - String param = paramValue.substring(0, paramValue.indexOf("=")); |
115 | | - String value = URLEncoder.encode(paramValue.substring(paramValue.indexOf("=") + 1, paramValue.length()), "UTF-8"); |
116 | | - if (first) { |
117 | | - url = param + "=" + value; |
118 | | - first = false; |
119 | | - } else { |
120 | | - url = url + "&" + param + "=" + value; |
121 | | - } |
122 | | - sortedParams.add(param.toLowerCase() + "=" + value.toLowerCase()); |
123 | | - } |
124 | | - Collections.sort(sortedParams); |
125 | | - |
126 | | - System.out.println("Sorted Parameters: " + sortedParams); |
127 | | - |
128 | | - // Step 3: Construct the sorted URL and sign and URL encode the sorted URL with your secret key |
129 | | - String sortedUrl = null; |
130 | | - first = true; |
131 | | - for (String param : sortedParams) { |
132 | | - if (first) { |
133 | | - sortedUrl = param; |
134 | | - first = false; |
135 | | - } else { |
136 | | - sortedUrl = sortedUrl + "&" + param; |
137 | | - } |
138 | | - } |
139 | | - System.out.println("sorted URL : " + sortedUrl); |
140 | | - String encodedSignature = signRequest(sortedUrl, secretKey); |
141 | | - |
142 | | - // Step 4: Construct the final URL we want to send to the CloudStack Management Server |
143 | | - // Final result should look like: |
144 | | - // http(s)://://client/api?&apiKey=&signature= |
145 | | - String finalUrl = host + "?" + url + "&apiKey=" + apiKey + "&signature=" + encodedSignature; |
146 | | - System.out.println("final URL : " + finalUrl); |
147 | | - |
148 | | - // Step 5: Perform a HTTP GET on this URL to execute the command |
149 | | - HttpClient client = new HttpClient(); |
150 | | - HttpMethod method = new GetMethod(finalUrl); |
151 | | - int responseCode = client.executeMethod(method); |
152 | | - if (responseCode == 200) { |
153 | | - // SUCCESS! |
154 | | - System.out.println("Successfully executed command"); |
155 | | - } else { |
156 | | - // FAILED! |
157 | | - System.out.println("Unable to execute command with response code: " + responseCode); |
158 | | - } |
159 | | - |
160 | | - } catch (Throwable t) { |
161 | | - System.out.println(t); |
162 | | - } |
163 | | - } |
164 | | - |
165 | | - /** |
166 | | - * 1. Signs a string with a secret key using SHA-1 2. Base64 encode the result 3. URL encode the final result |
167 | | - * |
168 | | - * @param request |
169 | | - * @param key |
170 | | - * @return |
171 | | - */ |
172 | | - public static String signRequest(String request, String key) { |
173 | | - try { |
174 | | - Mac mac = Mac.getInstance("HmacSHA1"); |
175 | | - SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); |
176 | | - mac.init(keySpec); |
177 | | - mac.update(request.getBytes()); |
178 | | - byte[] encryptedBytes = mac.doFinal(); |
179 | | - return URLEncoder.encode(Base64.encodeBytes(encryptedBytes), "UTF-8"); |
180 | | - } catch (Exception ex) { |
181 | | - System.out.println(ex); |
182 | | - } |
183 | | - return null; |
184 | | - } |
185 | | -} |
| 37 | + * |
| 38 | + * |
| 39 | + * |
| 40 | + * |
| 41 | + * |
| 42 | + */ |
| 43 | + |
| 44 | +/** |
| 45 | + * Sample CloudStack Management User API Executor. |
| 46 | + * |
| 47 | + * Prerequisites: - Edit usercloud.properties to include your host, apiUrl, apiKey, and secretKey - Use ./executeUserAPI.sh to |
| 48 | + * execute this test class |
| 49 | + * |
| 50 | + * @author will |
| 51 | + * |
| 52 | + */ |
| 53 | +public class UserCloudAPIExecutor { |
| 54 | + public static void main(String[] args) { |
| 55 | + // Host |
| 56 | + String host = null; |
| 57 | + |
| 58 | + // Fully qualified URL with http(s)://host:port |
| 59 | + String apiUrl = null; |
| 60 | + |
| 61 | + // ApiKey and secretKey as given by your CloudStack vendor |
| 62 | + String apiKey = null; |
| 63 | + String secretKey = null; |
| 64 | + |
| 65 | + try { |
| 66 | + Properties prop = new Properties(); |
| 67 | + prop.load(new FileInputStream("usercloud.properties")); |
| 68 | + |
| 69 | + // host |
| 70 | + host = prop.getProperty("host"); |
| 71 | + if (host == null) { |
| 72 | + System.out.println("Please specify a valid host in the format of http(s)://:/client/api in your usercloud.properties file."); |
| 73 | + } |
| 74 | + |
| 75 | + // apiUrl |
| 76 | + apiUrl = prop.getProperty("apiUrl"); |
| 77 | + if (apiUrl == null) { |
| 78 | + System.out.println("Please specify a valid API URL in the format of command=¶m1=¶m2=... in your usercloud.properties file."); |
| 79 | + } |
| 80 | + |
| 81 | + // apiKey |
| 82 | + apiKey = prop.getProperty("apiKey"); |
| 83 | + if (apiKey == null) { |
| 84 | + System.out.println("Please specify your API Key as provided by your CloudStack vendor in your usercloud.properties file."); |
| 85 | + } |
| 86 | + |
| 87 | + // secretKey |
| 88 | + secretKey = prop.getProperty("secretKey"); |
| 89 | + if (secretKey == null) { |
| 90 | + System.out.println("Please specify your secret Key as provided by your CloudStack vendor in your usercloud.properties file."); |
| 91 | + } |
| 92 | + |
| 93 | + if (apiUrl == null || apiKey == null || secretKey == null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + System.out.println("Constructing API call to host = '" + host + "' with API command = '" + apiUrl + "' using apiKey = '" + apiKey + "' and secretKey = '" + secretKey + "'"); |
| 98 | + |
| 99 | + // Step 1: Make sure your APIKey is URL encoded |
| 100 | + String encodedApiKey = URLEncoder.encode(apiKey, "UTF-8"); |
| 101 | + |
| 102 | + // Step 2: URL encode each parameter value, then sort the parameters and apiKey in |
| 103 | + // alphabetical order, and then toLowerCase all the parameters, parameter values and apiKey. |
| 104 | + // Please note that if any parameters with a '&' as a value will cause this test client to fail since we are using |
| 105 | + // '&' to delimit |
| 106 | + // the string |
| 107 | + List<String> sortedParams = new ArrayList<String>(); |
| 108 | + sortedParams.add("apikey=" + encodedApiKey.toLowerCase()); |
| 109 | + StringTokenizer st = new StringTokenizer(apiUrl, "&"); |
| 110 | + String url = null; |
| 111 | + boolean first = true; |
| 112 | + while (st.hasMoreTokens()) { |
| 113 | + String paramValue = st.nextToken(); |
| 114 | + String param = paramValue.substring(0, paramValue.indexOf("=")); |
| 115 | + String value = URLEncoder.encode(paramValue.substring(paramValue.indexOf("=") + 1, paramValue.length()), "UTF-8"); |
| 116 | + if (first) { |
| 117 | + url = param + "=" + value; |
| 118 | + first = false; |
| 119 | + } else { |
| 120 | + url = url + "&" + param + "=" + value; |
| 121 | + } |
| 122 | + sortedParams.add(param.toLowerCase() + "=" + value.toLowerCase()); |
| 123 | + } |
| 124 | + Collections.sort(sortedParams); |
| 125 | + |
| 126 | + System.out.println("Sorted Parameters: " + sortedParams); |
| 127 | + |
| 128 | + // Step 3: Construct the sorted URL and sign and URL encode the sorted URL with your secret key |
| 129 | + String sortedUrl = null; |
| 130 | + first = true; |
| 131 | + for (String param : sortedParams) { |
| 132 | + if (first) { |
| 133 | + sortedUrl = param; |
| 134 | + first = false; |
| 135 | + } else { |
| 136 | + sortedUrl = sortedUrl + "&" + param; |
| 137 | + } |
| 138 | + } |
| 139 | + System.out.println("sorted URL : " + sortedUrl); |
| 140 | + String encodedSignature = signRequest(sortedUrl, secretKey); |
| 141 | + |
| 142 | + // Step 4: Construct the final URL we want to send to the CloudStack Management Server |
| 143 | + // Final result should look like: |
| 144 | + // http(s)://://client/api?&apiKey=&signature= |
| 145 | + String finalUrl = host + "?" + url + "&apiKey=" + apiKey + "&signature=" + encodedSignature; |
| 146 | + System.out.println("final URL : " + finalUrl); |
| 147 | + |
| 148 | + // Step 5: Perform a HTTP GET on this URL to execute the command |
| 149 | + HttpClient client = new HttpClient(); |
| 150 | + HttpMethod method = new GetMethod(finalUrl); |
| 151 | + int responseCode = client.executeMethod(method); |
| 152 | + if (responseCode == 200) { |
| 153 | + // SUCCESS! |
| 154 | + System.out.println("Successfully executed command"); |
| 155 | + } else { |
| 156 | + // FAILED! |
| 157 | + System.out.println("Unable to execute command with response code: " + responseCode); |
| 158 | + } |
| 159 | + |
| 160 | + } catch (Throwable t) { |
| 161 | + System.out.println(t); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * 1. Signs a string with a secret key using SHA-1 2. Base64 encode the result 3. URL encode the final result |
| 167 | + * |
| 168 | + * @param request |
| 169 | + * @param key |
| 170 | + * @return |
| 171 | + */ |
| 172 | + public static String signRequest(String request, String key) { |
| 173 | + try { |
| 174 | + Mac mac = Mac.getInstance("HmacSHA1"); |
| 175 | + SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); |
| 176 | + mac.init(keySpec); |
| 177 | + mac.update(request.getBytes()); |
| 178 | + byte[] encryptedBytes = mac.doFinal(); |
| 179 | + return URLEncoder.encode(Base64.encodeBytes(encryptedBytes), "UTF-8"); |
| 180 | + } catch (Exception ex) { |
| 181 | + System.out.println(ex); |
| 182 | + } |
| 183 | + return null; |
| 184 | + } |
| 185 | +} |
0 commit comments