Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [orion-dev] server architecture

You don't need the entire set of Restlet bundles.  You must have the following bundles:

org.restlet.jar  664K
org.restlet.ext.servlet.jar 25K
org.eclipselabs.restlet.jar 4K
org.eclipselabs.restlet.servlet.jar 8K

for a total of 701K.  Restlet provides a very simple way to create resources, and handle representations such as XML, JSON, and handle security.  Here's an example resource that interfaces to MongoDB using the URI pattern http://host:port/{database}/{collection}

public class MongoResource extends ServerResource
{
@Get
public JSONObject getJSON() throws MongoException, UnknownHostException
{
DBCollection collection = getCollection();
DBObject dbObject = collection.findOne(new BasicDBObject("_id", new ObjectId((String) getRequestAttributes().get("id"))));

if (dbObject != null)
return new JSONObject(dbObject.toMap());
else
{
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
return null;
}
}

@Post
public String add(JsonRepresentation representation) throws JSONException, UnknownHostException
{
JSONObject json = representation.getJsonObject();
DBCollection collection = getCollection();
DBObject dbObject = (DBObject) JSON.parse(json.toString());
collection.insert(dbObject);
return getReference().toString() + dbObject.get("_id").toString();
}

@Put
public void update(JsonRepresentation representation) throws JSONException, UnknownHostException
{
JSONObject json = representation.getJsonObject();
DBCollection collection = getCollection();
DBObject dbObject = (DBObject) JSON.parse(json.toString());

if (collection.findAndModify(new BasicDBObject("_id", new ObjectId((String) getRequestAttributes().get("id"))), dbObject) == null)
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
}

@Delete
public void remove() throws UnknownHostException
{
DBCollection collection = getCollection();
collection.remove(new BasicDBObject("_id", new ObjectId((String) getRequestAttributes().get("id"))));
}

private DBCollection getCollection() throws UnknownHostException
{
IMongoDB mongoDB = Activator.getInstance().getMongoDB();
Mongo mongo = mongoDB.getMongo(new MongoURI("mongodb://localhost"));
DB db = mongo.getDB((String) getRequestAttributes().get("database"));
DBCollection collection = db.getCollection((String) getRequestAttributes().get("collection"));
return collection;
}
}

The only other piece of code you have to write is the provider to register the resource:

public class MongoResourceProvider implements IResourceProvider
{
@Override
public String getApplicationAlias()
{
return "/";
}

@Override
public String[] getPaths()
{
return new String[] { "/mongo/{database}/{collection}/", "/mongo/{database}/{collection}/{id}" };
}

@Override
public Finder getFinder()
{
return finder;
}

private Finder finder = new Finder(null, MongoResource.class);;
}

The code fully supports hot-swap, so the bundle providing the resource can be updated live.

Bryan

On Feb 2, 2011, at 10:32 AM, John Arthorne wrote:

We have been trying to keep the initial server implementation minimal and avoid too many technology dependencies. What's the big advantage of restlet over servlets? I just glanced at the download for restlet and it seems to be 20MB, which is bigger than our entire M5 download at the moment. I'm somewhat skeptical that something so big could indeed simplify our server, but I'm open to looking at it.

john




Bryan Hunt <bhunt@xxxxxxx>
Sent by: orion-dev-bounces@xxxxxxxxxxx

02/01/2011 10:13 PM

Please respond to
Orion developer discussions <orion-dev@xxxxxxxxxxx>

To
orion-dev@xxxxxxxxxxx
cc
Subject
[orion-dev] server architecture





I started poking around Orion a bit and noticed that it currently uses "old-school" servlets.  Would there be any consideration for using a modern framework such as Restlet for providing the RESTful services, resources, and representations?  With the OSGi integration work I've done, it's now pretty trivial to create a dynamic resource, and I think it would provide a much cleaner implementation.

Bryan

_______________________________________________
orion-dev mailing list
orion-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/orion-dev

_______________________________________________
orion-dev mailing list
orion-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/orion-dev


Back to the top