Unit Iv
Unit Iv
Unit Iv
3. Quora gives us many sign-in options like Google and Facebook apart from its
standard authentication.
a. If we have signed in to our Facebook already, it is not required for us to sign in to
Quora individually. Rather, we can make use of an option called, “Continue with
Facebook”.
b. Here, it is implicitly understood that Quora avails the login service of Facebook.
And, we see a lot of applications work this way in our day-to-day life like LinkedIn
or InfySpringboard.
c. But it doesn't mean that Google and Facebook are compatible with Quora, the
communication happens with the use of Web Services.
SOA - Service Oriented Architecture
SOA principles:
These principles are the driving factors behind SOA, ensuring a high level of abstraction,
reliability, scalability, reusability, and maintainability.
Implementing SOA:
CORBA: Common Object Request Broker Architecture is a standard that
achieves interoperability among the distributed objects. Using CORBA, it
is much possible to make the distributed application components
communicate with each other irrespective of the location (the place where
the components are available), language and platform.
The resource has representations like XML, HTML, and JSON. The current state is captured
by representational resource. When we request a resource, we provide the representation of
the resource. The important methods of HTTP are:
For example, if we want to perform the following actions in the social media application,
we get the corresponding results.
REST defines an architectural approach whereas SOAP poses a restriction on the format of
the XML. XML transfer data between the service provider and service consumer.
Remember that SOAP and REST are not comparable.
SOAP: SOAP acronym for Simple Object Access Protocol. It defines the standard XML
format. It also defines the way of building web services. We use Web Service Definition
Language (WSDL) to define the format of request XML and the response XML.
For example, we have requested to access the Todo application from the Facebook
application. The Facebook application sends an XML request to the Todo application.
Todo application processes the request and generates the XML response and sends back to
the Facebook application.
If we are using SOAP web services, we have to use the structure of SOAP.
1. <Envelop xmlns=?http://schemas.xmlsoap.org/soap/envelop/?>
2. <Body>
3. <getCourseDetailRequest xmlns=?http://udemy.com/course?>
4. <id>course1</id>
5. <getCourseDetailRequest>
6. </Body>
7. </Envelop>
XML Response
1. <SOAP-ENV:Envelope xmlns:SOAP-
ENV=?http://schemas.xmlsoap.org/soap/envelope/?>
2. <SOAP-ENV:Header /> <!?empty header-->
3. <SOAP-ENV:Body> <!?body begin-->
4. <ns2:getCourseDetailsResponse xmlns:ns2=?http://in28mi> <!-- content of the response-
->
5. <ns2:course>
6. <ns2:id>Course1</ns2:id>
7. <ns2:name>Spring<ns2:name>
8. <ns2:description>10 Steps</ns1:description>
9. </ns2:course>
10. </ns2:getCourseDetailResponse>
11. </SOAP-ENV:Body> <!?body end-->
12. </SOAP-ENV:Envelope>
Points to remember
The flow diagram shows how a SOAP client sends a message to a SOAP
processor, which then invokes the appropriate web service and returns the
response to the SOAP client.
Steps:
1. The SOAP client creates a SOAP message containing the request and
sends it to the SOAP processor.
2. The SOAP processor receives the SOAP message and parses it to
determine the requested web service and its parameters.
3. The SOAP processor invokes the requested web service and passes it the
parameters specified in the SOAP message.
4. The web service performs the requested operation and returns a response
to the SOAP processor.
5. The SOAP processor parses the response from the web service and
creates a SOAP message containing the response.
6. The SOAP processor sends the SOAP message containing the response
back to the SOAP client.
7. The SOAP client receives the SOAP message containing the response and
parses it to extract the results.
SOAP Processor:
Web Service:
A web service is a software application that is exposed over the web. It can be
invoked by SOAP clients to perform various operations.
SOAP Message:
XML Schema:
XML Schema is a language that is used to define the structure and data types of
XML documents. It is often used to define the structure of SOAP messages.
HTTP methods:
Example:
Resource URI
Data of an employee http://www.infy.com/employees/john
Web Page http://www.infy.com/about/infosys.html
Functionality that validates a credit
http://www.infy.com/creditcards/1000001
card
Use Plural Noun − Use plural noun to define resources. For example,
we've used users to identify users as a resource.
Avoid using spaces − Use underscore (_) or hyphen (-) when using a
long resource name. For example, use authorized_users instead of
authorized%20users.
Use lowercase letters − Although URI is case-insensitive, it is a good
practice to keep the url in lower case letters only.
Maintain Backward Compatibility − As Web Service is a public
service; a URI once made public should always be available. In case, URI
gets updated, redirect the older URI to a new URI using the HTTP
Status code, 300.
Use HTTP Verb − Always use HTTP Verb like GET, PUT and
DELETE to do the operations on the resource.
However, at present most of the web services are representing resources using
either XML or JSON format. There are plenty of libraries and tools available to
understand, parse, and modify XML and JSON data.
1. {
2. "name": "John",
3. "Level": 5,
4. "address":"Hyderbad",
5. "phoneno":998765432
6. }
1. <employee>
2. <name>John</name>
3. <address>hyderabad</address>
4. <level>5</level>
5. <phoneno>998765432</phoneno>
6. </employee>
This means the server can represent the resource in many ways.
The server will understand which operation has to be invoked based on the
HTTP method that was used to make a call.
This XML will be embedded as "payload" inside the SOAP request envelope
which in turn will be wrapped using HTTP request and then, will be sent using
HTTP POST.
- GET: specifies the HTTP method being used, which is "GET" in this case. A
GET request is used to retrieve data from the specified URL.
- <portno>: It is a placeholder for the port number on which the web service is
listening.
- /GreetingService/greetings: This is the path or endpoint of the web service. It
specifies the location of the resource you want to access on the server. In this
case, it seems to be related to a "GreetingService" and "greetings" resource.
SOAP vs REST
Both SOAP and REST provide support for building applications based on
Service Oriented Architecture.
SOAP-based Web Service RESTful Web Service
SOAP is a protocol (defines the way how REST is an architectural style.
messages have to be sent) which is
transported over a transport layer Protocol,
mostly HTTP.
When REST?
To better understand REST and SOAP, let us take a simple analogy of mailing
a letter.
is light-weight
can be accessed by any other application irrespective of the differences in
terms of language and platform.
leverages HTTP protocol
is stateless
does not require a specific message format to carry the request and
response (like SOAP)
represents data using various MIME (MultiPurpose Internet Mail Extension) types
which all applications can consume
REST APIs are stateless, meaning that calls can be made independently of one
another, and each call contains all of the data necessary to complete itself
successfully.
The Google Maps API provides web services as an interface for requesting
Maps API data from external services.
PayPal: Uses REST based payment services to accept online and mobile
payments in an easier and secure way.
YouTube: In order to fetch the list of videos available on YouTube, A REST
API service named "search" is provided by YouTube.
One example is the GitHub API. GitHub is a web-based platform for version
control and collaboration that provides an API for developers to interact with its
data programmatically. The GitHub API is well-designed and adheres to
RESTful principles.
REST Principles
Uniform Interface
We can use the HTTP Methods for the following operations on the resources.
HTTP CRUD
Description
Method Operation
GET Read Fetches a resource
POST Create Adds an existing resource to a server
Transfers a resource to the server and overwrites
PUT Update
the existing resource
DELETE Delete Discards resources
If the blog is another resource, then the operations that we can perform on the
blog will be read, create, update or delete.
Stateless
Advantage: Statelessness ensures that the requests from a consumer should get
treated independently. This helps to scale the APIs (making them available in
multiple servers) to support a large number of clients concurrently. Any server
is free to serve any client since there is no session related dependency. Also,
REST APIs stay less complex as server-side state synchronization logic is
removed. On top, the performance of APIs is improvised because of less space
occupation (no space is required for state-related info).
Client-Server
Advantage: Supports the independent evolution of the client and server side
logic.
Cacheable
Service consumers can cache the response data and reuse the same. For
example, HTTP GET and HEAD operations are cacheable.
RESTful Web Services use the HTTP protocol as a carrier for the data.
And, they can make use of the metadata that is available in the HTTP
headers to enable caching.
For example, Cache-Control headers are used to optimize caching to
enhance performance.
Layered System
Code-On-Demand
Advantage: Logic that is present in the client end (such as Web browsers) can
be modified/ maintained independently without affecting the logic at the server
side.
RESTful Web services can be developed and consumed in Java using JAX-RS
API or Spring REST.
JAX-RS 2.0 and above are annotation-driven which eases the development of
Java-based RESTful services.
By using JAX-RS technology, REST applications are simpler to develop, simpler to consume,
and simpler to scale when compared to other types of distributed systems
It also provides supports for creating clients for RESTful Web Services.
Specifications
Following are the most commonly used annotations to map a resource as a web
service resource.
Annotation Description
JAX-RS implementations
One among such modules is, Spring MVC that provides the support for REST
in addition to the standard MVC support.
The developers who use Spring MVC can go for constructing RESTful services
in an effortless way. Also, it is guaranteed that the application stays lightweight
as the build path is not polluted with many external dependencies, just to
support REST.