springrestresttemplatealm

ALM 12 REST using SpringFramework RestTemplate: "401 QCSession cookie missing"


In ALM 12 we have to explicity call "qcbin/rest/site-session" to get session. When I GET call "/qcbin/rest/site-session" I receive the following:

"Set-Cookie=[BIGipServerALMAST330P-QC=rd100o00000000000000000000ffff0fe0dd74o8080; path=/, ]""

I extract the cookie as described here: HP ALM 12 REST not returning QCSession cookie. Instead of this RestConnector, our project is using RestTemplate from SpringFramework, so I did:

        private HashMap getQCSession() throws Exception {
            URL url = new URL("https://almxxxx.saas.hp.com/qcbin/rest/site-session?login-form-required=y");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setRequestProperty("Accept", "application/xml");
            conn.connect();

            HashMap cookies = updateCookies(conn);
            return cookies;
        }


        public HashMap updateCookies(HttpURLConnection conn) {
            String cookie2 = conn.getHeaderField("Set-Cookie");             
            int equalIndex = cookie2.indexOf('=');
            int semicolonIndex = cookie2.indexOf(';');
            String cookieKey = cookie2.substring(0, equalIndex);
            String cookieValue = cookie2.substring(equalIndex + 1, semicolonIndex);

            HashMap cookies = new HashMap();
            cookies.put(cookieKey, cookieValue);
            System.out.println(cookies.toString());
            return cookies;
        }

To send the cookie in the GET call using the RestTemplate I followed the instructions from http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate/, so I did:

public <U extends Object> ResponseEntity<U> getFromURL(URI url, Class<U> responseBodyClass) throws Exception {
        logger.info("GET na URL: {} esperando classe: {} no response", url, responseBodyClass);

        HashMap cookie = this.getQCSession();
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Cookie", this.getQCSession().toString());
        this.httpEntity = new HttpEntity(null, requestHeaders);

        return super.exchange(url, HttpMethod.GET, this.httpEntity, responseBodyClass);
    }

The requestHeaders content added to the HttpEntity (SpringFramework) is:

{Cookie=[{BIGipServerALMAST330P-QC=rd100o00000000000000000000ffff0fe0dd74o8080}]}

However I'm still getting "401 QCSession cookie missing".
I already tried to send in the GET call the JSESSIONID, with no success as well.

I appreciate any help. Any clue?


Solution

  • I ran into this. As of ALM12 you need to create a session also.

    I POST some XML or JSON to here "/authentication-point/alm-authenticate" to authenticate Then collect the Set-Cookie header I then POST to "/rest/site-session" with the cookie from the previous response.
    I collect the session cookies from that response to use in my subsequent requests.

    Hope that helps