Tuesday, March 06, 2007

json with domino servlets

I have a domino servlet which basically receive a GET like: http://myserver/servlet/displayfolder?fld=name_of_folder

and should return that folder's server databases with couple of information: pathfile,database title,template name of the database.

Before discovering JSON, I've created my own convension for this string of parameters. The response to the browser was parsed by custom JS code, respecting my chosen syntax. This was not good, this was not standard but it worked.

Starting my JSON discovery (every day we learn something new), I've also learned that my required information about a server's folder can be put in a JSON string as following:

[{path:folder1\aaa.nsf,title:title_aaa,template:template_aaa},
{path:folder1\bbb.nsf,title:title_bbb,template:template_bbb},
{path:folder1\ccc.nsf,title:title_ccc,template:template_ccc}]

Above string is an JSON Array containing three JSON Objects, each with the path,title,template attributes (read http://json.org for more details about the syntax)

Now comes the big question: how to generate above from within my Domino servlet ? Fortunatelly, there are some JSON Java classes which comes to help.

In fact there are several frameworks for working with JSON under Java. For me as Domino dev, the first choice does the trick, as I'm gonna show you.

So, my objective was to add support for JSON into the Domino server JVM, available for my servlet.
- From the mentioned page I've downloaded the json.zip archive which contains all the *.org.json classes as source code.
- Next, I've added these classes to my eclipse project where I play with the Domino servlets - no, I'm not gonna tell you how to work with eclipse+domino, there are plenty of good articles already written :)
- build the eclipse project so that I could get the .class of the json libraries.
- archived the compiled classes into a json.jar archive
- upload the json.jar into domino\jvm\lib\ext on the server, so that they're available to other servlets I will be playing with (now jQuery/JSON is for me a standard when I talk about domino-ajax apps)
- give the server a restart and rethink the servlet, which now has the following code on the doGet function:


...
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import lotus.domino.Database;
import lotus.domino.DbDirectory;
import lotus.domino.NotesException;
import lotus.domino.NotesFactory;
import lotus.domino.NotesThread;
import lotus.domino.Session;
...

public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {

try {
res.setContentType("text");
PrintWriter toBrowser = res.getWriter();

NotesThread.sinitThread();
Session session = NotesFactory.createSession();

String query = req.getQueryString();
String searchFolder = parseQueryString (query);
if (searchFolder == null ) {
throw (new ServletException ("servlet should receive a query param like &fld=")); }

DbDirectory dir = session.getDbDirectory(null);
String server = session.getServerName();
Database db = dir.getFirstDatabase(DbDirectory.DATABASE);

//create the JSON Array which will hold all other objects
JSONArray jsonArray = new JSONArray();

//loop though all server databases
while (db != null) {
String fp = db.getFilePath();
StringTokenizer fpTokens = new StringTokenizer (fp, "\\") ;
while (fpTokens.hasMoreTokens()){
String token = fpTokens.nextToken().toLowerCase();
// System.out.println(token);
if (token.equals(searchFolder.toLowerCase())){
JSONObject jsonObject = new JSONObject();
String file = fp ;
String title = db.getTitle();
String template= db.getDesignTemplateName();

jsonObject.put("file", file);
jsonObject.put("title", title);
jsonObject.put("template", template);
jsonArray.put(jsonObject);
jsonObject = null ;
}
}

db = dir.getNextDatabase();
}
toBrowser.print(jsonArray) ;
session.recycle();
}
catch (JSONException e) {
e.printStackTrace();
}

catch (NotesException e) {
e.printStackTrace();
}
catch (ServletException e) {
e.printStackTrace();
}
finally {
NotesThread.stermThread();

}
}


please criticize me for the Java code, i'm kind of a newbie, I'm sure this can be improved :)

If you code Java agents instead of servlets, I'm sure you can get the same Java source classes and add them directly into your agent. I think it will grow its size, though ... Or you can pack the compiled classes into a similar json.jar and import it into the Java agent.

What I was surprised about was that no compilation errors were thrown, which makes JSON available in Domino Java agents or servlets starting at least with Domino 6.5.x which is my current server version.
Post a Comment

Disclaimer: The information in this weblog is provided "AS IS" with no warranties whatsoever, and confers no rights to any institution/person/organization. These are my personal thoughts and ideas. When I am inspired by others I give credit, I do not assume other's work and efforts. This site is NOT supported, approved, blessed etc. by Lotus Software and/or IBM Corporation.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 License.