Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.featureprobe</groupId>
<artifactId>server-sdk-java</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<name>server-sdk-java</name>
<url>https://github.com/FeatureProbe/server-sdk-java</url>
<description>FeatureProbe Server Side SDK for Java</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public class DefaultEventProcessor implements EventProcessor {

private static final Logger logger = Loggers.EVENT;

private static final String GET_SDK_KEY_HEADER = "Authorization";

private static final int EVENT_BATCH_HANDLE_SIZE = 50;

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

SendEventsTask(FPContext context, List<EventRepository> repositories) {
this.apiUrl = context.getEventUrl();
Headers.Builder headerBuilder = new Headers.Builder();
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectionPool(context.getHttpConfiguration().connectionPool)
.connectTimeout(context.getHttpConfiguration().connectTimeout)
.readTimeout(context.getHttpConfiguration().readTimeout)
.writeTimeout(context.getHttpConfiguration().writeTimeout)
.retryOnConnectionFailure(false);
httpClient = builder.build();
headers = headerBuilder.add(GET_SDK_KEY_HEADER, context.getServerSdkKey()).build();
headers = context.getHeaders();
this.repositories = repositories;
}

Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/featureprobe/sdk/server/FPContext.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.featureprobe.sdk.server;

import okhttp3.Headers;
import org.slf4j.Logger;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
Expand All @@ -11,6 +13,14 @@ final class FPContext {

private static final Logger logger = Loggers.MAIN;

private static final String GET_SDK_KEY_HEADER = "Authorization";

private static final String USER_AGENT_HEADER = "user-agent";

private static final String DEFAULT_SDK_VERSION = "unknown";

private static final String SDK_FLAG_PREFIX = "Java/";

private static final String GET_REPOSITORY_DATA_API = "/api/server-sdk/toggles";

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

private final HttpConfiguration httpConfiguration;

private final Headers headers;

FPContext(String serverSdkKey, FPConfig config) {
try {
if (Objects.isNull(config.synchronizerUrl)) {
Expand All @@ -46,6 +58,16 @@ final class FPContext {
this.refreshInterval = config.refreshInterval;
this.location = config.location;
this.httpConfiguration = config.httpConfiguration;
String sdkVersion = DEFAULT_SDK_VERSION;
try {
sdkVersion = Utils.readSdkVersion();
} catch (IOException e) {
logger.error("read sdk version error", e);
}
this.headers = config.httpConfiguration.headers.newBuilder()
.add(GET_SDK_KEY_HEADER, serverSdkKey)
.add(USER_AGENT_HEADER, SDK_FLAG_PREFIX + sdkVersion)
.build();
}

public URL getSynchronizerUrl() {
Expand All @@ -71,4 +93,8 @@ public String getLocation() {
public HttpConfiguration getHttpConfiguration() {
return httpConfiguration;
}

public Headers getHeaders() {
return headers;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/featureprobe/sdk/server/HttpConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.featureprobe.sdk.server;

import okhttp3.ConnectionPool;
import okhttp3.Headers;

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

final Duration writeTimeout;

final Headers headers;

protected HttpConfiguration(Builder builder) {
this.connectionPool = Builder.connectionPool;
this.connectTimeout = Builder.connectTimeout;
this.readTimeout = Builder.readTimeout;
this.writeTimeout = Builder.writeTimeout;
this.headers = Builder.headers;
}

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

private static Duration writeTimeout = DEFAULT_WRITE_TIMEOUT;

private static Headers headers = new Headers.Builder().build();

public Builder() {
}

public Builder headers(Headers headers) {
Builder.headers = headers == null ? new Headers.Builder().build() : headers;
return this;
}

public Builder connectionPool(ConnectionPool connectionPool) {
Builder.connectionPool = connectionPool == null ? DEFAULT_CONNECTION_POOL : connectionPool;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ final class PollingSynchronizer implements Synchronizer {
.readTimeout(context.getHttpConfiguration().readTimeout)
.writeTimeout(context.getHttpConfiguration().writeTimeout)
.retryOnConnectionFailure(false);
Headers.Builder headerBuilder = new Headers.Builder();
headers = headerBuilder.add(GET_SDK_KEY_HEADER, context.getServerSdkKey()).build();
headers = context.getHeaders();
httpClient = builder.build();
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/featureprobe/sdk/server/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.featureprobe.sdk.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Utils {

public static String readSdkVersion() throws IOException {
InputStream in = Utils.class.getClassLoader().getResourceAsStream("main.properties");
Properties properties = new Properties();
properties.load(in);
return properties.getProperty("version");
}

}
1 change: 1 addition & 0 deletions src/main/resources/main.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version='@project.version@'
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class FeatureProbeSpec extends Specification {
try {
featureProbe = new FeatureProbe("foo")
} catch (Exception ignored) {
fail("ctor should not fail with not empty sdk key")
fail("should not fail with not empty sdk key")
}
}

Expand Down