Webservices Workshop - september 2014

34
September 2014 – Webservices Workshop By Avinash Ramineni

Transcript of Webservices Workshop - september 2014

September 2014 – Webservices Workshop

ByAvinash Ramineni

Workshop Objectives

• During this workshop, you will learn– XML– JSON– About Web services, SOA• SOAP• REST

– Security with Services– Invoke and Testing web services– Best Practices while dealing with Web services

System Setup

• SOAP UI• Oxygen XML Editor• Advanced Rest Client Plugin for Chrome

XML – Extensible Markup Language -1

• Designed to Describe Data

– Self Descriptive way representing the Data and Documents

• Platform agnostic Data representation for transferring data

• Easy to understand and follow

• Easy to convert to other formats

– HTML ,PDF , Word

XML – Example<?xml version="1.0" encoding="UTF-8"?> <StudentTranscript> <ProgramName> Masters in Business Adminstration </ProgramName>

<Courses> <Course> <CourseCode>MBA/510</CourseCode> <CourseCompletionDate> 8/8/1900</CourseCompletionDate> <Credits>4</Credits> <CourseGrade>A</CourseGrade> </Course> <Course> <CourseCode>MBA/520</CourseCode> <CourseCompletionDate> 8/8/1910</CourseCompletionDate> <Credits>4</Credits> <CourseGrade>B</CourseGrade> </Course>

<Courses> </StudentTranscript>

XML – Extensible Markup Language -2• XML Document

– Elements

– Attributes

• Exactly one root element

– Parent of all the elements

– Forms a tree structure

• Start Tag and End Tag

– XML Tags are not predefined - Makeup your own tags

• CDATA – No Unescaped < or & characters in the data

• Well Formed XML

– All Elements need to have a closing tag

– XML tags are case-sensitive

– XML tags must be properly nested

– All attributes need to be enclosed in quotations

– Elements cannot have attributes of the same name

– Elements can be empty <studentTranscript/>

XML – Example

<?xml version="1.0" encoding="UTF-8"?> <StudentTranscript> <ProgramName> Masters in Business Adminstration </ProgramName>

<Courses> <Course code=“MBA/510” completionDate=“8/8/1900” credits=“4” grade=“A”/> <Course code=“MBA/520” completionDate=“8/8/1920” credits=“4” grade=“B”/>

<Courses> </StudentTranscript>

What is the Difference ?

Hands-On Exercise 1

• Build an XML file to represent Student Profile Data

Namespace• Element Name Conflicts

– What if same element name needs to be under different entities ? • Trying to combine data from two different xmls

– Ex: description of a course and description of a program

• Use namespaces– Similar to packages – separates the elements with unique prefixes.

Namespace <Program>

<Name>MBA V1</Name><description>Master of Business Administration </description>

</Program>

<Course><Name>MBA/510</Name><description>Marketing 101</description>

</Course>

We would want to combine them or using it together is student transcription xml

<p:Program xmlns:p=“http://program”><p:Name>MBA V1</p:Name><p:description>Master of Business Administration </p:description>

</p:Program>

<c:Course xmlns:c=“http://course”><c:Name>MBA/510</c:Name><c:description>Marketing 101</c:description>

</c:Course>

XSLT and XML Schema

• XML Schema– Describes the structure of an xml Document

• Supports data types

• Put restrictions on possible values

– Written in XML

– Validate that the XML is Valid• Valid Vs Wellformed

• XSLT – extensible stylesheet language transformation– Uses XPATH

– Outputs a file – another html,xml pdf …

Web service• “Web Services are self-contained, modular, distributed, dynamic applications that can be described, published,

located, or invoked over the network to create products, processes, and supply chains. These applications can be local, distributed, or Web-based. Web services are built on top of open standards such as TCP/IP, HTTP, Java, HTML, and XML. “

• Services offered over the web ?– A modular component that does a specific functionality– The functionality is exposed out which can be invoked by other services or

applications.– Input / Output is XML

• Ex: Profile Service ?– Retrieves all the demographics data given a profile ID– Ability to search for a student – Ability to create a new profile for an user– Ability to update address for an user

• How do we know what Input to be passed and what Output to expect ?

Why use XML here?

• Can .NET invoke Java logic ?

• What is so unique about XML , that we have to use it here?

– Not tied to a specific programming language

– Any programming language that can parse XML can use the services

– Interoperability

• Transport Protocol

– HTTP, FTP,SMTP, JMS

SOAP - 1

• Simple Object Access Protocol• XML based protocol for accessing web services• Platform Independent• Language Independent• Extensible• Transport Protocol– HTTP , HTTPS, SMTP ..

SOAP - 2

• SOAP Payload is nothing but XML• SOAP Payload– Soap Envelope : Root Element • SOAP Header• SOAP Body• SOAP Fault

• All elements are declared in default namespace

SOAP Example 1<?xml version="1.0"?><soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"> <soap:Body> <m:GetStockQuote xmlns:m="http://nyse.com/ticker"> <m:Item>AAPL</m:Item> </m:GetStockQuote> </soap:Body></soap:Envelope>

<?xml version="1.0"?><soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope"> <soap:Body> <m:GetStockQuoteResponse xmlns:m="http://www.w3schools.com/prices"> <m:Price>500</m:Price> </m:GetStockQuoteResponse> </soap:Body></soap:Envelope>

SOAP Fault

• Inside SOAP Body• Fault Code– Code for Identifying the Fault• Server• Client

• Fault String– Explanation of the Fault

• Detail– Application Specific Error

SOAP Fault Example <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <SOAP-ENV:Fault> <faultcode>SOAP-ENV:Client</faultcode> <faultstring>Message does not have necessary info</faultstring> <faultactor>http://gizmos.com/order</faultactor> <detail> <PO:order xmlns:PO="http://gizmos.com/orders/"> Quantity element does not have a value</PO:order> <PO:confirmation xmlns:PO="http://gizmos.com/confirm"> Incomplete address: no zip code</PO:confirmation> </detail> </SOAP-ENV:Fault> </SOAP-ENV:Body></SOAP-ENV:Envelope>

WSDL

• Web Services Description Language• XML-based language for describing Web

services.• How do we know what Input to be passed and

what Output to expect ? – Input and Output for a web service are described

using a WSDL• Can there be a SOAP Service without WSDL ?

Hands-On Exercise 2

• Invoke a few SOAP Webservices

Review HTTP

• Hyper Text Transfer Protocol– Request Headers– Request Body

• HTTP VERBs– GET– POST– PUT– DELETE

• HTTP Headers– Content-type – Accept – Cookie – Authorization

HTTP Response Codes• 1xx Informational

– 100 - Continue• 2xx Success

– 200 – OK– 201 – Created– 202 – Accepted

• 3xx Redirection– 301 – Moved Permanently– 302 - Found– 307 - Temporary Redirect

• 4xx Client Error– 400 – Bad Request– 401 – Unauthorized– 402 - Payment Required– 403 – Forbidden– 404 – Not Found– 405 – Method Not Allowed– 409 - Edit Conflict

• 5xx Server Error– 500 Internal Server Error– 502 Bad Gateway

What is REST ?

• Representational State Transfer– Architectural Style for developing web services– Based on Doctoral Thesis from Roy Fielding– REST is not a Standard

• Rest web services communicate over HTTP Services – HTTP VERBS - GET , POST, PUT,DELETE– Resources addressed through URI– Media Types - – Http Response Codes

Characteristics of REST

• Uniform Interface• Client-Server Decoupling• Stateless• Cacheable• Layered System

HTTP – GET Examples• Query Parameter

– http://hostname/student?studentId=122– http://hostname/student?studentId=122,111,111– http://hostname/student/address?studentId=122

• Matrix parameter– http://hostname/student/studentId=122;– http://hostname/student;name=122;zipcode=85286;

• URI parameter– http://hostname/student/1– http://hostname/student/Name/avi/zipcode/85286– http://hostname/student/1/address

• Header Parameters

Hands-On Exercise 3

• Invoke a REST Service

PUT,POST,DELETE

• POST – Submits information to the service for processing– Should typically return the new or modified resource.– http://localhost/student

• Payload is passed in body and Id is part of the payload

• PUT – Add a new resource at the request URL– http://localhost/student/{id}

• Payload is passed in body

• DELETE – Removes the resource at the request URL– http://locathost/student/{id}

• OPTIONS – Indicates which methods are supported• HEAD – Returns meta information about the request URL

JSON• JavaScript Object Notation

• XML

<COURSE><ID>MBA/510</ID><NAME>Marketing</NAME>

</COURSE>

• JSON

{ course :{ id: MBA/510 , name: “marketing”}

}

JSON Example• {

"StudentTranscript":{ "ProgramName":"Masters in Business Adminstration", "Courses":{ "Course":[ { "CourseCode":"MBA/510", "CourseCompletionDate":"8/8/1900", "Credits":4, "CourseGrade":"A" }, { "CourseCode":"\" <MBA/520 \"", "CourseCompletionDate":"8/8/1910", "Credits":4, "CourseGrade":"B" } ] } }}

Hands-On Exercise 4

• Build a JSON file to represent Student Profile Data

• Compare the XML file and JSON• What is the difference ? • What do you prefer ?

Security

• SOAP– Https– WS-Security– Http Headers

• Rest– Https– Authorization Token

Transactions

• ACID– Atomicity– Consistency– Isolation– Durability

SOA• Principle and Practices for designing shared , reusable ,

distributed services

– Loose Coupling

– Self Describing Interfaces

– Synchronous and Asynchronous

– Service Registry

– Quality of Service

– Service Aggregation and service orchestration

SAAS,PAAS,IAAS• PLATFORM

– Thing of this LEGO blocks using which you can build any application

• Software As a Service

• Platform AS a Service

• Infrastructure As a Service