Rest dengan restlet

Post on 10-May-2015

784 views 2 download

Tags:

Transcript of Rest dengan restlet

REZA LESMANAUniversitas Gunadarma

Saturday, February 5, 2011

RESTUsing Restlet Framework

Saturday, February 5, 2011

RESTRepresentational State Transfer

Saturday, February 5, 2011

WHAT IS REST?

GAYA ARSITEKTUR SOFTWARE UNTUK ARSITEKTUR APLIKASI BERBASIS JARINGAN

BASICALLYWEB SERVICES ARCHITECTURE

Saturday, February 5, 2011

WHY USE REST?UNIFORM INTERFACE

SEMUA KOMPONEN APLIKASI DALAM WEB SERVICES BERINTERAKSI DENGAN CARA YANG SAMA

Saturday, February 5, 2011

UNIFORM INTERFACEone of the feature of REST

Saturday, February 5, 2011

UNIFORM INTERFACEResource Identification [1]

RESOURCEinformasi yang dibutuhkan oleh user untuk

menjalankan proses bisnis dari suatu aplikasi

Saturday, February 5, 2011

UNIFORM INTERFACEResource Identification [2]

Uniform Resource Identifier (URI)

Contoh : HTTP URL

Saturday, February 5, 2011

UNIFORM INTERFACEResource Identification [3]

URI Template

http://contohaja.com/message/{index}

Saturday, February 5, 2011

RESTLET FRAMEWORKapplication building block

Saturday, February 5, 2011

RESTLET FRAMEWORKservlet configuration (web.xml)

<!-- Application class name --> <context-param> <param-name>org.restlet.application</param-name> <param-value> com.helloworld.main.MainApplication </param-value> </context-param> <!-- Restlet adapter --> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class> org.restlet.ext.servlet.ServerServlet </servlet-class> </servlet> <!-- Catch all requests --> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>

Saturday, February 5, 2011

UNIFORM INTERFACEResource Identification [4]

aplikasi penyimpanan pesan dalam stack

•Daftar Pesan --->

• Top of Stack --->

• Pesan Tunggal --->

• “/” ---> MessagesResource

• “/message/ ---> MessageResource

• “/message/{index} ---> MessageResource

Saturday, February 5, 2011

RESTLET FRAMEWORKMainApplication

public class MainApplication extends Application {

public MainApplication(Context parentContext) { super(parentContext); } @Override public synchronized Restlet createInboundRoot() { Router router = new Router(getContext()); router.attach("/", MessagesResource.class); router.attach("/message/", MessageResource.class); router.attach("/message/{messageIndex}", MessageResource.class); return router; }}

Saturday, February 5, 2011

UNIFORM INTERFACEManipulation Through Representation [1]

REPRESENTATION

representasi dari resource

Saturday, February 5, 2011

RESTLET FRAMEWORKapplication building block

Saturday, February 5, 2011

UNIFORM INTERFACEManipulation Through Representation [2]

HTTP METHOD as Popular Practices

• HTTP POST --->

• HTTP GET --->

• HTTP PUT --->

• HTTP DELETE --->

•membuat Resource baru (Create)

•mendapatkan Resource (Read)

•mengubah Resource (Update)

•menghapus Resource (Delete)

Saturday, February 5, 2011

UNIFORM INTERFACEManipulation Through Representation [3]

Another Protocol?

• Gunakan standar protokol komunikasi sesuai dengan spesifikasinya

• Perhatikan Safety dan Idempotency dari method

• Safety : tidak menghasilkan “efek samping” pada resource

• Idempotency : dilakukan berulang-ulang hasilnya sama

Saturday, February 5, 2011

UNIFORM INTERFACEManipulation Through Representation [4]

Standardized Document Structure as Representation

• XHTML

• XML

• Atom Pub

•OData

•MPEG

•MP3

• AVI

• Text/Audio/Video Streaming

Saturday, February 5, 2011

UNIFORM INTERFACEManipulation Through Representation [5]

Not Only HTTP

TEXT/AUDIO/VIDEO STREAMINGContoh : VoIP

Using SIP or XMPP

Saturday, February 5, 2011

RESTLET FRAMEWORKMainApplication

public class MainApplication extends Application {

public MainApplication(Context parentContext) { super(parentContext); } @Override public synchronized Restlet createInboundRoot() { Router router = new Router(getContext()); router.attach("/", MessagesResource.class); router.attach("/message/", MessageResource.class); router.attach("/message/{messageIndex}", MessageResource.class); return router; }}

Saturday, February 5, 2011

RESTLET FRAMEWORKManipulation Through Representation [1]

public class MessageResource extends ServerResource { @Post public Representation addMessage(Representation entity) { StringRepresentation representation = null; String result = ""; try { result = HelloBusiness.create(entity.getText()); } catch (IOException e) { setStatus(Status.SERVER_ERROR_INTERNAL ); } representation = new StringRepresentation (result, MediaType.APPLICATION_XML); return representation; }

Saturday, February 5, 2011

RESTLET FRAMEWORKManipulation Through Representation [2]

@Get("xml") public Representation get(){ String index = (String)getRequest().getAttributes().get("messageIndex"); StringRepresentation representation = null; String result = ""; try{ if(index != "" && index != null){ result = HelloBusiness.get(Integer.parseInt(index)); }else{ result = HelloBusiness.get(); } representation = new StringRepresentation (result, MediaType.APPLICATION_XML); return representation; }catch(Exception e){ setStatus(Status.CLIENT_ERROR_NOT_FOUND); representation = new StringRepresentation ("no item found", MediaType.TEXT_PLAIN); return representation; } }

Saturday, February 5, 2011

RESTLET FRAMEWORKManipulation Through Representation [3]

@Put public Representation put(Representation entity){ StringRepresentation representation = null; String result = ""; try { result = HelloBusiness.update(entity.getText()); } catch (IOException e) { setStatus(Status.SERVER_ERROR_INTERNAL ); } representation = new StringRepresentation (result, MediaType.APPLICATION_XML); return representation; }

Saturday, February 5, 2011

RESTLET FRAMEWORKManipulation Through Representation [4]

@Delete public Representation delete(){ StringRepresentation representation = null; String result = ""; try { result = HelloBusiness.delete(); }catch(NoMoreItemException ne){ setStatus(Status.CLIENT_ERROR_FORBIDDEN); representation = new StringRepresentation ("Tidak boleh, udah kosong", MediaType.TEXT_PLAIN); return representation; } representation = new StringRepresentation (result, MediaType.APPLICATION_XML); return representation; }

Saturday, February 5, 2011

UNIFORM INTERFACESelf-Descriptive Messages [1]

TWO FACTORS

The Protocol MessageThe Media Type (Type of Representation)

Saturday, February 5, 2011

UNIFORM INTERFACESelf-Descriptive Messages [2]

The Protocol Message

•HTTP Header --> Content Negotiation

• Contoh : Accept : application/xml

•Status Respon :

• Contoh : HTTP 1.1 / 200 OK ,HTTP 1.1 / 403 FORBIDDEN

Saturday, February 5, 2011

UNIFORM INTERFACESelf-Descriptive Messages [3]

The Media Type

•Struktur Dokumen Bisa Mudah Diolah

•Contoh :

•XML (application/xml)

•Atom Pub (application/atom+xml)

Saturday, February 5, 2011

Contoh MediaType XML

<messages><message>

<messageid>120</messageid><author>Reza</author><created>2011/02/04 08:20:12</created><value>Hello, World!</value><link>/message/120</link>

<message>................

<messages>

Saturday, February 5, 2011

<entry> <id>urn:uuid:95506d98-aae9-4d34-a8f4-1ff30bece80c</id> <title type=ʹ′ʹ′textʹ′ʹ′>product created</title> <updated>2009-07-05T10:25:00Z</updated> <link rel=ʹ′ʹ′selfʹ′ʹ′ href=ʹ′ʹ′http://starbucks.com/products/notifications/95506d98-aae9-4d34-a8f4-1ff30bece80cʹ′ʹ′/><link rel=ʹ′ʹ′relatedʹ′ʹ′ href=ʹ′ʹ′http://starbucks.com/products/527ʹ′ʹ′/> <category scheme=ʹ′ʹ′http://starbucks.com/products/categories/typeʹ′ʹ′ term=ʹ′ʹ′productʹ′ʹ′/> <category scheme=ʹ′ʹ′http://starbucks.com/products/categories/statusʹ′ʹ′ term=ʹ′ʹ′newʹ′ʹ′/> <content type=ʹ′ʹ′application/vnd.starbucks+xmlʹ′ʹ′>

<product xmlns=ʹ′ʹ′http://schemas.starbucks.com/productʹ′ʹ′ href=ʹ′ʹ′http://starbucks.com/products/527ʹ′ʹ′>

<name>Fairtrade Roma Coffee Beans</name> <size>1kg</size><price>10</price> <currency>$</currency></product>

</content></entry>

Contoh MediaType Atom

Saturday, February 5, 2011

RESTLET FRAMEWORKSelf-Descriptive Message [1]

@Get("xml") public Representation get(){ String index = (String)getRequest().getAttributes().get("messageIndex"); StringRepresentation representation = null; String result = ""; try{ if(index != "" && index != null){ result = HelloBusiness.get(Integer.parseInt(index)); }else{ result = HelloBusiness.get(); } representation = new StringRepresentation (result, MediaType.APPLICATION_XML); return representation; }catch(NotFoundException e){ setStatus(Status.CLIENT_ERROR_NOT_FOUND); representation = new StringRepresentation ("no item found", MediaType.TEXT_PLAIN); return representation; } }

Saturday, February 5, 2011

RESTLET FRAMEWORKSelf-Descriptive Message [2]

<messages><request>GET : ALL</request><message> <request>GET</request> <value>Hello, World!</value> <link>/message/0</link></message><message> <request>GET</request> <value>Halo, Dunia!</value> <link>/message/1</link></message><message> <request>GET</request> <value>Apa Kabar?</value> <link>/message/2</link></message></messages>

Saturday, February 5, 2011

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia as The Engine of Application

State (HATEOAS) [1]

orHypermedia constraints

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia Constraint [1]

• Inti dari Representational State Transfer

•Menyatukan informasi proses bisnis dalam representasi resource

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia Constraint [2]

explains why named as “representational state transfer”

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia Constraint [3]

execution of business process

“Consumers in a hypermedia system cause state transitions by visiting and manipulating resource state. Interestingly, the application state changes that result from a consumer driving a hypermedia system resemble the execution of a business process. This suggests that our services can advertise workflows using hypermedia.” - Jim Webber - REST In Practice

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia Constraint [3]

Best Practices

•Menambahkan informasi possible next states langsung di dalam Representation

•Caranya (untuk saat ini) :

•possible next states ----> hyperlinks

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia Constraint [4]

examples<entry><order xmlns=ʹ′ʹ′http://schemas.starbucks.comʹ′ʹ′> <location>takeAway</location> <item>

<name>latte</name> <quantity>1</quantity> <milk>whole</milk> <size>small</size>

</item> <cost>2.0</cost> <status>payment-expected</status><link rel=ʹ′ʹ′http://relations.restbucks.com/paymentʹ′ʹ′

href=ʹ′ʹ′https://restbucks.com/payment/1234ʹ′ʹ′/> <link rel=ʹ′ʹ′http://relations.restbucks.com/special-offerʹ′ʹ′

href=ʹ′ʹ′http://restbucks.com/offers/cookie/1234ʹ′ʹ′/></entry>

Saturday, February 5, 2011

UNIFORM INTERFACEHypermedia Constraint [5]

hypermedia support in MediaType

•Lebih baik jangan gunakan MediaType XML untuk menambahkan hyperlinks di dalamnya.

• Tidak ada dukungan langsung (native link tag) untuk hyperlinks

•Gunakan Hypermedia Types (-Mike Amundsen)

•Contoh : Atom

•http://amundsen.com/hypermedia/Saturday, February 5, 2011

RESTLET FRAMEWORKHypermedia Constraints [1]

•Native support untuk mempermudah manajemen hypermedia constraints terdapat pada Restlet versi 2.1 (dalam pengembangan)

•Solusi : Tambahkan secara manual ! :D

•Solusi : Pakai versi 2.1 dengan resiko sendiri

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [1]

• Indeks Aplikasi Memuat :

•Daftar Toko Online (dengan link masing-masing)

• Link ke User Profile

• Link ke Pengaturan Toko Online milik user

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [2]

•Masuk ke salah satu Toko Online (melalui link). Memuat :

• Profil singkat penjual (dengan link ke profil lengkap)

•Daftar Deskripsi Singkat Produk (dengan link masing-masing untuk deskripsi lengkap)

• Link ke Indeks

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [3]

• Lihat deskripsi lengkap produk (melalui link). Memuat :

• Informasi produk

• Link untuk memesan

• Link kembali ke Toko Online

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [4]

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [5]

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [6]

Saturday, February 5, 2011

CONTOH APLIKASI LAINSOCIAL COMMERCE [7]

Saturday, February 5, 2011

THANK YOU !

QUESTION?

Saturday, February 5, 2011