Tutorials IBM Worklight How to create a HTTP adapter in IBM Worlight

How to create a HTTP adapter in IBM Worlight

This tutorial explains how to create a HTTP adapter in IBM Worlight and easy steps to deploy on Worklight Server. This example reads feeds from stacktips.com using HTTP Adapter.

1. What is an IBM Worklight Adapter?

Adapter is a mediator between mobile applications and enterprise system. Adapters provide an easy and secure access to enterprise system. And it is efficient to provide enterprise data to mobile devices in a uniform data format. The data can be presented to mobile device using various interchange formats like XML or JSON (used mostly)

IBM Worklight provides three different types of adapters

  1. HTTP adapter: HTTP adapters provide access to HTTP-based enterprise services, including RESTful and SOAP-based services.
  2. SQL adapter: SQL adapters provide access to enterprise databases.
  3. Cast IRON adapter: Cast Iron adapters initiate orchestrations in IBM Web Sphere Cast Iron.

Adapters are coded in JavaScript and it runs at server-side on the IBM Worklight mobile application platform. IBM internally uses Rhino JavaScript engine for executing the JavaScript source code.

2. How to create HTTP adapter?

2.1 Architecture of HTTP Adapter

Adapters are developed using JavaScript and always runs on the IBM Worklight server. When the mobile application makes request to the HTTP adapter, the HTTP adapter sends the web request to the back end web services. As a response the web service returns the response either in json or xml format, and the adapters converts the response to JSON format and sends it to the requested client application.

In my example, I am witting an adapter to fetch feeds from stacktips.com. To fetch the feeds I have to connect to http://stacktips.com/feed/

2.2 Now let us move to create a HTTP Adapter

1. Start Eclipse, and create a new Worklight project.

2. Right-click on the newly created Worklight project and then, New-> Worklight Adapter. You require giving the project name, adapter name and choosing HTTP adapter type.

3. Go to the adapter’s folder and then open the newly created adapter xml file. This XML contains the configuration required to connect to the REST Web service from adapter.

4. Change the connection parameters like protocol, domain name and port. See my connection parameter in this example. My xml file after changes

<wl:adapter xmlns:wl="http://www.worklight.com/integration" 
xmlns:http="http://www.worklight.com/integration/http" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
name="JavatechigFeedReader">
   <displayName>stacktips.com</displayName>
   <description>Javatechig FeedReader</description>
   <connectivity>
          <connectionPolicy xsi:type="http:HTTPConnectionPolicyType">
              <protocol>http</protocol>
              <domain>stacktips.com</domain>
              <port>80</port>
          </connectionPolicy>
          <loadConstraints maxConcurrentConnectionsPerNode="2"/>
   </connectivity>

<procedure name="getFeeds"/>
</wl:adapter>

I have changed domain name to “stacktips.com”

The xml file lists the procedure declaration for the adapters. It declares all the procedures that the adapter implements. The adapter implementation resides on the JavaScript impl file.

5. Now let us implement the procedure.

function getFeeds() {	
	var input = {
	    method : 'get',
	    returnedContentType : 'xml',
	    path : "feed",
	    transformation : {
		    type : 'xslFile',
		    xslFile : 'filtered.xsl'
	    }
	};

	return WL.Server.invokeHttp(input);
}

3.  Testing Adapter Procedures?

Right click on the adapter folder -> run as -> Deploy as Worklight adapter. Once deployed you can see it from the admin console. Open your browser and hit http://localhost:8080/console/

Now, RunAs-> Invoke Worklight procedure. Select the procedure to test. For now you can test for getFeeds, results will appear as JSON data.

4. How to invoke worklight Adapter Procedure?

Refer my earlier post  “Invoking Adapter procedure in IBM Worklight”

5. References

http://pic.dhe.ibm.com/infocenter/wrklight

 

Total
0
Shares
11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
akb48 通販
akb48 通販
9 years ago

I’m learning from you. I certainly liked reading all that is written on your site.Keep the tips coming. I liked it!

Unendoro
Unendoro
9 years ago

am getting the following error “Runtime: Http request failed: java.net.UnknownHostException: stacktips.com” when i invoke the procedure. I have a proxy server could it be the cause? i think so because i think am unable to connect to the internet. Do i configure it in the HTTP Adapter?

user
user
9 years ago
Reply to  Unendoro

Your back-end application must be accessed through a proxy. You can configure a proxy during configuration in xml.

<connectionPolicy xsi:type="http:HTTPConnectionPolicyType"
cookiePolicy="cookie-policy" maxRedirects="int">
<protocol>protocol</protocol>
<domain>host-name</domain>
<port>host-port</port>
<sslCertificateAlias>ssl-certificate-alias</sslCertificateAlias>
<sslCertificatePassword>ssl-certificate-password</sslCertificatePassword>
<authentication> … </authentication>
<proxy> … </proxy>
</connectionPolicy>

Unendoro
Unendoro
9 years ago
Reply to  user

Thanks a lot, its working now. How can i show my gratitude?

user
user
9 years ago
Reply to  Unendoro

We are happy that it helped you.

Unendoro
Unendoro
9 years ago

i am failing to load the div the RSS reults into a UI control (div). I can invoke the procedure and get results but need to show them in a div.

Cody
Cody
9 years ago

How can I access rss feeds that are on protected sites? For example, the IBM Greenhouse Connections environment?

javatechig
javatechig
9 years ago
Reply to  Cody

I am not very sure. But as I understand you cannot access RSS from protected sites

Cody
Cody
9 years ago
Reply to  javatechig

Thanks for the reply! That is sad to hear, quite disappointing to say the least. Do you think that is the case even if you have valid credentials for the protected site? For example, anyone can sign up to the IBM Greenhouse and make credentials. Is there a way to use those credentials when connecting the http adapter?

Thanks again for everything!

kbm718
kbm718
9 years ago

When I attempt do Test my Adapter Procedure I get this error.

[ERROR ] FWLSE0099E: An error occurred while invoking procedure [project MobileApp]RSSReader/HttpRequestFWLSE0100E: parameters: [project MobileApp]{
“arr”: [
{
“method”: “get”,
“path”: “feed”,
“returnedContentType”: “xml”,
“transformation”: {
“type”: “xslFile”,
xslFile”: “filtered.xsl”
}
}
]
}
Http request failed: org.apache.http.conn.HttpHostConnectException: Connection to http://stacktips.com:80 refused
FWLSE0101E: Caused by: [project MobileApp]org.apache.http.conn.HttpHostConnectException: Connection to http://stacktips.com:80 refused
com.worklight.common.log.filters.ErrorFilter

Can you help me figure this out?

boris
boris
6 years ago

Please i am working with mobilefirst in one project which needs http adapter. My adapter give a good answers when in run it in to mobilefirst server,but when i create an android environement and deside to run the same project in my physic android device (my android phone),this return error. I try to get solution without reponse please help me thank you.

Previous Post

Invoking Adapter procedure in IBM Worklight

Next Post
pass a data from one Activity to another in Android

How to Pass a Data from One Activity to Another in Android

By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All