diff --git a/ReleaseNotes.md b/ReleaseNotes.md index e69a8019..61526991 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -74,6 +74,10 @@ * [JENKINS-56585][jissue-56585] Change request method of `QuietDown()` to POST + + * [JENKINS-48782][jissue-48782] + + Added constructors with custom `ObjectMapper` ## Release 0.3.8 diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java index 4eadc5c9..8cccebfd 100755 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/client/JenkinsHttpClient.java @@ -79,12 +79,23 @@ public class JenkinsHttpClient implements JenkinsHttpConnection { * @param client Configured CloseableHttpClient to be used */ public JenkinsHttpClient(URI uri, CloseableHttpClient client) { + this(uri, client, getDefaultMapper()); + } + + /** + * Create an unauthenticated Jenkins HTTP client + * + * @param uri Location of the jenkins server (ex. http://localhost:8080) + * @param client Configured CloseableHttpClient to be used + * @param objectMapper Jackson object mapper + */ + public JenkinsHttpClient(URI uri, CloseableHttpClient client, ObjectMapper objectMapper) { this.context = uri.getPath(); if (!context.endsWith("/")) { context += "/"; } this.uri = uri; - this.mapper = getDefaultMapper(); + this.mapper = objectMapper; this.client = client; this.httpResponseValidator = new HttpResponseValidator(); // this.contentExtractor = new HttpResponseContentExtractor(); @@ -102,6 +113,17 @@ public JenkinsHttpClient(URI uri, HttpClientBuilder builder) { this(uri, builder.build()); } + /** + * Create an unauthenticated Jenkins HTTP client + * + * @param uri Location of the jenkins server (ex. http://localhost:8080) + * @param builder Configured HttpClientBuilder to be used + * @param objectMapper Jackson object mapper + */ + public JenkinsHttpClient(URI uri, HttpClientBuilder builder, ObjectMapper objectMapper) { + this(uri, builder.build(), objectMapper); + } + /** * Create an unauthenticated Jenkins HTTP client * @@ -111,6 +133,16 @@ public JenkinsHttpClient(URI uri) { this(uri, HttpClientBuilder.create()); } + /** + * Create an unauthenticated Jenkins HTTP client + * + * @param uri Location of the jenkins server (ex. http://localhost:8080) + * @param objectMapper Jackson object mapper + */ + public JenkinsHttpClient(URI uri, ObjectMapper objectMapper) { + this(uri, HttpClientBuilder.create(), objectMapper); + } + /** * Create an authenticated Jenkins HTTP client * @@ -122,6 +154,18 @@ public JenkinsHttpClient(URI uri, String username, String password) { this(uri, HttpClientBuilder.create(), username, password); } + /** + * Create an authenticated Jenkins HTTP client + * + * @param uri Location of the jenkins server (ex. http://localhost:8080) + * @param username Username to use when connecting + * @param password Password or auth token to use when connecting + * @param objectMapper Jackson object mapper + */ + public JenkinsHttpClient(URI uri, String username, String password, ObjectMapper objectMapper) { + this(uri, HttpClientBuilder.create(), username, password, objectMapper); + } + /** * Create an authenticated Jenkins HTTP client * @@ -131,7 +175,20 @@ public JenkinsHttpClient(URI uri, String username, String password) { * @param password Password or auth token to use when connecting */ public JenkinsHttpClient(URI uri, HttpClientBuilder builder, String username, String password) { - this(uri, addAuthentication(builder, uri, username, password)); + this(uri, builder, username, password, getDefaultMapper()); + } + + /** + * Create an authenticated Jenkins HTTP client + * + * @param uri Location of the jenkins server (ex. http://localhost:8080) + * @param builder Configured HttpClientBuilder to be used + * @param username Username to use when connecting + * @param password Password or auth token to use when connecting + * @param objectMapper Jackson object mapper + */ + public JenkinsHttpClient(URI uri, HttpClientBuilder builder, String username, String password, ObjectMapper objectMapper) { + this(uri, addAuthentication(builder, uri, username, password), objectMapper); if (isNotBlank(username)) { localContext = new BasicHttpContext(); localContext.setAttribute("preemptive-auth", new BasicScheme()); @@ -445,7 +502,7 @@ public void close() { } } - + /** * Add authentication to supplied builder. * @param builder the builder to configure @@ -454,7 +511,7 @@ public void close() { * @param password the password * @return the passed in builder */ - protected static HttpClientBuilder addAuthentication(final HttpClientBuilder builder, + protected static HttpClientBuilder addAuthentication(final HttpClientBuilder builder, final URI uri, final String username, String password) { if (isNotBlank(username)) { @@ -469,7 +526,7 @@ protected static HttpClientBuilder addAuthentication(final HttpClientBuilder bui return builder; } - + /** * Get the local context. * @return context @@ -478,7 +535,7 @@ protected HttpContext getLocalContext() { return localContext; } - + /** * Set the local context. * @param localContext the context @@ -487,10 +544,10 @@ protected void setLocalContext(final HttpContext localContext) { this.localContext = localContext; } - - - - + + + + private T objectFromResponse(Class cls, HttpResponse response) throws IOException { InputStream content = response.getEntity().getContent(); byte[] bytes = IOUtils.toByteArray(content); @@ -501,7 +558,7 @@ private T objectFromResponse(Class cls, HttpResponse re return result; } - private ObjectMapper getDefaultMapper() { + private static ObjectMapper getDefaultMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES); return mapper;