Search code examples
ibm-mobilefirstmobilefirst-adaptersmobilefirst-server

PUT HTTP Adapter in MobileFirst Platform


I am trying to post some data from my native android application.

Native Code:

WLProcedureInvocationData invocationData = new WLProcedureInvocationData("TaskAdapter", "updateTask");

int taskId = Integer.parseInt(tvTaskId.getText().toString());

String assignedTo = tvAssignedTo.getText().toString();

String address = "";

String description = "";

String latitude = "5.0";

String longitude = "5.0";

String status = "5.0";
String comments = "5.0";
String lastupdate = "5.0";
String userLatitude = "5.0";
String userLongitude = "5.0";
String userLocation = "5.0";
String photoData = "5.0";

Object[] parameters = new Object[]{
    taskId,
    assignedTo,
    description,
    address,
    latitude,
    longitude,
    status,
    comments,
    lastupdate,
    userLatitude,
    userLongitude,
    userLocation,
    photoData
};

invocationData.setParameters(parameters);

WLRequestOptions options = new WLRequestOptions();
options.setTimeout(30000);


client.getInstance().invokeProcedure(invocationData, new MyInvokeListener(), options);

Adapter Code:

function updateTask(id) {   
    var input = {
        method : 'PUT',
        returnedContentType : 'json',
        path : '/Api/Task?taskid=' + id
    };


    return WL.Server.invokeHttp(input);
}

Adapter XML:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed Materials - Property of IBM
5725-I43 (C) Copyright IBM Corp. 2011, 2013. All Rights Reserved.
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
-->
<wl:adapter name="TaskAdapter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:wl="http://www.ibm.com/mfp/integration" xmlns:http="http://www.ibm.com/mfp/integration/http">

    <displayName>TaskAdapter</displayName>
    <description>TaskAdapter</description>
    <connectivity>
        <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
            <protocol>http</protocol>
            <domain>testmeternative.vdot.virginia.gov</domain>
            <port>80</port>
            <connectionTimeoutInMilliseconds>30000</connectionTimeoutInMilliseconds>
            <socketTimeoutInMilliseconds>30000</socketTimeoutInMilliseconds>
            <maxConcurrentConnectionsPerNode>50</maxConcurrentConnectionsPerNode>

            <!-- Following properties used by adapter's key manager for choosing specific 
                certificate from key store <sslCertificateAlias></sslCertificateAlias> <sslCertificatePassword></sslCertificatePassword> -->
        </connectionPolicy>
    </connectivity>

    <procedure name="getAllTasks" />
    <procedure name="updateTask" />


</wl:adapter>

I am not sure whether I am sending body in the right way. Moreover, how do I send the id (parameter) to the adapter function.

When I click Call Mobile First Adapter in eclipse, it is showing me the procedure name but the REST call type as GET in the drop down, I want it as PUT.


Solution

  • You will need to update your adapter code as follow:

    function updateTask(id, assignedTo, description, address, latitude, longitude,
            status, comments, lastupdate, userLatitude, userLongitude,
            userLocation, photoData) {
    
        var data = {
            "assignedTo" : assignedTo,
            "description" : description,
            "address" : address,
            "latitude" : latitude,
            "longitude" : longitude,
            "status" : status,
            "comments" : comments,
            "lastupdate" : lastupdate,
            "userLatitude" : userLongitude,
            "userLocation" : userLocation,
            "photoData" : photoData
        };
    
        var input = {
            method : 'PUT',
            returnedContentType : 'json',
            path : '/Api/Task?taskid=' + id,
            body : {
                contentType : 'application/json',
                content : data
            }
        };
    
        return WL.Server.invokeHttp(input);
    }
    

    Since you are passing the values to the adapter via invocationData.setParameters(parameters); in your native code that means the adapter will take that same number of parameters in the same order.

    I created an object data that will contain all these values except the id or taskId since you are passing it as a query param. Then I'm assuming your back-end services accepts a Content-Type of application/json, you can change the content-type if need be.