Skip to content

Commit f188bc8

Browse files
committed
Job build parameters are passed via request body
1 parent b526c33 commit f188bc8

File tree

1 file changed

+22
-18
lines changed
  • jenkins-client/src/main/java/com/offbytwo/jenkins/model

1 file changed

+22
-18
lines changed

jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java

+22-18
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66

77
package com.offbytwo.jenkins.model;
88

9-
import static org.apache.commons.lang.StringUtils.join;
10-
119
import java.io.ByteArrayOutputStream;
1210
import java.io.IOException;
1311
import java.io.InputStream;
1412
import java.net.URI;
13+
import java.util.ArrayList;
14+
import java.util.List;
1515
import java.util.Map;
1616

17+
import org.apache.http.HttpResponse;
18+
import org.apache.http.NameValuePair;
19+
import org.apache.http.message.BasicNameValuePair;
20+
1721
import com.google.common.base.Function;
1822
import com.google.common.collect.Collections2;
19-
import com.google.common.escape.Escaper;
20-
import com.google.common.net.UrlEscapers;
2123

2224
public class Job extends BaseModel {
2325

@@ -27,7 +29,7 @@ public class Job extends BaseModel {
2729

2830
public Job() {
2931
}
30-
32+
3133
public Job(String name, String url) {
3234
this();
3335
this.name = name;
@@ -49,7 +51,7 @@ public String getName() {
4951
public String getUrl() {
5052
return url;
5153
}
52-
54+
5355
public String getFullName() {
5456
return fullName;
5557
}
@@ -60,9 +62,9 @@ public JobWithDetails details() throws IOException {
6062

6163
/**
6264
* Get a file from workspace.
63-
*
65+
*
6466
* @param fileName The name of the file to download from workspace. You can
65-
* also access files which are in sub folders of the workspace.
67+
* also access files which are in sub folders of the workspace.
6668
* @return The string which contains the content of the file.
6769
* @throws IOException in case of an error.
6870
*/
@@ -79,7 +81,7 @@ public String getFileFromWorkspace(String fileName) throws IOException {
7981

8082
/**
8183
* Trigger a build without parameters
82-
*
84+
*
8385
* @return {@link QueueReference} for further analysis of the queued build.
8486
* @throws IOException in case of an error.
8587
*/
@@ -91,7 +93,7 @@ public QueueReference build() throws IOException {
9193

9294
/**
9395
* Trigger a build with crumbFlag.
94-
*
96+
*
9597
* @param crumbFlag true or false.
9698
* @return {@link QueueReference} for further analysis of the queued build.
9799
* @throws IOException in case of an error.
@@ -115,15 +117,18 @@ public QueueReference build(Map<String, String> params) throws IOException {
115117
/**
116118
* Trigger a parameterized build
117119
*
118-
* @param params the job parameters
120+
* @param params the job parameters
119121
* @param crumbFlag determines whether crumb flag is used
120122
* @return {@link QueueReference} for further analysis of the queued build.
121123
* @throws IOException in case of an error.
122124
*/
123125
public QueueReference build(Map<String, String> params, boolean crumbFlag) throws IOException {
124-
String qs = join(Collections2.transform(params.entrySet(), new MapEntryToQueryStringPair()), "&");
125-
ExtractHeader location = client.post(url + "buildWithParameters?" + qs, null, ExtractHeader.class, crumbFlag);
126-
return new QueueReference(location.getLocation());
126+
List<NameValuePair> paramsList = new ArrayList<>(Collections2.transform(params.entrySet(),
127+
new MapEntryToNameValuePair()));
128+
HttpResponse response = this.client
129+
.post_form_with_result(this.url + "buildWithParameters", paramsList, crumbFlag);
130+
String location = response.getFirstHeader("Location").getValue();
131+
return new QueueReference(location);
127132
}
128133

129134
@Override
@@ -152,11 +157,10 @@ public int hashCode() {
152157
return result;
153158
}
154159

155-
private static class MapEntryToQueryStringPair implements Function<Map.Entry<String, String>, String> {
160+
private static class MapEntryToNameValuePair implements Function<Map.Entry<String, String>, NameValuePair> {
156161
@Override
157-
public String apply(Map.Entry<String, String> entry) {
158-
Escaper escaper = UrlEscapers.urlFormParameterEscaper();
159-
return escaper.escape(entry.getKey()) + "=" + escaper.escape(entry.getValue());
162+
public NameValuePair apply(Map.Entry<String, String> entry) {
163+
return new BasicNameValuePair(entry.getKey(), entry.getValue());
160164
}
161165
}
162166
}

0 commit comments

Comments
 (0)