42
RESTful Spring Brian Sletten [email protected]

RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Embed Size (px)

Citation preview

Page 2: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Speaker Qualifications

Independent Software Consultant13 years of software experienceWork in Semantic Web, AOP, Grid Computing, P2P and security consulting spacesBuilt Spring-based application for large customer

Page 3: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Agenda

Why Are We Here?REST OverviewSpring w/ RestletsSecuring RESTful SpringSpring w/ NetKernelConclusion

Page 4: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Why Are We Here?

Page 5: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

WS-Tenacity

SOA = WSDL + SOAP + UDDI

Page 6: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

WS-Tenacity

SOA = WSDL + SOAP + UDDI!!!!

Page 7: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

WS-Complexity

Real ComplexityHard things hard

Artificial ComplexityEasy things are (still!) hard

Page 8: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

WS-Inoperability

SOAPMessage-oriented requestMixes verb space and content space (No nouns!)

WSDLWhat You See Is What You Get

UDDIPublished metadata about serviceSimultaneously complex and limited

Page 9: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

WS-Insecurity

Conventional Web Service bigots have the gall to claim the moral highground on security

Complexity is the bane of securityRouting Around Network ArchitectureImpotent Intermediaries

Page 10: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

WS-Flexibility

If you build it...Amazon supports both SOAP-based and RESTful Web ServicesDevelopers have spoken (80-90% prefer REST)

Architectural improvements when you support both styles via the Command Pattern

Page 11: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

REST Overview

Page 12: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

REST is History

REST (REpresentational State Transfer)Based largely on Roy Fielding’s Ph.D. thesisArchitectural style designed to promote

PerformanceScalabilityGeneralitySimplicityModifiability

Page 13: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

REST Architecture

From Roy Fielding’s Thesis

Page 14: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

RESTful Web Services

Putting the “Web” in Web ServicesReusing existing technologiesSimple things easy, hard thing possible

Can layer on complexity as necessaryNothing necessarily to buy

Page 15: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Comparison to SOAP

Separation of ConcernsNoun space, Verb space, Content space

Identifiable resourcesIdentifiable requestsConstrained semanticsEmpowered intermediariesContracts not required (but possible)

Page 16: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Noun Space

Resources are an abstraction for what is available

FilesGenerated ContentComputational ResultsConcepts/Organizations/People

What comes back can change over timeThink about today’s /. Page

Page 17: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

What’s In a Name?

Resources are referenced via Identifiershttp://www.bosatsu.nethttp://www.菩薩.net

urn:isbn:0977616665Dereferencing URIs is orthogonal to transferring content

Page 18: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Verb Space

Constrained semantics for acting upon resourcesTraditionally

GETPOSTPUTDELETE

Allows intermediaries to apply security/caching policies

Page 19: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

GET

Consequence-free (idempotent) request for a resource

Page 20: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

POST

Transfer all or some portion of a resource to a processing engine on the serverCreate/Update depending on context

Page 21: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

PUT

Create a resource if it doesn’t exist or overwrite it if it does

Page 22: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Delete

Remove a resource from a server

Page 23: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Content Space

Byte streams annotated with metadataLast-modifiedMIME-type

No deeper structure specifiedClients have freedom/responsibility to know how to interpretClients can negotiate content form with serverResources are sent to/from client as a concrete Representation

Page 24: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Spring w/ Restlets

Page 25: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Why not WebLogic? Geronimo? Tomcat?

Possible to expose RESTful services through these containersWe lose many of the architectural benefits by tying ourselves to non-REST-oriented containers

URLs + verbs + resource abstraction provides benefitsURLs != scalability

Page 26: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Why not Spring MVC WebFlow?

Again, you certainly are encouraged to build RESTful APIs w/ theseHowever, tied to Servlet API

Assumes synchronous I/O in request mechanismNot as easy to support other transports

Page 27: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Restlet API

Simple Java API for providing/consuming RESTful Web ServicesReplacement for Servlet API to avoid I/O and transport limitationsContainer independentObject model for RESTful conceptsUseful to help define RESTful APIs

Page 28: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Restlet Features

Blocking/Non-blocking IORepresentations

String, XML, JSON, Freemarker templatesTransports/Protocols

HTTP/HTTPS, SMTP, JDBC, FILE, AJPFiltersSpring integration!

Page 29: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Simple Serverpackage net.bosatsu.spring;

import org.restlet.Restlet;import org.restlet.Server;import org.restlet.data.MediaType;import org.restlet.data.Protocol;import org.restlet.data.Request;import org.restlet.data.Response;

public class SimpleServer { public static void main( String [] args ) throws Exception { Restlet restlet = new Restlet() { public void handle(Request request, Response response) { response.setEntity("Hello, Florida!", MediaType.TEXT_PLAIN); } };

new Server(Protocol.HTTP, 8183, restlet).start(); }}

Page 30: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Client Side Support!

package net.bosatsu.spring;

import org.restlet.Client;import org.restlet.data.Protocol;

public class SimpleClient { public static void main( String [] args ) throws Exception { Client client = new Client(Protocol.HTTP); client.get("http://localhost:8183").getEntity().write(System.out); }}

Page 31: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Spring Restlet

Examples

Page 32: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Securing RESTful Spring

Page 33: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Basic/Digest Auth

Same mechanisms used by web servers can be used to protect access to simple RESTful APIsSimple and easyAssumes trusted sources and should leverage SSL

Page 34: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

ACEGI

Same security framework that protects normal Spring apps can be used to secure Spring-based RESTful APIsInterception-based checks can be as elaborate as need be

Page 35: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

SecuringRESTfulSpring

Examples

Page 36: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Spring w/ NetKernel

Page 37: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

NetKernel

Java-based microkernel architecture built around the ideas of REST, Unix pipes and SOATremendously productive and scalable architectureHomogenizes everything into URI-addressable featuresAdvanced features allow us to improve upon our Spring-based RESTful architecture

Page 38: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Spring NetKernel Examples

Page 39: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

Conclusion

Page 40: RESTful Spring - Bosatsu · Why not Spring MVC WebFlow? Again, you certainly are encouraged to build RESTful APIs w/ these However, tied to Servlet API Assumes synchronous I/O in

REST is AN Answer

For architectural styles that support it, REST allows systems to be simple but completeIt is possible to layer on extra complexity as neededPromotes separation of noun, verb and content spaces for simplicity and extensibilitySystems built on principles of REST demonstrate great scalabilitySome places it is not the right answer