Sunday, 27 July 2014

Basic web services part 1

Web Service :- Every project consists of web services as a part of it . Its a way to explore the functionalities provide by any organization. We can make some logic reusable and expose them via standard xml so any underlying programming language can use it.

Mediator :- Now we need a xml technology who does the mediation process so that any technology can understand it. Such technology is WSDL.

WSDL : -  Web service description language is used as intermediate layer to show the methods provided as service. For example third party gateway as a service. Now we will see how to write services in wsdl and structure them.

Basic structure of wsdl  :-
<definitions> 
<types>
   What all data types you are using as your input and output parameters .For example think them as class level data type variables.
</types>

<message>
   define your input parameter as wrapper by grouping above data types. You can define your output parameters also.
</message>

<portType>
      <operation>
 You can have multiple operations by grouping input and output parameters
      </operation>
</portType>

<binding>
    Here you will define how your operations will communicate via wired networks either soap or http protocol
</binding>

<service>
   Here you will define the url to reach your service i mean the end point has to be defined. You can name your service here
</service>

</definitions>

Usecase :- Think we have to provide a service which passes location name as input we have to provide temperature back to them. We will start constructing WSDl for it

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="Weather" targetNamespace="http://weather.service.sample/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://weather.service.sample/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types> //Provide all inputs
<xsd:schema targetNamespace="http://weather.service.sample/">
<xsd:element name="inputCity">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<xsd:schema targetNamespace="http://weather.service.sample/">
<xsd:element name="outPutTemp">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="TemperatureRequest"> // input wrapper
<wsdl:part element="tns:inputCity" name="parameters" />
</wsdl:message>
<wsdl:message name="TemperatureResponse"> //output wrapper
<wsdl:part element="tns:outPutTemp" name="parameters" />
</wsdl:message>
<wsdl:portType name="Weather"> //combination operation
<wsdl:operation name="QuerryTemperature">
<wsdl:input message="tns:TemperatureRequest" />
<wsdl:output message="tns:TemperatureResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="WeatherSOAP" type="tns:Weather"> //binding to a operation
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="QuerryTemperature">
<soap:operation soapAction="" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Weather"> //Service definition
<wsdl:port binding="tns:WeatherSOAP" name="WeatherSOAP">
<soap:address location="http://localhost:9080/PracticseWebService/Weather" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

Note :- Will see approach in second part

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...