RESTful Web Servicesin VA Smalltalk
How to integrate your applications
with today‘s technology trends
Joachim Tuchel
www.objektfabrik.de
© 2009 by Joachim Tuchel
Agenda
• About me and objektfabrik
• „Classic“ Web Services
• RESTful Web Services: What‘s different?
• RESTful Web Services with VA Smalltalk
• DEMO: REST Clients & Server
• Questions
About me
• Founder and director of objektfabrik
• Business Informatics (Banking)
• Smalltalk since 1996(VA Smalltalk, VisualWorks and Squeak)
• Java since 2004
Some projects
• Fat Client and Web Applications in Smalltalk and Java
• Project Planning and Controlling
• Loan & Collateral Management
• Accounting for Small Businesses (Seaside)
• Migration of VA Smalltalk Projects to V6&7
About objektfabrik
• Founded 1999 in Ludwigsburg, Germany
• Training, Consulting, Professional Services
• Application and System architecture
• Software Development in Smalltalk, Java, Ruby and Objective-C
• Instantiations Business Partner since 2006
Training offerings
• Object Technology Basics
• Smalltalk Basics
• VA Smalltalk Application Development
• Advanced VA Smalltalk workshops (Packaging, Config Management, SUnit...)
• New in 2009: Seaside
Web Servicesand VA Smalltalk
Web Services
• Uses HTTP/S POST for transport
• in theory uses any transport protocol
• XML Messages
• SOAP-Envelopes
• Namespaces
• Many standards / schemas available
Web Services
• W3C Standard
• Huge set of domain/industry specific and vertical (cross industry) standards (WS-*)
• Large selection of tools and vendors
• „Enterprise integration technology“
Web Services and VA Smalltalk
• Supported by VA Smalltalk since V 5.5
• Based on Server Smalltalk
• Constantly improved in 6.x, 7.x and 8
• Expose a Smalltalk method as a service
• Consume a service in Smalltalk
What‘s wrong with Web Services
• Heavy weight standard
• One size fits all (by being large enough)
• Can be very complicated to deploy and debug
• Very complex XML structures
• Way too heavy for simple jobs
RESTful Web Services
RESTful Web Services
• REST = Representational State Transfer
• What is it?
• Not a standard
• It‘s an architecture
• Simplicity and Reuse of existing Standards like the HTTP (=Protocol) are design goals
REST is everywhere
• Amazon Web Services
• Simple Storage System (S3)
• SimpleDB - SQL - like Database
• eBay Shopping APIs
• PayPal
REST is everywhere
• Google REST APIs
• Search, Blogger, Maps, Analytics...
• Yahoo! REST APIs
• Finance, YQL, Travel, Weather...
• Twitter, Technorati, all over „Web 2.0“
• Apache CouchDB
REST is everywhere
• REST is part of „The Web“ today
• REST is used for
• Integration
• Distribution
• Scaling
• Mashups
Basic Concepts
Basic Concepts
• RESTful Web Services are about Resources, not about Services (operations)
Basic Concepts
• RESTful Web Services are about Resources, not about Services (operations)
• Addressability: Every Resource has a uniform name = URI
Basic Concepts
• RESTful Web Services are about Resources, not about Services (operations)
• Addressability: Every Resource has a uniform name = URI
http://myhost/users/joachim/todolists...
Basic Concepts
• RESTful Web Services are about Resources, not about Services (operations)
• Addressability: Every Resource has a uniform name = URI
http://myhost/users/joachim/todolists...
• Statelessness: Server doesn‘t save any application state ➠ Scalability
Basic Concepts
• Operations defined in HTTP standard
• Create: POST a new resource
• Read: GET a resource
• Update: PUT a resource
• Delete: DELETE a resource
• References to objects are IDs or hyperlinks
What‘s a Resource?• Customer
• Purchasing Order
• Line Item
• Hotel Room
• Hotel Room Reservation
• User Account
• Database Transaction
• Flight Booking
• Message
• Dataset (RDB/OODB)
• any entity we deal with in our systems
What‘s a Resource?
• Not necessarily a Business Object!
• Not all aspects need to be transported between applications
• Some aspects belong to a different Business Object (save bandwidth)
• References become IDs or Hyperlinks
Basic Concepts
Basic Concepts
Client
Basic Concepts
Client ResourceServer
Basic Concepts
Client ResourceServer
GET /users/Joachim
HTTP Request
Basic Concepts
Client ResourceServer
GET /users/Joachim
HTTP Request
HTTP Response
HTTP/1.1 200 OKContent-Type: application/xml<?xml version=...> <User firstname=“...
Basic Concepts
Client ResourceServer
GET /users/Joachim
HTTP Request
HTTP Response
HTTP/1.1 200 OKContent-Type: application/xml<?xml version=...> <User firstname=“...
Contents can beXML, JSON, CSV, Binary Data
...any MIME-Type
HTTP and Response Codes: GET
• 200 OK
• 400 Bad Request
• 401 Unauthorized / 403 Forbidden
• 404 Not found
• 500 Internal Server Error
HTTP Methods and Codes: POST
• 201 Created
• 409 Conflict
• 415 Unsupported Media Type
• 500 Internal Server Error
• Many more ...
Useful Advanced HTTP - Features
• If-Modified-Since / Last-Modified / 304 Not Modified for caching
• Cache-Control (read-only objects or infrequently changing objects)
• Content-Type to determine marshalers
• Accept-Ranges / Content-Range for partial loading of long lists etc.
Benefits of bare HTTP?
• Reduced Complexity: HTTP is easy
• Advantage in Development & Maintenance
• Uniform interface (HTTP) to every resource
• More flexibility: serving/accepting Resources instead of exposing a set of operations (➠Mashups)
RESTful Web Services with VA Smalltalk
Ingredients for RESTful Web Services
• HTTP communications (Possibly HTTPS)
• Transport format for resource data
• XML, JSON, plain ascii
• Marshalling and unmarshalling on both ends
• Naming Service (URI ➠ Resource ➠ URI)
VAST provides all the Building Blocks
• Server Smalltalk
• HTTP Client and Server
• Highly configurable and extendable on many levels
• Scalable (Multithreading by default)
• Mature (~10 years) and in use
• XML marshalling
Building a REST Client
• Extends SstHttpClient
• Adds xml marshalling (or other marshalling like)
• Wraps simple HTTP requests and handles response codes
• Optional: Session / cookie handling, caching ... (Maybe not „pure“ REST)
PRESTON client
• getResourceNamed: aURI queryParameters: aDictionary
• postResource: anObject toResourceNamed: aURI
• deleteResourceNamed: aURI
• getOptionsForResourceNamed: aURI
• handles session cookies (optional)
PRESTON client
• Mapping between Resource (e.g. XML) and Smalltalk objects
• Optional caching (URI → object)
• Proxies for hyperlinks (linked resource is only fetched if needed)
• Can act as database client to a RESTful web service
Demo:Yahoo! Traffic Client
Building a REST Server
• Extends several components of SST HTTP Server
• Is very similar to a servlet container (it serves resources with a certain URI)
• Adds marshalling, naming and more
SST Building BlocksSstServerApplicationContext
containerKey
sessionManagerConfig.
attributes
initializeRequest:
handlesRequest:
SstBasicServer
applicationContexts
sessionManagers
startUp
shutDown
processClientRequest:
basicProcessRequest:
SstHttpServer
SstHttpServletEngine
SstHttpSessionManager
createSessionForRequest:
processRequest:
createCookieForSession:
cookieFromRequestHeader...
SstServerApplicationContext
containerKey
sessionManagerConfig.
attributes
initializeRequest:
handlesRequest:
SstBasicServer
applicationContexts
sessionManagers
startUp
shutDown
processClientRequest:
basicProcessRequest:
SstHttpServer
SstHttpSessionManager
createSessionForRequest:
processRequest:
createCookieForSession:
cookieFromRequestHeader...
PrestonApplicationContext
resourceManagers
xmlMappingSpec
rootObject
PrestonServer
PrestonResourceManager
supportedHttpMethods
pathSegment
handleGETRequest..
handlePostRequest...
PRESTON Server
• Yahoo! Traffic Client
• Yahoo! Traffic Client on a Seaside Server
• Todomatic Server & Client
Demos
PRESTON Client
Application Logic
GET MapsService/V1/trafficDatawith request parameters
Yahoo! Traffic REST APIhttp://local.yahooapis.com/
Sample Request URI:http://local.yahooapis.com/MapsService/V1/trafficData?appid=YdnDemo&street=701+First+Ave&city=Sunnyvale&state=CA
HTTP Response
with XML
VA Smalltalk Image
TodomaticRESTful Server
VA Smalltalk Image
HTTP Response
with optional
XML Document
HTTP Client* Web Browser* PRESTON Smalltalk client* Any other HTTP Client
GET users/joachim/todolistsPOST users/joachim/todolistsPUT users/joachim
eventually with
a message body
containing XML
document
User TodoList TodoItem
TodoLists andTodoItems
OODB
Summary
RESTful Web Services
• Anywhere on the web today and growing
• Far less complex than Web Services
• HTTP standard all the way down
• Can be used for Internet applications as well as for internal systems
• Integration
• Scaling
VA Smalltalk and RESTful Web Services• Most Web Services are CRUD operations
• Integration today mostly means combiningHTTP with XML or other text formats
• VA ST provides all the building blocks
• Some extensions needed
• Can be combined with Seaside easily
• Mashup your own services
Questions?
More info on my blog: joachimtuchel.wordpress.com
Joachim Tuchel
Fliederweg 1
71640 Ludwigsburg, Germany
www.objektfabrik.de