6
6
7
7
package com .offbytwo .jenkins ;
8
8
9
- import java .io .IOException ;
10
- import java .net .URI ;
11
- import java .util .Arrays ;
12
- import java .util .List ;
13
- import java .util .Map ;
14
-
15
- import javax .xml .bind .JAXBException ;
16
-
17
- import org .apache .http .HttpStatus ;
18
- import org .apache .http .client .HttpResponseException ;
19
- import org .apache .http .entity .ContentType ;
20
- import org .dom4j .DocumentException ;
21
- import org .slf4j .Logger ;
22
- import org .slf4j .LoggerFactory ;
23
-
24
9
import com .google .common .base .Function ;
25
10
import com .google .common .base .Optional ;
26
11
import com .google .common .collect .ImmutableMap ;
27
12
import com .google .common .collect .Maps ;
28
13
import com .offbytwo .jenkins .client .JenkinsHttpClient ;
29
14
import com .offbytwo .jenkins .client .util .EncodingUtils ;
30
15
import com .offbytwo .jenkins .helper .JenkinsVersion ;
31
- import com .offbytwo .jenkins .model .Build ;
32
- import com .offbytwo .jenkins .model .Computer ;
33
- import com .offbytwo .jenkins .model .ComputerSet ;
34
- import com .offbytwo .jenkins .model .FolderJob ;
35
- import com .offbytwo .jenkins .model .Job ;
36
- import com .offbytwo .jenkins .model .JobConfiguration ;
37
- import com .offbytwo .jenkins .model .JobWithDetails ;
38
- import com .offbytwo .jenkins .model .LabelWithDetails ;
39
- import com .offbytwo .jenkins .model .MainView ;
40
- import com .offbytwo .jenkins .model .MavenJobWithDetails ;
41
- import com .offbytwo .jenkins .model .PluginManager ;
42
- import com .offbytwo .jenkins .model .Queue ;
43
- import com .offbytwo .jenkins .model .QueueItem ;
44
- import com .offbytwo .jenkins .model .QueueReference ;
45
- import com .offbytwo .jenkins .model .View ;
16
+ import com .offbytwo .jenkins .model .*;
17
+ import com .offbytwo .jenkins .model .credentials .Credential ;
18
+ import com .offbytwo .jenkins .model .credentials .CredentialManager ;
19
+ import org .apache .commons .collections .CollectionUtils ;
20
+ import org .apache .commons .collections .Predicate ;
21
+ import org .apache .http .HttpStatus ;
22
+ import org .apache .http .client .HttpResponseException ;
23
+ import org .apache .http .entity .ContentType ;
24
+ import org .dom4j .DocumentException ;
25
+ import org .slf4j .Logger ;
26
+ import org .slf4j .LoggerFactory ;
27
+
28
+ import javax .xml .bind .JAXBException ;
29
+ import java .io .IOException ;
30
+ import java .net .URI ;
31
+ import java .rmi .server .ExportException ;
32
+ import java .util .Arrays ;
33
+ import java .util .List ;
34
+ import java .util .Map ;
46
35
47
36
/**
48
37
* The main starting point for interacting with a Jenkins server.
@@ -52,6 +41,8 @@ public class JenkinsServer {
52
41
53
42
private final JenkinsHttpClient client ;
54
43
44
+ private CredentialManager credentialManager ;
45
+
55
46
/**
56
47
* Create a new Jenkins server reference given only the server address
57
48
*
@@ -934,24 +925,104 @@ private String toViewBaseUrl(FolderJob folder, String name) {
934
925
* @param jobName the fullName of the job.
935
926
* @return the path of the job including folders if present.
936
927
*/
937
- private String parseFullName (String jobName )
938
- {
928
+ private String parseFullName (String jobName ) {
939
929
if (!jobName .contains ("/" )) {
940
930
return jobName ;
941
931
}
942
-
932
+
943
933
List <String > foldersAndJob = Arrays .asList (jobName .split ("/" ));
944
-
934
+
945
935
String foldersAndJobName = "" ;
946
-
936
+
947
937
for (int i = 0 ; i < foldersAndJob .size (); i ++) {
948
938
foldersAndJobName += foldersAndJob .get (i );
949
-
950
- if (i != foldersAndJob .size () -1 ) {
939
+
940
+ if (i != foldersAndJob .size () - 1 ) {
951
941
foldersAndJobName += "/job/" ;
952
942
}
953
943
}
954
-
944
+
955
945
return foldersAndJobName ;
946
+
947
+ }
948
+
949
+ /**
950
+ * List the credentials from the Jenkins server.
951
+ * @return a hash map of the credentials. The key is the id of each credential.
952
+ * @throws IOException
953
+ */
954
+ public Map <String , Credential > listCredentials () throws IOException {
955
+ return this .getCredentialManager ().listCredentials ();
956
+ }
957
+
958
+ /**
959
+ * Create the given credential
960
+ * @param credential a credential instance
961
+ * @param crumbFlag
962
+ * @throws IOException
963
+ */
964
+ public void createCredential (Credential credential , boolean crumbFlag ) throws IOException {
965
+ this .getCredentialManager ().createCredential (credential , crumbFlag );
966
+ }
967
+
968
+ /**
969
+ * Update an existing credential
970
+ * @param credentialId the id of the credential
971
+ * @param credential the updated credential instance
972
+ * @param crumbFlag
973
+ * @throws IOException
974
+ */
975
+ public void updateCredential (String credentialId , Credential credential , boolean crumbFlag ) throws IOException {
976
+ this .getCredentialManager ().updateCredential (credentialId , credential , crumbFlag );
977
+ }
978
+
979
+ /**
980
+ * Delete an existing credential
981
+ * @param credentialId the id of the credential to delete
982
+ * @param crumbFlag
983
+ * @throws IOException
984
+ */
985
+ public void deleteCredential (String credentialId , boolean crumbFlag ) throws IOException {
986
+ this .getCredentialManager ().deleteCredential (credentialId , crumbFlag );
987
+ }
988
+
989
+ /**
990
+ * Return the credentialManager instance. Will initialise it if it's never used before.
991
+ * @return the credentialManager instance
992
+ * @throws IOException
993
+ */
994
+ private CredentialManager getCredentialManager () throws IOException {
995
+ if (this .credentialManager == null ) {
996
+ Plugin credentialPlugin = findPluginWithName ("credentials" );
997
+ if (credentialPlugin == null ) {
998
+ throw new ExportException ("credential plugin is not installed" );
999
+ }
1000
+ String version = credentialPlugin .getVersion ();
1001
+ this .credentialManager = new CredentialManager (version , this .client );
1002
+ }
1003
+ return this .credentialManager ;
1004
+ }
1005
+
1006
+ /**
1007
+ * Find a plugin that matches the given short name
1008
+ * @param pluginShortName the short name of the plugin to find
1009
+ * @return the pluin object that is found. Can be null if no match found.
1010
+ * @throws IOException
1011
+ */
1012
+ public Plugin findPluginWithName (final String pluginShortName ) throws IOException {
1013
+ List <Plugin > plugins = this .getPluginManager ().getPlugins ();
1014
+ Object foundPlugin = CollectionUtils .find (plugins , new Predicate () {
1015
+ @ Override
1016
+ public boolean evaluate (Object o ) {
1017
+ Plugin p = (Plugin ) o ;
1018
+ if (p .getShortName ().equalsIgnoreCase (pluginShortName )) {
1019
+ return true ;
1020
+ } else {
1021
+ return false ;
1022
+ }
1023
+ }
1024
+ });
1025
+
1026
+ return foundPlugin == null ? null : (Plugin ) foundPlugin ;
956
1027
}
957
1028
}
0 commit comments