0

Is there any way to get the session that is created in a httpAdapter into a java class in the adapter? I am connecting to a https://jazz.net/ with a httpadapter, but i now want to use some java code to connect to same url in the adapter. So i dont knw how to access the session already created.

Here is my connection code:

In javaScript, 
function login(){
var lpath = getLoginPath();
var input = {
    method : 'post',
    returnedContentType : 'html',
    path : lpath,
    parameters : {
        j_password : passwd,
        j_username : username
    }

};

var request = WL.Server.getClientRequest();
UserSession = request.getHeader("Cookie"); // here i am setting the value for a global variable UserSession.
return WL.Server.invokeHttp(input);
}

function getConnection(){
var instance = new com.worklight.customcode.PostRequest();

var rspath = getRootServicesPath();

var input = {
    method : 'get',
    returnedContentType : 'xml',
    path : rspath
};
return {
    result : instance.getXml(UserSession) // Here i am passing the value of session cookie into the java class 
};}

In Java Class,

class PostRequest{
 public String getXml(String cookieString){
    String params = "?offlineData={'projectAreaItemId':'_iTuLUmxfEeKR3t32SVbLKQ','executables':[{'terItemId':'_TNoyQW6qEeKR3t32SVbLKQ','scriptItemId':'_DF89AW6qEeKR3t32SVbLKQ'}]}";
    String url = "https://jazz.net/sandbox02-qm/secure/service/com.ibm.rqm.execution.common.service.IOfflineExecutionRestService"
            + params;
URLConnection connection = new URL(url).openConnection();
connection = new URL(url).openConnection();
    connection.setRequestProperty("User-Agent", "yes");
    connection.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    connection.setRequestProperty("Accept-Encoding", "gzip");
    connection.setRequestProperty("Accept", "application/xml");
    connection.setRequestProperty("Cookie", cookieString);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    ((HttpURLConnection) connection).setRequestMethod("POST");
    connection.setDoOutput(true);
String result = "";
        InputStream resp = connection.getInputStream();
        StringBuilder s = inputStreamToString(resp);
return s;
}
}
Ashwini Maddala
  • 197
  • 1
  • 6
  • 26

1 Answers1

0

You do not need to access the session ID, as you already "have it". The Java class you write is part of the adapter session, or, scope.

What you need to do is declare your class in the adapter. Something like this:

The Class

Class myHTTPClient
// implementation code goes here - the java code to connect to the same URL as in the adapter XML.

The Adapter

var httpClient = new com.myHTTPClient
// your adapter code

This way the Java is in the same scope as the adapter, hence there is no need to "get" the session ID.

In order to pass the JSESSIONID also in the POST from the Java code, follow the example provided in the WL.Server.invokeHttp API (bottom of page). In the adapter JavaScript, implement the example... response.responseHeader contains a "Set-Cookie" header, from which you can extract the JSESSIONID, pass it and use it.

Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • but when i m trying to do a post from the java class, it is asking user credentials again – Ashwini Maddala Apr 18 '13 at 09:00
  • I have used these 2 lines during login in httpAdapter, where Cookies is a global variable.I tried to pass that to the java class. it gets passed correctly only. but i dont know if i am doing something wrong in the establishing the connection injava class. var request = WL.Server.getClientRequest(); Cookies = request.getHeader("Cookie"); – Ashwini Maddala Apr 18 '13 at 10:34
  • Don't use comments for code. EDIT your question. Add your java code, add your javascript. – Idan Adar Apr 18 '13 at 10:47
  • Also, the header name is "Set-Cookie". – Idan Adar Apr 18 '13 at 12:44
  • I tried doing that, but i am getting a response saying bad request (status code 400) – Ashwini Maddala Apr 25 '13 at 06:06