Skip to content

Commit 8c71ba4

Browse files
author
jianggang
authored
feat: add http user-agent (#10)
feat: add http user-agent
1 parent 11a7da0 commit 8c71ba4

File tree

8 files changed

+58
-8
lines changed

8 files changed

+58
-8
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.featureprobe</groupId>
88
<artifactId>server-sdk-java</artifactId>
9-
<version>1.2.0</version>
9+
<version>1.2.1</version>
1010
<name>server-sdk-java</name>
1111
<url>https://github.com/FeatureProbe/server-sdk-java</url>
1212
<description>FeatureProbe Server Side SDK for Java</description>

src/main/java/com/featureprobe/sdk/server/DefaultEventProcessor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public class DefaultEventProcessor implements EventProcessor {
2929

3030
private static final Logger logger = Loggers.EVENT;
3131

32-
private static final String GET_SDK_KEY_HEADER = "Authorization";
33-
3432
private static final int EVENT_BATCH_HANDLE_SIZE = 50;
3533

3634
private final AtomicBoolean closed = new AtomicBoolean(false);
@@ -159,15 +157,14 @@ private static final class SendEventsTask implements Runnable {
159157

160158
SendEventsTask(FPContext context, List<EventRepository> repositories) {
161159
this.apiUrl = context.getEventUrl();
162-
Headers.Builder headerBuilder = new Headers.Builder();
163160
OkHttpClient.Builder builder = new OkHttpClient.Builder()
164161
.connectionPool(context.getHttpConfiguration().connectionPool)
165162
.connectTimeout(context.getHttpConfiguration().connectTimeout)
166163
.readTimeout(context.getHttpConfiguration().readTimeout)
167164
.writeTimeout(context.getHttpConfiguration().writeTimeout)
168165
.retryOnConnectionFailure(false);
169166
httpClient = builder.build();
170-
headers = headerBuilder.add(GET_SDK_KEY_HEADER, context.getServerSdkKey()).build();
167+
headers = context.getHeaders();
171168
this.repositories = repositories;
172169
}
173170

src/main/java/com/featureprobe/sdk/server/FPContext.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.featureprobe.sdk.server;
22

3+
import okhttp3.Headers;
34
import org.slf4j.Logger;
45

6+
import java.io.IOException;
57
import java.net.MalformedURLException;
68
import java.net.URL;
79
import java.time.Duration;
@@ -11,6 +13,14 @@ final class FPContext {
1113

1214
private static final Logger logger = Loggers.MAIN;
1315

16+
private static final String GET_SDK_KEY_HEADER = "Authorization";
17+
18+
private static final String USER_AGENT_HEADER = "user-agent";
19+
20+
private static final String DEFAULT_SDK_VERSION = "unknown";
21+
22+
private static final String SDK_FLAG_PREFIX = "Java/";
23+
1424
private static final String GET_REPOSITORY_DATA_API = "/api/server-sdk/toggles";
1525

1626
private static final String POST_EVENTS_DATA_API = "/api/events";
@@ -27,6 +37,8 @@ final class FPContext {
2737

2838
private final HttpConfiguration httpConfiguration;
2939

40+
private final Headers headers;
41+
3042
FPContext(String serverSdkKey, FPConfig config) {
3143
try {
3244
if (Objects.isNull(config.synchronizerUrl)) {
@@ -46,6 +58,16 @@ final class FPContext {
4658
this.refreshInterval = config.refreshInterval;
4759
this.location = config.location;
4860
this.httpConfiguration = config.httpConfiguration;
61+
String sdkVersion = DEFAULT_SDK_VERSION;
62+
try {
63+
sdkVersion = Utils.readSdkVersion();
64+
} catch (IOException e) {
65+
logger.error("read sdk version error", e);
66+
}
67+
this.headers = config.httpConfiguration.headers.newBuilder()
68+
.add(GET_SDK_KEY_HEADER, serverSdkKey)
69+
.add(USER_AGENT_HEADER, SDK_FLAG_PREFIX + sdkVersion)
70+
.build();
4971
}
5072

5173
public URL getSynchronizerUrl() {
@@ -71,4 +93,8 @@ public String getLocation() {
7193
public HttpConfiguration getHttpConfiguration() {
7294
return httpConfiguration;
7395
}
96+
97+
public Headers getHeaders() {
98+
return headers;
99+
}
74100
}

src/main/java/com/featureprobe/sdk/server/HttpConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.featureprobe.sdk.server;
22

33
import okhttp3.ConnectionPool;
4+
import okhttp3.Headers;
45

56
import java.time.Duration;
67
import java.util.concurrent.TimeUnit;
@@ -26,11 +27,14 @@ public final class HttpConfiguration {
2627

2728
final Duration writeTimeout;
2829

30+
final Headers headers;
31+
2932
protected HttpConfiguration(Builder builder) {
3033
this.connectionPool = Builder.connectionPool;
3134
this.connectTimeout = Builder.connectTimeout;
3235
this.readTimeout = Builder.readTimeout;
3336
this.writeTimeout = Builder.writeTimeout;
37+
this.headers = Builder.headers;
3438
}
3539

3640
public static Builder builder() {
@@ -47,9 +51,16 @@ public static class Builder {
4751

4852
private static Duration writeTimeout = DEFAULT_WRITE_TIMEOUT;
4953

54+
private static Headers headers = new Headers.Builder().build();
55+
5056
public Builder() {
5157
}
5258

59+
public Builder headers(Headers headers) {
60+
Builder.headers = headers == null ? new Headers.Builder().build() : headers;
61+
return this;
62+
}
63+
5364
public Builder connectionPool(ConnectionPool connectionPool) {
5465
Builder.connectionPool = connectionPool == null ? DEFAULT_CONNECTION_POOL : connectionPool;
5566
return this;

src/main/java/com/featureprobe/sdk/server/PollingSynchronizer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ final class PollingSynchronizer implements Synchronizer {
4747
.readTimeout(context.getHttpConfiguration().readTimeout)
4848
.writeTimeout(context.getHttpConfiguration().writeTimeout)
4949
.retryOnConnectionFailure(false);
50-
Headers.Builder headerBuilder = new Headers.Builder();
51-
headers = headerBuilder.add(GET_SDK_KEY_HEADER, context.getServerSdkKey()).build();
50+
headers = context.getHeaders();
5251
httpClient = builder.build();
5352
}
5453

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.featureprobe.sdk.server;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Properties;
6+
7+
public class Utils {
8+
9+
public static String readSdkVersion() throws IOException {
10+
InputStream in = Utils.class.getClassLoader().getResourceAsStream("main.properties");
11+
Properties properties = new Properties();
12+
properties.load(in);
13+
return properties.getProperty("version");
14+
}
15+
16+
}

src/main/resources/main.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version='@project.version@'

src/test/groovy/com/featureprobe/sdk/server/FeatureProbeSpec.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FeatureProbeSpec extends Specification {
4141
try {
4242
featureProbe = new FeatureProbe("foo")
4343
} catch (Exception ignored) {
44-
fail("ctor should not fail with not empty sdk key")
44+
fail("should not fail with not empty sdk key")
4545
}
4646
}
4747

0 commit comments

Comments
 (0)