Pages

Friday 31 July 2015

RESTFul vs SOAP

                                SOAP
                        REST

SOAP is a protocol.


REST is an architectural style.

SOAP stands for Simple Object Access Protocol.


REST stands for REpresentational State Transfer.

SOAP can't use REST because it is a protocol.

REST can use SOAP web services because it is a concept and can use any protocol like HTTP, SOAP.

SOAP uses services interfaces to expose the business logic.

REST uses URI to expose business logic.

JAX-WS is the java API for SOAP web services.

JAX-RS is the java API for RESTful web services.


SOAP defines standards to be strictly followed.


REST does not define too much standards like SOAP.

SOAP requires more bandwidth and resource than REST.


REST requires less bandwidth and resource than SOAP.

SOAP defines its own security.

RESTful web services inherits security measures from the underlying transport.


SOAP permits XML data format only.

REST permits different data format such as Plain text, HTML, XML, JSON etc.


SOAP is less preferred.


More preferred than SOAP.

Representational State Transfer

Representational State Transfer (REST) is a software architecture style for building scalable web services. REST gives a coordinated set of constraints to the design of components in a distributed hypermedia system that can lead to a more performant and maintainable architecture.

REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.

REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.).


RESTful programming is about:

1.Resources being identified by a persistent identifier: URIs are the found everywhere/ubiquitous choice of identifier these days.

2.Resources being manipulated using a common set of verbs:HTTP methods are the commonly seen case - the venerable Create, Retrieve, Update, Delete becomes POST, GET, PUT, and DELETE.

3.The actual representation retrieved for a resource is dependent on the request and not the identifier: use HTTP Accept headers to control whether you want XML, HTTP, or even a Java Object representing the resource.

4.Maintaining the state in the object and representing the state in the representation.

5.Representing the relationships between resources in the representation of the resource: the links between objects are embedded directly in the representation.

6.Resource representations describe how the representation can be used and under what circumstances it should be discarded/refetched in a consistent manner: usage of HTTP Cache-Control headers.

Understanding REST

Understanding REST
REST stands for Representational State Transfer.
It is stateless, client-server relationship, cacheable communications protocol and use HTTP protocol.

REST is an architecture style for designing networked applications.

It is useful over the complex mechanisms such as CORBA, RPC or SOAP to connect between machines because HTTP is used to make calls between machines.

World Wide Web itself, based on HTTP, can be viewed as a REST-based architecture.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL etc.).

Principles of REST
Resources expose easily understood directory structure URIs.
Representations transfer JSON or XML to represent data objects and attributes.
Messages use HTTP methods explicitly (for example, GET, POST, PUT, and DELETE).
Stateless interactions store no client context on the server between requests. State dependencies limit and restrict scalability. The client holds session state.

HTTP methods
Use HTTP methods to map CRUD (create, retrieve, update, delete) operations to HTTP requests.

GET
Retrieve information.
Retrieve an address with an ID of 1:
GET /addresses/1

POST
Request that the resource at the URI do something with the provided entity.
Often POST is used to create a new entity, but it can also be used to update an entity.
Create a new address:
POST /addresses

PUT
Store an entity at a URI.
PUT can create a new entity or update an existing one.

A PUT request is idempotent. Idempotency is the main difference between the expectations of PUT versus a POST request.

Modify the address with an ID of 1:
PUT /addresses/1

DELETE
Request that a resource be removed.
Delete an address with an ID of 1:
DELETE /addresses/1

HTTP status codes
Status codes indicate the result of the HTTP request.
1XX – informational
2XX – success
3XX – redirection
4XX - client error
5XX - server error

Media types
The Accept and Content-Type HTTP headers can be used to describe the content being sent or requested within an HTTP request.
JSON response:
The client may set Accept to application/json if it is requesting a response in JSON.
XML response:
Set Content-Type to application/xml tells the client that the data being sent in the request is XML.

Tuesday 21 July 2015

Nth node from end in linked list

1. Maintain two pointers – reference pointer and main pointer.

2. Initialize both reference and main pointers to head. First move reference pointer to n nodes from head.

3. Now move both pointers one by one until reference pointer reaches end.

4. Now main pointer will point to nth node from the end.
    Return main pointer.

Implementation according to list in C

/** implementation according to list in C.**/
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class NthNodeFromLast {
       public static void main(String[] args) {
              List<String> list = new LinkedList<String>();
              list.add("19"); list.add("18");
              list.add("17"); list.add("16");
              list.add("15"); list.add("14");
              list.add("13"); list.add("12");
              list.add("11"); list.add("10");
              list.add("9"); list.add("8");
              list.add("7"); list.add("6");
              list.add("5"); list.add("4");
              list.add("3"); list.add("2");
              list.add("1");
             
              /** find nth index. */
              int n = 5; int count = 0; String ele = null;
              ListIterator<String> itr = list.listIterator();
              ListIterator<String> itr1 = list.listIterator();

              while(itr.hasNext()) {
                     itr.next();
                     count ++;
                     if(count<n) {
                           continue;
                     } else {
                           if(itr1.hasNext()) {
                                  ele = itr1.next();
                           }
                     }
              }
              System.out.println(ele);
       }
}

Implementation according predefined Java collection

/** implementation according predefined java collection.**/
import java.util.LinkedList;
import java.util.List;
public class NthNodeFromLast {

       public static void main(String[] args) {
              List<String> list = new LinkedList<String>();
             
              list.add("19"); list.add("18");
              list.add("17"); list.add("16");
              list.add("15"); list.add("14");
              list.add("13"); list.add("12");
              list.add("11"); list.add("10");
              list.add("9"); list.add("8");
              list.add("7"); list.add("6");
              list.add("5"); list.add("4");
              list.add("3"); list.add("2");
              list.add("1");
             
              /** find nth index. */
              int n = 5;
              /**
               * java LinkedList implementation maintain the size
               * (keep update while insertion/deletion).
               * So there no extra loop to get the size.
               * */
              int size = list.size();
              int idxStart = size - n;
              System.out.println(list.get(idxStart));
       }
}