Creating a Web of Data with Restlet Andrew Newman 1 Monday, 8 December 2008
Jan 15, 2015
Creating a Web of Data with Restlet
Andrew Newman
1Monday, 8 December 2008
REST ArchitectureResource Oriented Analysis and Design (ROAD)
Stateless Access
Where Everything is a Resource
Identified by URI
With Different Representations
Linked Together
2Monday, 8 December 2008
Restlet OveviewGWT, Client and Server APIs
Standalone, App Servers, Oracle, OSGi, Spring
HTTP, SMTP, POP3, JDBC, File Connectors
JSON, XML, HTML, Atom Representations
Integration with Groovy, JPA, Seam, Struts Hibernate
3Monday, 8 December 2008
A little bit of HTTPVerb Action
GET Return the entity
POST Server create/modify entity
PUT Client create/modify entity
DELETE X Delete an entity
HEAD Entity metadata (not data)
OPTIONS ? Methods supported on entity
4Monday, 8 December 2008
Restlet ModelUniform
Restlet
Application
Filter FinderConnector Router
Component Redirector
Client Server Guard
Transformer
Route
5Monday, 8 December 2008
Restlet Model• Reference - Mutable java.net.URI
• Request - Client Request
• Response - Server Response
• MediaType - Mime Types e.g. text/html, application/xml
• Resource - Anything with a Reference e.g. video, document, collection
• Variant - Metadata about a representation.
• Representation - The current state/value of a Resource.
6Monday, 8 December 2008
Resource APIGET Representation represent(Variant)
POST void acceptRepresentation(Representation)
PUT void storeRepresentation(Representation)
DELETE void removeRepresentations()
HEAD Calls GET without stream.
OPTIONS Updates Response’s getAllowedMethods
7Monday, 8 December 2008
Applications and Components// Create Application for http://localhost/helloWorldApplication application = new Application() { public synchronized Restlet createRoot() { Router router = new Router(getContext()); // Attach a resource router.attach("helloWorld", HelloWorldResource.class); return router; }};// Create the component and attach the applicationComponent component = new Component();component.getServers().add(Protocol.HTTP);component.getDefaultHost().attach(application);// Start the web servercomponent.start();
8Monday, 8 December 2008
Data and Resourcespublic HelloWorldResource(Context context, Request request, Response response) { super(context, request, response); // Declare all kind of representations supported by the resource getVariants().add(new Variant(MediaType.TEXT_PLAIN));}
// Respond to GETpublic Representation represent(Variant variant) throws ResourceException { Representation representation = null; // Generate a representation according to the variant media type. if (MediaType.TEXT_PLAIN.equals(variant.getMediaType())) { representation = new StringRepresentation("hello, world", MediaType.TEXT_PLAIN); } return representation;}
9Monday, 8 December 2008
A Bit More HTTPGET /index HTTP/1.1 Host: www.realestate.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)... Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,... Accept-Language: us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-15,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive
10Monday, 8 December 2008
Drop the DotYeshttp://realestate.com/house/1 + type Nohttp://realestate.com/house/1.htmlhttp://realestate.com/house/1.xmlhttp://realestate.com/house/1.jsonhttp://realestate.com/house/1.png
11Monday, 8 December 2008
No iPhoneYeshttp://realestate.com/house/1 + user-agent Nohttp://www.realestate.com/house/1http://iphone.realestate.com/house/1http://iphone-v2.realestate.com/house/1http://n95-8GB-Black.realestate.com/house/1
12Monday, 8 December 2008
Client Side API// GET a resource from uri as a XML DOM.Response response = new Client(Protocol.HTTP).get(uri);DomRepresentation document = response.getEntityAsDom();// Specify content types.ClientInfo clientInfo = request.getClientInfo();List<Preference<MediaType>> preferenceList = new ArrayList<Preference<MediaType>>();preferenceList.add(new Preference<MediaType>(TEXT_XML));preferenceList.add(new Preference<MediaType>(APPLICATION_XML));preferenceList.add(new Preference<MediaType>(APPLICATION_XHTML_XML));preferenceList.add(new Preference<MediaType>(TEXT_HTML, 0.9f));clientInfo.setAcceptedMediaTypes(preferenceList);// Specify encodingsclientInfo.setAcceptedEncodings(Arrays.asList(new Preference<Encoding>(Encoding.GZIP));// Finally, GET.client.get(uri);
13Monday, 8 December 2008
Simple WebDAV
Directory Demo
See also: http://code.google.com/p/gogoego/
14Monday, 8 December 2008
Restlet and SpringConfigurableRestletResource<bean id="someResource" class="SomeResource" scope="prototype"> <property name="representationTemplates"> <map> <entry key-ref="TEXT_HTML" value-ref="htmlRep"/> <entry key-ref="APPLICATION_JSON" value-ref="jsonRep"/> <entry key-ref="APPLICATION_XML" value-ref="xmlRep"/> </map> </property></bean>
15Monday, 8 December 2008
Restlet and SpringFreemarkerRepresentationFactory<bean id="htmlRep" class="org.jrdf.query.server.FreemarkerRepresentationFactory"> <property name="templateName" value="dbLocal-html.ftl"/> <property name="freemarkerConfig" ref="freemarkerConfig"/></bean>
dbLocal-html.ftl<html><head> <title>Query Page -- Database ${database}</title></head><body> <h1> Query for database <i>${database}</i>...
16Monday, 8 December 2008
Restlet and SpringSpringRouter<bean id="router" class="org.restlet.ext.spring.SpringRouter"> <property name="attachments"> <map> <entry key="/databases"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="createResource" bean="listDbResource"/> </bean> </entry> <entry key="/databases/{database}"> <bean class="org.restlet.ext.spring.SpringFinder"> <lookup-method name="createResource" bean="queryDbResource"/> </bean> </entry> </map> </property></bean>
17Monday, 8 December 2008
Restlet and Springpublic Representation represent(Variant variant) { Representation rep = null; try { graphName = (String) getRequest().getAttributes().get("database"); if (getRequest().getResourceRef().hasQuery()) { queryString = getRequest().getResourceRef().getQueryAsForm(). getFirst("query").getValue(); } if (queryString == null) { rep = queryPageRepresentation(variant); } else { rep = queryResultRepresentation(variant); } getResponse().setStatus(SUCCESS_OK); } catch (Exception e) { getResponse().setStatus(SERVER_ERROR_INTERNAL, e, e.getMessage().replace("\n", "")); } return rep;}
18Monday, 8 December 2008
Linking Data
recipeID Desc. Cat.1 Chips Snack2 Ice Water Drink
recipeID Desc. Cat.1 Toast Snack2 Tea Drink
URIshttp://myfood.com/recipe/chipshttp://myfood.com/recipe/water
http://myfood.com/cat/snackhttp://myfood.com/cat/drink
URIshttp://yourfood.com/recipe/toasthttp://yourfood.com/recipe/teahttp://yourfood.com/cat/snackhttp://yourfood.com/cat/drink
MyFood.Com YourFood
19Monday, 8 December 2008
recipeID Desc. Cat.1 Chips Snack2 Ice Water Drink
recipeID Desc. Cat.1 Toast Snack2 Tea Drink
URIshttp://myfood.com/recipe/chipshttp://myfood.com/recipe/water
http://myfood.com/cat/snackhttp://myfood.com/cat/drink
URIshttp://yourfood.com/recipe/toasthttp://yourfood.com/recipe/teahttp://yourfood.com/cat/snackhttp://yourfood.com/cat/drink
MyFood.Com YourFood
Linking Data
?20Monday, 8 December 2008
Linking Datahttp://myfood.com/cat/snack
http://yourfood.com/cat/snack
http://myfood.com/recipe/chips
IsA
http://yourfood.com/recipe/toast
IsA
21Monday, 8 December 2008
Linking Datahttp://myfood.com/cat/snack
http://yourfood.com/cat/snack
isTheSameAs
http://myfood.com/recipe/chips
IsA
http://yourfood.com/recipe/toast
IsA
22Monday, 8 December 2008
Linking Datahttp://myfood.com/cat/snack
http://yourfood.com/cat/snack
isTheSameAs
http://myfood.com/recipe/chips
IsA
http://yourfood.com/recipe/toast
IsA
http://purl.org/recipeIsA
IsA
23Monday, 8 December 2008
Querying the WebUser at myFood.Com wants all the snack recipes:
1. Find all http://purl.org/recipe and http://myfood.com/cat/snack
recipe {my:chips, my:water}, snack {my:snack}
2. my:snack = your:snack
3. Query YourFood
recipe {your:toast, your:tea}, snack {your:tea}
4. Answer: {my:chips, your:toast}
24Monday, 8 December 2008
How do you Link Data?RDF (Linking URIs)
<my:chips> <rdfs:type> <purl:snack>
SPARQL (Querying)SELECT * FROM myfood, yourfood WHERE { ?recipe <rdfs:type> <purl:recipe> .?recipe <rdfs:type> <my:cat/snack> }
OWL (Adding Relationships)<my:cat/snack> <owl:sameAs> <your:cat/snack>
25Monday, 8 December 2008
Questions?
26Monday, 8 December 2008