1. REZA LESMANA
Universitas Gunadarma
Saturday, February 5, 2011
2. REST
Using Restlet Framework
Saturday, February 5, 2011
3. REST
Representational State Transfer
Saturday, February 5, 2011
4. WHAT IS REST?
GAYA ARSITEKTUR SOFTWARE UNTUK ARSITEKTUR
APLIKASI BERBASIS JARINGAN
BASICALLY
WEB SERVICES ARCHITECTURE
Saturday, February 5, 2011
5. WHY USE REST?
UNIFORM INTERFACE
SEMUA KOMPONEN APLIKASI DALAM WEB SERVICES
BERINTERAKSI DENGAN CARA YANG SAMA
Saturday, February 5, 2011
6. UNIFORM INTERFACE
one of the feature of REST
Saturday, February 5, 2011
7. UNIFORM INTERFACE
Resource Identification [1]
RESOURCE
informasi yang dibutuhkan oleh user untuk
menjalankan proses bisnis dari suatu aplikasi
Saturday, February 5, 2011
9. UNIFORM INTERFACE
Resource Identification [3]
URI Template
http://contohaja.com/message/{index}
Saturday, February 5, 2011
10. RESTLET FRAMEWORK
application building block
Saturday, February 5, 2011
11. RESTLET FRAMEWORK
servlet 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
12. UNIFORM INTERFACE
Resource Identification [4]
aplikasi penyimpanan pesan dalam stack
• Daftar Pesan ---> • “/” ---> MessagesResource
• Top of Stack ---> • “/message/ ---> MessageResource
• Pesan Tunggal ---> • “/message/{index} ---> MessageResource
Saturday, February 5, 2011
13. RESTLET FRAMEWORK
MainApplication
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
14. UNIFORM INTERFACE
Manipulation Through Representation [1]
REPRESENTATION
representasi dari resource
Saturday, February 5, 2011
15. RESTLET FRAMEWORK
application building block
Saturday, February 5, 2011
16. UNIFORM INTERFACE
Manipulation Through Representation [2]
HTTP METHOD as Popular Practices
• HTTP POST ---> • membuat Resource baru (Create)
• HTTP GET ---> • mendapatkan Resource (Read)
• HTTP PUT ---> • mengubah Resource (Update)
• HTTP DELETE ---> • menghapus Resource (Delete)
Saturday, February 5, 2011
17. UNIFORM INTERFACE
Manipulation 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
18. UNIFORM INTERFACE
Manipulation Through Representation [4]
Standardized Document Structure as Representation
• XHTML • MPEG
• XML • MP3
• Atom Pub • AVI
• OData • Text/Audio/Video Streaming
Saturday, February 5, 2011
19. UNIFORM INTERFACE
Manipulation Through Representation [5]
Not Only HTTP
TEXT/AUDIO/VIDEO STREAMING
Contoh : VoIP
Using SIP or XMPP
Saturday, February 5, 2011
20. RESTLET FRAMEWORK
MainApplication
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
21. RESTLET FRAMEWORK
Manipulation 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
22. RESTLET FRAMEWORK
Manipulation 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
23. RESTLET FRAMEWORK
Manipulation 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
24. RESTLET FRAMEWORK
Manipulation 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
25. UNIFORM INTERFACE
Self-Descriptive Messages [1]
TWO FACTORS
The Protocol Message
The Media Type (Type of Representation)
Saturday, February 5, 2011
27. UNIFORM INTERFACE
Self-Descriptive Messages [3]
The Media Type
• Struktur Dokumen Bisa Mudah Diolah
• Contoh :
• XML (application/xml)
• Atom Pub (application/atom+xml)
Saturday, February 5, 2011
33. UNIFORM INTERFACE
Hypermedia as The Engine of Application
State (HATEOAS) [1]
or
Hypermedia constraints
Saturday, February 5, 2011
34. UNIFORM INTERFACE
Hypermedia Constraint [1]
• Inti dari Representational State Transfer
• Menyatukan informasi proses bisnis dalam
representasi resource
Saturday, February 5, 2011
35. UNIFORM INTERFACE
Hypermedia Constraint [2]
explains why named as “representational state transfer”
Saturday, February 5, 2011
36. UNIFORM INTERFACE
Hypermedia 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
37. UNIFORM INTERFACE
Hypermedia 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
39. UNIFORM INTERFACE
Hypermedia 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
40. RESTLET FRAMEWORK
Hypermedia 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
41. CONTOH APLIKASI LAIN
SOCIAL 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
42. CONTOH APLIKASI LAIN
SOCIAL 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
43. CONTOH APLIKASI LAIN
SOCIAL COMMERCE [3]
• Lihat deskripsi lengkap produk (melalui link). Memuat :
• Informasi produk
• Link untuk memesan
• Link kembali ke Toko Online
Saturday, February 5, 2011