Oracle MAF Consuming Rest Services using Oracle Mobile Cloud Service (MCS)

Version 4

     

    Namit Kakkar examines how Oracle Mobile Application Framework consumes JSON REST services using Oracle Mobile Cloud Service (MCS). Article used manufacturer REST services deployed on one of the Oracle JCS instance and returns List of Mobile Manufacturers. The article showcases the configuration of a REST service in MCS and the service's consumption in a MAF application. The article also explains security implementation at the MCS and MAF layers, respectively.

     


     

     

     

    REST SERVICE

     

     

     

    For the article we use the manufacturer rest service deployed on one of the Oracle JCS instance that returns list of Mobile Manufacturers. Below is the Output from that service.

     

     

     

    001RestService.jpg

     

     

     

    MCS Configuration

     

     

     

    Mobile Backends

     

     

     

    A Mobile Backend (MB) is a container that is used for hosting one or more APIs, perform users management, configuring Storage APIs, Diagnostics to diagnose health, logs or request of the application, register or sending Notification to the mobile client.

     

     

     

    To create a MB, login to the MCS instance and click development Tab and then Mobile Backends.

     

     

     

    002MCShome.jpg003MCS.jpg

     

     

     

    Enter Name and Description and click Create.

     

     

     

    Create User

     

     

     

    Open the Mobile Backed created above and clicks the Users Link in LHS menu of Mobile Backend.

     

     

     

    004MCSUsers.jpg

     

     

     

    Click on the NEW USER button and provide the details and click Create button.

     

     

     

    005MCSUser.jpg

     

    Once the user is created the password will be send to the email Id given above.

     

     

     

    Connectors

     

     

     

    Click on the Development tab and click the Connectors Icon Link.

     

     

     

    Click on the New Connector button and create a REST connector

     

     

     

    013Connector.jpg

     

     

     

    Provide the Display Name, API Name, REST Service Remote URL and Description

     

     

     

    014Conectors.jpg

     

     

     

    No other configuration is required, now just Test the connector created

     

     

     

    015Connectors.jpg016Connectors.jpg

     

     

     

    APIs

     

     

     

    The Oracle Mobile Cloud Service (MCS) platform APIs let your mobile apps interact with a mobile backend and access MCS services. These APIs can be custom API or standard OOTB APIs.

     

    To create new custom API and declare methods click on APIs on the development Tab or from a Backend LHS menu and click NEW API

     

     

     

    006API.jpg007API.jpg

     

     

     

    Click on the Endpoints Tab and Click on New Resources Button

     

    008API.jpg009API.jpg

     

     

     

    Add a New Resource and enters the details as above and then click the Methods Links

     

     

     

    010API.jpg

     

     

     

    Click on the Add Method and create a Get Method

     

     

     

    0011API.jpg

     

     

     

    Now click back on ALL APIs link and click Security Tab.

     

    Make Allow Anonymous User Access as OFF so that only authorized user can access the APIs.

     

     

     

    012API.jpg

     

     

     

    Click on the Implementation Link and click the Java Script Scaffold and It will download a ZIP file having below files

     

     

     

    017API.jpg018ZIP.jpg

     

     

     

    Open the package.json and ocs_wm_manufacturer.js and paste below code in it.

     

     

     

    Package.json

     

     

     

    {
    
    "name" : "ocs_wm_manufacturers",
    
    "version" : "1.0.0",
    
    "description" : "OCS_WM_Manufacturers",
    
    "main" : "ocs_wm_manufacturers.js",
    
    "oracleMobile" : {
    
    "dependencies" : {
    
    "apis" : { "/mobile/custom/OCS_WM_Manufacturers": "1.0" },
    
    "connectors" : {"/mobile/connector/ocs_wm_manufacturer_connector": "1.0"}
    
    }
    
    }
    
    }
    
    
    
    

     

    ocs_wm_manufacturer.js

     

    module.exports = function(service) {
    
    /**
    
    * The file samples.txt in the archive that this file was packaged with contains some example code.
    
    */
    
    service.get('/mobile/custom/OCS_WM_Manufacturers/getManufacturers', function(req,res) {
    
    
    
    var result = {};
    
    var sdkInstance = req.oracleMobile;
    
    var handler = function(error, response, body){
    
    var responseMessage = body;
    
    if (error){
    
    responseMessage = error.message;
    
    }
    
    res.status(response.statusCode).send(responseMessage);
    
    res.end;
    
    };
    
    var optionsList = {};
    
    optionsList.uri = '/mobile/connector/ocs_wm_manufacturer_connector/getManufacturers';
    
    sdkInstance.rest.get(optionsList, handler);
    
    
    
    });
    
    
    
    };
    
    
    
    

     

     

    Test the API created and provide Authentication details like Mobile Backend, version, User name and password and click Test End Point

     

     

    019API.jpg

     

     

     

    MAF Application

     

    Create a Mobile MAF application in JDeveloper 12C with name say "MafApp"

     

    Create Rest Connection and name it "MCSConn"

     

     

    020MCSConn.jpg

     

    Create a MAF feature and say name is manufacturer and enable Security on it.

     

    022MAF.jpg

     

    Open the maf-application.xml file and create Application Login Server below details

     

    021MAF.jpg

     

    Use defaults for AutoLogin and authorize tabs.

     

    In the "Policy" column, double click on the pencil icon to launch a dialog to select the security policy to be used for the connection. Select "oracle/wss_http_token_client_policy". This policy can be used for Basic-Auth protected connections with either HTTP or HTTPS.

     

    023MAF.jpg

     

    Call the MCS Rest Service from the Mobile MAF application

     

    Create a ServiceDC class and create a Data control out of it using JDeveloper 12C default feature.

     

     

    package application.DataControl;
    
    
    
    import application.serviceimplementation.ServiceImpl;
    
    import application.entities.ManufacturerEntity;
    
    import java.util.List;
    
    
    
    public class ServiceDC {
    
    public ServiceDC() {
    
    super();
    
    }
    
    
    
    private ManufacturerEntity[] manufacturers;
    
    public void setManufacturers(ManufacturerEntity[] manufacturers) {
    
    this.manufacturers = manufacturers;
    
    }
    
    
    
    public ManufacturerEntity[] getManufacturers() throws Exception{
    
    
    
    ServiceImpl simpl = new ServiceImpl();
    
    List manufList = simpl.fetchManufacturers();
    
    manufacturers = (ManufacturerEntity[]) manufList.toArray(new ManufacturerEntity[manufList.size()]);
    
    return manufacturers;
    
    }
    
    }
    
    
    
    

     

     

     

    Create a ServiceImpl Class and copy below code

     

    package application.serviceimplementation;
    
    
    
    
    
    import application.entities.ManufacturerEntity;
    
    import application.helper.ManufacturerEntityToJson;
    
    import application.uri.ServiceURI;
    
    import application.utils.WarrantyUtils;
    
    import java.math.BigDecimal;
    
    import java.util.ArrayList;
    
    import java.util.Collections;
    
    import java.util.Iterator;
    
    import java.util.List;
    
    import oracle.adfmf.dc.ws.rest.RestServiceAdapter;
    
    import oracle.adfmf.framework.api.JSONBeanSerializationHelper;
    
    import oracle.adfmf.framework.api.Model;
    
    import oracle.adfmf.json.JSONArray;
    
    import oracle.adfmf.json.JSONObject;
    
    
    
    public class ServiceImpl {
    
    public ServiceImpl() {
    
    super();
    
    }
    
    private String errorMessage;
    
    
    
    public List fetchManufacturers() throws Exception {
    
    
    
    WarrantyUtils.logInfo("Inside fetchManufacturers ::", "Starts");
    
    RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
    
    
    
    // Clear any previously set request properties, if any
    
    restServiceAdapter.clearRequestProperties();
    
    
    
    // Set the connection name
    
    restServiceAdapter.setConnectionName("MCSConn");
    
    
    
    // Specify the type of request
    
    
    
    restServiceAdapter.setRequestURI("/getManufacturers");
    
    restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);
    
    restServiceAdapter.addRequestProperty("Authorization", "Basic TUNTVXNlcjE6bVkzW2R1eWo=");
    
    restServiceAdapter.addRequestProperty("Oracle-Mobile-Backend-Id", "fc76c855-d031-4b7e-aeaf-c35b44e44b12");
    
    
    
    // Specify the number of retries
    
    restServiceAdapter.setRetryLimit(0);
    
    
    
    // Set the URI which is defined after the endpoint in the connections.xml.
    
    // The request is the endpoint + the URI being set
    
    String response = "not found";
    
    JSONBeanSerializationHelper jsonHelper = new JSONBeanSerializationHelper();
    
    JSONArray jArray = null;
    
    try {
    
    // For GET request, there is no payload
    
    response = restServiceAdapter.send("");
    
    response = restServiceAdapter.send("");
    
    JSONObject jsonObj = new JSONObject(response);
    
    jArray = (JSONArray) jsonObj.get("manufLists");
    
    } catch (Exception e) {
    
    if (errorMessage == null) {
    
    errorMessage = "Error while getting the getManufacturers list";
    
    }
    
    e.printStackTrace();
    
    WarrantyUtils.logError("Inside ServiceImpl :: Inside getManufacturers ::", e.getMessage(), e);
    
    throw e;
    
    }
    
    WarrantyUtils.logInfo("Inside fetchManufacturers ::", "Ends");
    
    
    
    return getManufacturerList(jArray);
    
    }
    
    
    
    /**
    
    *
    
    * @param manufacturerArrayObj
    
    * @return
    
    * @throws Exception
    
    */
    
    public List getManufacturerList(JSONArray manufacturerArrayObj) throws Exception {
    
    WarrantyUtils.logInfo("Inside getManufacturerList ::", "Starts");
    
    List manufacturerList = new ArrayList();
    
    try {
    
    
    
    for (int a = 0; a < manufacturerArrayObj.length(); a++) {
    
    JSONObject manufacturerObj = (JSONObject) manufacturerArrayObj.get(a);
    
    String NAME = manufacturerObj.getString("NAME");
    
    String CONTACT = manufacturerObj.getString("CONTACT");
    
    String EMAIL = manufacturerObj.getString("EMAIL");
    
    String ENROLLEDON = manufacturerObj.getString("ENROLLEDON");
    
    ManufacturerEntity manuent = new ManufacturerEntity();
    
    manuent.setCONTACT(CONTACT);
    
    manuent.setEMAIL(EMAIL);
    
    manuent.setENROLLEDON(ENROLLEDON);
    
    manuent.setNAME(NAME);
    
    manufacturerList.add(manuent);
    
    }
    
    } catch (Exception e) {
    
    if (errorMessage == null) {
    
    errorMessage = "Error while getting the manyfacturer list";
    
    }
    
    WarrantyUtils.logError("Inside ServiceImpl :: Inside getManufacturerList ::", e.getMessage(), e);
    
    e.printStackTrace();
    
    throw e;
    
    }
    
    WarrantyUtils.logInfo("Inside getManufacturerList ::", "Ends");
    
    return manufacturerList;
    
    }
    
    
    
    public String getError() {
    
    String error = errorMessage;
    
    //clear the error message
    
    errorMessage = null;
    
    return error;
    
    }
    
    
    
    /**
    
    *
    
    * @return
    
    * @throws Exception
    
    */
    
    public RestServiceAdapter getRestServiceAdapter() throws Exception {
    
    WarrantyUtils.logInfo("Inside getRestServiceAdapter ::", "Starts");
    
    try {
    
    
    
    RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
    
    // Clear any previously set request properties, if any
    
    restServiceAdapter.clearRequestProperties();
    
    // Set the connection name
    
    restServiceAdapter.setConnectionName("MCSConn");
    
    WarrantyUtils.logInfo("Inside getRestServiceAdapter ::", "Ends");
    
    return restServiceAdapter;
    
    } catch (Exception e) {
    
    errorMessage = "Unable to connect to the Host";
    
    e.printStackTrace();
    
    WarrantyUtils.logError("Inside ServiceImpl :: Inside getRestServiceAdapter ::", e.getMessage(), e);
    
    throw e;
    
    }
    
    
    
    }}
    
    
    
    

     

    You can get the Authorization code through postman extension of chrome.

     

    Enters Username, password and the API's URL and click update Request

     

    024Postman.jpg025Postman.jpg

     

    Authorization code will be updated. Once click the send button will get the response below.

     

    026Postman.jpg

     

    Create amx page in MAF application and drag the manufacturers as list item from the generated Data Control from the LHS.

     

    027MAF.jpg

    Deploy the code and view the application in emulator.

      028A.jpg028MAF.jpg

     

     

     

    About the Author

     

    Namit Kakkar is a Principal Consultant in Oracle Consulting Services-Global Service Delivery, where his focus is on ADF, Webcenter, Oracle mobility and cloud solutions.