Class MFP.Server

MFP.Server

Description:
The IBM® MobileFirst® server-side JavaScript API comprises these functions and objects.
Quick Reference
  • Accessing a web service.
    • MFP.Server.invokeHttp
  • Accessing a JDBC database.
    • MFP.Server.invokeSQLStoredProcedure
    • MFP.Server.invokeSQLStatement
  • Calling other procedures.
    • MFP.Server.invokeProcedure
  • Accessing an HttpServletRequest object.
    • MFP.Server.getClientRequest
  • Accessing Server configuration.
    • MFP.Server.getPropertyValue
  • Getting the authenticated user.
    • MFP.Server.getAuthenticatedUser
  • Getting the introspection data of the access token.
    • MFP.Server.getTokenIntrospectionData
  • Debugging.
    • MFP.Logger.debug, error, and log

Method Summary
Method Attributes Method Name and Description
 
Changes the response headers when using adapters.
 
Gets the authenticated user information.
 
Returns a reference to the Java™ HttpServletRequest object that was used to invoke an adapter procedure.
 
Gets the value for the given property name.
 
Gets the introspection data of the access token provided in the request.
 
Calls an HTTP service.
 
MFP.Server.invokeProcedure(invocationData)
Invokes a procedure exposed by another MobileFirst® JavaScript adapter.
 
Calls a prepared SQL statement.
 
Calls a stored procedure on a database.
 
MFP.Server.logActivity(activityType, deviceContext)
Reports user activity for auditing or reporting purposes.
Method Detail
MFP.Server.addResponseHeader
MFP.Server.addResponseHeader(input, value)
Changes the response headers when using adapters. The function can be used only inside an adapter procedure and is used to add a new header(s) to the response.
In most cases, it is used to modify the cache headers (that is, Cache-Control, Expires, and Pragma HTTP headers).
Parameters:
input -
Property Description
name HTTP header name. A string.
value HTTP header value. A string. Cannot be null.
value
Example:
MFP.Server.addResponseHeader("Expires","Sun, 5 October 2014 18:00:00 GMT");

MFP.Server.getAuthenticatedUser
MFP.Server.getAuthenticatedUser()
Gets the authenticated user information.
Returns:
The authenticated user information, formatted as a JSON object. The returned object has the following structure:
{
   "id":"test",
   "authenticatedBy":"UsernamePasswordCheck",
   "isSuccessful":true,
   "authenticatedAt":1470314267025,
   "attributes":{},
   "displayName":"test"
}
The invocation results object contains the following properties:
Property Description
attributes Map of attributes
authenticateedAt Time of authentication
authenticateedBy Security check authenticed for user
displayName Display name of the user
id ID of the user

MFP.Server.getClientRequest
MFP.Server.getClientRequest()
Returns a reference to the Java™ HttpServletRequest object that was used to invoke an adapter procedure. This function can be used in any adapter procedure.
Returns:
A reference to an HttpServletRequest object.
Example:
var request = MFP.Server.getClientRequest();
var userAgent = request.getHeader("User-Agent");

MFP.Server.getPropertyValue
MFP.Server.getPropertyValue(options)
Gets the value for the given property name. This function returns the value from the actual adapter configuration, if it was modified. Otherwise, the default value (defined in the adapter.xml descriptor file) is returned.
Parameters:
options - The name of the property
Returns:
Value for the given property

MFP.Server.getTokenIntrospectionData
MFP.Server.getTokenIntrospectionData()
Gets the introspection data of the access token provided in the request.
Returns:
The introspection data of the access token, formatted as a JSON object. The returned object has the following structure:
{
  "mfp-application":
      {
          "id":"com.ibm.mfpstarterweb",
          "clientPlatform":"web"
      },
  "exp":1470311497928,
  "username":"anonymous",
  "mfp-device":
      {
          "id":"6747046e-0393-ced4-632d-19fe2af0fa87",
          "platform":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36",
          "hardware":"MacIntel",
          "status":"ACTIVE",
          "displayName":null
      },
  "scope":"RegisteredClient",
  "isSuccessful":true,
  "mfp-user":null,
  "active":true,
  "client_id":"e3d0a789-6ec1-4451-930d-12d06a1834d0",
  "mfp-checks":{}
 }
The invocation results object contains the following properties:
Property Description
client_id Client identifier
exp Expiration of the token
mfp-application Application Data
Property Description
id Applcation Id
clientPlatform Client Platform
version Applcation Version
mfp-device Device Data
Property Description
id Device Id
Platform Client Device Platform
hardware Device Hardware
status Device status
displayName Device Display Name
mfp-user User Data
Property Description
attributes Map of attributes
authenticateedAt Time of authentication
authenticateedBy Security check authenticed for user
displayName Display name of the user
id ID of the user
mfp-checks custom introspection data provided by the security checks.
The data is provided as a map between the name of the security check and the custom introspection data it provided. The map includes only security checks that provided a non-empty custom introspection data.

MFP.Server.invokeHttp
MFP.Server.invokeHttp(options)
Calls an HTTP service. The method can be used only inside a procedure declared within an HTTP adapter. It calls a back-end HTTP service and converts the results to JSON.
Parameters:
options - The invokeHttp method accepts the following JSON block of parameters:
Property Description
method Mandatory. Defines the HTTP method. Valid values are get, post, put, and delete.
returnedContentType Optional. Defines the type of content that is returned by the called HTTP service, overriding the HTTP response’s MIME type.
If this parameter is not provided, the MobileFirst® Server handles the response according to the MIME type.
If it is provided, the MobileFirst Server handles the response according to the indicated value. The field can receive the following values:
  • css, csv, html, json, plain, and xml. If the invocation failed, the failure handler for the request is called.
  • Any MIME type that includes one of these values. Note that any response with MIME type that contains JavaScript or JSON is considered to contain JSON objects.
returnedContentEncoding Optional. Defines the encoding of the returned content. Default is utf-8.
path Mandatory. Defines the path of the URL defining the HTTP service.
parameters Optional. Defines the set of parameters that need to be passed to the HTTP service.
headers Optional. Defines the headers for the HTTP request.
cookies Optional. Defines cookies to be passed with the HTTP request.
body Defines the content of the request body.
  • When the method is GET, this property is not allowed.
  • When the method is POST, this property is optional.
Note: body.content must be a string. If you are explicitly creating a DOM object, such as in: var request = <soap:Envelope> … </soap:Envelope>, you must convert the object to a string before you assign it to body.content, for example: request.toString();
transformation Optional. If defined, the response of the service undergoes the defined XSL transformation. If the service returns HTML, the MobileFirst Server first converts the response to XHTML, and then runs the XSL transformation on the XHTML response.
connectionTimeoutInMilliseconds Optional. The timeout (in milliseconds) until a connection to the back-end can be established. The default value is 30000. By passing a value for connectionTimeoutInMilliseconds, you can override the value defined for connectionTimeoutInMilliseconds in the element of the HTTP adapter descriptor file.
socketTimeoutInMilliseconds Optional. The timeout (in milliseconds) between two consecutive packets, starting from the connection packet. The default value is 30000. By passing a value for socketTimeoutInMilliseconds, you can override the value defined for socketTimeoutInMilliseconds in the element of the HTTP adapter descriptor file.
Returns:
The method returns the response of the HTTP service, after the following processing:
  1. If the service returns HTML, the MobileFirst Server converts the HTML response to XHTML. If the service returns XML, the MobileFirst Server keeps it as is.
  2. If an XSL transformation has been defined in the transformation property, the MobileFirst Server executes the transformation on the result of Step 1. The transformation should convert its XML input to JSON. If no transformation was defined, the MobileFirst Server automatically converts the result of Step 1 to JSON.
Example:
var response = MFP.Server.invokeHttp(invocationData);
response.responseHeader; // responseHeader property contains HTTP response headers
response.statusCode; // statusCode property contains HTTP response status code

MFP.Server.invokeProcedure
MFP.Server.invokeProcedure(invocationData)
Invokes a procedure exposed by another MobileFirst® JavaScript adapter.
Parameters:
invocationData - The invokeProcedure function accepts the following JSON block of parameters:
Property Description
adapter Mandatory. A string that contains the name of the adapter as specified when the adapter was defined.
procedure Mandatory. A string that contains the name of the procedure as specified when the adapter was defined.
parameters Optional. An array of parameter values that are passed to the back-end procedure. A parameter can be a scalar or an object.
Returns:
The returned object has the following structure:
{
	isSuccessful: Boolean,
	errorMessages: ["Error Msg1", ...],
	// Application object returned by procedure
}
The invocation results object contains the following properties:
Property Description
isSuccessful Identifies whether the function invocation succeeded or failed. Valid values are:
  • true: The function invocation succeeded. This is the default value.
  • false: The function invocation failed.
errorMessages Optional. An array of strings that contain error messages. If no errors are provided, the returned array is empty.
Application object Any object that is returned by the procedure.
Example:
adapter : "AcmeBank"|procedure : " getTransactions"|parameters : [accountId|fromDate|toDate]|
;

MFP.Server.invokeSQLStatement
MFP.Server.invokeSQLStatement(options)
Calls a prepared SQL statement. The function can be used only inside a procedure that is declared within an SQL adapter.
Parameters:
options -
{
	 //Mandatory
	 preparedStatement : prepared-statement-variable,

	 //Optional
	 parameters: [value-1, value-2, .... ]
}
The invokeSQLStatement function accepts the following JSON block of parameters:
Property Description
preparedStatement Mandatory. A string representing the SQL statement.
parameters Optional. An array of parameters to the prepared statement.

Example:

If you write the following code:
function procedure1(param) {
    return MFP.Server.invokeSQLStatement({
	        preparedStatement : "select transactionType, transactionAmount, transactionId, toAccount, fromAccount, transactionDate from TRANSACTIONS where fromAccount = ?",,
	        parameters : ["12345"]
     });
}
Then, assuming that the result set is:
  transactionType    transactionAmount   transactionId    toAccount   fromAccount   transactionDate
  -------------------------------------------------------------------------------------------------
  "Funds Transfer"   180.00              "W06091500863"   "54321"     "12345"       1236762519000
  "ATM Withdrawal"   130.00              "W214122/5337"   NULL        "12345"       1236416979000
  
The JSON result is:
   {
    "isSuccessful":true,
    "resultSet":[
      {
         "transactionType":"Funds Transfer",
         "transactionAmount":180.00,
      "transactionId":"W06091500863",
      "toAccount":"54321",
      "fromAccount":"12345",
      "transactionDate":1236762519000
      },
      {
        "transactionType":"ATM Withdrawal",
        "transactionAmount":130.00,
        "transactionId":"W214122/5337",
        "toAccount":null,
        "fromAccount":"12345",
        "transactionDate":1236416979000
      }
    ]
   }
  
Returns:
The function returns the result set of the prepared statement. This returned value is formatted as a JSON array, in which each element corresponds to a row in the result set of the prepared statement.

MFP.Server.invokeSQLStoredProcedure
MFP.Server.invokeSQLStoredProcedure(options)
Calls a stored procedure on a database. The function can be used only inside a procedure that is declared within an SQL adapter.
Parameters:
options - The invokeSQLStoredProcedure function accepts the following JSON block of parameters:
{
	//Mandatory
	procedure : procedure-name,

	//Optional
	parameters: [value-1, value-2, ... ]
}

Example:

Assuming that the result set is:
  transactionType    transactionAmount   transactionId    toAccount   fromAccount   transactionDate
  -------------------------------------------------------------------------------------------------
  "Funds Transfer"   180.00              "W06091500863"   "54321"     "12345"       1236762519000
  "ATM Withdrawal"   130.00              "W214122/5337"   NULL        "12345"       1236416979000
  
The JSON result is:
   {
    "isSuccessful":true,
    "resultSet":[
      {
         "transactionType":"Funds Transfer",
         "transactionAmount":180.00,
      "transactionId":"W06091500863",
      "toAccount":"54321",
      "fromAccount":"12345",
      "transactionDate":1236762519000
      },
      {
        "transactionType":"ATM Withdrawal",
        "transactionAmount":130.00,
        "transactionId":"W214122/5337",
        "toAccount":null,
        "fromAccount":"12345",
        "transactionDate":1236416979000
      }
    ]
   }
  
Returns:
The function returns the result set of the SQL stored procedure. This returned value is formatted as a JSON array, in which each element corresponds to a row in the result set of the SQL stored procedure.

MFP.Server.logActivity
MFP.Server.logActivity(activityType, deviceContext)
Reports user activity for auditing or reporting purposes.
The IBM® MobileFirst® server maintains a separate database table to store application statistics.
Parameters:
{string} activityType - A string that identifies the activity.
deviceContext
Deprecated:
The function is deprecated from V7.0. Use MFP.Logger instead.

© Copyright IBM Corp. 2011, 2016.