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