From f188bc872ece72593efe2818d5f6dafadb3c668a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Jagielski?= Date: Tue, 16 Jan 2018 13:05:44 +0100 Subject: [PATCH] Job build parameters are passed via request body --- .../java/com/offbytwo/jenkins/model/Job.java | 40 ++++++++++--------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java index 7ef6a6cf..587e8524 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java @@ -6,18 +6,20 @@ package com.offbytwo.jenkins.model; -import static org.apache.commons.lang.StringUtils.join; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; + import com.google.common.base.Function; import com.google.common.collect.Collections2; -import com.google.common.escape.Escaper; -import com.google.common.net.UrlEscapers; public class Job extends BaseModel { @@ -27,7 +29,7 @@ public class Job extends BaseModel { public Job() { } - + public Job(String name, String url) { this(); this.name = name; @@ -49,7 +51,7 @@ public String getName() { public String getUrl() { return url; } - + public String getFullName() { return fullName; } @@ -60,9 +62,9 @@ public JobWithDetails details() throws IOException { /** * Get a file from workspace. - * + * * @param fileName The name of the file to download from workspace. You can - * also access files which are in sub folders of the workspace. + * also access files which are in sub folders of the workspace. * @return The string which contains the content of the file. * @throws IOException in case of an error. */ @@ -79,7 +81,7 @@ public String getFileFromWorkspace(String fileName) throws IOException { /** * Trigger a build without parameters - * + * * @return {@link QueueReference} for further analysis of the queued build. * @throws IOException in case of an error. */ @@ -91,7 +93,7 @@ public QueueReference build() throws IOException { /** * Trigger a build with crumbFlag. - * + * * @param crumbFlag true or false. * @return {@link QueueReference} for further analysis of the queued build. * @throws IOException in case of an error. @@ -115,15 +117,18 @@ public QueueReference build(Map params) throws IOException { /** * Trigger a parameterized build * - * @param params the job parameters + * @param params the job parameters * @param crumbFlag determines whether crumb flag is used * @return {@link QueueReference} for further analysis of the queued build. * @throws IOException in case of an error. */ public QueueReference build(Map params, boolean crumbFlag) throws IOException { - String qs = join(Collections2.transform(params.entrySet(), new MapEntryToQueryStringPair()), "&"); - ExtractHeader location = client.post(url + "buildWithParameters?" + qs, null, ExtractHeader.class, crumbFlag); - return new QueueReference(location.getLocation()); + List paramsList = new ArrayList<>(Collections2.transform(params.entrySet(), + new MapEntryToNameValuePair())); + HttpResponse response = this.client + .post_form_with_result(this.url + "buildWithParameters", paramsList, crumbFlag); + String location = response.getFirstHeader("Location").getValue(); + return new QueueReference(location); } @Override @@ -152,11 +157,10 @@ public int hashCode() { return result; } - private static class MapEntryToQueryStringPair implements Function, String> { + private static class MapEntryToNameValuePair implements Function, NameValuePair> { @Override - public String apply(Map.Entry entry) { - Escaper escaper = UrlEscapers.urlFormParameterEscaper(); - return escaper.escape(entry.getKey()) + "=" + escaper.escape(entry.getValue()); + public NameValuePair apply(Map.Entry entry) { + return new BasicNameValuePair(entry.getKey(), entry.getValue()); } } }