forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageGatewayUtils.java
More file actions
77 lines (66 loc) · 3.08 KB
/
StorageGatewayUtils.java
File metadata and controls
77 lines (66 loc) · 3.08 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
/*
* Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amazonaws.services.storagegateway;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import com.amazonaws.AmazonClientException;
/**
* Utilities for working with the AWS Storage Gateway service, such as
* requesting a running AWS Storage Gateway instances's activation key for
* registering a Storage Gateway server.
*/
public class StorageGatewayUtils {
/**
* Sends a request to the AWS Storage Gateway server running at the
* specified address, and returns the activation key for that server.
*
* @param gatewayAddress
* The DNS name or IP address of a running AWS Storage Gateway
*
* @return The activation key required for some API calls to AWS Storage Gateway.
*
* @throws AmazonClientException
* If any problems are encountered while trying to contact the
* remote AWS Storage Gateway server.
*/
public static String getActivationKey(String gatewayAddress) throws AmazonClientException {
try {
HttpParams httpClientParams = new BasicHttpParams();
httpClientParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
DefaultHttpClient client = new DefaultHttpClient(httpClientParams);
HttpGet method = new HttpGet("http://" + gatewayAddress);
HttpResponse response = client.execute(method);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 302)
throw new AmazonClientException("Could not fetch activation key. HTTP status code: " + statusCode);
Header[] headers = response.getHeaders("Location");
if (headers.length < 1)
throw new AmazonClientException("Could not fetch activation key, no location header found");
String activationUrl = headers[0].getValue();
String[] parts = activationUrl.split("activationKey=");
if (parts.length < 2 || null == parts[1])
throw new AmazonClientException("Unable to get activation key from : " + activationUrl);
return parts[1];
} catch (IOException ioe) {
throw new AmazonClientException("Unable to get activation key", ioe);
}
}
}