rest principle &_play_routes

23
REST principles and Play routes configuration Rakesh Chouhan Date-24-01-2013

Upload: chrakesh5

Post on 07-Jul-2015

184 views

Category:

Software


0 download

DESCRIPTION

REST principle and configuration of play routes

TRANSCRIPT

Page 1: Rest principle &_play_routes

REST principles

and

Play routes

configuration

Rakesh Chouhan

Date-24-01-2013

Page 2: Rest principle &_play_routes

REST(Representational State Transfer)

● REST is a Style of the Software architecture for

distributed system such as World wide web.

● It is nothing but using the current feature of web in

simple and effective way. some of the amazing feature

of web are :

o Widely accepted HTTP protocol.

o Standard and Unified method PUT, POST, GET and

DELETE.

o Stateless nature of HTTP protocol.

o Easy to use URI (Uniform resource identifier) format

to locate any resource.

It is a set of constraints not rules.

Page 3: Rest principle &_play_routes

HTTP VERBS

● GET

● POST

● PUT

● DELETE

● HEAD

● OPTION

Page 4: Rest principle &_play_routes

Principles of REST services

● Everything is a Resource.

● Every resource is identified by unique identifier.

● Use simple and uniform interfaces.

● Communication is done by representation.

● Be stateless.

Page 5: Rest principle &_play_routes

Principles of REST services

● Everything is a Resource

Page 6: Rest principle &_play_routes

Principles of REST services

● Every resource is identified by unique identifier

Example:

Customer Data URI

Get all Customer list GET http://www.custapp.com/employee/all

Get Customer by ID GET http://www.custapp.com/employee/101

Page 7: Rest principle &_play_routes

Principles of REST services

● Use simple and Uniform interfaces.

Page 8: Rest principle &_play_routes

Principles of REST services

● Use simple and Uniform interfaces.

eg : we exposed the customer and order data on the web,

Normal Method HTTP Method Uniform URI

AddCustomer PUT /customer/raj

InsertOrders PUT /order/001

SelectCustomer GET /customer/raj

getOrders GET /order/001

DeleteCustomer DELETE /customer/raj

RemoveOrder DELETE /order/001

Page 9: Rest principle &_play_routes

Principles of REST services

● Communication is done by representation.

The client and server exchanging the representations.

Page 10: Rest principle &_play_routes

Principles of REST services

● Communication is done by representation.

eg : we want to create a customer, we send some kind of

representation to the server using HTTP PUT,

<customer>

<name>raj</name>

<address>Vashi</address>

<customer>

and the server send some other representation to show that

the resource is created, and move towards the next step.

<customer>

<name>raj</name>

<next>http://www.custapp.com/customers/Orders</next>

</customer>

Page 11: Rest principle &_play_routes

Principles of REST services

● Be Stateless.

Every request should be independent.

It means that once the request is processed, the next

request of the web visitor, does not have to come back to

same machine.

eg. VE.Box Restful services.

Page 12: Rest principle &_play_routes

Play routes

Play routes consist of the HTTP method and URI and both

are associated with and Action generator ( controller's

static method)

routes are defined in "conf/routes" file.

Modules doesn't have the routes file, if we deploy the

routes in module, it will override the main application's

routes file.

route syntex : HTTP Method URI ActionGET /customer/:id controller.Application.selectCustomer(id :

Long)

Page 13: Rest principle &_play_routes

Play URI patterns

● Static path

GET /Customer/all controller.Customer.list()

● Dynamic parts

GET /Customer/:id controller.Customer.getCustomer(id: Long)

GET /Customer/:id/:name controller.Customer.getCustomer(id: Long,

name :String)

Page 14: Rest principle &_play_routes

Play URI patterns

● Dynamic parts spanning several

dynamic part using the *id syntax

GET /files/*name

controllers.Application.download(name)

eg : GET http://www.custapp.com/files/image/001.gif

then the dynamic part will capture the " image/001.gif".

● Dynamic parts using regular expression.

regular expression for a dynamic part, using the $id<regex> syntax

GET /customer/$id< [0-9]+>

controller.customer.getCust(id: Long)

Page 15: Rest principle &_play_routes

Action generator Methods

● The last part of the route definition.

● If the action method doesn't have any parameter name just give the fully

qualified name

GET / controller.CustApp.home()

● If the action method defines the parameter, the corresponding parameter

value will be searched in URI, either extracted from the URI path or from

the query String

Extract the page parameter from the path.

i.e. http://www.custapp.com/index

GET /:page controllers.CustApp.show(page)

Extract the page parameter from the query string.

i.e. http://www.custapp.com/?page=index

GET / controllers.Application.show(page)

Page 16: Rest principle &_play_routes

Parameter Type

● Default parameter type is String

GET /:page controllers.CustApp.show(page)

page parameter is String here

● Parameter with fixed value.

GET /

controllers.CustApp.show(page = "home")

GET /:page controllers.CustApp.show(page)

● Parameter with default value

GET /

controllers.CustApp.show( page: Integer ?= 1)

Page 17: Rest principle &_play_routes

Assets

● Serves the application’s static resources such as JavaScript, CSS and

images.

● The play 2.0 has a built-in controller Assets with the action "at(path:String,

file: String )" to serve the public data.

GET /assets/*file controllers.Assets.at(path="/public", file)

we can also get the static data using this

GET /jquery controllers.Assets.at("/public", "javascripts/jquery.js")

Page 18: Rest principle &_play_routes

Assets

● Etag support

Etag is an identifier assigned by the server to a specific version of resource

found at the URL, if resource available at the URL change the new Etag is

assigned. Comparing ETags only makes sense with respect to one URL—

ETags for resources obtained from different URLs may or may not be equal.

● The Assets Controller automatically manage the ETag HTTP header, the

ETag value is generated through the resource name and file modification

date.

● when the browser makes a request specifying the ETag header the server

can respond with the "304 not modified"

Page 19: Rest principle &_play_routes

Assets

● Gzip support

If a resource with the same name but using a .gz suffix is found, the Assets

controller will serve this one by adding the proper HTTP header:

Content-Encoding: gzip

● Cache-Control directive

Etag is enough for caching but if we want to add the custom cache-control for

any specific resource then in application.conf file we have to add the following

line :

"assets.cache./public/javascript/jquery.js" = "max-age=1000"

Page 20: Rest principle &_play_routes

Assets

● By default the play compile all managed assets, the compilation process

clean and compile all the managed resources. This is the safest strategy

since tracking dependencies can be very tricky with front end technologies.

If you are dealing with a lot of managed assets this strategy can be very

slow. For this reason there is a way to recompile only the change file and

its supposed dependencies. You can turn on this experimental feature by

adding the following to your settings:

incrementalAssetsCompilation := true

in application.conf file.

Page 21: Rest principle &_play_routes

Reverse Routing

in conf/routes file

Hello action

GET /error/:errorString controllers.CustApp.showError(errorString)

and we want to redirect our application control to the error page, then

public static Result index(Long id) {

if( id <= 0){

return redirect(controllers.routes.CustApp.showError("Invalid ID"));

}

return ok("OK");

}

Page 22: Rest principle &_play_routes

Imp links

https://docs.google.com/viewer?a=v&q=cache:T_r8a7FMSlEJ:info.apigee.com/

Portals/62317/docs/web%2520api.pdf+&hl=en&gl=in&pid=bl&srcid=ADGEESg

RRCSELrGALLSKNIL9rv4_9_LE2aU6IlkjLuIfsnhxedcCBaCUQbl_WztHIWO5p

YIGXdfOQu548ONbARnxuz1xSFNefne_CAYYZES83umHNuFps3Wnw5JVAE

Pd8gjY8RBSGBw1&sig=AHIEtbTOsWoJBz-AeMkXSAVrjaf0j58rhA

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rj

a&sqi=2&ved=0CDkQtwIwAQ&url=http%3A%2F%2Fvimeo.com%2F17785736

&ei=ERQBUdOuKsPprQfWkoCIBQ&usg=AFQjCNHSv7WOu5AyLZ_NIyrZtzMl

k-4W8g&bvm=bv.41524429,d.bmk

Page 23: Rest principle &_play_routes

Thank you