Super Quick Start
The following runs an in-memory implementation is Jetty. You can hack about with the impementation classes to see how it works.
- checkout the source
svn co svn://milton.ettrema.com/milton/trunk/milton milton
- build the entire project
cd milton
mvn install - start the test project in jetty
cd milton-test
mvn jetty:run
Or, you can run the filesystem implementation. Just use
cd milton-filesystem-test
mvn jetty:run
NOTE 1: if you get an error using mvn jetty:run, try the following command instead -
mvn org.mortbay.jetty:maven-jetty-plugin:run
NOTE for Linux developers: the quick start project is configured to start on port 80!! (a requirement to support windows vista drive mapping)
This will fail unless you're running as root. To change this just edit the following line in milton-test/pom.xml (about line 25)
<port>80</port>
Not Quite So Quick Start
Implement Resource
Implement resource for each type of data you want to expose. For example, if you have a CUSTOMER table accessed through a hibernate Customer class you might want to expose each record as an xml file. So, start by implementing Resource:
public class CustomerResource implements Resource{
private Customer customer;
String getName() {
return customer.getName();
}
}
But, this won't get you very far. Each resource class must implement an interface corresponding to each HTTP method it supports. So, if you want to be able to delete your customer records (ie, to support the HTTP DELETE method) it must implement Deletable
public class CustomerResource implements Resource, Deletable {
...
public void delete() {
..(get the session)
session.delete(customer);
..(commit the transaction)
}
}
To make things easy for you, there's a couple of resource interfaces which bundle up all the ones you'll probably want - FileResource and FolderResource. Don't use these for real though, since you will probably not want to support all the methods they support. Just implement method interfaces singly.
Implementing Files and Folders
It's usually not obvious what entities in your domain model should be files and which should be folders. Usually, you'll want a single entity to be both.
For example, you might want your customer to be exposed as an xml file, but also to contain a list of orders relating to that customer. Thats easy, just have a FolderResource derived from the existence of the Customer record, which also contains a FileResource which has the xml data itself. You can then have another subfolder containing orders.
Implement ResourceFactory
The job of ResourceFactory is to take a URL and return the associated Resource object, or null if there is none.
How to do this translation is completely up to you. As discussed in Files and Folders the association is arbitrary, but should coincide with user intuition.
public Resource getResource(String host, String url) {
if( !url.startsWith("customer") return null;
String customerCode = ...; // parse the url
Customer c = (Customer)session.load(productCode);
if( c == null ) return null;
return new CustomerResource(c);
}