88
99import java .io .IOException ;
1010import java .net .URI ;
11- import java .util .Arrays ;
1211import java .util .List ;
1312import java .util .Map ;
1413
2726import com .google .common .collect .Maps ;
2827import com .offbytwo .jenkins .client .JenkinsHttpClient ;
2928import com .offbytwo .jenkins .client .util .EncodingUtils ;
29+ import com .offbytwo .jenkins .client .util .UrlUtils ;
3030import com .offbytwo .jenkins .helper .JenkinsVersion ;
3131import com .offbytwo .jenkins .model .Build ;
3232import com .offbytwo .jenkins .model .Computer ;
@@ -155,7 +155,7 @@ public Map<String, Job> getJobs(String view) throws IOException {
155155 * @throws IOException in case of an error.
156156 */
157157 public Map <String , Job > getJobs (FolderJob folder , String view ) throws IOException {
158- String path = toBaseUrl (folder );
158+ String path = UrlUtils . toBaseUrl (folder );
159159 Class <? extends MainView > viewClass = MainView .class ;
160160 if (view != null ) {
161161 path = path + "view/" + EncodingUtils .encode (view ) + "/" ;
@@ -192,7 +192,7 @@ public Map<String, View> getViews() throws IOException {
192192 public Map <String , View > getViews (FolderJob folder ) throws IOException {
193193 // This is much better than using &depth=2
194194 // http://localhost:8080/api/json?pretty&tree=views[name,url,jobs[name,url]]
195- List <View > views = client .get (toBaseUrl (folder ) + "?tree=views[name,url,jobs[name,url]]" , MainView .class ).getViews ();
195+ List <View > views = client .get (UrlUtils . toBaseUrl (folder ) + "?tree=views[name,url,jobs[name,url]]" , MainView .class ).getViews ();
196196 return Maps .uniqueIndex (views , new Function <View , String >() {
197197 @ Override
198198 public String apply (View view ) {
@@ -232,7 +232,7 @@ public View getView(String name) throws IOException {
232232 */
233233 public View getView (FolderJob folder , String name ) throws IOException {
234234 try {
235- View resultView = client .get (toViewBaseUrl (folder , name ) + "/" , View .class );
235+ View resultView = client .get (UrlUtils . toViewBaseUrl (folder , name ) + "/" , View .class );
236236 resultView .setClient (client );
237237
238238 // TODO: Think about the following? Does there exists a simpler/more
@@ -262,7 +262,7 @@ public View getView(FolderJob folder, String name) throws IOException {
262262 * @throws IOException in case of an error.
263263 */
264264 public JobWithDetails getJob (String jobName ) throws IOException {
265- return getJob (null , parseFullName (jobName ));
265+ return getJob (null , UrlUtils . toFullJobPath (jobName ));
266266 }
267267
268268 /**
@@ -275,7 +275,7 @@ public JobWithDetails getJob(String jobName) throws IOException {
275275 */
276276 public JobWithDetails getJob (FolderJob folder , String jobName ) throws IOException {
277277 try {
278- JobWithDetails job = client .get (toJobBaseUrl (folder , jobName ), JobWithDetails .class );
278+ JobWithDetails job = client .get (UrlUtils . toJobBaseUrl (folder , jobName ), JobWithDetails .class );
279279 job .setClient (client );
280280
281281 return job ;
@@ -290,12 +290,12 @@ public JobWithDetails getJob(FolderJob folder, String jobName) throws IOExceptio
290290 }
291291
292292 public MavenJobWithDetails getMavenJob (String jobName ) throws IOException {
293- return getMavenJob (null , parseFullName (jobName ));
293+ return getMavenJob (null , UrlUtils . toFullJobPath (jobName ));
294294 }
295295
296296 public MavenJobWithDetails getMavenJob (FolderJob folder , String jobName ) throws IOException {
297297 try {
298- MavenJobWithDetails job = client .get (toJobBaseUrl (folder , jobName ), MavenJobWithDetails .class );
298+ MavenJobWithDetails job = client .get (UrlUtils . toJobBaseUrl (folder , jobName ), MavenJobWithDetails .class );
299299 job .setClient (client );
300300
301301 return job ;
@@ -381,7 +381,7 @@ public void createJob(FolderJob folder, String jobName, String jobXml) throws IO
381381 * @throws IOException in case of an error.
382382 */
383383 public void createJob (FolderJob folder , String jobName , String jobXml , Boolean crumbFlag ) throws IOException {
384- client .post_xml (toBaseUrl (folder ) + "createItem?name=" + EncodingUtils .encodeParam (jobName ), jobXml , crumbFlag );
384+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "createItem?name=" + EncodingUtils .encodeParam (jobName ), jobXml , crumbFlag );
385385 }
386386
387387 /**
@@ -433,7 +433,7 @@ public void createView(FolderJob folder, String viewName, String viewXml) throws
433433 * @throws IOException in case of an error.
434434 */
435435 public void createView (FolderJob folder , String viewName , String viewXml , Boolean crumbFlag ) throws IOException {
436- client .post_xml (toBaseUrl (folder ) + "createView?name=" + EncodingUtils .encodeParam (viewName ), viewXml ,
436+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "createView?name=" + EncodingUtils .encodeParam (viewName ), viewXml ,
437437 crumbFlag );
438438 }
439439
@@ -484,7 +484,7 @@ public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) th
484484 // here
485485 ImmutableMap <String , String > params = ImmutableMap .of ("mode" , "com.cloudbees.hudson.plugins.folder.Folder" ,
486486 "name" , EncodingUtils .encodeParam (jobName ), "from" , "" , "Submit" , "OK" );
487- client .post_form (toBaseUrl (folder ) + "createItem?" , params , crumbFlag );
487+ client .post_form (UrlUtils . toBaseUrl (folder ) + "createItem?" , params , crumbFlag );
488488 }
489489
490490 /**
@@ -507,7 +507,7 @@ public String getJobXml(String jobName) throws IOException {
507507 * @throws IOException in case of an error.
508508 */
509509 public String getJobXml (FolderJob folder , String jobName ) throws IOException {
510- return client .get (toJobBaseUrl (folder , jobName ) + "/config.xml" );
510+ return client .get (UrlUtils . toJobBaseUrl (folder , jobName ) + "/config.xml" );
511511 }
512512
513513 /**
@@ -577,11 +577,11 @@ public void updateView(String viewName, String viewXml, boolean crumbFlag) throw
577577 }
578578
579579 public void updateView (FolderJob folder , String viewName , String viewXml ) throws IOException {
580- client .post_xml (toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , true );
580+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , true );
581581 }
582582
583583 public void updateView (FolderJob folder , String viewName , String viewXml , boolean crumbFlag ) throws IOException {
584- client .post_xml (toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , crumbFlag );
584+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , crumbFlag );
585585 }
586586
587587 /**
@@ -617,7 +617,7 @@ public void updateJob(String jobName, String jobXml, boolean crumbFlag) throws I
617617 * @throws IOException in case of an error.
618618 */
619619 public void updateJob (FolderJob folder , String jobName , String jobXml , boolean crumbFlag ) throws IOException {
620- client .post_xml (toJobBaseUrl (folder , jobName ) + "/config.xml" , jobXml , crumbFlag );
620+ client .post_xml (UrlUtils . toJobBaseUrl (folder , jobName ) + "/config.xml" , jobXml , crumbFlag );
621621 }
622622
623623 /**
@@ -685,7 +685,7 @@ public void deleteJob(FolderJob folder, String jobName) throws IOException {
685685 * @throws IOException in case of problems.
686686 */
687687 public void deleteJob (FolderJob folder , String jobName , boolean crumbFlag ) throws IOException {
688- client .post (toJobBaseUrl (folder , jobName ) + "/doDelete" , crumbFlag );
688+ client .post (UrlUtils . toJobBaseUrl (folder , jobName ) + "/doDelete" , crumbFlag );
689689 }
690690
691691 /**
@@ -878,80 +878,10 @@ public void renameJob(FolderJob folder, String oldJobName, String newJobName) th
878878 */
879879 public void renameJob (FolderJob folder , String oldJobName , String newJobName , Boolean crumbFlag )
880880 throws IOException {
881- client .post (toJobBaseUrl (folder , oldJobName ) + "/doRename?newName=" + EncodingUtils .encodeParam (newJobName ),
882- crumbFlag );
883- }
884-
885- /**
886- * Helper to create a base url in case a folder is given
887- *
888- * @param folder the folder or {@code null}
889- * @return The created base url.
890- */
891- private String toBaseUrl (FolderJob folder ) {
892- String path = "/" ;
893- if (folder != null ) {
894- path = folder .getUrl ();
895- }
896- return path ;
897- }
898-
899- /**
900- * Helper to create the base url for a job, with or without a given folder
901- *
902- * @param folder the folder or {@code null}
903- * @param jobName the name of the job.
904- * @return converted base url.
905- */
906- private String toJobBaseUrl (FolderJob folder , String jobName ) {
907- String jobBaseUrl = toBaseUrl (folder ) + "job/" ;
908-
909- String [] jobNameParts = jobName .split ("/" );
910- for (int i = 0 ; i < jobNameParts .length ; i ++) {
911- jobBaseUrl += EncodingUtils .encode (jobNameParts [i ]);
912-
913- if (i != jobNameParts .length - 1 ) {
914- jobBaseUrl += "/" ;
915- }
916- }
917-
918- return jobBaseUrl ;
881+ client .post (UrlUtils .toJobBaseUrl (folder , oldJobName )
882+ + "/doRename?newName=" + EncodingUtils .encodeParam (newJobName ),
883+ crumbFlag );
919884 }
920885
921- /**
922- * Helper to create the base url for a view, with or without a given folder
923- *
924- * @param folder the folder or {@code null}
925- * @param name the of the view.
926- * @return converted view url.
927- */
928- private String toViewBaseUrl (FolderJob folder , String name ) {
929- return toBaseUrl (folder ) + "view/" + EncodingUtils .encode (name );
930- }
931886
932- /**
933- * Parses the provided job name for folders to get the full path for the job.
934- * @param jobName the fullName of the job.
935- * @return the path of the job including folders if present.
936- */
937- private String parseFullName (String jobName )
938- {
939- if (!jobName .contains ("/" )) {
940- return jobName ;
941- }
942-
943- List <String > foldersAndJob = Arrays .asList (jobName .split ("/" ));
944-
945- String foldersAndJobName = "" ;
946-
947- for (int i = 0 ; i < foldersAndJob .size (); i ++) {
948- foldersAndJobName += foldersAndJob .get (i );
949-
950- if (i != foldersAndJob .size () -1 ) {
951- foldersAndJobName += "/job/" ;
952- }
953- }
954-
955- return foldersAndJobName ;
956- }
957887}
0 commit comments