Skip to content

Commit 3a661e8

Browse files
authored
Merge pull request #253 from Countly/custom_headers
Custom Network Headers
2 parents 3771f93 + 5826588 commit 3a661e8

File tree

10 files changed

+48
-7
lines changed

10 files changed

+48
-7
lines changed

.idea/modules/sdk-java/countly-sdk-java.sdk-java.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/sdk-java/countly-sdk-java.sdk-java.main.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules/sdk-java/countly-sdk-java.sdk-java.test.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 24.1.2
2+
3+
* Added e new configuration function "addCustomNetworkRequestHeaders(Map<String, String>)" to add custom request headers to each request.
4+
15
## 24.1.1
26

37
* Added a new function "setID(newDeviceId)" for managing device id changes according to the device ID Type.

app-java/src/main/java/ly/count/java/demo/Example.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,14 @@ public static void main(String[] args) throws Exception {
210210
}
211211
}
212212

213+
Map<String, String> customNetworkRequestHeaders = new ConcurrentHashMap<>();
214+
customNetworkRequestHeaders.put("X-Countly-Example", "true");
215+
customNetworkRequestHeaders.put("X-Countly-Example-Version", "1.0");
216+
213217
Config config = new Config(COUNTLY_SERVER_URL, COUNTLY_APP_KEY, sdkStorageRootDirectory)
214218
.setLoggingLevel(Config.LoggingLevel.DEBUG)
215219
.setDeviceIdStrategy(Config.DeviceIdStrategy.UUID)
220+
.addCustomNetworkRequestHeaders(customNetworkRequestHeaders)
216221
.enableFeatures(Config.Feature.Events, Config.Feature.Sessions, Config.Feature.CrashReporting, Config.Feature.Views, Config.Feature.UserProfiles, Config.Feature.Location, Config.Feature.Feedback)
217222
.setRequiresConsent(true)
218223
//.enableParameterTamperingProtection("test-salt-checksum")

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ buildscript {
2020
}
2121

2222
allprojects {
23-
ext.CLY_VERSION = "24.1.1"
23+
ext.CLY_VERSION = "24.1.2"
2424
ext.POWERMOCK_VERSION = "1.7.4"
2525

2626
tasks.withType(Javadoc) {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# org.gradle.parallel=true
1919

2020
# RELEASE FIELD SECTION
21-
VERSION_NAME=24.1.1
21+
VERSION_NAME=24.1.2
2222
GROUP=ly.count.sdk
2323

2424
POM_URL=https://github.com/Countly/countly-sdk-java

sdk-java/src/main/java/ly/count/sdk/java/Config.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class Config {
6868
/**
6969
* Countly SDK version to be sent in HTTP requests
7070
*/
71-
protected String sdkVersion = "24.1.1";
71+
protected String sdkVersion = "24.1.2";
7272

7373
/**
7474
* Countly SDK version to be sent in HTTP requests
@@ -230,6 +230,12 @@ public class Config {
230230
*/
231231
protected boolean unhandledCrashReportingEnabled = true;
232232

233+
/**
234+
* Custom network request headers to be sent with each request.
235+
* If you want to add a header, use {@link #addCustomNetworkRequestHeaders(Map)}.
236+
*/
237+
protected Map<String, String> customNetworkRequestHeaders = null;
238+
233239
public ConfigViews views = new ConfigViews(this);
234240

235241
protected String location = null;
@@ -1330,6 +1336,20 @@ public Config disableLocation() {
13301336
return this;
13311337
}
13321338

1339+
/**
1340+
* Allows you to add custom header key/value pairs to each request
1341+
*
1342+
* @return Returns the same config object for convenient linking
1343+
*/
1344+
public Config addCustomNetworkRequestHeaders(Map<String, String> customHeaderValues) {
1345+
this.customNetworkRequestHeaders = customHeaderValues;
1346+
return this;
1347+
}
1348+
1349+
public Map<String, String> getCustomNetworkRequestHeaders() {
1350+
return customNetworkRequestHeaders;
1351+
}
1352+
13331353
/**
13341354
* Logging level for {@link Log} module
13351355
*/

sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ HttpURLConnection connection(final Request request) throws IOException {
149149
https.setSSLSocketFactory(sslContext.getSocketFactory());
150150
}
151151

152+
if (config.getCustomNetworkRequestHeaders() != null) {
153+
//if there are custom header values, add them
154+
L.v("[Transport] connection, Adding [" + config.getCustomNetworkRequestHeaders() + "] custom header fields");
155+
for (Map.Entry<String, String> entry : config.getCustomNetworkRequestHeaders().entrySet()) {
156+
String key = entry.getKey();
157+
String value = entry.getValue();
158+
if (key != null && value != null && !key.isEmpty()) {
159+
connection.addRequestProperty(key, value);
160+
}
161+
}
162+
}
163+
152164
if (!usingGET) {
153165
OutputStream output = null;
154166
PrintWriter writer = null;

sdk-java/src/test/java/ly/count/sdk/java/internal/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class TestUtils {
3939
static String SERVER_APP_KEY = "COUNTLY_APP_KEY";
4040
static String DEVICE_ID = "some_random_test_device_id";
4141
static String SDK_NAME = "java-native";
42-
static String SDK_VERSION = "24.1.1";
42+
static String SDK_VERSION = "24.1.2";
4343
static String APPLICATION_VERSION = "1.0";
4444

4545
public static final String[] eKeys = new String[] { "eventKey1", "eventKey2", "eventKey3", "eventKey4", "eventKey5", "eventKey6", "eventKey7" };

0 commit comments

Comments
 (0)