MobileFirst - Invoking Java SQL Adapter adapter procedure - ibm-mobilefirst

I am following the Java SQL Adapter tutorial for MobileFirst Platform 7.
I'm trying to Get User with userId = "bjones", but I don't know how to set the params {userId} into the procedure /adapters/UserAdapter/{userId}.
function loadUsers(){
busyIndicator.show();
var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET);
resourceRequest.setQueryParameter("userId", "bjones");
resourceRequest.send().then(
loadUsersSuccess,
loadUsersFailure
);}
function loadUsersSuccess(result){
WL.Logger.debug("Feed retrieve success");
busyIndicator.hide();
WL.Logger.debug(JSON.stringify(result));
if (result.responseJSON.length>0)
displayFeeds(result.responseJSON);
else
loadUsersFailure();}
function loadUsersFailure(result){
WL.Logger.error("Feed retrieve failure");
busyIndicator.hide();
WL.SimpleDialog.show("Banking Application", "Service not available. Try again later.",
[{
text : 'Reload',
handler : WL.Client.reloadApp
},
{
text: 'Close',
handler : function() {}
}]
);}
My request is
localhost:10080/JavaAdapters/adapters/UserAdapter/?userId=bjones
but the JSON response contains all user stored in my database
Image for response
In addition, how about the REST call type #PUT, with Path param "userId" and body params: "firstName", "lastName", "password", in order to update an user

From the tutorial the adapter endpoint is /{userId} which means the userId is not a query param but it is part of the url. You need to update your loadUsers function so that it appends the userId at the end of the url, so in your example the fullpath will be /adapters/UserAdapter/bjones
function loadUsers(){
busyIndicator.show();
var usedId = "bjones";
var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/"+userId, WLResourceRequest.GET);
resourceRequest.send().then(loadUsersSuccess,loadUsersFailure);
}
UPDATE:
function loadUsersSuccess(result) {
WL.Logger.debug("Feed retrieve success");
busyIndicator.hide();
WL.Logger.debug(JSON.stringify(result));
// if responseJSON is not null user data was returned
if (result.responseJSON != null) {
displayFeeds(result.responseJSON);
} else{
loadUsersFailure();
}
}

there are basically two type of URL with Parameters:
1. Path parameter:
/adapters/UserAdapter/users/{userId}
2. Query Parameter:
/adapters/UserAdapter/users?userId={userId}
java adapter with query parameter:
#GET
#Produces("application/json")
#OAuthSecurity(enabled = false)
#Path("/users")
public String getuserById(#QueryParam("userID") String userId)
{
System.out.println(userId);
}
java adapter with path parameter:
#GET
#Produces("application/json")
#OAuthSecurity(enabled = false)
#Path("/users/{userId}")
public String getuserById(#PathParam("userId") String userId)
{
System.out.println(userId);
}
I hope, second example answers your question in java adapter.

Related

How to call Azure function from Kotlin

I currently have deployed an Azure function used to get an AD token.
Function:
https://getadtokennet.azurewebsites.net/api/getadtokennet
Request header:
x-functions-key = {key}
How can I call this function from my Kotlin app?
This is the way I call it from Javascript
function getTokenAzure(onsuccess, onerror) {
var tokenUrl = 'https://getadtokennet.azurewebsites.net/api/getadtokennet';
$.ajax(tokenUrl, {
method: 'GET',
beforeSend: function (request) {
request.setRequestHeader("x-functions-key", "function key");
},
success: function (data) {
onsuccess(data);
console.log('token: ' + data.token);
},
error: function (xhr, status, error) {
var failureMessage = "GetToken error: " + status + " - " + error;
onerror(failureMessage);
console.log(failureMessage);
}
});
}
In IntelliJ IDEA, select Create New Project.
In the New Project window, select Maven from the left pane.
Select the Create from archetype check box, and then select Add Archetype for the azure-functions-kotlin-archetype.
In the Add Archetype window, complete the fields as follows:
GroupId: com.microsoft.azure
ArtifactId: azure-functions-kotlin-archetype
Version: Use the latest version from the central repository
Select OK, and then select Next.
Enter your details for current project, and select Finish.
For complete information refer to the below links which has same information.
Kotlin Function and Running Kotlin in Azure Functions
I found the way, here it is.
fun getToken(): String {
val tokenUrl = URL("https://getadtokennet.azurewebsites.net/api/getadtokennet")
val connection = tokenUrl.openConnection() as HttpURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("x-functions-key", "function key")
connection.doOutput = true
val responseCode = connection.responseCode
if (responseCode == HTTP_OK) {
val readerIn = BufferedReader(InputStreamReader(connection.inputStream))
var inputLine = readerIn.readLine()
val response = StringBuffer()
do {
response.append(inputLine)
} while (inputLine.length < 0)
readerIn.close()
// Return token
return response.toString()
} else {
val responseError = Error(code = "BadRequest", message = "There was an error getting the token.")
throw IOException(responseError.toString())
}
}

How to use POST to set the results of a SurveyJS survey?

Is it possible to use POST to set the results of a SurveyJS survey?
I can use GET to get the survey results, but I am struggling with setting.
Here is the code I use to GET the results:
urlToSurvey = "https://dxsurvey.com/api/MySurveys/getSurveyResults/surveyID?accessKey=myKey";
$.get(urlToSurvey, function(res) {
console.log(res);
});
I want to use SurveyJS to store students' progress in an open-source plugin (Adapt Learning), so I want to directly post the progress data to SurveyJS as I cannot run a stand-alone html in the plugin.
Any help is appreciated. Thanks!
You can check this file - https://github.com/surveyjs/surveyjs/blob/master/src/dxSurveyService.ts
Here is the code responsible for sending the result:
public sendResult(
postId: string,
result: JSON,
onSendResult: (success: boolean, response: any) => void,
clientId: string = null,
isPartialCompleted: boolean = false
) {
var xhr = new XMLHttpRequest();
xhr.open("POST", dxSurveyService.serviceUrl + "/post/");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var data = { postId: postId, surveyResult: JSON.stringify(result) };
if (clientId) data["clientId"] = clientId;
if (isPartialCompleted) data["isPartialCompleted"] = true;
var dataStringify: string = JSON.stringify(data);
var self = this;
xhr.onload = xhr.onerror = function() {
if (!onSendResult) return;
onSendResult(xhr.status == 200, xhr.response);
};
xhr.send(dataStringify);
}
The required params are the postId and result json. You can get your postId from the MySurveys page of the service (https://surveyjs.io/Service/MySurveys/ note that MySurveys page requires authorization).
This is a TypeScript code, but I'm sure it can easily be converted to the JS.

login authentication in worklight

I referred to this question Login Authentication In IBM Worklight I have read all the pdfs but still my login is not working.I have used another method to check for the database.
var procedure1Statement = WL.Server.createSQLStatement("select t_id from teacher where
t_id=? and t_password=?");
var response;
function login(id,pass) {
response= WL.Server.invokeSQLStatement({
preparedStatement : procedure1Statement,
parameters : [id,pass]});
return response;
}
function submitAuthentication(id, pass){
var invocationData={
adapter : "admin",
procedure : " login",
parameters : [id,pass],
};
var result=WL.Server.invokeProcedure (invocationData);
if(result.t_id >1 )
{
var userIdentity = {
userId : id,
displayName : id,
attributes: {
role: "admin"
}
};
WL.Server.setActiveUser("adminRealm",userIdentity);
return{
authRequired : false
};
}
return onAuthRequired(null,"Invalid Login Credentials");
}
function onLogout(){
WL.Logger.debug("Logged Out");
}
What condition should I use here to make it work
if(result.t_id >1 )
if (result.resultSet.length == 1)
Or that plus whatever other checks you want to make on the returned record.
if (result.resultSet.length == 1 && result.resultSet[0].t_id > 0)
BTW:
You have a stray space in your invocationData before the procedure name:
procedure : " login",

Google Analytics API fails to login with bad request and invalidKey

I´m trying to get some data from analytics, but can´t get authorized. It returns the following error:
I renewed my credentials at google console several times.
The code I´m using:
var clientId = '*****************0m1fnmuae00abaaq.apps.googleusercontent.com';
var apiKey = '********fB9eVMVfQ0oR6';
var scopes = 'https://www.googleapis.com/auth/analytics.readonly';
function handleClientLoad() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth, 1);
}
function checkAuth() {
gapi.auth.authorize({
client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
if (authResult) {
gapi.client.load('analytics', 'v3', handleAuthorized);
} else {
handleUnauthorized();
}
}
function handleAuthorized() {
var authorizeButton = document.getElementById('authorize-button');
var runDemoButton = document.getElementById('run-demo-button');
authorizeButton.style.visibility = 'hidden';
runDemoButton.style.visibility = '';
runDemoButton.onclick = makeApiCall;
outputToPage('Click the Run Demo button to begin.');
}
function handleUnauthorized() {
var authorizeButton = document.getElementById('authorize-button');
var runDemoButton = document.getElementById('run-demo-button');
runDemoButton.style.visibility = 'hidden';
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
outputToPage('Please authorize this script to access Google Analytics.');
}
function handleAuthClick(event) {
gapi.auth.authorize({
client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
}
When I run the code, it returns the following error:
error: {errors:[{domain:usageLimits, reason:keyInvalid, message:Bad Request}], code:400, message:Bad Request}
code: 400
errors: [{domain:usageLimits, reason:keyInvalid, message:Bad Request}]
0: {domain:usageLimits, reason:keyInvalid, message:Bad Request}
domain: "usageLimits"
message: "Bad Request"
reason: "keyInvalid"
message: "Bad Request"
Someone can help with this issue?
Find the error.
In Google Console, you have to create the OAuth ID and the Public API Access ID.
Take the cliendId from the first and the APIKey from the second.
I think it´s very confuse, and it could be more explicit in documentation.

Change HTTP URL in Worklight adapter

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