Access Rational Team Concert with OSLC: a quick start guide

Read and write information to work items in Rational Team Concert using Java

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.

Paulo Cavoto (pcavoto@br.ibm.com), IT specialist, IBM

author photoPaulo Cavoto is an IT Specialist working with integrations on the IBM Rational software team in Brazil. He worked extensively on developing Web based applications using J2EE and PHP, from the database layer using MySQL, DB2 and Oracle to the UI design using JQuery, Sencha and Dojo.



03 September 2013

Also available in Chinese

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.

Connect to Rational Team Concert with an SSL certificate

By default, Rational Team Concert uses a self-signed certificate when the server is configured to use secure sockets layer (SSL). You must accept the self-signed certificate before you connect to Rational Team Concert. To do this, first create a new TrustManager object, which accepts and validates the SSL certificate by checking the expiration date, certificate signature, and so on. (See the Download section to get an Eclipse project with the full source code for the examples.)

Next, create a new Array object containing an instance of TrustManager, which implements the interface X509TrustManager. Override the methods of the interface to ensure that the validation checks are not made. See Listing 1:

Listing 1. Override the methods of the interface X509TrustManager
public static void main(String[] args) throws ClientProtocolException, IOException,
       KeyManagementException, NoSuchAlgorithmException {
      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

         }
      } };

Next, create an instance of SSLContext:

SSLContext mySSLContext = SSLContext.getInstance("SSL");

Then, initialize the SSLContext with the TrustManager instance we created:

mySSLContext.init(null, trustAllCerts, new java.security.SecureRandom());

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");

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:

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.


Update a work item property in Rational Team Concert

To update a property of a work item, do a Put request to the same address you used in the last example:

httpclient = new DefaultHttpClient();
httpclient.setCookieStore(cookieStore);
HttpPut httpput = new HttpPut("https://localhost:7443/ccm/oslc/workitems/42.json");

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:

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.


Download

DescriptionNameSize
Example project filesOSLC_Example_EclipseProject.zip528KB

Resources

Learn

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

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into Rational software on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Rational
ArticleID=943006
ArticleTitle=Access Rational Team Concert with OSLC: a quick start guide
publish-date=09032013