|
1 | 1 | package com.stripe.net; |
2 | 2 |
|
| 3 | +import com.google.gson.Gson; |
3 | 4 | import com.stripe.Stripe; |
4 | 5 | import com.stripe.exception.ApiConnectionException; |
5 | 6 | import com.stripe.exception.StripeException; |
| 7 | +import java.io.InputStream; |
6 | 8 | import java.net.ConnectException; |
7 | 9 | import java.net.SocketTimeoutException; |
8 | 10 | import java.time.Duration; |
9 | 11 | import java.util.HashMap; |
10 | 12 | import java.util.Map; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.Properties; |
11 | 15 | import java.util.concurrent.ThreadLocalRandom; |
12 | 16 |
|
13 | 17 | /** Base abstract class for HTTP clients used to send requests to Stripe's API. */ |
@@ -176,10 +180,31 @@ protected static String buildXStripeClientUserAgentString() { |
176 | 180 | if (Stripe.getAppInfo() != null) { |
177 | 181 | propertyMap.put("application", ApiResource.INTERNAL_GSON.toJson(Stripe.getAppInfo())); |
178 | 182 | } |
| 183 | + getGsonVersion().ifPresent(ver -> propertyMap.put("gson.version", ver)); |
179 | 184 |
|
180 | 185 | return ApiResource.INTERNAL_GSON.toJson(propertyMap); |
181 | 186 | } |
182 | 187 |
|
| 188 | + /** |
| 189 | + * Gets the Gson version being used at runtime. |
| 190 | + * |
| 191 | + * @return the Gson version string, or Optional.empty() if it cannot be determined |
| 192 | + */ |
| 193 | + private static Optional<String> getGsonVersion() { |
| 194 | + try (InputStream in = |
| 195 | + Gson.class.getResourceAsStream( |
| 196 | + "/META-INF/maven/com.google.code.gson/gson/pom.properties")) { |
| 197 | + if (in != null) { |
| 198 | + Properties props = new Properties(); |
| 199 | + props.load(in); |
| 200 | + return Optional.ofNullable(props.getProperty("version")); |
| 201 | + } |
| 202 | + } catch (Exception ignore) { |
| 203 | + // Silently ignore - we'll just not include the version |
| 204 | + } |
| 205 | + return Optional.empty(); |
| 206 | + } |
| 207 | + |
183 | 208 | private static String formatAppInfo(Map<String, String> info) { |
184 | 209 | String str = info.get("name"); |
185 | 210 |
|
|
0 commit comments