Skip to content

Commit ca98f9a

Browse files
committed
DXE-3345 Code cleanup
1 parent 16f46cc commit ca98f9a

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

src/main/java/com/akamai/edgeauth/EdgeAuth.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public EdgeAuth(
121121
}
122122

123123
/**
124-
* Makes a string array to joined a string with delimiter.
124+
* Makes a string array to join a string with delimiter.
125125
*
126126
* @param delimiter {@code ACL_DELIMITER}
127127
* @param lists ACL(Access Control List) string array
@@ -149,7 +149,7 @@ public static String join(char delimiter, String[] lists) throws EdgeAuthExcepti
149149
* @throws EdgeAuthException EdgeAuthException
150150
*/
151151
private String escapeEarly(final String text) throws EdgeAuthException {
152-
if (this.escapeEarly == true) {
152+
if (this.escapeEarly) {
153153
try {
154154
StringBuilder newText = new StringBuilder(URLEncoder.encode(text, "UTF-8"));
155155
Pattern pattern = Pattern.compile("%..");
@@ -183,7 +183,7 @@ private String generateToken(String path, boolean isUrl) throws EdgeAuthExceptio
183183
Long startTime = this.startTime;
184184
Long endTime = this.endTime;
185185

186-
if (startTime == EdgeAuth.NOW) {
186+
if (EdgeAuth.NOW.equals(startTime)) {
187187
startTime = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000L;
188188
} else if(startTime != null && startTime < 0) {
189189
throw new EdgeAuthException("startTime must be ( > 0 )");
@@ -287,9 +287,7 @@ private String generateToken(String path, boolean isUrl) throws EdgeAuthExceptio
287287
byte[] hmacBytes = hmac.doFinal(hashSource.toString().getBytes());
288288
return newToken.toString() + "hmac=" +
289289
String.format("%0" + (2*hmac.getMacLength()) + "x", new BigInteger(1, hmacBytes));
290-
} catch (NoSuchAlgorithmException e) {
291-
throw new EdgeAuthException(e.toString());
292-
} catch (InvalidKeyException e) {
290+
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
293291
throw new EdgeAuthException(e.toString());
294292
}
295293
}
@@ -302,7 +300,7 @@ private String generateToken(String path, boolean isUrl) throws EdgeAuthExceptio
302300
* @throws EdgeAuthException EdgeAuthException
303301
*/
304302
public String generateURLToken(String url) throws EdgeAuthException {
305-
if (url == null || url == "") {
303+
if (url == null || url.trim().isEmpty()) {
306304
throw new EdgeAuthException("You must provide a URL.");
307305
}
308306
return generateToken(url, true);
@@ -316,7 +314,7 @@ public String generateURLToken(String url) throws EdgeAuthException {
316314
* @throws EdgeAuthException EdgeAuthException
317315
*/
318316
public String generateACLToken(String acl) throws EdgeAuthException {
319-
if (acl == null || acl == "") {
317+
if (acl == null || acl.trim().isEmpty()) {
320318
throw new EdgeAuthException("You must provide an ACL.");
321319
}
322320
return generateToken(acl, false);
@@ -350,7 +348,7 @@ public void setTokenType(String tokenType) {
350348
* @throws EdgeAuthException EdgeAuthException
351349
*/
352350
public void setTokenName(String tokenName) throws EdgeAuthException {
353-
if (tokenName == null || tokenName == "") {
351+
if (tokenName == null || tokenName.trim().trim().isEmpty()) {
354352
throw new EdgeAuthException("You must provide a token name.");
355353
}
356354
this.tokenName = tokenName;
@@ -361,7 +359,7 @@ public void setTokenName(String tokenName) throws EdgeAuthException {
361359
* @throws EdgeAuthException EdgeAuthException
362360
*/
363361
public void setKey(String key) throws EdgeAuthException {
364-
if (key == null || key == "") {
362+
if (key == null || key.trim().isEmpty()) {
365363
throw new EdgeAuthException("You must provide a secret in order to generate a new token.");
366364
}
367365
this.key = key;
@@ -530,7 +528,7 @@ public Long getEndTime() {
530528
/**
531529
* @return windowSeconds
532530
*/
533-
public Long getwindowSeconds() {
531+
public Long getWindowSeconds() {
534532
return this.windowSeconds;
535533
}
536534

src/main/java/com/akamai/edgeauth/EdgeAuthBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class EdgeAuthBuilder {
4040
/** additional data validated by the token but NOT included in the token body. It will be deprecated. */
4141
private String salt = null;
4242

43-
/** IP Address to restrict this token to. Troublesome in many cases (roaming, NAT, etc) so not often used. */
43+
/** IP Address to restrict this token to. Troublesome in many cases (roaming, NAT, etc.) so not often used. */
4444
private String ip = null;
4545

4646
/** additional text added to the calculated digest. */

src/test/java/com/akamai/edgeauth/EdgeAuthTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import java.net.UnknownHostException;
1010
import java.util.Map;
1111

12-
import static org.junit.Assert.*;
12+
import static org.junit.Assert.assertEquals;
1313

1414
import org.junit.After;
1515
import org.junit.Before;
1616
import org.junit.Test;
1717

1818

1919
public class EdgeAuthTest {
20-
private static long DEFAULT_WINDOW_SECONDS = 500L;
20+
private static final long DEFAULT_WINDOW_SECONDS = 500L;
2121

2222
private EdgeAuth ea;
2323
private EdgeAuth cea;
@@ -108,13 +108,13 @@ private static String requests(String hostname, String path, String qs, String h
108108
sb.append("Host: ");
109109
sb.append(hostname);
110110
sb.append("\r\n");
111-
if (header != null && header != "") {
111+
if (header != null && !header.isEmpty()) {
112112
sb.append(header);
113113
sb.append("\r\n");
114114
}
115115
sb.append("\r\n");
116116

117-
request.print(sb.toString());
117+
request.print(sb);
118118
request.flush();
119119

120120
InputStream inStream = socket.getInputStream();
@@ -137,13 +137,13 @@ private static String requests(String hostname, String path, String qs, String h
137137
return statusCode;
138138
}
139139

140-
private void queryAssertEqual(String path, String expacted, boolean escapeEarly, boolean transition, String payload, String sessionId, boolean isUrl) throws EdgeAuthException, UnknownHostException, IOException {
140+
private void queryAssertEqual(String path, String expected, boolean escapeEarly, boolean transition, String payload, String sessionId, boolean isUrl) throws EdgeAuthException, UnknownHostException, IOException {
141141
this.tokenSetting('q', escapeEarly, transition);
142142

143143
ea.setPayload(payload);
144144
ea.setSessionId(sessionId);
145145

146-
String token = "";
146+
String token;
147147
if (isUrl) {
148148
token = this.ea.generateURLToken(path);
149149
} else {
@@ -152,16 +152,16 @@ private void queryAssertEqual(String path, String expacted, boolean escapeEarly,
152152

153153
String qs = this.ea.getTokenName() + "=" + token;
154154
String statusCode = EdgeAuthTest.requests(this.eaHostname, path, qs, null);
155-
assertEquals(expacted, statusCode);
155+
assertEquals(expected, statusCode);
156156
}
157157

158-
private void cookieAssertEqual(String path, String expacted, boolean escapeEarly, boolean transition, String payload, String sessionId, boolean isUrl) throws EdgeAuthException, UnknownHostException, IOException {
158+
private void cookieAssertEqual(String path, String expected, boolean escapeEarly, boolean transition, String payload, String sessionId, boolean isUrl) throws EdgeAuthException, UnknownHostException, IOException {
159159
this.tokenSetting('c', escapeEarly, transition);
160160

161161
cea.setPayload(payload);
162162
cea.setSessionId(sessionId);
163163

164-
String token = "";
164+
String token;
165165
if (isUrl) {
166166
token = this.cea.generateURLToken(path);
167167
} else {
@@ -170,16 +170,16 @@ private void cookieAssertEqual(String path, String expacted, boolean escapeEarly
170170

171171
String cookie = "Cookie: " + this.cea.getTokenName() + "=" + token;
172172
String statusCode = EdgeAuthTest.requests(this.eaHostname, path, null, cookie);
173-
assertEquals(expacted, statusCode);
173+
assertEquals(expected, statusCode);
174174
}
175175

176-
private void headerAssertEqual(String path, String expacted, boolean escapeEarly, boolean transition, String payload, String sessionId, boolean isUrl) throws EdgeAuthException, UnknownHostException, IOException {
176+
private void headerAssertEqual(String path, String expected, boolean escapeEarly, boolean transition, String payload, String sessionId, boolean isUrl) throws EdgeAuthException, UnknownHostException, IOException {
177177
this.tokenSetting('h', escapeEarly, transition);
178178

179179
hea.setPayload(payload);
180180
hea.setSessionId(sessionId);
181181

182-
String token = "";
182+
String token;
183183
if (isUrl) {
184184
token = this.hea.generateURLToken(path);
185185
} else {
@@ -188,7 +188,7 @@ private void headerAssertEqual(String path, String expacted, boolean escapeEarly
188188

189189
String header = this.hea.getTokenName() + ":" + token;
190190
String statusCode = EdgeAuthTest.requests(this.eaHostname, path, null, header);
191-
assertEquals(expacted, statusCode);
191+
assertEquals(expected, statusCode);
192192
}
193193

194194
private void testCaseSet(String queryPath, String cookiePath, String headerPath, boolean escapeEarly, boolean isUrl) throws UnknownHostException, EdgeAuthException, IOException {
@@ -309,7 +309,7 @@ public void test_acl_deli_escape_on__ignoreQuery_yes() throws UnknownHostExcepti
309309
.key(this.eaEncryptionKey)
310310
.windowSeconds(EdgeAuthTest.DEFAULT_WINDOW_SECONDS)
311311
.build();
312-
String acl[] = { "/q_escape_ignore", "/q_escape_ignore/*" };
312+
String[] acl = { "/q_escape_ignore", "/q_escape_ignore/*" };
313313

314314
// For Java 8=
315315
// String token = ead.generateACLToken(String.join(EdgeAuth.ACL_DELIMITER, acl));
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.akamai.edgeauth;
22

33

4-
public class SecretSample {
5-
public static final String ET_HOSTNAME = "YOUR_HOSTNAME";
6-
public static final String ET_ENCRYPTION_KEY = "0123456789";
7-
public static final String ET_TRANSITION_KEY = "0123456789";
8-
public static final String ET_SALT_KEY = "YOUR_SALT";
4+
class SecretSample {
5+
public static final String EA_HOSTNAME = "YOUR_HOSTNAME";
6+
public static final String EA_ENCRYPTION_KEY = "0123456789";
7+
public static final String EA_TRANSITION_KEY = "0123456789";
8+
public static final String EA_SALT_KEY = "YOUR_SALT";
99
}

0 commit comments

Comments
 (0)