Skip to content

Commit a242795

Browse files
committed
fixed lint issues
1 parent e95fd5b commit a242795

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
*/
1616

1717
package com.google.cdn;
18-
import java.io.UnsupportedEncodingException;
19-
import java.net.URLEncoder;
20-
import java.security.InvalidKeyException;
21-
import java.security.NoSuchAlgorithmException;
22-
import java.util.Base64;
2318

24-
import javax.crypto.Mac;
25-
import javax.crypto.spec.SecretKeySpec;
2619
import java.nio.file.Files;
2720
import java.nio.file.Paths;
21+
import java.security.InvalidKeyException;
2822
import java.security.Key;
23+
import java.security.NoSuchAlgorithmException;
24+
import java.util.Base64;
2925
import java.util.Calendar;
3026
import java.util.Date;
27+
import javax.crypto.Mac;
28+
import javax.crypto.spec.SecretKeySpec;
3129

30+
/**
31+
* Samples to create a signed URL for a Cloud CDN endpoint
32+
*/
3233
public class SignedUrls {
3334

3435
// [START signUrl]
@@ -52,7 +53,7 @@ public class SignedUrls {
5253
* @param expirationTime the date that the signed URL expires
5354
* @return a properly formatted signed URL
5455
* @throws InvalidKeyException when there is an error generating the signature for the input key
55-
* @throws NoSuchAlgorithmException when the HmacSHA1 algorithm is not available in the environment
56+
* @throws NoSuchAlgorithmException when HmacSHA1 algorithm is not available in the environment
5657
*/
5758
public static String signUrl(String url,
5859
byte[] key,
@@ -62,16 +63,18 @@ public static String signUrl(String url,
6263

6364
final long unixTime = expirationTime.getTime() / 1000;
6465

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

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

74-
public static String getSignature(byte[] privateKey, String input) throws InvalidKeyException, NoSuchAlgorithmException {
75+
public static String getSignature(byte[] privateKey, String input)
76+
throws InvalidKeyException, NoSuchAlgorithmException {
77+
7578
final String algorithm = "HmacSHA1";
7679
Key key = new SecretKeySpec(privateKey, 0, privateKey.length, algorithm);
7780
Mac mac = Mac.getInstance(algorithm);
@@ -87,7 +90,8 @@ public static void main(String[] args) throws Exception {
8790
Date tomorrow = cal.getTime();
8891

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

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,37 @@
1717

1818
package com.google.cdn;
1919

20+
import static com.google.cdn.SignedUrls.signUrl;
21+
import static junit.framework.TestCase.assertEquals;
22+
2023
import java.util.Base64;
2124
import java.util.Date;
2225
import org.junit.Test;
2326
import org.junit.runner.RunWith;
2427
import org.junit.runners.JUnit4;
2528

26-
import static com.google.cdn.SignedUrls.signUrl;
27-
import static junit.framework.TestCase.assertEquals;
28-
29-
29+
/**
30+
* Test SignedUrls samples
31+
*/
3032
@RunWith(JUnit4.class)
3133
@SuppressWarnings("checkstyle:abbreviationaswordinname")
3234
public class SignedUrlsTest {
33-
private static Date EXPIRATION = new Date((long)1518135754*1000);
35+
private static long TIMESTAMP = 1518135754;
36+
private static Date EXPIRATION = new Date(TIMESTAMP * 1000);
3437
private static byte[] KEY_BYTES = Base64.getUrlDecoder().decode("aaaaaaaaaaaaaaaaaaaaaa==");
3538
private static String KEY_NAME = "my-key";
3639
private static String BASE_URL = "https://www.google.com/";
3740

3841
@Test
3942
public void testUrlPath() throws Exception {
40-
String result = signUrl(BASE_URL+"foo", KEY_BYTES, KEY_NAME, EXPIRATION);
43+
String result = signUrl(BASE_URL + "foo", KEY_BYTES, KEY_NAME, EXPIRATION);
4144
final String expected = "https://www.google.com/foo?Expires=1518135754&KeyName=my-key&Signature=SBdQtypBTcz0gvHRDZjy2pc-F0s=";
4245
assertEquals(result, expected);
4346
}
4447

4548
@Test
4649
public void testUrlParams() throws Exception {
47-
String result = signUrl(BASE_URL+"?param=true", KEY_BYTES, KEY_NAME, EXPIRATION);
50+
String result = signUrl(BASE_URL + "?param=true", KEY_BYTES, KEY_NAME, EXPIRATION);
4851
final String expected = "https://www.google.com/?param=true&Expires=1518135754&KeyName=my-key&Signature=ilkstIAKFvOlckbVdfZBWAror3o=";
4952
assertEquals(result, expected);
5053
}

0 commit comments

Comments
 (0)