-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDeleteResource.groovy
More file actions
105 lines (86 loc) · 3.55 KB
/
DeleteResource.groovy
File metadata and controls
105 lines (86 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//This script is licensed under:
//Apache License
//Version 2.0, January 2004
//http://www.apache.org/licenses/
//Tested with IBM UrbanCode Deploy 6.2.7.2
//Command line usage:
//groovy -cp udclient.jar DeleteResource.groovy https://hostname:8443 username password resourcePath
//where resourcePath is the path of the resource to delete
//Example:
//groovy -cp udclient.jar DeleteResource.groovy https://localhost:8443 admin admin /TopResource/MyFolder1/MyFolder2
import org.apache.commons.lang.StringUtils
import org.apache.http.HttpHeaders
import org.apache.http.HttpRequest
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpDelete
import com.urbancode.commons.httpcomponentsutil.HttpClientBuilder
class Credentials{
String serverURL
String username
String password
}
class Parameters{
String resourcePath
}
//sets the values of the Credentials class which contains the server url and the username/password
void initializeParameters(Credentials credentials, Parameters parameters){
//if the user provided the input parameters, we're running on the command line
if (args.size()==4){
credentials.serverURL = args[0]
credentials.username = args[1]
credentials.password = args[2]
parameters.resourcePath = args[3]
}
else{
println "Command Line usage: DeleteResource ServerURL username password resourcePath"
System.exit(1)
}
}
//Initializes an HttpClient that accepts all certificates
//depends on import com.urbancode.commons.httpcomponentsutil.HttpClientBuilder
//included in HttpComponents-Util.jar, which is partly included in udclient.jar
HttpClient initializeClient(String username,String password){
HttpClientBuilder builder = new HttpClientBuilder()
builder.setPreemptiveAuthentication(true)
builder.setUsername(username)
builder.setPassword(password)
//Accept all certificates
builder.setTrustAllCerts(true)
//use proxy if defined
if (!StringUtils.isEmpty(System.getenv("PROXY_HOST")) &&
StringUtils.isNumeric(System.getenv("PROXY_PORT")))
{
builder.setProxyHost(System.getenv("PROXY_HOST"))
builder.setProxyPort(Integer.valueOf(System.getenv("PROXY_PORT")))
}
if (!StringUtils.isEmpty(System.getenv("PROXY_USERNAME")) &&
!StringUtils.isEmpty(System.getenv("PROXY_PASSWORD")))
{
builder.setProxyUsername(System.getenv("PROXY_USERNAME"))
builder.setProxyPassword(System.getenv("PROXY_PASSWORD"))
}
return builder.buildClient()
}
//Perform a given HttpRequest, assume answer is <= 299 , parse the outcome as JSON. Also release the connection for the Request.
void performDeleteRequest(HttpClient client, String requestURL){
HttpRequest deleteRequest = new HttpDelete(requestURL)
//Execute the REST Delete call
HttpResponse response = client.execute(deleteRequest)
//Check that the call was successful
int statusCode = response.getStatusLine().getStatusCode()
if ( statusCode > 299 ) {
println "ERROR : HttpDelete to: "+requestURL+ " returned: " +statusCode
println response.getStatusLine()
println response.entity.content.getText()
}
else {
println "SUCCESS : HttpDelete to: "+requestURL+ " returned: " +statusCode
}
}
//Main script contents
Credentials credentials = new Credentials()
Parameters parameters = new Parameters()
initializeParameters(credentials, parameters)
HttpClient client = initializeClient(credentials.username,credentials.password)
performDeleteRequest(client,credentials.getServerURL()+"/cli/resource/deleteResource?resource="+parameters.resourcePath)