Skip to content

Commit de2a3f0

Browse files
committed
fixed indents
1 parent 481a26e commit de2a3f0

File tree

2 files changed

+85
-87
lines changed

2 files changed

+85
-87
lines changed

cdn/signed-urls/src/main/java/com/google/cdn/SignedUrls.java

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -32,70 +32,70 @@
3232
*/
3333
public class SignedUrls {
3434

35-
// [START signUrl]
36-
/**
37-
* Creates a signed URL for a Cloud CDN endpoint with the given key
38-
* should pass in a properly formatted URL
39-
* will work:
40-
* https://www.google.com/
41-
* http://www.google.com/
42-
* https://www.google.com/?foo=test&bar=test
43-
* https://www.google.com/foo
44-
*
45-
* won't work:
46-
* https://www.google.com
47-
* https://www.google.com?test
48-
* www.google.com
49-
*
50-
* @param url the Cloud CDN endpoint to sign
51-
* @param key encoded as a 16-byte array
52-
* @param keyName the name of the signing key added to the back end bucket or service
53-
* @param expirationTime the date that the signed URL expires
54-
* @return a properly formatted signed URL
55-
* @throws InvalidKeyException when there is an error generating the signature for the input key
56-
* @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment
57-
*/
58-
public static String signUrl(String url,
59-
byte[] key,
60-
String keyName,
61-
Date expirationTime)
62-
throws InvalidKeyException, NoSuchAlgorithmException {
35+
// [START signUrl]
36+
/**
37+
* Creates a signed URL for a Cloud CDN endpoint with the given key
38+
* should pass in a properly formatted URL
39+
* will work:
40+
* https://www.google.com/
41+
* http://www.google.com/
42+
* https://www.google.com/?foo=test&bar=test
43+
* https://www.google.com/foo
44+
*
45+
* won't work:
46+
* https://www.google.com
47+
* https://www.google.com?test
48+
* www.google.com
49+
*
50+
* @param url the Cloud CDN endpoint to sign
51+
* @param key encoded as a 16-byte array
52+
* @param keyName the name of the signing key added to the back end bucket or service
53+
* @param expirationTime the date that the signed URL expires
54+
* @return a properly formatted signed URL
55+
* @throws InvalidKeyException when there is an error generating the signature for the input key
56+
* @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment
57+
*/
58+
public static String signUrl(String url,
59+
byte[] key,
60+
String keyName,
61+
Date expirationTime)
62+
throws InvalidKeyException, NoSuchAlgorithmException {
6363

64-
final long unixTime = expirationTime.getTime() / 1000;
64+
final long unixTime = expirationTime.getTime() / 1000;
6565

66-
String urlToSign = url
67-
+ (url.contains("?") ? "&" : "?")
68-
+ "Expires=" + unixTime
69-
+ "&KeyName=" + keyName;
66+
String urlToSign = url
67+
+ (url.contains("?") ? "&" : "?")
68+
+ "Expires=" + unixTime
69+
+ "&KeyName=" + keyName;
7070

71-
String encoded = SignedUrls.getSignature(key, urlToSign);
72-
return urlToSign + "&Signature=" + encoded;
73-
}
71+
String encoded = SignedUrls.getSignature(key, urlToSign);
72+
return urlToSign + "&Signature=" + encoded;
73+
}
7474

75-
public static String getSignature(byte[] privateKey, String input)
76-
throws InvalidKeyException, NoSuchAlgorithmException {
75+
public static String getSignature(byte[] privateKey, String input)
76+
throws InvalidKeyException, NoSuchAlgorithmException {
7777

78-
final String algorithm = "HmacSHA1";
79-
Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm);
80-
Mac mac = Mac.getInstance(algorithm);
81-
mac.init(key);
82-
return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes()));
83-
}
84-
// [END signUrl]
78+
final String algorithm = "HmacSHA1";
79+
Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm);
80+
Mac mac = Mac.getInstance(algorithm);
81+
mac.init(key);
82+
return Base64.getUrlEncoder().encodeToString(mac.doFinal(input.getBytes()));
83+
}
84+
// [END signUrl]
8585

86-
public static void main(String[] args) throws Exception {
87-
Calendar cal = Calendar.getInstance();
88-
cal.setTime(new Date());
89-
cal.add(Calendar.DATE, 1);
90-
Date tomorrow = cal.getTime();
86+
public static void main(String[] args) throws Exception {
87+
Calendar cal = Calendar.getInstance();
88+
cal.setTime(new Date());
89+
cal.add(Calendar.DATE, 1);
90+
Date tomorrow = cal.getTime();
9191

92-
//read the key as a base 64 url-safe encoded string
93-
final String keyPath = "./src/main/resources/my-key";
94-
String base64String = new String(Files.readAllBytes(Paths.get(keyPath)));
95-
//turn the key string into a byte array
96-
byte[] keyBytes = Base64.getUrlDecoder().decode(base64String);
92+
//read the key as a base 64 url-safe encoded string
93+
final String keyPath = "./src/main/resources/my-key";
94+
String base64String = new String(Files.readAllBytes(Paths.get(keyPath)));
95+
//turn the key string into a byte array
96+
byte[] keyBytes = Base64.getUrlDecoder().decode(base64String);
9797

98-
String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow);
99-
System.out.println(result);
100-
}
98+
String result = signUrl("http://35.186.234.33/index.html", keyBytes, "my-key", tomorrow);
99+
System.out.println(result);
100+
}
101101
}

cdn/signed-urls/src/test/java/com/google/cdn/SignedUrlsTest.java

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,31 @@
3232
@RunWith(JUnit4.class)
3333
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3434
public class SignedUrlsTest {
35-
private static long TIMESTAMP = 1518135754;
36-
private static Date EXPIRATION = new Date(TIMESTAMP * 1000);
37-
private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa==");
38-
private static String KEY_NAME = "my-key";
39-
private static String BASE_URL = "https://www.google.com/";
40-
41-
@Test
42-
public void testUrlPath() throws Exception {
43-
String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION);
44-
final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s=";
45-
assertEquals(result, expected);
46-
}
47-
48-
@Test
49-
public void testUrlParams() throws Exception {
50-
String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION);
51-
final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o=";
52-
assertEquals(result, expected);
53-
}
54-
55-
56-
@Test
57-
public void testStandard() throws Exception {
58-
String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION);
59-
final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM=";
60-
assertEquals(result, expected);
61-
}
62-
63-
35+
private static long TIMESTAMP = 1518135754;
36+
private static Date EXPIRATION = new Date(TIMESTAMP * 1000);
37+
private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa==");
38+
private static String KEY_NAME = "my-key";
39+
private static String BASE_URL = "https://www.google.com/";
40+
41+
@Test
42+
public void testUrlPath() throws Exception {
43+
String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION);
44+
final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s=";
45+
assertEquals(result, expected);
46+
}
47+
48+
@Test
49+
public void testUrlParams() throws Exception {
50+
String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION);
51+
final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o=";
52+
assertEquals(result, expected);
53+
}
54+
55+
56+
@Test
57+
public void testStandard() throws Exception {
58+
String result = signUrl(BASE_URL, KEY_BYTES, KEY_NAME, EXPIRATION);
59+
final String expected = "https://www.google.com/?Expires=1518135754&KeyName=my-key&Signature=yYnIFLMqsuGfpSuo7nf7wk21boM=";
60+
assertEquals(result, expected);
61+
}
6462
}

0 commit comments

Comments
 (0)