Skip to content

Commit c3314d5

Browse files
committed
Work-around for User-Agent not being copied from JerseyClientConfiguration
1 parent 7bee5aa commit c3314d5

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/main/java/nl/knaw/dans/lib/util/ClientProxyBuilder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ public D build() {
7575
/*
7676
* Note that BeanUtils.setProperty will be silently ignored, probably because the setter returns the 'this' reference.
7777
*/
78-
7978
// End-slashes trip up the API client, so we remove them from the base path.
8079
// Set basePath
8180
Field basePathField = apiClient.getClass().getDeclaredField("basePath");
@@ -91,11 +90,19 @@ public D build() {
9190
// By default no Accept header is set, which sometimes causes the server to try to format errors as HTML and failing, making it hard to get at the underlying error message.
9291
Method addDefaultHeaderMethod = apiClient.getClass().getMethod("addDefaultHeader", String.class, String.class);
9392
addDefaultHeaderMethod.invoke(apiClient, "Accept", "application/json");
93+
94+
// Actually, the line .user(httpClient) above should have set the User-Agent header already but it doesn't seem to work, so we set it manually if present as a workaround.
95+
if (httpClient.getUserAgent().isPresent()) {
96+
Method setUserAgentMethod = apiClient.getClass().getMethod("setUserAgent", String.class);
97+
setUserAgentMethod.invoke(apiClient, httpClient.getUserAgent().get());
98+
}
99+
100+
return defaultApiCtor.apply(apiClient);
94101
}
95102
catch (Exception e) {
96103
log.error("Error setting properties on apiClient", e);
97104
throw new RuntimeException(e);
98105
}
99-
return defaultApiCtor.apply(apiClient);
106+
100107
}
101108
}

0 commit comments

Comments
 (0)