Skip to content

Commit 61fb2ee

Browse files
committed
Creating RetryAfterParser
1 parent bb084c3 commit 61fb2ee

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.opentelemetry.opamp.client.internal.connectivity.http;
2+
3+
import io.opentelemetry.opamp.client.internal.tools.SystemTime;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.time.Duration;
7+
import java.util.Locale;
8+
import java.util.regex.Pattern;
9+
10+
public final class RetryAfterParser {
11+
private final SystemTime systemTime;
12+
public static final Pattern SECONDS_PATTERN = Pattern.compile("\\d+");
13+
public static final Pattern DATE_PATTERN =
14+
Pattern.compile(
15+
"^([A-Za-z]{3}, [0-3][0-9] [A-Za-z]{3} [0-9]{4} [0-2][0-9]:[0-5][0-9]:[0-5][0-9] GMT)$");
16+
private static final SimpleDateFormat dateFormat =
17+
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
18+
19+
public static RetryAfterParser getInstance() {
20+
return new RetryAfterParser(SystemTime.getInstance());
21+
}
22+
23+
RetryAfterParser(SystemTime systemTime) {
24+
this.systemTime = systemTime;
25+
}
26+
27+
public Duration parse(String value) {
28+
if (SECONDS_PATTERN.matcher(value).matches()) {
29+
return Duration.ofSeconds(Long.parseLong(value));
30+
} else if (DATE_PATTERN.matcher(value).matches()) {
31+
return Duration.ofMillis(toMilliseconds(value) - systemTime.getCurrentTimeMillis());
32+
}
33+
throw new IllegalArgumentException("Invalid Retry-After value: " + value);
34+
}
35+
36+
@SuppressWarnings({"JavaUtilDate", "ThrowSpecificExceptions"})
37+
private static long toMilliseconds(String value) {
38+
try {
39+
return dateFormat.parse(value).getTime();
40+
} catch (ParseException e) {
41+
throw new RuntimeException(e);
42+
}
43+
}
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.opentelemetry.opamp.client.internal.tools;
2+
3+
/** Utility to be able to mock the current system time for testing purposes. */
4+
public final class SystemTime {
5+
private static final SystemTime INSTANCE = new SystemTime();
6+
7+
public static SystemTime getInstance() {
8+
return INSTANCE;
9+
}
10+
11+
private SystemTime() {}
12+
13+
public long getCurrentTimeMillis() {
14+
return System.currentTimeMillis();
15+
}
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.opentelemetry.opamp.client.internal.connectivity.http;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
6+
7+
import io.opentelemetry.opamp.client.internal.tools.SystemTime;
8+
import java.time.Duration;
9+
import org.junit.jupiter.api.Test;
10+
11+
class RetryAfterParserTest {
12+
13+
@Test
14+
void verifyParsing() {
15+
SystemTime systemTime = mock();
16+
long currentTimeMillis = 1577836800000L;
17+
when(systemTime.getCurrentTimeMillis()).thenReturn(currentTimeMillis);
18+
19+
RetryAfterParser parser = new RetryAfterParser(systemTime);
20+
21+
assertThat(parser.parse("123")).isEqualTo(Duration.ofSeconds(123));
22+
assertThat(parser.parse("Wed, 01 Jan 2020 01:00:00 GMT")).isEqualTo(Duration.ofHours(1));
23+
}
24+
}

0 commit comments

Comments
 (0)