Darwino REST Services
Darwino REST Services
• Darwino comes with built-in REST services similar to the ExtLib’s DAS or
other document data store HTTP APIs
• These can form the basis of a full application, or just be used for
bootstrapping while custom services are developed
• These are in the “/$darwino-jstore” URL space
– This is a common convention: “$” URLs can be used to avoid the need to worry
about the place in the URL hierarchy
Darwino Playground
playground.darwino.com
I hope you like lists!
Database Services
• GET /databases – returns the list of the available databases
• GET /databases/{id} – returns information about a specific database
• GET /databases/{id}/documentscount – returns the count of documents
• DELETE /databases/{id}/documents – delete all documents
• GET /databases/{id}/documents/{docid} – get document by internal ID
• GET /databases/{id}/documentexists/{docid} – check document existence by
internal ID
Database Maintenance/Meta Info
• GET /databases/{id}/updateftindex – update full-text index
• GET /databases/{id}/instances – get list of available instances
• POST /databases/{id}/reset – removes all documents
• POST /databases/{id}/deploy – deploy (overwriting if needed) a database to
the server
• GET /databases/{id}/undeploy – undeploy a database
• GET /database/{id}/stores – get list of available stores
• GET /database/{id}/stores/{store}/indexes – get list of indexes in a store
Document CRUD
• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/…
– POST /documents/{unid} – creates a new document (UNID optional)
• Include doc content in “json” property of posted JSON
• POST /documents/{unid}/content – shorthand to create a document by content
– GET /documents/{unid} – read a document by UNID
• GET /documents/{unid}/content – shorthand to read a document’s content
– PUT /documents/{unid} – update a document by UNID
• PUT /documents/{unid}/content – shorthand to update just a document’s content
– DELETE /documents/{unid} – delete a document by UNID
Document Attachments
• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/documents/{unid}/…
– GET /attachments – get list of attachments
– GET /attachments/{name} – download a named attachment
– HEAD /attachments/{name} – check if a named attachment exists
– POST /attachments/{name} – create a named attachment
– PUT /attachments/{name} – update an existing named attachment
– DELETE /attachments/{name} – delete a named attachment
Indexes (like views)
• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/…
– GET /indexes/{index_id}/documentexists/{key} – check index entry existence
– GET /indexes/{index_id}/documents/{key} – get document by key
– PUT /indexes/{index_id}/documents/{key} – update document by key
– DELETE /indexes/{index_id}/documents/{key} – delete document by key
Document Meta Services
• All paths /$darwino-jstore/databases/{database_id}/stores/{store}/…
– GET /documentexists/{unid} – check existence
– GET /documentdeleted/{unid} – check deleted
– GET /childrencount/{unid} – check child (response) count
– GET /children/{unid} - list of children by parent
– GET /slaves/{unid} – list of slave (associated) documents
– GET /documents/{unid}/social – read social data
– POST /documents/{unid}/social – set social data
Custom Services
• Darwino has the concept of “service factories” for custom REST services
• Intended to be cross-platform, so they work consistently on “full” web
servers as well as embedded mobile/desktop servers
• Each newly-created app has an “AppServiceFactory” class that provides the
entry point to register these
• /$darwino-app URL space
HttpService and HttpServiceContext Classes
• HTTP Services should extend the small abstract class
com.darwino.commons.services.HttpService
• The primary method to implement is service(HttpServiceContext context)
• HttpServiceContext is a consistent representation of HTTP data, similar to
what you’d expect with the javax.servlet classes
– Access to the request data (most get* and is* methods)
– Provides authenticated-user information tied to the Darwino user service
– Access to the response, with convenience methods (e.g. emitJson(…))
Custom Services Example: Social Analyzer
binders.add(new RestServiceBinder("feeds") {
@Override public HttpService createService(HttpServiceContext context, String[] parts) {
return new FeedsService();
}
});
binders.add(new RestServiceBinder("userActions", "assign") {
@Override public HttpService createService(HttpServiceContext context, String[] parts) {
return new AssignUserService();
}
});
binders.add(new RestServiceBinder("users", null) {
@Override public HttpService createService(HttpServiceContext context, String[] parts) {
return new UserInfoService(parts[1]);
}
});
Social Analyzer: FeedsService
JsonObject result = new JsonObject();
for(FeedProvider<?, ?, ?> service : Platform.findExtensions(FeedProvider.class)) {
JsonObject node = new JsonObject();
node.put(PROP_USERCLASS, service.getUserClass().getName());
node.put(PROP_NAME, service.getName());
node.put(PROP_ICONCLASS, service.getIconClass());
node.put(PROP_CLASSNAME, service.getClass().getName());
result.put(service.getKey(), node);
}
context.emitJson(result);
Social Analyzer: AssignUserService
protected void doPost(HttpServiceContext context) throws Exception {
Object jsonObj = context.getContentAsJson();
JsonObject json = (JsonObject)jsonObj;
String unid = json.getAsString("unid");
String name = json.getAsString(PROP_NAME);
Session session = DarwinoContext.get().getSession();
Database database = session.getDatabase(AppDatabaseDef.DATABASE_NAME);
Store store = database.getStore(Database.STORE_DEFAULT);
Document doc = store.loadDocument(unid);
Document userDoc = findOrCreateUserDoc(database, name);
doc.set(PROP_USERDOCUNID, userDoc.getUnid());
doc.set(PROP_USERDOCNAME, userDoc.get(PROP_NAME));
doc.save();
context.emitJson(doc.getJson());
}
Social Analyzer: UserInfoService
public UserInfoService(String userId) {
this.userId = userId;
}
protected void doGet(HttpServiceContext context) throws Exception {
String dn = context.getUserDn();
Session session = DarwinoContext.get().getSession();
Database database = session.getDatabase(AppDatabaseDef.DATABASE_NAME);
Store store = database.getStore(Database.STORE_DEFAULT);
Document userDoc = store.loadDocument(this.userId);
JsonObject result = new JsonObject();
result.put(PROP_USER, userDoc.getJson());
result.put(PROP_KEY, userDoc.getUnid());
// *snip* tons of code
context.emitJson(result);
}
Thank you for your attention!

07 darwino rest services

  • 1.
  • 2.
    Darwino REST Services •Darwino comes with built-in REST services similar to the ExtLib’s DAS or other document data store HTTP APIs • These can form the basis of a full application, or just be used for bootstrapping while custom services are developed • These are in the “/$darwino-jstore” URL space – This is a common convention: “$” URLs can be used to avoid the need to worry about the place in the URL hierarchy
  • 3.
  • 4.
    I hope youlike lists!
  • 5.
    Database Services • GET/databases – returns the list of the available databases • GET /databases/{id} – returns information about a specific database • GET /databases/{id}/documentscount – returns the count of documents • DELETE /databases/{id}/documents – delete all documents • GET /databases/{id}/documents/{docid} – get document by internal ID • GET /databases/{id}/documentexists/{docid} – check document existence by internal ID
  • 6.
    Database Maintenance/Meta Info •GET /databases/{id}/updateftindex – update full-text index • GET /databases/{id}/instances – get list of available instances • POST /databases/{id}/reset – removes all documents • POST /databases/{id}/deploy – deploy (overwriting if needed) a database to the server • GET /databases/{id}/undeploy – undeploy a database • GET /database/{id}/stores – get list of available stores • GET /database/{id}/stores/{store}/indexes – get list of indexes in a store
  • 7.
    Document CRUD • Allpaths /$darwino-jstore/databases/{database_id}/stores/{store}/… – POST /documents/{unid} – creates a new document (UNID optional) • Include doc content in “json” property of posted JSON • POST /documents/{unid}/content – shorthand to create a document by content – GET /documents/{unid} – read a document by UNID • GET /documents/{unid}/content – shorthand to read a document’s content – PUT /documents/{unid} – update a document by UNID • PUT /documents/{unid}/content – shorthand to update just a document’s content – DELETE /documents/{unid} – delete a document by UNID
  • 8.
    Document Attachments • Allpaths /$darwino-jstore/databases/{database_id}/stores/{store}/documents/{unid}/… – GET /attachments – get list of attachments – GET /attachments/{name} – download a named attachment – HEAD /attachments/{name} – check if a named attachment exists – POST /attachments/{name} – create a named attachment – PUT /attachments/{name} – update an existing named attachment – DELETE /attachments/{name} – delete a named attachment
  • 9.
    Indexes (like views) •All paths /$darwino-jstore/databases/{database_id}/stores/{store}/… – GET /indexes/{index_id}/documentexists/{key} – check index entry existence – GET /indexes/{index_id}/documents/{key} – get document by key – PUT /indexes/{index_id}/documents/{key} – update document by key – DELETE /indexes/{index_id}/documents/{key} – delete document by key
  • 10.
    Document Meta Services •All paths /$darwino-jstore/databases/{database_id}/stores/{store}/… – GET /documentexists/{unid} – check existence – GET /documentdeleted/{unid} – check deleted – GET /childrencount/{unid} – check child (response) count – GET /children/{unid} - list of children by parent – GET /slaves/{unid} – list of slave (associated) documents – GET /documents/{unid}/social – read social data – POST /documents/{unid}/social – set social data
  • 11.
    Custom Services • Darwinohas the concept of “service factories” for custom REST services • Intended to be cross-platform, so they work consistently on “full” web servers as well as embedded mobile/desktop servers • Each newly-created app has an “AppServiceFactory” class that provides the entry point to register these • /$darwino-app URL space
  • 12.
    HttpService and HttpServiceContextClasses • HTTP Services should extend the small abstract class com.darwino.commons.services.HttpService • The primary method to implement is service(HttpServiceContext context) • HttpServiceContext is a consistent representation of HTTP data, similar to what you’d expect with the javax.servlet classes – Access to the request data (most get* and is* methods) – Provides authenticated-user information tied to the Darwino user service – Access to the response, with convenience methods (e.g. emitJson(…))
  • 13.
    Custom Services Example:Social Analyzer binders.add(new RestServiceBinder("feeds") { @Override public HttpService createService(HttpServiceContext context, String[] parts) { return new FeedsService(); } }); binders.add(new RestServiceBinder("userActions", "assign") { @Override public HttpService createService(HttpServiceContext context, String[] parts) { return new AssignUserService(); } }); binders.add(new RestServiceBinder("users", null) { @Override public HttpService createService(HttpServiceContext context, String[] parts) { return new UserInfoService(parts[1]); } });
  • 14.
    Social Analyzer: FeedsService JsonObjectresult = new JsonObject(); for(FeedProvider<?, ?, ?> service : Platform.findExtensions(FeedProvider.class)) { JsonObject node = new JsonObject(); node.put(PROP_USERCLASS, service.getUserClass().getName()); node.put(PROP_NAME, service.getName()); node.put(PROP_ICONCLASS, service.getIconClass()); node.put(PROP_CLASSNAME, service.getClass().getName()); result.put(service.getKey(), node); } context.emitJson(result);
  • 15.
    Social Analyzer: AssignUserService protectedvoid doPost(HttpServiceContext context) throws Exception { Object jsonObj = context.getContentAsJson(); JsonObject json = (JsonObject)jsonObj; String unid = json.getAsString("unid"); String name = json.getAsString(PROP_NAME); Session session = DarwinoContext.get().getSession(); Database database = session.getDatabase(AppDatabaseDef.DATABASE_NAME); Store store = database.getStore(Database.STORE_DEFAULT); Document doc = store.loadDocument(unid); Document userDoc = findOrCreateUserDoc(database, name); doc.set(PROP_USERDOCUNID, userDoc.getUnid()); doc.set(PROP_USERDOCNAME, userDoc.get(PROP_NAME)); doc.save(); context.emitJson(doc.getJson()); }
  • 16.
    Social Analyzer: UserInfoService publicUserInfoService(String userId) { this.userId = userId; } protected void doGet(HttpServiceContext context) throws Exception { String dn = context.getUserDn(); Session session = DarwinoContext.get().getSession(); Database database = session.getDatabase(AppDatabaseDef.DATABASE_NAME); Store store = database.getStore(Database.STORE_DEFAULT); Document userDoc = store.loadDocument(this.userId); JsonObject result = new JsonObject(); result.put(PROP_USER, userDoc.getJson()); result.put(PROP_KEY, userDoc.getUnid()); // *snip* tons of code context.emitJson(result); }
  • 17.
    Thank you foryour attention!