DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Creating a Web of Data with Restlet
1. Creating a Web of Data
with Restlet
Andrew Newman
Monday, 8 December 2008 1
2. REST Architecture
Resource Oriented Analysis and Design (ROAD)
Stateless Access
Where Everything is a Resource
Identified by URI
With Different Representations
Linked Together
Monday, 8 December 2008 2
3. Restlet Oveview
GWT, 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
Monday, 8 December 2008 3
4. A little bit of HTTP
Verb 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
Monday, 8 December 2008 4
5. Restlet Model
Uniform
Restlet
Application Component Redirector
Connector Router Filter Finder
Client Server Guard Route
Transformer
Monday, 8 December 2008 5
6. 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.
Monday, 8 December 2008 6
7. Resource API
GET Representation represent(Variant)
POST void acceptRepresentation(Representation)
PUT void storeRepresentation(Representation)
DELETE void removeRepresentations()
HEAD Calls GET without stream.
OPTIONS Updates Response’s getAllowedMethods
Monday, 8 December 2008 7
8. Applications and Components
// Create Application for http://localhost/helloWorld
Application application = new Application() {
public synchronized Restlet createRoot() {
Router router = new Router(getContext());
// Attach a resource
router.attach(quot;helloWorldquot;, HelloWorldResource.class);
return router;
}
};
// Create the component and attach the application
Component component = new Component();
component.getServers().add(Protocol.HTTP);
component.getDefaultHost().attach(application);
// Start the web server
component.start();
Monday, 8 December 2008 8
9. Data and Resources
public 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 GET
public 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(quot;hello, worldquot;,
MediaType.TEXT_PLAIN);
}
return representation;
}
Monday, 8 December 2008 9
10. A Bit More HTTP
GET /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
Monday, 8 December 2008 10
11. Drop the Dot
Yes
http://realestate.com/house/1 + type
No
http://realestate.com/house/1.html
http://realestate.com/house/1.xml
http://realestate.com/house/1.json
http://realestate.com/house/1.png
Monday, 8 December 2008 11
12. No iPhone
Yes
http://realestate.com/house/1 + user-agent
No
http://www.realestate.com/house/1
http://iphone.realestate.com/house/1
http://iphone-v2.realestate.com/house/1
http://n95-8GB-Black.realestate.com/house/1
Monday, 8 December 2008 12
13. 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 encodings
clientInfo.setAcceptedEncodings(Arrays.asList(new
Preference<Encoding>(Encoding.GZIP));
// Finally, GET.
client.get(uri);
Monday, 8 December 2008 13
14. Simple WebDAV
Directory Demo
See also: http://code.google.com/p/gogoego/
Monday, 8 December 2008 14
15. Restlet and Spring
ConfigurableRestletResource
<bean id=quot;someResourcequot; class=quot;SomeResourcequot; scope=quot;prototypequot;>
<property name=quot;representationTemplatesquot;>
<map>
<entry key-ref=quot;TEXT_HTMLquot; value-ref=quot;htmlRepquot;/>
<entry key-ref=quot;APPLICATION_JSONquot; value-ref=quot;jsonRepquot;/>
<entry key-ref=quot;APPLICATION_XMLquot; value-ref=quot;xmlRepquot;/>
</map>
</property>
</bean>
Monday, 8 December 2008 15
16. Restlet and Spring
FreemarkerRepresentationFactory
<bean id=quot;htmlRepquot;
class=quot;org.jrdf.query.server.FreemarkerRepresentationFactoryquot;>
<property name=quot;templateNamequot; value=quot;dbLocal-html.ftlquot;/>
<property name=quot;freemarkerConfigquot; ref=quot;freemarkerConfigquot;/>
</bean>
dbLocal-html.ftl
<html>
<head>
<title>Query Page -- Database ${database}</title>
</head>
<body>
<h1>
Query for database <i>${database}</i>
...
Monday, 8 December 2008 16
18. Restlet and Spring
public Representation represent(Variant variant) {
Representation rep = null;
try {
graphName = (String) getRequest().getAttributes().get(quot;databasequot;);
if (getRequest().getResourceRef().hasQuery()) {
queryString = getRequest().getResourceRef().getQueryAsForm().
getFirst(quot;queryquot;).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(quot;nquot;, quot;quot;));
}
return rep;
}
Monday, 8 December 2008 18
19. Linking Data
MyFood.Com YourFood
recipe recipe
ID Desc. Cat. ID Desc. Cat.
1 Chips Snack 1 Toast Snack
2 Ice Water Drink 2 Tea Drink
URIs URIs
http://myfood.com/recipe/chips http://yourfood.com/recipe/toast
http://myfood.com/recipe/water http://yourfood.com/recipe/tea
http://myfood.com/cat/snack http://yourfood.com/cat/snack
http://myfood.com/cat/drink http://yourfood.com/cat/drink
Monday, 8 December 2008 19
20. Linking Data
?
MyFood.Com YourFood
recipe recipe
ID Desc. Cat. ID Desc. Cat.
1 Chips Snack 1 Toast Snack
2 Ice Water Drink 2 Tea Drink
URIs URIs
http://myfood.com/recipe/chips http://yourfood.com/recipe/toast
http://myfood.com/recipe/water http://yourfood.com/recipe/tea
http://myfood.com/cat/snack http://yourfood.com/cat/snack
http://myfood.com/cat/drink http://yourfood.com/cat/drink
Monday, 8 December 2008 20
21. Linking Data
http://myfood.com/cat/snack
http://yourfood.com/cat/snack
IsA
IsA
http://myfood.com/recipe/chips
http://yourfood.com/recipe/toast
Monday, 8 December 2008 21
22. Linking Data
http://myfood.com/cat/snack
isTheS
ameA
s
http://yourfood.com/cat/snack
IsA
IsA
http://myfood.com/recipe/chips
http://yourfood.com/recipe/toast
Monday, 8 December 2008 22
23. Linking Data
http://myfood.com/cat/snack
isTheS
ameA
s
http://yourfood.com/cat/snack
IsA
IsA
http://myfood.com/recipe/chips
IsA
IsA
http://yourfood.com/recipe/toast
http://purl.org/recipe
Monday, 8 December 2008 23
24. Querying the Web
User 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}
Monday, 8 December 2008 24
25. 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>
Monday, 8 December 2008 25