Skip to content

Commit e85b16d

Browse files
committed
feat: minor fix
1 parent 694a1aa commit e85b16d

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

library/src/main/java/com/mastercard/developer/oauth2/http/UserAgent.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ public final class UserAgent {
1212
private static final String PRODUCT = "Mastercard-OAuth2-Client";
1313
private static final String UNKNOWN_VERSION = "0.0.0-unknown";
1414
private static final String VERSION = readVersionFile();
15+
// Cache the user-agent string to avoid rebuilding it on every call
16+
private static final String CACHED_USER_AGENT = buildUserAgent();
1517

1618
private UserAgent() {}
1719

1820
/**
19-
* Builds a stable user-agent string:
21+
* Returns a stable, pre-built user-agent string:
2022
* Product/Version (Runtime; OS [OS Version])
2123
* Example:
2224
* Mastercard-OAuth2-Client/1.0.0 (Java/17.0.2; Linux 5.15)
2325
*/
2426
public static String get() {
27+
return CACHED_USER_AGENT;
28+
}
29+
30+
private static String buildUserAgent() {
2531
String javaVer = System.getProperty("java.version", "unknown");
2632
String osName = System.getProperty("os.name", "unknown");
2733
String osVer = System.getProperty("os.version", "").trim();
28-
var runtime = "Java/" + javaVer;
34+
String runtime = "Java/" + javaVer;
2935
String osPart = osName + (osVer.isEmpty() ? "" : " " + osVer);
30-
return String.format("%s/%s (%s; %s)", PRODUCT, VERSION, runtime, osPart);
36+
return PRODUCT + "/" + VERSION + " (" + runtime + "; " + osPart + ")";
3137
}
3238

3339
private static String readVersionFile() {

library/src/main/java/com/mastercard/developer/oauth2/internal/json/GsonJsonProvider.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mastercard.developer.oauth2.internal.json;
22

33
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
45
import com.google.gson.reflect.TypeToken;
56
import com.mastercard.developer.oauth2.internal.json.exception.OAuth2ClientJsonException;
67
import java.lang.reflect.Type;
@@ -9,13 +10,18 @@
910

1011
public class GsonJsonProvider implements JsonProvider {
1112

12-
private static final Gson gson = new Gson();
13+
// Use a configured Gson instance and cache the Map type to avoid allocating a new TypeToken on each parse.
14+
private static final Gson gson = new GsonBuilder()
15+
.disableHtmlEscaping()
16+
.serializeNulls()
17+
.create();
18+
19+
private static final Type MAP_TYPE = new TypeToken<Map<String, Object>>() {}.getType();
1320

1421
@Override
1522
public Map<String, Object> parse(String json) throws OAuth2ClientJsonException {
1623
try {
17-
Type type = new TypeToken<Map<String, Object>>() {}.getType();
18-
return gson.fromJson(json, type);
24+
return gson.fromJson(json, MAP_TYPE);
1925
} catch (Exception e) {
2026
throw new OAuth2ClientJsonException("Failed to read JSON", e);
2127
}

library/src/main/java/com/mastercard/developer/oauth2/internal/json/JacksonJsonProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ public class JacksonJsonProvider implements JsonProvider {
1010

1111
private static final ObjectMapper mapper = new ObjectMapper();
1212

13+
// Cache the TypeReference to avoid creating a new anonymous subclass on each parse call.
14+
private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, Object>>() {};
15+
1316
@Override
1417
public Map<String, Object> parse(String json) throws OAuth2ClientJsonException {
1518
try {
16-
var typeReference = new TypeReference<Map<String, Object>>() {};
17-
return mapper.readValue(json, typeReference);
19+
return mapper.readValue(json, MAP_TYPE_REFERENCE);
1820
} catch (Exception e) {
1921
throw new OAuth2ClientJsonException("Failed to read JSON", e);
2022
}

0 commit comments

Comments
 (0)