Professional Documents
Culture Documents
Read and write information to work items in Rational Team Concert using Java
Paulo Cavoto (pcavoto@br.ibm.com) IT specialist IBM 03 September 2013
This article is a quick guide to using Open Services for Lifecycle Collaboration (OSLC) for reading and writing information to work items in IBM Rational Team Concert using Java. The examples demonstrate how to connect, retrieve, and store information in Rational Team Concert in a practical way. IBM Rational Team Concert, which is based on the Jazz platform, provides a lean, collaborative lifecycle management solution. One important feature of Rational Team Concert is its ability to integrate with external systems. Every Jazz product natively has OSLC services for reading and writing information, providing an easy point of integration between Jazz and other tools. OSLC provides services using the REST API, so standard callouts make information available for every language and platform. This article demonstrates how you can authenticate, connect, retrieve, and store information in Rational Team Concert using Java.
Copyright IBM Corporation 2013 Access Rational Team Concert with OSLC: a quick start guide
Trademarks Page 1 of 8
developerWorks
ibm.com/developerWorks/
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkclientTrusted(X509Certificate[] certs, String authType) { // Leave blank to trust every client } public void checkServerTrusted(X509Certificate[] certs, String authType) { // Leave blank to trust every server } public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { // TODO Auto-generated method stub } } };
Finally, override the default SSLSocketFactory from the HttpsURLConnection service, which the Apache HTTP client uses in its connections. This step ensures that HttpsURLConnection will use the SSL context we just created:
HttpsURLConnection.setDefaultSSLSocketFactory(mySSLContext.getSocketFactory());
You have now accepted the self-signed certificate. This step was necessary to complete first to ensure that the certificate will be accepted throughout the duration of the session.
Authenticate credentials
Now you'll create all the Java objects necessary to establish the connection with the server. The connection itself is pretty much the same process you would create to make any Get process. Use the Apache library to create the connection and provide the needed parameters. Create the following objects:
DefaultHttpClient httpclient = new DefaultHttpClient(); CookieStore cookieStore = new BasicCookieStore(); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
Instantiate an object HttpGet with the URL of the server you want to connect, including the door if necessary, followed by /jazz /authenticated/identity:
HttpGet httpGetID = new HttpGet("https://localhost:7443/ccm/authenticated/identity");
Page 2 of 8
ibm.com/developerWorks/
developerWorks
Now you're storing the server URL, creating the session, and recording it locally in the form of cookies. Call the execute method of the HttpClient object with the objects you created earlier. You're just saving your cookies, so the connection should be closed:
httpclient.execute(httpGetID, localContext); httpGetID.abort();
If you want to make sure that you created your cookies correctly, try to retrieve the saved information:
List<Cookie> cookies1 = cookieStore.getCookies(); for (Cookie cookie : cookies1) { System.out.println("\t" + cookie.getName() + " : " + cookie.getValue()); }
The next step is to authenticate with the j_security_check, using a valid username and password credentials. For this step, we use the Apache HttpClient class NameValuePair:
List<NameValuePair> authFormParams = new ArrayList<NameValuePair>(); authFormParams.add(new BasicNameValuePair("j_username", "TestJazzAdmin1")); authFormParams.add(new BasicNameValuePair("j_password", "TestJazzAdmin1"));
Now, build an UrlEcodedFormEntity which encodes the URL and credentials and performs an HTTP Post. See Listing 2: Listing 2. Build an UrlEncodedFormEntity
UrlEncodedFormEntity encodedentity = new UrlEncodedFormEntity(authFormParams, "UTF-8"); HttpPost httpPostAuth = new httpPost("https://localhost:7443/ccm/authenticated/j_security_check"); httpPostAuth.setEntity(encodedentity); httpclient.execute(httpPostAuth, localContext);
It's a good idea to check now that the cookies were created correctly to avoid any problems that you might have later in connecting. This code is only informative, but if you want to make sure that cookies are stored correctly, try to retrieve the information saved:
List<Cookie> cookies2 = cookieStore.getCookies(); for (Cookie cookie : cookies2) { System.out.println("\t" + cookie.getName() + " : " + cookie.getValue()); }
If the cookie is stored correctly, your login has been validated and you have access to Rational Team Concert.
Retrieve information from a Rational Team Concert work item using its ID
Now that you have set up the connection, you can retrieve the information about a specific work item by its ID. Make a Get request to this URL:
Access Rational Team Concert with OSLC: a quick start guide Page 3 of 8
developerWorks
ibm.com/developerWorks/
https://localhost:9443/ccm/oslc/workitems/ID_WI.TIPO?oslc_cm.properties=property1, property2
and substitute the numeric ID of the work item for WI_ID, the expected return type for TYPE (the possible values are JSON or XML), and if you want to return only some properties the property IDs for property1, property2. If you want to retrieve all the information, remove the oslc_cm.properties parameter. For example, see Listing 3: Listing 3. Retrieve information about a work item
// READ BY ID httpclient = new DefaultHttpClient(); httpclient.setCookieStore(cookieStore); HttpGet httpget = new HttpGet( "https://localhost:7443/ccm/resource/itemName/com.ibm.team.workitem.WorkItem/42"); httpget.addHeader("Accept", "application/json"); httpget.addHeader("OSLC-Core-Version", "2.0"); HttpResponse response = httpclient.execute(httpget); Header[] ooo = response.getAllHeaders(); for (Header header : ooo) { System.out.println(header.getName() + " - " + header.getValue()); } HttpEntity entity = response.getEntity(); if (entity != null) { entity.consumeContent(); } // Create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpget, responseHandler); System.out.println(responseBody);
In this case, a JSON representation of the contents of work item 42 is printed to the console.
Add this header to indicate that you are requesting a change in the work item:
httpput.setHeader("Content-Type", "application/x-oslc-cm-change-request+json");
Create a HttpEntity and inform the property that you want to change to the new value. Set the entity on the Put object:
HttpEntity myEntity = new StringEntity( "{\"dc:title\":{\"rdf:resource\":\"Title has changed\"}}"); httpput.setEntity(myEntity);
Make the request normally and check the console. See Listing 4:
Access Rational Team Concert with OSLC: a quick start guide Page 4 of 8
ibm.com/developerWorks/
developerWorks
Listing 4. Use the new objects and send the Put command
HttpResponse responsePut = httpclient.execute(httpput); HttpEntity entityPut = responsePut.getEntity(); BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(entityPut.getContent())); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); } reader.close(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } reader.close ();
Check the title of the work item you are working with. The title should have been changed.
Conclusion
The Rational Team Concert integration features, such as OSLC, help you implement systems, facilitate the exchange of information, and enhance the use of external tools. Most integration requirements can be met with OSLC services, thus avoiding complex customizations and reducing deployment time. This quick guide demonstrates how to use OSLC for reading and writing information to work items in Rational Team Concert using Java.
Page 5 of 8
developerWorks
ibm.com/developerWorks/
Downloads
Description
Example project files
Name
OSLC_Example_EclipseProject.zip
Size
528KB
Page 6 of 8
ibm.com/developerWorks/
developerWorks
Resources
Learn More about Rational Team Concert: Find Rational Team Concert articles and links to many other resources on IBM developerWorks, and check the product overview page, features and benefits, system requirements, and the user information center. Check the Rational Team Concert page on Jazz.net. Watch the Using Rational Team Concert in a globally distributed team webcast or a demonstration of the Dashboards and reports, or listen to the podcast about IBM Rational Team Concert and Jazz. Explore the Rational software area on developerWorks for technical resources, best practices, and information about Rational collaborative and integrated solutions for software and systems delivery. Stay current with developerWorks technical events and webcasts focused on a variety of IBM products and IT industry topics. Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBM products and tools, as well as IT industry trends. Watch developerWorks on-demand demos, ranging from product installation and setup demos for beginners to advanced functionality for experienced developers. Improve your skills. Check the Rational training and certification catalog, which includes many types of courses on a wide range of topics. You can take some of them anywhere, anytime, and many of the Getting Started ones are free. Get products and technologies Download Rational Team Concert from Jazz.net and try it free on up to 10 developers for as long as you want (requires registration). If you'd prefer, you can try it in the sandbox instead, without installing it on your own system. Evaluate IBM software in the way that suits you best: Download it for a trial, try it online, use it in a cloud environment, or spend a few hours in the SOA Sandbox learning how to implement service-oriented architecture efficiently. Discuss Get connected with your peers and keep up on the latest information in the Rational community. Rate or review Rational software. It's quick and easy. Share your knowledge and help others who use Rational software by writing a developerWorks article. Find out what makes a good developerWorks article and how to proceed. Follow Rational software on Facebook, Twitter (@ibmrational), and YouTube, and add your comments and requests. Ask and answer questions and increase your expertise when you get involved in the Rational forums, cafs, and wikis.
Access Rational Team Concert with OSLC: a quick start guide Page 7 of 8
developerWorks
ibm.com/developerWorks/
Page 8 of 8