After creating a successful environment for web services in Eclipse, it’s time to take things to another level by writing a service that actually does something. Here, we create a web service (still a simple one) which takes the salaries of employees, including allowances, and calculates their taxes. The client sends an XML document and receives the same document back except the taxes are now filled in. Later in our SOA course, we’ll see how these documents can be encrypted and signed to ensure integrity and privacy. Anyway, let’s get on with the web service.
First, follow the tutorial in which we set up the environment for web services in eclipse. In that tutorial, you write a class on the service provider side that has two function – getNums and getNums2. In that step, create a class file with one function. The code follows:
package edu.serg.samples; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class ProcessTax { public Document processTax(Document employees) { try { NodeList nl = employees.getFirstChild().getChildNodes(); for (int i = 0; i \< nl.getLength(); i++) calculateTax(employees, nl.item(i)); } catch (Exception e) { } return employees; } private void calculateTax(Document emps, Node emp) { try { if (emp == null) return; NodeList nl = emp.getChildNodes(); if (nl == null) return; float total = 0; for (int i = 0; i \< nl.getLength(); i++) { Node nn = nl.item(i); Node nc = nn.getFirstChild(); if (nn != null) { if (nn.getNodeName().equals("basic") || nn.getNodeName().equals("allowance")) total += Integer.parseInt(nc.getNodeValue()); else if (nn.getNodeName().equals("tax")) { if (nc != null) nc.setNodeValue(Float.toString(total \* 12 / 100)); else { nc = emps.createTextNode(Float .toString(total \* 12 / 100)); nn.appendChild(nc); } } } } } catch (Exception e) { e.printStackTrace(); } } }
Now, follow the rest of the tutorial and create a client. At the very end of the tutorial, you create a client SOATestClient. Here, we create a TaxClient:
package edu.serg.samples; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class TaxClient { /\*\* \* @param args \*/ public static void main(String[] args) throws Exception { // create the source document from file DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse("src/edu/serg/samples/employees.xml"); doc.normalize(); // call the web service to process the file ProcessTaxSoapBindingStub srv = new ProcessTaxSoapBindingStub(new URL( "http://localhost:14593/TaxApp/services/ProcessTax"), new ProcessTaxServiceLocator()); Document res = srv.processTax(doc); // output the new file DOMSource src = new DOMSource(res); StreamResult sr = new StreamResult(System.out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(src, sr); } }
This code may look daunting if you’ve only been working with DOM for a week but you need to figure out what the different classes do. The rest is pretty straight forward. Note that you may need to change the xml file path and end point (URL) of your web service on your system. These settings are for my system.
The employees.xml file which we’ve used in the code above is this:
\<?xml version="1.0" encoding="UTF-8"?\> \<employees\> \<employee\> \<name\>employee 1\</name\> \<basic\>100\</basic\> \<allowance\>10\</allowance\> \<tax\>\</tax\> \</employee\> \<employee\> \<name\>employee 1\</name\> \<basic\>200\</basic\> \<allowance\>20\</allowance\> \<tax\>\</tax\> \</employee\> \<employee\> \<name\>employee 1\</name\> \<basic\>300\</basic\> \<allowance\>30\</allowance\> \<tax\>\</tax\> \</employee\> \</employees\>