Skip to content

FIX JENKINS-48782 #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
*
Expand All @@ -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
*
Expand All @@ -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
*
Expand All @@ -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());
Expand Down Expand Up @@ -445,7 +502,7 @@ public void close() {
}
}


/**
* Add authentication to supplied builder.
* @param builder the builder to configure
Expand All @@ -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)) {
Expand All @@ -469,7 +526,7 @@ protected static HttpClientBuilder addAuthentication(final HttpClientBuilder bui
return builder;
}


/**
* Get the local context.
* @return context
Expand All @@ -478,7 +535,7 @@ protected HttpContext getLocalContext() {
return localContext;
}


/**
* Set the local context.
* @param localContext the context
Expand All @@ -487,10 +544,10 @@ protected void setLocalContext(final HttpContext localContext) {
this.localContext = localContext;
}





private <T extends BaseModel> T objectFromResponse(Class<T> cls, HttpResponse response) throws IOException {
InputStream content = response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(content);
Expand All @@ -501,7 +558,7 @@ private <T extends BaseModel> T objectFromResponse(Class<T> cls, HttpResponse re
return result;
}

private ObjectMapper getDefaultMapper() {
private static ObjectMapper getDefaultMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
return mapper;
Expand Down