在Worklight适配器中更改HTTP URL

时间:2013-03-15 12:33:53

标签: ibm-mobilefirst jsonstore

我需要为worklight创建一个HTTP适配器,但必须通过参数以编程方式提供url。

1)我能够传递用户/密码,但不能传递网址。有没有办法做到这一点?

我也尝试创建自己的java适配器来调用REST API,它在我测试适配器时起作用,但似乎我的响应不符合worklight的预期格式。我收到了这个错误:

2)BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENT。

我的Java适配器返回一个JSONArtifact(JSONObject),但似乎worklight希望将它嵌入到另一个JSONObject中,例如{“array”:{...}}。 有没有办法将JSONObject转换为worklight所期望的格式。


    import org.apache.wink.json4j.JSON;
    import org.apache.wink.json4j.JSONArtifact;
    import org.apache.wink.json4j.JSONException;

private Header headerUserAgent = new Header("User-Agent", "Mozilla");
private Header headerAccept = new Header("Accept", "application/json");

private String hostName;
private String baseURL;

protected MyHttpClient(String userName, String userPassword, String hostName, String baseURL ) {
    super();
    Credentials defaultcreds = new UsernamePasswordCredentials(userName,
            userPassword);
    this.getState().setCredentials(AuthScope.ANY, defaultcreds);
    this.hostName = hostName;
    this.baseURL = baseURL;
}


private GetMethod getGetMethod(String url) throws URIException {
    GetMethod httpMethod = new GetMethod(new HttpsURL("https://"+hostName+baseURL+url).getEscapedURI());
    addCommonHeaders(httpMethod);
    return httpMethod;
}
private JSONArtifact getResponseAsJSONObject(InputStream inputStream) throws IOException {
    InputStreamReader reader = new InputStreamReader(inputStream);
    try {
        JSONArtifact json = JSON.parse(reader);
        return json;
    } catch (NullPointerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
         }

适配器:


    function getResponse(user,password) {
        var client = new com.itdove.mypackage.MyHttpClient(user,password,"myurl","mybaseurl");
        return {
            array : client.executeGet("mypath")
        };
    }

它适用于此但此解决方案不提供服务URL作为参数:


    function getResponseAdapters(path, username, password) {

        var input = {
            method : 'get',
            returnedContentType : 'json',
            headers: {
                'User-Agent':'Mozilla',
                'Authorization': 'Basic '+Base64.encode(username+':'+password),
            }  , 
            path : '/resources/' + path
        };

        return WL.Server.invokeHttp(input);
    }

    function getResponse(username, password) {
        return getMySCAWSAdapters(path, username, password);
    }

集合


                vAPPArrayAdapterOptions = {
                    name: 'myResponseAdapter',
                    replace: '',
                    remove: '',
                    add: '',
                    load: {
                        procedure: 'getResponse',
                        params: ["user","password"],
                        key: 'array'
                    },
                    accept: function (data) {
                        return (data.status === 200);
                    }
                },

    ...
                    vAPPArray = wlJsonStore.initCollection(
                            "vAPPArray",
                            vAPPArraySearchFields,
                            {adapter: vAPPArrayAdapterOptions, 
                            onSuccess: initCollectionSuccessCallback, 
                            onFailure: initCollectionFailureCallback, 
                            load:true});

非常感谢 多米尼克

1 个答案:

答案 0 :(得分:0)

找到解决方案:

首先,我使用的是apache wink JSONArtifact而不是com.ibm.json.java.JSONArtifact!

其次我修改了我的收集器实现方法,如下所示添加状态(不确定是否需要)

function getResponse(user,password,hostname) {
    var client = new com.itdove.mypackage.IWDHttpClient(user,password,hostname,"mypath");
    return {
        array :client.executeGet("mymethod"),
        statusCode: client.getStatusCode(),
        statusReason: client.getStatusReason()
    };
}

在myCollector.js中我在调用initCollection之前设置了用户,密码和主机名。


        params = [ settings.json.user, settings.json.password, settings.json.hostname ];
        myAdapterOptions.load["params"] = params;