Thursday, March 21, 2019


                             RESTful Web services

Message-Oriented Communication

Message-oriented communication is a way of communicating between processes. Messages, which correspond to events, are the basic units of data delivered. Tanenbaum and Steen classified message-oriented communication according to two factors---synchronous or asynchronous communication, and transient or persistent communication. In synchronous communication, the sender blocks waiting for the receiver to engage in the exchange. Asynchronous communication does not require both the sender and the receiver to execute simultaneously. So, the sender and recipient are loosely-coupled. The amount of time messages are stored determines whether the communication is transient or persistent. Transient communication stores the message only while both partners in the communication are executing. If the next router or receiver is not available, then the message is discarded. Persistent communication, on the other hand, stores the message until the recipient receives it.

Resource oriented communication

Resource oriented communication is a communication library that aims to exploit the low-level communication facilities of today’s cluster networking hardware and to merge, via the resource oriented paradigm, those facilities and the high-level degree of parallelism achieved on SMP systems through multi-threading. The communication model defines three major entities – contexts, resources and buffers – which permit the design of high-level solutions. A low-level distributed directory is used to support resource registering and discovering. The usefulness and applicability of RoCL is briefly addressed through a basic modelling example – the implementation of TPVM over RoCL. Performance results for Myrinet and Gigabit Ethernet, currently supported in RoCL through GM and MVIA, respectively, are also presented. Keywords: cluster computing, message-passing, directory, multi-threading.

Resource based nature of the REST style


•Representational State Transfer (REST) is an architectural style for the service of web
•The REST is used as a basis for HTTP 1.1 development
•Thus the WWW including servers, proxies, etc…
•REST is
•Resource-based
•Focuses on Representations (of data/information/resources)
•Derived using Six Constraints

“Representations” in REST style

•How resources get manipulated and presented
•Part of the resource state is transferred between client and server •Typically JSON or XML
•Or even can be a web page, file, image, etc…
•E.g. the state of a student (as per the database) can be represented by a resource of webpage, XML dataset, etc…

Constraints of REST

1            Client-server

 •The REST is for explicitly for networked distributed systems, which are based   on the client-server style

2           Layered System

 •A client doesn’t need to know whether it is connected directly to the end server, or to an intermediary along the way.
 •Intermediary servers may improve system scalability by enabling load-balancing and by providing shared caches.
 •Layers may also enforce security policies.

     3) Stateless

•One client can send multiple requests to the server •each request is independent
•every request must contain all the necessary information so that the server can understand it and process it accordingly.
          •the server must not hold any information about the clientstate.
          •Any state information must stay on client –such as sessions.

     4) Cacheable

          •As many clients access the same server, often they may request     the same resources
          •it is necessary that these responses might be cached, avoiding unnecessary processing, which may affect performance

      5) Code-On-Demand (Optional)

         •This allows the customer to run some code on demand, that is, extend part of server logic to the client, either through an applet or scripts.

      6) Uniform Interface

        •defines the interface between clients and servers.
        •It simplifies and decouples the architecture, which enables each part to evolve independently 
        •REST is defined by four interface constraints:
        •identification of resources
        •manipulation of resources through representations
        •self-descriptive messages
       •hypermedia as the engine of application state

Uniform Interface –Identification of resources •Based on the web’s Request-Response model
•Request = URL + HTTP Verbs (GET,POST,PUT,DELETE) •HTTP/1.1 GET http://abc.com/user/John
•Response = HTML, XML, JSON, TXT, etc…

{
"name": “John",
"job": “Web Developer",
 "hobbies": ["blogging", "coding", "music"]
}

Implementations for the elements of REST style

Components

 •Software that interacts with one another
•Communicate by transferring representations of resources through a standard interface rather than operating directly upon there source itself
•Used to access, provide access to, or mediate access to resources

Connector

•represent activities involved in accessing resources and transferring representations.
•REST encapsulates different activities of accessing and transferring representations into different connector types

Data

•key aspect of REST is the state of the data elements, its components communicate by transferring representations of the current or desired state of data elements
•REST manages data in the following ways:
•render the data (traditional client-server style) where it is located and send a fixed-format image to the recipient,
•encapsulate the data (mobile object style) with a rendering engine and send both to the recipient or,
•send the raw data to the recipient along with metadata that describes the data type, so that the recipient can choose their own rendering engine
•connectors are abstract interfaces for component communication, enhancing simplicity by hiding the underlying implementation of resources and communication mechanisms

RESTful web services using RESTful URLs

Request = URL + HTTP Verbs (GET,POST,PUT,DELETE)
•URL is the key technique in RESTful communication
•The API of the RESTful service is a set of URLs
•The resource is determined by the URL segments
•The CRUD operations are determined by the HTTP verb

Operation=Read, Verb=GET

•Collection
              •SLIIT.com/students/
              •SLIIT.com/students/DS
              •SLIIT.com/students/DS/IT123456/posts
•Single item
             •SLIIT.com/students/IT123456
             •Blog.com/posts/pid15948
•Multiple items
             •SLIIT.com/students/IT123456;IT456456;IT998877

Operation=Create, Verb=POST

Single item (single item in the payload)
               •SLIIT.com/students/
               •SLIIT.com/students/DS       
               •SLIIT.com/students/DS/IT123456/posts
•Multiple items
•Same URL, items will be in the payload

Operation=Update, Verb=PUT

•Collection (data in the payload)
                 •SLIIT.com/students/
                 •SLIIT.com/students/DS
                 •SLIIT.com/students/DS/IT123456/posts
•Single item
                   •SLIIT.com/students/IT123456
                   •Blog.com/posts/pid15948
•Multiple items
                      •SLIIT.com/students/IT123456;IT456456;IT998877

Operation=Delete, Verb=DELETE

•Collection
             •SLIIT.com/students/
             •SLIIT.com/students/DS
             •SLIIT.com/students/DS/IT123456/posts
 •Single item
                •SLIIT.com/students/IT123456
                •Blog.com/posts/pid15948
Multiple items
                SLIIT.com/students/IT123456;IT456456;IT998877

MVC for RESTful web service development



JAX-RS API

•JAX-RS is an API
 •There are two implementations
            1. jersey
            2.RESTeasy

Annotations in JAX-RS

Annotation

@Path

The@Path annotation’s value is a relative URI path indicating where the Java class will be hosted: for example,/helloworld. You can also embed variables in the URIs to make a URI path template. For example, you could ask for the name of a user and pass it to the application as a variable in the URI:/helloworld/{username}.

@GET

The@GET annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP GET requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

 @POST

The@POST annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP POST requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@PUT

The@PUT annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP PUT requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.

@DELETE

The@DELETE annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests. The behavior of a resource is determined by the HTTP method to which the resource is responding

Importance of “media type” in JAX-RS

Hello.java packagecom.javatpoint.rest; importjavax.ws.rs.*; importjavax.ws.rs.core.MediaType;
@Path("/hello") publicclassHello{ @GET @Produces(MediaType.TEXT_PLAIN) publicStringsayPlainTextHello(){ return"Helloworld";
 }
 }
Web.xml <servlet-mapping> <servlet-name>JerseyRESTService</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
index.html <ahref="rest/hello">ClickHere</a>





Thursday, March 14, 2019


                                                                                                                                                                                      WEB SERVICES AND SOAP

Web applications with web services

Web Applications

Web applications are defined by being interactive. You're supposed to use a web application in order to perform a function and use some of the web applications features. Lots of web applications don't even have real informative content or data exactly. People are just supposed to use them in order to perform additional tasks, using their features to accomplish something. You use a web application to check your incoming messages, for instance, or play a game.
The browser capabilities involved with web applications are significantly more high-tech, which is one reason why it's usually harder for people to design a web application than a website. Websites are all about getting more data, and web applications are all about doing things. One of your actions is probably going to be getting more information or learning more information, but the web application helped you perform that action. You got the information from a website.
The user interface of a web application is also usually much more complicated than the user interface of a website. Websites might have tags and categories that you need to understand, but you don't have to go through and learn any potentially complicated tasks in order to use websites. Web applications often require step-by-step guides, or you're not going to be able to complete them.
The setup of websites are completely different from web applications in most cases. The rhythm of typing in the address, loading websites, and going back and forth between websites is often absent with web applications.
Web applications, unsurprisingly, are usually harder to design and create than websites. Lots of people have their own websites today, and this was the case even ten years ago. The people who are able to create their own web applications can more easily make money off of them because it takes more work to learn how to code and create a web application.
Web Services

Web service is a technology by which two or more remote web applications interact with each other over network/internet. It can be implemented using Java, .net, PHP etc. Web pages allow people to communicate and collaborate with each other while web services allow programs to communicate and collaborate with each other.
A web service is essentially a collection of open protocols and standards used for exchanging data between applications or systems. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Java and Python, or Windows and Linux applications) is due to the use of open standards (XML, SOAP, HTTP).
All the standard Web Services works using following components:
·        SOAP (Simple Object Access Protocol)
·        UDDI (Universal Description, Discovery and Integration)
·        WSDL (Web Services Description Language)

 Need for web services
 Re-usability

 Once we develop some business logic, we can make it reuse for other applications
 Example:
If 10 different applications requires to use our logic
We can expose our logic over a network as a web service
So all the 10 different application can access it from the network.
Interoperability
It provides the freedom for a developers to choose whatever the technology they want to use for development.
Web services uses a set of standards and protocols and enable us to achieve interoperability.
Hence applications developed in Java,Mainframe,Ruby or any other technology can call the web service and use it.
Loosely coupled

Web service exist independent of the other parts of the application that uses it.
So any changes to the application can be made without affecting the web service.
Deployability

It is very easy to deploy the web application as they are deployed over standard internet technologies.

WEB SERVICE DESCRIPTION LANGUAGE (WSDL)

•WSDL is an XML vocabulary for describing Web services allowing developers to describe Web Services and their capabilities, in a standard manner.
 •standard format to describe a Web Service (description stack)
 •specifies three fundamental properties:
•What a service does -operations (methods) provided by the service
•How a service is accessed -data format and protocol details
•Where a service is located -Address (URL) details
•The document written in WSDL is also simple called a WSDL (or WSDL document) •WSDL is a contract between the XML (SOAP) Web service and the client who wishes to use this service
•The service provides the WSDL document and the Web service client uses the WSDL document to create the stub (or to dynamically decode messages).

Fundamental properties of a WSDL

•specifies three fundamental properties:
•What a service does -operations (methods) provided by the service •How a service is accessed -data format and protocol details
•Where a service is located -Address (URL) details

Use of WSDL document in web services and client development

•WSDL is a contract between the XML(SOAP) Web service and the client who wishes to use this service
•The service provides the WSDL document and the Web service client uses the WSDL document to create the stub (or to dynamically decode messages)

Structure of the WSDL document


Web Services Description Language (WSDL) is an XML grammar for describing network services as collections of communication endpoints capable of exchanging messages. The diagram below illustrates the elements that are present in a WSDL document, and indicates their relationships. To see an example of how this is implemented in a WSDL document.

Elements in WSDL
TYPE

•use the XML schema language to declare complex data types and elements that are used elsewhere in the WSDL document
•serve as a container for defining any data types that are not described by the XML schema built-in types: complex types and custom simple types,
•the data types and elements defined in the type’s element are used by message definitions when declaring the parts (payloads) of messages

MESSAGE

•any WSDL interface is the set of messages that the service behind the interface expects to send and receive
•messages are abstractions of request and response messages that clients and web services exchange partitioned into a number of logical parts to ease access to its contents
•messages are constructed from a number of XML Schema typed part elements, which can come from the types part of the description or an external schema that has been imported into the same WSDL document
•they contain types of other schemas and user defined types into input, output and fault messages that the web service will consume and produce

PORT TYPE

•define web service functionality at abstract level grouping sets of message exchanges into operations
•contain a set of operations that incorporates input, output and fault messages and parameter order
•WSDL supports at least a single input and output message, but permits the declaration of an arbitrary number of faults
•portTypeelement may have one or more operation elements, each of which defines an RPC-or document style Web service method
•Java equivalence:
•port Type-> java interface
operation -> method name

BINDING

map a PortType to a specific protocol, typically SOAP over http, using a specific data encoding style
•one portType can be bound to several different protocols by using more than one port
•bindings refer back to portTypes by name, just as operations point to messages.
•binding styles may be either “RPC” or “Document”(SOAP). •also specify SOAP encoding

SERVICE

•it binds the Web service to a specific network-addressable location, i.e. it takes the bindings declared previously and ties them to a port, which is a physical network endpoint to which clients bind over the specified protocol,
•contain one or more port elements, each of which represents a different Web service and the port element assigns the URL to a specific binding,
•reference a particular binding, and along with addressing information is wrapped together into a service element to form the final physical, network addressable Web service,
•access point for each port
•port type -The operations performed by the web service.
•message -The messages used by the web service.
•types -The data types used by the web service.
•binding -The communication protocols used by the web service.

WSDL Elements:PortType

Port type is a collection of one or more related operations describing the interface of a web service.
PortType definition is a collection of operation elements.
Generally,WSDL documents contain only one portType elements, because different web service interface defenitions are written with different documents.
portType has a single name attribute.
The name of portType together with the namespace of the WDSL document define a unique name for the portType.

SOAP is used with HTTP

SOAP is a messaging protocol and in a nutshell is just another XML language.
Its purpose is the data exchange over networks. Its concern is the encapsulation of these data and the rules for transmitting and receiving them.
HTTP is an application protocol and SOAP messages are placed as the HTTP payload.
Although there is the overhead of HTTP, it has the advantage that it is a protocol that is open to firewalls, well-understood and widely-supported. Thus, web services can be accessed and exposed via technology already in-place.
SOAP messages are usually exchanged via HTTP. Although it is possible to use other (application) protocols, e.g. SMTP or FTP, the non-HTTP bindings are not specified by SOAP specs and are not supported by WS-BP (interoperability spec).
You could exchange SOAP messages over raw TCP but then you would have web services that are not interoperable (not compliant to WS-BP).
Nowadays the debate is why the SOAP have overhead at all and not send data over HTTP (RESTful WS).

SOAP can be used for functional oriented communication


Structure of SOAP message in message oriented communication and elements used


•SOAP messages
•consistent envelope -header and body
•consistent data encoding -based on XML Schema type system •protocol binding framework
•has encoding rules
•defines a serialization mechanism that can be used to exchange instances of application-defined objects
•Envelope
•wraps entire message and contains header and body
•defines an overall framework for expressing what is in a message; who should deal with it, and whether it is optional or mandatory
•Header
•optional element with additional info such as security or routing •Body
•application-specific message content being communicated as arbitrary XML payloads in the request and response messages
•fault element provides information about errors that occurred while processing the message

Importance of the SOAP attachments

SOAP (abbreviation for Simple Object Access Protocol) is a messaging protocol specification for exchanging structured information in the implementation of web services in computer networks. Its purpose is to provide extensibilityneutrality and independence. It uses XML Information Set for its message format, and relies on application layer protocols, most often Hypertext Transfer Protocol (HTTP) or Simple Mail Transfer Protocol (SMTP), for message negotiation and transmission.
SOAP allows processes running on disparate operating systems (such as Windows and Linux) to communicate using Extensible Markup Language (XML). Since Web protocols like HTTP are installed and running on all operating systems, SOAP allows clients to invoke web services and receive responses independent of language and platforms.

Advantages


·        SOAP's neutrality characteristic explicitly makes it suitable for use with any transport protocol. Implementations often use HTTP as a transport protocol, but other popular transport protocols can be used. For example, SOAP can also be used over SMTP, JMS and message queues.
·        SOAP, when combined with HTTP post/response exchanges, tunnels easily through existing firewalls and proxies, and consequently doesn't require modifying the widespread computing and communication infrastructures that exist for processing HTTP post/response exchanges
·        SOAP has available to it all the facilities of XML, including easy internationalization and extensible with XML Namespaces.

MIME header

•SOAP messages may have one or more attachments
•each Attachment Part object has a MIME header to indicate the type of data it contains.
•it may also have additional MIME headers to identify it or to give its location, which can be useful when there are multiple attachments •when a SOAP message has one or more Attachment Part objects, its SOAP Part object may or may not contain message content
·        Text in character sets other than ASCII
·        Non-text attachments: audio, video, images, application programs etc.
·        Message bodies with multiple parts
·        Header information in non-ASCII character sets

Frameworks/libraries for SOAP web service development

JAX-WS (Java API for XML Web Services) is a java package/library for developing web services
•It supports both Functional oriented and message oriented communication via SOAP
•JAX-WS API hides the complexity of SOAP from the application developer
•JAX-WS uses the javax.jwspackage
•It uses annotations
•@WebService
•@WebMethod
•@OneWay
•@WebParam
•@WebResult

Annotations in JAX-WS

Java API for XML-Based Web Services (JAX-WS) relies on the use of annotations to specify metadata associated with web services implementations and to simplify the development of web services. Annotations describe how a server-side service implementation is accessed as a web service or how a client-side Java class accesses web services.
The JAX-WS programming standard introduces support for annotating Java classes with metadata that is used to define a service endpoint application as a web service and how a client can access the web service. JAX-WS supports the use of annotations based on the Metadata Facility for the Java Programming Language (Java Specification Request (JSR) 175) specification, the Web Services Metadata for the Java Platform (JSR 181) specification and annotations defined by the JAX-WS 2.0 and later (JSR 224) specification which includes JAXB annotations. Using annotations from the JSR 181 standard, you can simply annotate the service implementation class or the service interface and now the application is enabled as a web service. Using annotations within the Java source simplifies development and deployment of web services by defining some of the additional information that is typically obtained from deployment descriptor files, WSDL files, or mapping metadata from XML and WSDL into the source artifacts.
Use annotations to configure bindings, handler chains, set names of portType, service and other WSDL parameters. Annotations are used in mapping Java to WSDL and schema, and at runtime to control how the JAX-WS runtime processes and responds to web service invocations.

Web service can be tested using different approaches

Testing of web services is one of the important type of software testing approach, which is mainly used to determine the expectations for reliability, functionality, performance, etc.
As these days automated testing is considered as one of the most trending methodology in the field of software testing, hence testing web apps based on RESTful APIs through automation will provide effective test results. Some of the best & popular tools for web services testing are:
1.   SoapUI,
2.    TestingWhiz,
3.  SOATest
4.  TestMaker,
5.  Postman, etc.
6.   As mentioned in my previous posts related to web services or API testing, among above mentioned tools, I consider Testingwhiz and SoupUI as the most user-friendly tools for API testing. Automating API testing, as a part of automated web services testing solution that helps you to test whether your application communicates and accesses functions correctly from the Web by effectively verifying the behavior of web services connected to them.
7.     For better clarity and more information, you can go through the following informative article