Sunday, July 21, 2013

Java WebService Tutorial - Part 02

 In the last tutorial we saw , what is web service , what is the use of web services and different jargon's of web service.such as SOAP,WSDL ,UDDI.

In this tutorial we will be seeing in detail about SOAP.

SOAP : 

-  Soap is a XML -Based Protocol for exchanging information between computers.

-  It can be used in variety of Messaging System and can be delivered in variety of Transport   Protocols.

-  It is Platform Independent.

-  Soap can be implemented in any technologies such as Java,.NET,C++,Perl etc.
  
Soap Specification  has "3" Major Parts.

1. Soap Envelope

2.Data Encoding Rules

3. RPC Conventions

1. Soap Envelope : A Soap Envelope contains the specific rules for encapsulating data being transferred between computers.This includes Application-Specific data ,such as Method name to invoke,Method Parameters and return types.


2. Data Encoding Rules: 

To exchange data, computers must agree on rules for encoding specific data types.For example, two computers that process stock quotes need an agreed-upon rule for encoding float data types; likewise, two computers that process multiple stock quotes need an agreed-upon rule for encoding arrays. SOAP therefore includes its own set of conventions for encoding data types. Most of these conventions are based on the W3C XML Schema specification.

3. RPC Conventions :

SOAP can be used in a variety of messaging systems, including one-way and two way messaging. For two-way messaging, SOAP defines a simple convention for representing remote procedure calls and responses. This enables a client application to specify a remote method name, include any number of parameters, and receive a response from the server.

Example :

A simple soap example which has the method "Temperature " and passes the zip code in the request and gets the temperature in Response.

Request :

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getTemp
xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<zipcode xsi:type="xsd:string">10016</zipcode>
</ns1:getTemp>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

In this example couple of things have to be notice :

- The first line is the XML declaration with version and encoding.
- The second line contains mandatory "Soap Envelope"which has the details of all information which we need to pass.
- The Envelope contains different namespace which are required for making a soap request.
- The next line we have Soap body tag which again a mandatory data element.
- Next we have the Method name "Temperature" and parameter zip-code as String.

Note :  We don't exactly need to know every thing happens in soap, but basic understanding is important.  So when we request a service from any language we just access the method and pass the value behind the scenes , request will formed in this manner and send to the service.

Response :

 <SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:getTempResponse
xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<return xsi:type="xsd:float">71.0</return>
</ns1:getTempResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


 As we can see , a response with float return type of Temp is returned.From this Response we have to extract the data. This is how the request and response happens in real world.

But, as i said when we access a web service from  languages such as java,c++ etc we don't really see what is happening in background we formed the request using various web service API's and framework and extract the response by the same way.

 Now we will see about the elements of SOAP Messaging :

A Soap Message basically contains the following elements

1. Envelope - Mandatory
2. Headers - Optional
3. Body - Mandatory
4. Fault - Optional                                 


1. Envelope : 

- It is a root element of SOAP Messages.
- Soap uses XML namespaces to differentiate versions.

Ex:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
The SOAP 1.1 namespace URI is http://schemas.xmlsoap.org/soap/envelope/, whereas the SOAP 1.2 namespace URI is http://www.w3.org/2001/09/soap-envelope.If the Envelope is in any other namespace, it is considered a versioning error. 

2. Headers :

- It is an optional element is Soap Messaging.
- The optional Header element offers a flexible framework for specifying additional
  application-level requirements. 
-  For example, the Header element can be used to specify a  digital signature for password-protected services.
-The Header framework provides an open mechanism for authentication, transaction management, and payment authorization.

It has "2" Main Attributes :

 - Actor
 - Must understand Attribute

Actor attribute:
The SOAP protocol defines a message path as a list of SOAP service nodes. Each of these intermediate nodes can perform some processing and then forward the message to the next node in the chain. By setting the Actor attribute, the client can specify the recipient of the SOAP header.

MustUnderstand attribute :
Indicates whether a Header element is optional or mandatory. If set to true ,  the recipient must understand and process the Header attribute according to its defined semantics, or return a fault. 

SOAP 1.1 uses integer values of 1/0 for the MustUnderstand attribute; SOAP 1.2 uses Boolean values of true/1/false/0.

The Header specifies a payment account, which must be understood and processed by the SOAP server.

 Here is an example Header :
<SOAP-ENV:Header>
<ns1:PaymentAccount xmlns:ns1="urn:ecerami" SOAP-ENV: mustUnderstand="true">
orsenigo473
</ns1:PaymentAccount >
</SOAP-ENV:Header>

3. Body :

The Body element is mandatory for all SOAP messages. As we have already seen, typical
uses of the Body element include RPC requests and responses.

<SOAP-ENV:Body>
<ns1:getTemp
xmlns:ns1="urn:xmethods-Temperature"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<zipcode xsi:type="xsd:string">10016</zipcode>
</ns1:getTemp>
</SOAP-ENV:Body>

4. Fault :

- It is an optional element.
- In the event of an error, the Body element will include a Fault element. 
- The following code is a sample Fault. The client has requested a method named
  ValidateCreditCard , but the service does not support such a method. 

This represents a client request error, and the server returns the following SOAP response:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode xsi:type="xsd:string">SOAP-ENV:Client</faultcode>
<faultstring xsi:type="xsd:string">
Failed to locate method (ValidateCreditCard) in class
(examplesCreditCard) at /usr/local/ActivePerl-5.6/lib/
site_perl/5.6.0/SOAP/Lite.pm line 1555.
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

SOAP Fault codes :

SOAPENV: VersionMismatch
Indicates that the SOAP Envelope element included an invalid namespace, signifying a version mismatch.

SOAPENV:MustUnderstand
Indicates that the recipient is unable to properly process a Header-element with a must-understand attribute set to true. This ensures that must-understand elements are not silently ignored.

SOAP-ENV:Client
Indicates that the client request contained an error. For example,the client has specified a nonexistent method name, or has supplied the incorrect parameters to the method.

SOAP-ENV:Server
Indicates that the server is unable to process the client request. For example, a service providing product data may be unable to connect to the database.

<< Previous                                 Index                                                   Next >>                                                           
                 
                 -------------------------------------------------------------------------------------------------

In the next part , we will see soap encoding and WSDL details.....Please comment your thoughts.


Technology Blogs
blogs

27 comments:

  1. pretty good and very useful to beginners.

    ReplyDelete
  2. Hi,There are more types of websites than you can shake a stick at.The basic types, noting that we will not include all Web Design Cochin and that there are hybrids.Thanks.....

    ReplyDelete
  3. Whats up are using Wordpress for your site platform?
    I'm new to the blog world but I'm trying to get started and set up my own. Do
    you need any html coding knowledge to make your own blog?
    Any help would be really appreciated!

    Also visit my web site Louis Vuitton Outlet

    ReplyDelete
  4. I'm very pleased to find this page. I need to to thank you for ones time for this fantastic
    read!! I definitely appreciated every bit of it and i also have you saved to fav to look at new things on your web site.


    my homepage Replica New Balance

    ReplyDelete
  5. Hi there, just became aware of your blog through Google, and found that it's really
    informative. I'm going to watch out for brussels.
    I will appreciate if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!


    my web-site: Cheap Louis Vuitton Bags

    ReplyDelete
  6. I pay a visit day-to-day some sites and sites to read posts,
    except this web site presents feature based posts.

    Also visit my website ... Christian Louboutin Online

    ReplyDelete
  7. I am genuinely thankful to the owner of this website who has shared
    this great paragraph at here.

    Stop by my webpage :: Christian Louboutin Outlet

    ReplyDelete
  8. Hey, I think your blog might be having browser compatibility issues.
    When I look at your blog site in Opera, it looks
    fine but when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up! Other then that, awesome blog!


    Feel free to visit my blog; Christian Louboutin Shoes

    ReplyDelete
  9. Excellent blog! Do you have any tips for aspiring
    writers? I'm hoping to start my own website soon but I'm a little lost
    on everything. Would you recommend starting
    with a free platform like Wordpress or go for a paid option? There
    are so many choices out there that I'm totally confused ..
    Any recommendations? Thanks a lot!

    Feel free to surf to my webpage :: Christian Louboutin Heels

    ReplyDelete
  10. I love what you guys tend to be up too. Such
    clever work and coverage! Keep up the fantastic works
    guys I've included you guys to our blogroll.



    Here is my site; Discount Christian Louboutin

    ReplyDelete
  11. I read this piece of writing completely about the comparison of
    latest and previous technologies, it's amazing
    article.

    Check out my web-site ... Christian Louboutin Pumps

    ReplyDelete
  12. Hi there I am so excited I found your website, I really found you by error,
    while I was searching on Bing for something else, Anyways I
    am here now and would just like to say kudos for a marvelous post and a all round entertaining blog (I also love the theme/design), I don't have time
    to read through it all at the minute but I have bookmarked it and also
    added in your RSS feeds, so when I have time I will be back to read a great deal
    more, Please do keep up the fantastic job.

    Feel free to visit my blog :: Christian Louboutin Sydney

    ReplyDelete
  13. Wonderful beat ! I would like to apprentice while you amend your website, how can i subscribe for a weblog site?

    The account helped me a acceptable deal. I were a
    little bit familiar of this your broadcast provided brilliant clear concept

    Also visit my weblog; New Balance Shoes (http://www.brumosracing.com/index.php?/member/1282844/)

    ReplyDelete
  14. Thank you for sharing your info. I truly appreciate your efforts and I am waiting for your next post thanks once again.

    My homepage; Running Shoes

    ReplyDelete
  15. What a material of un-ambiguity and preserveness of valuable knowledge about unexpected emotions.



    My homepage: New Balance Shoe

    ReplyDelete
  16. Greetings! I've been following your blog for some time
    now and finally got the courage to go ahead and give you a shout
    out from Humble Tx! Just wanted to tell you keep up the excellent work!


    Here is my weblog - New Balance Running

    ReplyDelete
  17. It's difficult to find experienced people
    in this particular topic, but you sound like
    you know what you're talking about! Thanks

    Visit my web page New Balance Minimus

    ReplyDelete
  18. Very nice article, exactly what I needed.

    Here is my weblog Replica Christian Louboutin

    ReplyDelete
  19. Asking questions are really nice thing if you are not understanding something totally, except
    this post presents fastidious understanding even.

    Look into my site :: Running Shoes

    ReplyDelete
  20. I get pleasure from, result in I found just what I used to be taking a look
    for. You've ended my four day long hunt! God Bless you man. Have
    a nice day. Bye

    My page: Louis Vuitton Discount

    ReplyDelete
  21. Please let me know if you're looking for a writer for your site.
    You have some really good articles and I think I would be a good
    asset. If you ever want to take some of the load off, I'd really like to write some articles for
    your blog in exchange for a link back to mine. Please blast me an e-mail if interested.
    Kudos!

    My site ... Christian Louboutin Heels

    ReplyDelete
  22. hi ,
    this is really nice
    i will like to upload online can you help me by giving the details

    ReplyDelete
  23. Its great. Informative and very helpful. Thanks for sharing.

    ReplyDelete