It's all about the answers!

Ask a question

change the status of workitem using oslc


Rajat Singh (59833543) | asked Mar 06 '14, 7:32 a.m.
Hi Team

I am using RTC 4.0.1.
My requirement is to update the status of any workitem in RTC using either poster addon for firefox or from RTC java perspective.
I would appreciate if anyone could give me a step by step procedure (rather than pointing me to another post) or if they can modify the below sample code that I obtained from developerWorks to update the title of the workitem:

package com.ibm.olsc.tests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class Connect {

    /**
     * @param args
     * @throws IOException
     * @throws ClientProtocolException
     * @throws KeyManagementException
     * @throws NoSuchAlgorithmException
     */

    // 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

            }
        } };

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

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

        HttpsURLConnection.setDefaultSSLSocketFactory(mySSLContext.getSocketFactory());

        // Authenticate credentials
        DefaultHttpClient httpclient = new DefaultHttpClient();
        CookieStore cookieStore = new BasicCookieStore();
        HttpContext localContext = new BasicHttpContext();

        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

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

        httpclient.execute(httpGetID, localContext);
        httpGetID.abort();

        List<Cookie> cookies1 = cookieStore.getCookies();
        for (Cookie cookie : cookies1) {
            System.out.println("\t" + cookie.getName() + " : " + cookie.getValue());
        }
        //
        List<NameValuePair> authFormParams = new ArrayList<NameValuePair>();
        authFormParams.add(new BasicNameValuePair("j_username", "TestJazzAdmin1"));
        authFormParams.add(new BasicNameValuePair("j_password", "TestJazzAdmin1"));

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

        List<Cookie> cookies2 = cookieStore.getCookies();
        for (Cookie cookie : cookies2) {
            System.out.println("\t" + cookie.getName() + " : " + cookie.getValue());
        }

        // Listing 3. Retrieve information about a work item

        // READ BY ID
        httpclient = new DefaultHttpClient();
        // // IMPORTANT STUFF
        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);

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        // httpclient.getConnectionManager().shutdown();
        //

        httpclient = new DefaultHttpClient();
        httpclient.setCookieStore(cookieStore);
        HttpPut httpput = new HttpPut(
                "https://localhost:7443/ccm/oslc/workitems/42.json");
        httpput.setHeader("Content-Type", "application/x-oslc-cm-change-request+json");

        HttpEntity myEntity = new StringEntity(
                "{\"dc:title\":{\"rdf:resource\":\"Title has changed\"}}");
        httpput.setEntity(myEntity);
        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();
        }

        httpclient = new DefaultHttpClient();
        httpclient.setCookieStore(cookieStore);
        httpput = new HttpPut(
                "https://localhost:7443/ccm/oslc/workitems/42.json?_action=com.ibm.team.workitem.workflow.condicao.action.a4");
        httpput.setHeader("Content-Type", "application/x-oslc-cm-change-request+json");

        myEntity = new StringEntity(
                "{\"rtc_cm:state\":{\"rdf:resource\":\"https://localhost:7443/ccm/oslc/workflows/_gl8nMDbaEeGq__0Ju8tMow/states/condicao.state.s2\"}}");
        httpput.setEntity(myEntity);
        responsePut = httpclient.execute(httpput);
        entityPut = responsePut.getEntity();

        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();
        }
    }
}

Best Regards
Rajat

2 answers



permanent link
Sudipto Sarkar (63536) | answered Jun 03 '15, 1:11 a.m.
Hi Dinesh, Rajat

 Can I give the state of a workitem as "Inprogress" while creating a workitem in oslc?
I mean, I am trying to create a workitem via oslc 2.0 and trying to give the state as "In progress" in oslc body, but after creating, the state is new.

Thanks!
Sud

permanent link
Dinesh Kumar B (4.1k413) | answered Mar 06 '14, 8:52 a.m.
JAZZ DEVELOPER
Hi Rajat,

use discovery mechanism to reach to specific work item url and
perform a
  PUT on specific work item URL
with request having the following details
   a.  headers
        -  OSLC-Core-Version : 2.0
        -  Content-Type : application/rdf+xml (or JSON as per your business need)
        -  If-Match set to ETag value from the response header from a GET on the work item url
    b.   body containing
        -  all the mandatory fields along with
        -  rtc_cm:state

where
   rtc_cm:state
is set to appropriate
   state value
picked from the work item type's resource shape...

if you need more explanation to get through this, I would recommend going through this  :
     http://www.youtube.com/watch?v=eXVWtiuygr0
which has an excellent walk through for starters...

hope it helps...

Your answer


Register or to post your answer.