【问题标题】:Getting session from HttpAdapter to Java based Adapter从 HttpAdapter 获取会话到基于 Java 的适配器
【发布时间】:2013-04-16 06:47:30
【问题描述】:

有什么方法可以将在 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 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;
}
}

【问题讨论】:

    标签: session https adapter ibm-mobilefirst


    【解决方案1】:

    您不需要访问会话 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 代码的 POST 中也传递 JSESSIONID,follow the example provided in the WL.Server.invokeHttp API(页面底部)。在适配器 JavaScript 中,实现示例... response.responseHeader 包含一个“Set-Cookie”标头,您可以从中提取 JSESSIONID,传递并使用它。

    【讨论】:

    • 但是当我尝试从 java 类发帖时,它再次询问用户凭据
    • 我在 httpAdapter 的登录过程中使用了这两行,其中 Cookies 是一个全局变量。我试图将它传递给 java 类。它只会正确传递。但我不知道我在建立连接 injava 类时是否做错了什么。 var request = WL.Server.getClientRequest(); Cookies = request.getHeader("Cookie");
    • 不要将 cmets 用于代码。编辑您的问题。添加你的java代码,添加你的javascript。
    • 另外,标题名称是“Set-Cookie”。
    • 我尝试过这样做,但我收到的回复说请求错误(状态代码 400)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多