Services by Example: Web Services Composition

When I am reading about mashups, I am getting often an impression that REST folks are trying to take over this term and restrict it mainly to REST based services. Equally at the same time in the Web Services community we got used to hear about Web Services composition, but not really about Web Services mashups (although there are of course exceptions). While both terms are not really equivalent, there are some overlaps and basically there is nothing preventing us from using Web Services to provide appealing mashups for Web applications. At seekda we strongly support the idea that Web APIs should be based not on one, but on different technologies. We can successfully mashup Web Services and REST services together to compose good Web applications so in our opinion the discussion about superiority of one technology over the other is quite pointless. The user is important and a new functionality we can provide to him or her; not really the technology or terminology, which have been used by particular communities. In this new series of tutorials, which I will call “Services by Example”, I will be mashing up (composing) various Web APIs to show how in a couple of simple steps to deliver mashups, which could not be produced, when using standard software libraries. Today I will show how to make a useful application composed of Web Services coming from two different service providers.

Teddy BearsIt is my our next step towards demystifying Web Services and yet another very simple, but practical example which you can grab and use on your own portal. In this new scenario I am running a small e-commerce shop. My shop is selling online the most beautiful Teddy Bears in the world. While I was setting my e-commerce shop I was expecting my customers to come mainly from USA so I decided to set the price of my Teddy Bears in USD. Analyzing trends of recent Web traffic to my site, I have realized that actually many of my customers come not only from USA, but also from India and Euro zone countries. Why not then geographically identify my visitors and convert on the fly the price of Teddy Bears to the currency of my visitors? To implement this scenario I will compose (mashup) two Web Services – first I want to use the geo location service to identify the country of my customer and next the currency conversion service to get the most recent currency exchange rate for the currency used in the country of my customer.

For the currency conversion, I will use the currency converter Web Service by webservices.com, which I have already used in my previous tutorial. To locate second service for my Web Services Composition example I will then use seekda Web Services search engine to locate the appropriate Web Service allowing to geographically locate my customers. I use keyword based search at seekda for a service, which does geo ip resolution. Following the previous link I have found a promising service in the first five returned results from cdyne. By using Web Services Tester I can find out that this service does exactly what I need (please be aware that the test version of the service is having restricted developers access only, so while you will be trying this service from seekda Web Services Tester, the IP address of seekda might not be allowed any more for testing). seekda Web Services quality control mechanism assures me that the service has almost 100 percent availability. You have to remember that this particular Web Service is provided for free just for testing, so in case you would like to use it professionally on your own portal you would have to become familiar with terms and conditions of the service and obtain a license key from cdyne.

I use exactly the same set of components for this Web Services composition tutorial, as for previous ones:

  1. Java Development Kit. Download JDK 6 from Sun download page.
  2. Tomcat 6.x. Find it at Apache Tomcat download page.
  3. Eclipse. Please use Eclipse IDE for Java EE Developers (or at least add Web Tools (WTP) to your existing Eclipse installation). You will find a complete download for eclipse at Eclipse download page.

It is very important to remember that when you run your test Web Services composition application on the local instance of an application server (Tomcat) than your own IP address is resolved to 127.0.0.1, which is a localhost. If you want then to test your application for your own location, you have to either deploy it on the server, which is available from the public Internet or to read to the end of the tutorial, where I show how to test this code on your local environment.

First, I create an eclipse project:

  1. Start Eclipse
  2. Select Window > Open Perspective > Other > Java EE
  3. Select File > New > Other > Web > Dynamic Web Project
  4. Give a name to your project (I use “my e-commerce shop“) and click Finish.

Next I create a skeleton of JSP page:

  1. Select project “my e-commerce shop” in the Project Explorer
  2. Click the right button of your mouse on the project name and from the context menu select New > JSP
  3. Give a new name to your page. I call my page “product

I will now use some usual html tags to inform my visitors that I am selling Teddy Bears. I will also define java variables to hold price (line 12) and currency type (line 13). Additionally I will use values from these two variables to display price and currency to my users (line 15).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Buy Teddy Bear</title>
	</head>
	<body>
		<h1>Buy the greatest Teddy Bear in the world!</h1>
		<%
		  	double price = 10;
			String currency = "USD";
		%>	
		<p>Our greatest Bear will cost you only <%=price%> <%=currency %> 
   		<input type="submit" value="Buy Teddy Bear Now!"></p>
	</body>
</html>

Now it is time for eclipse to generate all necessary stubs and interfaces for two of Web Services, which I will be composing in this Web Services composition example:

  1. Select project “my e-commerce shop” in the Project Explorer.
  2. Select New > Other… > Web Services > Web Services Client
  3. Paste into Service Definition box the URL of the WSDL currency converter service, which you found at seekda: http://www.webservicex.com/CurrencyConvertor.asmx?wsdl
  4. Make sure that the client type is set to Java Proxy and that you Develop Client (move the slider to the bottom).
  5. Click Finish

Repeat the same steps for cdyne ip2geo Web Service; in the third step paste cdyne Web Service WSDL definition, which you found at seekda: http://ws.cdyne.com/ip2geo/ip2geo.asmx?wsdl

In my next step I will connect cdyne ip2geo Web Service to my Web Services composition application. From the request object I can obtain IP address of the customer (line 17); create a proxy to cdyne ip2geo Web Service (line 18); call the service by passing two arguments - one argument is an IP address and the second argument is a license key, which is set for testing purposes to 0 (line 19); and finally from the returned object I obtain the name of the country (line 20). Do not forget to make all the necessary imports of classes you plan to use for your jsp page (line 4 and 5).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.cdyne.ws.IP2Geo.IP2GeoSoapProxy"%>
<%@page import="com.cdyne.ws.IP2Geo.IPInformation"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Buy Teddy Bear</title>
	</head>
	<body>
		<h1>Buy the greatest Teddy Bear in the world!</h1>
		<%
		  	double price = 10;
			String currency = "USD";
 
			String clientIP = request.getRemoteAddr();
			IP2GeoSoapProxy geoProxy = new IP2GeoSoapProxy();
			IPInformation ipInformation = geoProxy.resolveIP(clientIP, "0");
			String country = ipInformation.getCountry();
		%>
 
		<p>Our greatest Bear will cost you only <%=price%> <%=currency %> 
   		<input type="submit" value="Buy Teddy Bear Now!"></p>
	</body>
</html>

And now the final step for our Web Services composition example in which I connect the currency conversion Web Service from webservicex. I must create an array of countries, which are currently using Euro as a currency (line 18-22); I create the CurrencyConvertorSoapProxy object and set it to null (line 29); I check if the country of origin of my customer is either India (line 30) or any of Euro countries (line 35). If any of these two conditions is true, than I instantiate CurrencyConvertorSoapProxy (lines 31 or 38); obtain the current conversion rate from the Web Service (lines 32 or 39); calculate new value for price by multiplying old price by currency conversion rate (lines 33 or 40) and finally change the type of currency respectively to INR or EUR (lines 34 or 41)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.cdyne.ws.IP2Geo.IP2GeoSoapProxy"%>
<%@page import="com.cdyne.ws.IP2Geo.IPInformation"%>
<%@page import="NET.webserviceX.www.CurrencyConvertorSoapProxy"%>
<%@page import="NET.webserviceX.www.Currency"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Buy Teddy Bear</title>
	</head>
	<body>
		<h1>Buy the greatest Teddy Bear in the world!</h1>
		<%
		  	double price = 10;
			String currency = "USD";
			String euroCountries[] = new String[]{"Andorra", "Austria", "Belgium",
						"Cyprus", "Finland", "France", "Germany", "Greece", "Ireland", 
						"Italy", "Luxembourg", "Malta", "Monaco", "Montenegro", 
						"Netherlands", "Portugal", "San Marino", "Slovenia", 
						"Spain", "Vatican City", "Cyprus", "Malta"};
 
			String clientIP = request.getRemoteAddr();
			IP2GeoSoapProxy geoProxy = new IP2GeoSoapProxy();
			IPInformation ipInformation = geoProxy.resolveIP(clientIP, "0");
			String country = ipInformation.getCountry();
 
			CurrencyConvertorSoapProxy currencyProxy = null;
			if (country.equals("India")) {
				currencyProxy = new CurrencyConvertorSoapProxy();
				double conversionRate = currencyProxy.conversionRate(Currency.USD, Currency.INR);
				price = price*conversionRate;
				currency = "INR";
			} else {
			  	for (String item : euroCountries) {
					if (item.equals(country)) {
						currencyProxy = new CurrencyConvertorSoapProxy();
						double conversionRate = currencyProxy.conversionRate(Currency.USD, Currency.EUR);
						price = price*conversionRate;
						currency = "EUR";
					 }
				 }
			 }
		%>
 
		<p>Our greatest Bear will cost you only <%=price%> <%=currency %> 
   		<input type="submit" value="Buy Teddy Bear Now!"></p>
	</body>
</html>

Next we will run product.jsp page in Tomcat container. Be aware that as long as you test your Web Services composition application in your own browser and run it on a local server, your application does not recognize your external IP address. I explain one section ahead how to test it on a local server

  1. Select project “my e-commerce shop” in the Project Explorer.
  2. Select WebContent > product.jsp.
  3. Click the right button of mouse on the product.jsp page and from the context menu select Run As > Run on Server
  4. Select Apache and next Tomcat v6.0 Server
  5. Click Next >

If you have configured your Tomcat in eclipse environment before the server should start immediately; otherwise you will be asked to point eclipse to the root installation directory of your application server (Tomcat).

To test your Web Services composition example on your local environment you will have to grab some random IP addresses from the Web and substitute line 26 of your code by passing them as an argument.

25
26
		IP2GeoSoapProxy geoProxy = new IP2GeoSoapProxy();
		IPInformation ipInformation = geoProxy.resolveIP("138.232.65.141", "0");

Doing a quick search on the web I found following IPs from different countries, which you can also use for testing Web Services composition example:

  • For India - 164.100.52.200
  • For USA - 72.47.198.32
  • For Austria - 138.232.65.141
  • For Germany - 87.230.100.58

That’s basically the end and you can see below the effect of Web Services composition (this result would be obtained for a customer coming from India).
Web Services Composition - final result

I showed in this short Web Services composition tutorial that with a minimal effort you can easily compose (mashup) Web Services and quickly provide an appealing functionality for your websites. If you develop professional e-commerce applications you should remember that there are many other aspects you must take into consideration when using public services. For example, although we found out that the monitoring data collected by seekda indicated almost 100 percent availability for cdyne Web Service, we have to remember that anything can happen in the future. The service might at some stage fail, change interface or in the worst scenario even disappear (all of these are inevitable characteristic of Web of services). As a developer of Web applications you must handle all standard exceptions in the code and similarly when you use Web Services, you must consider that failures might happen, so the appropriate recovery mechanism must be put in place. In this Web Services composition example I would simply recommend to do nothing and in case of Web Service failure, you can display the price of the product with the original currency.

« home | Posted by Michal Zaremba as tutorials May 13th, 2008 at 10:56 pm

14 Responses to “Services by Example: Web Services Composition”

  1. < blockquote >< a href=”http://pillspot.org/”>Pillspot.org. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices.Best quality drugs. High quality drugs. Buy drugs online< /a >…

    Buy:Cialis Professional.Viagra Super Active+.Tramadol.Levitra.Super Active ED Pack.Viagra.Cialis Soft Tabs.Soma.Zithromax.Viagra Super Force.Cialis.Viagra Soft Tabs.Propecia.VPXL.Cialis Super Active+.Maxaman.Viagra Professional….

  2. 18 Accessories Cars 118/ http://04FORDPARTS.US/tag/1 : 18 Accessories Cars 118/…

    1…

  3. Inurl http://01DODGEPARTS.US/tag/Eyeliner Inurl : Eyeliner…

    Posting.Php php inurl/…

  4. E http://02JEEPPARTS.US/tag/E : 60 E80 e80/…

    60 E80 e80/…

  5. 18 Accessories Cars 118/ http://04FORDPARTS.US/tag/1 : 18 Accessories Cars 118/…

    1…

  6. 00a RCA DTA/ http://APTAUTOPARTS.INFO/tag/Buy 8 : 8…

    Buy…

  7. electronic http://jmechanicalhl1ngx.ABABYCLOTHES.INFO/tag/electronic mechanical timer/ : electronic…

    mechanical…

Leave a Reply