8
© Copyright IBM Corporation 2013 Trademarks Access Rational Team Concert with OSLC: a quick start guide Page 1 of 8 Access Rational Team Concert with OSLC: a quick start guide Read and write information to work items in Rational Team Concert using Java Paulo Cavoto ([email protected]) 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. 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 {

Rational Team Concert Oslc PDF

Embed Size (px)

Citation preview

Page 1: Rational Team Concert Oslc PDF

© Copyright IBM Corporation 2013 TrademarksAccess Rational Team Concert with OSLC: a quick start guide Page 1 of 8

Access Rational Team Concert with OSLC: a quickstart guideRead and write information to work items in Rational Team Concertusing Java

Paulo Cavoto ([email protected])IT specialistIBM

03 September 2013

This article is a quick guide to using Open Services for Lifecycle Collaboration (OSLC) forreading 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 TeamConcert 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 Concertis its ability to integrate with external systems. Every Jazz product natively has OSLC servicesfor reading and writing information, providing an easy point of integration between Jazz andother tools. OSLC provides services using the REST API, so standard callouts make informationavailable 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 certificateBy default, Rational Team Concert uses a self-signed certificate when the server is configured touse secure sockets layer (SSL). You must accept the self-signed certificate before you connectto Rational Team Concert. To do this, first create a new TrustManager object, which accepts andvalidates the SSL certificate by checking the expiration date, certificate signature, and so on. (Seethe 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 theinterface X509TrustManager. Override the methods of the interface to ensure that the validationchecks are not made. See Listing 1:

Listing 1. Override the methods of the interface X509TrustManagerpublic static void main(String[] args) throws ClientProtocolException, IOException, KeyManagementException, NoSuchAlgorithmException {

Page 2: Rational Team Concert Oslc PDF

developerWorks® ibm.com/developerWorks/

Access Rational Team Concert with OSLC: a quick start guide Page 2 of 8

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 theApache HTTP client uses in its connections. This step ensures that HttpsURLConnection will usethe SSL context we just created:

HttpsURLConnection.setDefaultSSLSocketFactory(mySSLContext.getSocketFactory());

You have now accepted the self-signed certificate. This step was necessary to complete first toensure that the certificate will be accepted throughout the duration of the session.

Authenticate credentialsNow you'll create all the Java objects necessary to establish the connection with the server. Theconnection itself is pretty much the same process you would create to make any Get process. Usethe 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 ifnecessary, followed by /jazz /authenticated/identity:

HttpGet httpGetID = new HttpGet("https://localhost:7443/ccm/authenticated/identity");

Page 3: Rational Team Concert Oslc PDF

ibm.com/developerWorks/ developerWorks®

Access Rational Team Concert with OSLC: a quick start guide Page 3 of 8

Now you're storing the server URL, creating the session, and recording it locally in the form ofcookies. 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 savedinformation:

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 passwordcredentials. 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 anHTTP Post. See Listing 2:

Listing 2. Build an UrlEncodedFormEntityUrlEncodedFormEntity 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 thatyou might have later in connecting. This code is only informative, but if you want to make sure thatcookies 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 RationalTeam Concert.

Retrieve information from a Rational Team Concert work item usingits IDNow that you have set up the connection, you can retrieve the information about a specific workitem by its ID. Make a Get request to this URL:

Page 4: Rational Team Concert Oslc PDF

developerWorks® ibm.com/developerWorks/

Access Rational Team Concert with OSLC: a quick start guide Page 4 of 8

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 (thepossible values are JSON or XML), and — if you want to return only some properties — theproperty IDs for property1, property2. If you want to retrieve all the information, remove theoslc_cm.properties parameter. For example, see Listing 3:

Listing 3. Retrieve information about a work item// READ BY IDhttpclient = 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 handlerResponseHandler<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 ConcertTo update a property of a work item, do a Put request to the same address you used in the lastexample:

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 theentity 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:

Page 5: Rational Team Concert Oslc PDF

ibm.com/developerWorks/ developerWorks®

Access Rational Team Concert with OSLC: a quick start guide Page 5 of 8

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 integrationrequirements can be met with OSLC services, thus avoiding complex customizations and reducingdeployment time. This quick guide demonstrates how to use OSLC for reading and writinginformation to work items in Rational Team Concert using Java.

Page 6: Rational Team Concert Oslc PDF

developerWorks® ibm.com/developerWorks/

Access Rational Team Concert with OSLC: a quick start guide Page 6 of 8

Downloads

Description Name SizeExample project files OSLC_Example_EclipseProject.zip 528KB

Page 7: Rational Team Concert Oslc PDF

ibm.com/developerWorks/ developerWorks®

Access Rational Team Concert with OSLC: a quick start guide Page 7 of 8

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, systemrequirements, 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 IBMRational Team Concert and Jazz.

• Explore the Rational software area on developerWorks for technical resources, bestpractices, and information about Rational collaborative and integrated solutions for softwareand systems delivery.

• Stay current with developerWorks technical events and webcasts focused on a variety of IBMproducts and IT industry topics.

• Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBM productsand tools, as well as IT industry trends.

• Watch developerWorks on-demand demos, ranging from product installation and setupdemos for beginners to advanced functionality for experienced developers.

• Improve your skills. Check the Rational training and certification catalog, which includesmany 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 aslong 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 toimplement service-oriented architecture efficiently.

Discuss

• Get connected with your peers and keep up on the latest information in the Rationalcommunity.

• 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 toproceed.

• Follow Rational software on Facebook, Twitter (@ibmrational), and YouTube, and add yourcomments and requests.

• Ask and answer questions and increase your expertise when you get involved in the Rationalforums, cafés, and wikis.

Page 8: Rational Team Concert Oslc PDF

developerWorks® ibm.com/developerWorks/

Access Rational Team Concert with OSLC: a quick start guide Page 8 of 8

About the author

Paulo Cavoto

Paulo Cavoto is an IT Specialist working with integrations on the IBM Rationalsoftware team in Brazil. He worked extensively on developing Web based applicationsusing J2EE and PHP, from the database layer using MySQL, DB2 and Oracle to theUI design using JQuery, Sencha and Dojo.

© Copyright IBM Corporation 2013(www.ibm.com/legal/copytrade.shtml)Trademarks(www.ibm.com/developerworks/ibm/trademarks/)