从HttpAdapter获取会话到基于Java的适配器

时间:2013-04-16 06:47:31

标签: session https adapter ibm-mobilefirst

有没有办法将httpAdapter中创建的会话转换为适配器中的java类? 我正在使用httpadapter连接到https://jazz.net/,但我现在想使用一些java代码连接到适配器中的同一个url。所以我不知道如何访问已创建的会话。

这是我的连接代码:

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

在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;
}
}

1 个答案:

答案 0 :(得分:0)

您不需要访问会话ID,因为您已经“拥有它”。 您编写的Java类是适配器会话的一部分,或范围。

您需要做的是在适配器中声明您的类。像这样:

班级

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

适配器

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

这样,Java与适配器的范围相同,因此无需“获取”会话ID。

为了在Java代码{@ 3}}(页面底部)的POST中传递JSESSIONID。在适配器JavaScript中,实现示例... response.responseHeader包含一个“Set-Cookie”标头,您可以从中提取JSESSIONID,传递并使用它。