HP ALM 12 REST not returning QCSession cookie

2019-04-12 07:08发布

问题:

I am trying out HP ALM 12 and found REST api has changed but I could not figure out how to get it to work with the java code that works fine with ALM 11.5.

with ALM 11.5 I send following http requests:

http://myALM:8000/qcbin/rest/is-authenticated (which calls /authenticate)
http://myALM:8000/qcbin/rest/domains/DEFAULT/projects/DEMO/requirements?login-form-required=y&fields=father-name,name,req-priority,request-status,description,name,status&query={status[NOT%20N/A]}

Everything works fine above.

With ALM 12, its REST documentation said that I need to explicitly call /rest/site-session resource to obtain the QCSession (which was returned automatically in ALM 11.5 at the first call to any resource), but I could not get the call to return QCSession. Below is the list of http requests sent in order to ALM 12:

http://myALM:8000/qcbin/rest/is-authenticated (which calls authenticate)
http://myALM:8000/qcbin/rest/site-session
http://myALM:8000/qcbin/rest/domains/DEFAULT/projects/DEMO/requirements?login-form-required=y&fields=father-name,name,req-priority,request-status,description,name,status&query={status[NOT%20N/A]}

The call to rest/site-session returns JSESSIONID as if it was the first request and obviously the rest of the code just failed.

What am I doing wrong here?

回答1:

You need to POST and save cookies from response to RestConnector class.

This I added to class, which works as "thin" layer above RestConnector

public void GetQCSession(){
    String qcsessionurl = restConnector.buildUrl("rest/site-session");
    Map<String, String> requestHeaders = new HashMap<String, String>();
    requestHeaders.put("Content-Type", "application/xml");
    requestHeaders.put("Accept", "application/xml");
    try {
        Response resp = restConnector.httpPost(qcsessionurl, null, requestHeaders);
        restConnector.updateCookies(resp);
    } catch (Exception e) {
        e.printStackTrace();
    }   
}

RestConnector updateCookies method

public void updateCookies(Response response) {

    Iterable<String> newCookies =
        response.getResponseHeaders().get("Set-Cookie");
    if (newCookies != null) {

        for (String cookie : newCookies) {
            int equalIndex = cookie.indexOf('=');
            int semicolonIndex = cookie.indexOf(';');

            String cookieKey = cookie.substring(0, equalIndex);
            String cookieValue =
                cookie.substring(equalIndex + 1, semicolonIndex);

            cookies.put(cookieKey, cookieValue);
        }
    }
}