0
votes

I need to create an HTTP adapter for worklight but the url must be programmatically provided via a parameter.

1) I was able to pass the user/password but not the url. Is there a way to do that?

I also try to create my own java adapter to call the REST API, It works when I test the adapter but it seems my response is not in the expected format for worklight. I got this error:

2) BAD_PARAMETER_EXPECTED_DOCUMENT_OR_ARRAY_OF_DOCUMENT.

my Java adapter returns a JSONArtifact (JSONObject) but it seems that worklight want this to be embedded in another JSONObject such as { "array":{...}}. Is there a way to convert a JSONObject to the format expected by 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;
         }

Adapter:


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

it works with this but this solution doesn't provide the service url as parameter:


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

Collection


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

Many Thanks Dominique

1
1) You need to create an adapter and then 'link it' to a JSONStore collection. There's no way to create the adapter via JavaScript code, at least as far as I know. Read the adapter section here for more details.cnandreu
2) I'm not sure I understand the question. Did you try embedding the JSONObject inside another JSONObject like you mentioned? If that works, that's what I would do. Maybe you can try to mimic the output from a Worklight Adapter (InvocationResult, status, etc. keys).cnandreu
Side comment: It would be helpful if you provided code or pseudo-code samples of what you want to achieve. I assumed you where talking about JSONStore and tagged the question as such.cnandreu
@cnandreu, 1) it is what I did and modify the adapter to pass the user/password as parameters. I wonder if there is also a way to pass the url as parameter. 2) As I wasn't able to pass the url, I created my own Java adaptor (code above), I returned from my Java Adaptor a JSONOBject but the collection seems to complain with the error above.ITDoVe

1 Answers

0
votes

Found the solution:

First, I was using apache wink JSONArtifact instead of the com.ibm.json.java.JSONArtifact!

Secondly I modified my collector implement method as follow to add the status (not sure if it is needed or not)

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

in myCollector.js I set the user, password, hostname as follow before calling my initCollection.


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