/
CreateUpdateStoryOnSandbox.java
107 lines (90 loc) · 4.46 KB
/
CreateUpdateStoryOnSandbox.java
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
106
107
/*
ApiKey is preferred method of authentication on rally1.rallydev.com
but is not supported on sandbox.rallydev.com
Use basic authentication (username/password) on sandbox
NOTE: DO NOT APPEND SECURITY TOKEN TO POST REQUESTS
There is no need to hit security/authorize endpoint and append a token to POST requests
when using Rally API Toolkit for Java.
The toolkit does it under the hood.
*/
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Ref;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import com.rallydev.rest.client.HttpClient;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.conn.scheme.Scheme;
public class CreateUpdateStoryOnSandbox {
public static void main(String[] args) throws URISyntaxException, IOException {
String host = "https://sandbox.rallydev.com";
String username = "user@co.com";
String password = "secret";
String projectRef = "/project/12345";
String applicationName = "NickM:CreateUpdateStoryOnSandbox";
RallyRestApi restApi = new RallyRestApi(new URI(host),username, password);
//OPTIONAL: if you have proxy:
restApi.setProxy(new URI("http://myserver.company.com:1234"), "proxyusername", "proxypassword");
restApi.setApplicationName(applicationName);
/*
OPTIONAL:
This example also shows how to bypass
Exception in thread "main" javax.net.ssl. SSLPeerUnverifiedException: peer not authenticated
See https://rallycommunity.rallydev.com/answers?id=kA014000000PKJY for details
*/
HttpClient client = restApi.getClient();
try {
SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy() {
public boolean isTrusted(X509Certificate[] certificate, String authType)
throws CertificateException {
//trust all certs
return true;
}
}, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, sf));
//end of the block to bypass javax.net.ssl. SSLPeerUnverifiedException
System.out.println("Creating a story...");
JsonObject newStory = new JsonObject();
newStory.addProperty("Name", "some story 999");
newStory.addProperty("Project", projectRef);
CreateRequest createRequest = new CreateRequest("hierarchicalrequirement", newStory);
CreateResponse createResponse = restApi.create(createRequest);
if (createResponse.wasSuccessful()) {
System.out.println(String.format("Created %s", createResponse.getObject().get("_ref").getAsString()));
//Read story
String storyRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
System.out.println(String.format("\nReading Story %s...", storyRef));
//Update story
JsonObject storyUpdate = new JsonObject();
storyUpdate.addProperty("Description", "this is description of story");
storyUpdate.addProperty("Iteration", "/iteration/34715726689");
UpdateRequest updateStoryRequest = new UpdateRequest(storyRef,storyUpdate);
UpdateResponse updateResponse = restApi.update(updateStoryRequest);
if (updateResponse.wasSuccessful()) {
System.out.println("Successfully updated story: " + newStory.get("Name") +
"Description: " + storyUpdate.get("Description"));
}
} else {
String[] createErrors;
createErrors = createResponse.getErrors();
System.out.println("Error");
for (int i=0; i<createErrors.length;i++) {
System.out.println(createErrors[i]);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
//Release all resources
restApi.close();
}
}
}