66
77package com .offbytwo .jenkins .model ;
88
9- import static org .apache .commons .lang .StringUtils .join ;
10-
119import java .io .ByteArrayOutputStream ;
1210import java .io .IOException ;
1311import java .io .InputStream ;
1412import java .net .URI ;
13+ import java .util .ArrayList ;
14+ import java .util .List ;
1515import java .util .Map ;
1616
17+ import org .apache .http .HttpResponse ;
18+ import org .apache .http .NameValuePair ;
19+ import org .apache .http .message .BasicNameValuePair ;
20+ import org .apache .http .util .EntityUtils ;
21+
1722import com .google .common .base .Function ;
1823import com .google .common .collect .Collections2 ;
19- import com .google .common .escape .Escaper ;
20- import com .google .common .net .UrlEscapers ;
2124
2225public class Job extends BaseModel {
2326
@@ -27,7 +30,7 @@ public class Job extends BaseModel {
2730
2831 public Job () {
2932 }
30-
33+
3134 public Job (String name , String url ) {
3235 this ();
3336 this .name = name ;
@@ -49,7 +52,7 @@ public String getName() {
4952 public String getUrl () {
5053 return url ;
5154 }
52-
55+
5356 public String getFullName () {
5457 return fullName ;
5558 }
@@ -60,9 +63,9 @@ public JobWithDetails details() throws IOException {
6063
6164 /**
6265 * Get a file from workspace.
63- *
66+ *
6467 * @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.
68+ * also access files which are in sub folders of the workspace.
6669 * @return The string which contains the content of the file.
6770 * @throws IOException in case of an error.
6871 */
@@ -79,7 +82,7 @@ public String getFileFromWorkspace(String fileName) throws IOException {
7982
8083 /**
8184 * Trigger a build without parameters
82- *
85+ *
8386 * @return {@link QueueReference} for further analysis of the queued build.
8487 * @throws IOException in case of an error.
8588 */
@@ -91,7 +94,7 @@ public QueueReference build() throws IOException {
9194
9295 /**
9396 * Trigger a build with crumbFlag.
94- *
97+ *
9598 * @param crumbFlag true or false.
9699 * @return {@link QueueReference} for further analysis of the queued build.
97100 * @throws IOException in case of an error.
@@ -115,15 +118,19 @@ public QueueReference build(Map<String, String> params) throws IOException {
115118 /**
116119 * Trigger a parameterized build
117120 *
118- * @param params the job parameters
121+ * @param params the job parameters
119122 * @param crumbFlag determines whether crumb flag is used
120123 * @return {@link QueueReference} for further analysis of the queued build.
121124 * @throws IOException in case of an error.
122125 */
123126 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 ());
127+ List <NameValuePair > paramsList = new ArrayList <>(Collections2 .transform (params .entrySet (),
128+ new MapEntryToNameValuePair ()));
129+ HttpResponse response = this .client
130+ .post_form_with_result (this .url + "buildWithParameters" , paramsList , crumbFlag );
131+ String location = response .getFirstHeader ("Location" ).getValue ();
132+ EntityUtils .consume (response .getEntity ());
133+ return new QueueReference (location );
127134 }
128135
129136 @ Override
@@ -152,11 +159,10 @@ public int hashCode() {
152159 return result ;
153160 }
154161
155- private static class MapEntryToQueryStringPair implements Function <Map .Entry <String , String >, String > {
162+ private static class MapEntryToNameValuePair implements Function <Map .Entry <String , String >, NameValuePair > {
156163 @ 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 ());
164+ public NameValuePair apply (Map .Entry <String , String > entry ) {
165+ return new BasicNameValuePair (entry .getKey (), entry .getValue ());
160166 }
161167 }
162168}
0 commit comments