从HttpAdapter获取会话到基于Java的适配器 [英] Getting session from HttpAdapter to Java based Adapter

查看:153
本文介绍了从HttpAdapter获取会话到基于Java的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的连接代码:

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


推荐答案

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

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.

适配器

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

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

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

按顺序从Java代码也在POST中传递JSESSIONID,按照WL.Server.invokeHttp API (页面底部)中提供的示例进行操作。在适配器JavaScript中,实现示例... response.responseHeader包含一个Set-Cookie标头,您可以从中提取JSESSIONID,传递它并使用它。

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.

这篇关于从HttpAdapter获取会话到基于Java的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆