DomJson OSGI plug-in is a easy way to get content of a Notes document as json
when using a Lotus Domino server.
UPDATE
I have create a projekt on openntf.org named DomJson
You can get all items (only some type are supported) or seleced items from
a Notes document.
Once installed on the Domino server you can use it on all databases - just
by typing <path to database>/domjson?unid=<unid>. If <unid>
does not exist in the database {} is return as result.
http://www.hostname.org/names.nsf/domjson?unid=F7939F01DB8962CEC125792000441CA1
will return all field like
{
Form:"Person",
Type:"Person",
Owner:"CN=Michael Nielsen/O=demo",
--- line cut out --
MailSystem:"1",
FirstName:"Michael",
MiddleInitial:"",
LastName:"Nielsen",
FullName:["CN=Michael Nielsen/O=demo","Michael Nielsen"]
--- line cut out --
}
http://www.hostname.org/names.nsf/domjson?unid=F7939F01DB8962CEC125792000441CA1&fields=FullName~Form
will return only FullName and Form
{
Form:"Person",
FullName:["CN=Michael Nielsen/O=demo","Michael Nielsen"]
}
Install:
The plug-in is droped in subfolder under <Domino>\\data\\domino\\workspace\\applications\\eclipse.
dk.jarry.plugin.domino.osgi.domjson.feature_1.0.0.201110122133.jar
goes into <Domino>\\data\\domino\\workspace\\applications\\eclipse\\feature and dk.jarry.plugin.domino.osgi.domjson_1.0.0.201110122133.jar
goes into <Domino>\\data\\domino\\workspace\\applications\\eclipse\\plugins.
Supported item type
Item.AUTHORS
Item.DATETIMES
Item.NAMES
Item.NUMBERS
Item.READERS
Item.RICHTEXT
Item.TEXT
Downloads
dk.jarry.plugin.domino.osgi.domjson.feature_1.0.0.201110122133.jar
dk.jarry.plugin.domino.osgi.domjson_1.0.0.201110122133.jar
dk.jarry.plugin.domino.osgi.domjson.zip (sourcecode)
References
Servlet Sample
JSON and REST Samples
OpenNTF - New Sample on OpenNTF: How to write Servlets and Video how to set up IDE
Code
package dk.jarry.plugin.domino.osgi;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.StringCharacterIterator;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lotus.domino.Database;
import lotus.domino.DateTime;
import lotus.domino.Document;
import lotus.domino.Item;
import lotus.domino.Session;
import com.ibm.domino.osgi.core.context.ContextInfo;
/**
* This class return all or selected fields from a document as json.<br />
* <br />
* Input is:
* <ul>
* <li>unid - required</li>
* <li>fields - optional (use ~ as separator)</li>
* <li>jsoncallback - optional</li>
* </ul>
* <br />
*
* @author Michael Bornholdt Nielsen
*
*/
public class DomJson extends HttpServlet {
private static final long serialVersionUID = -993328140261388062L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/json;charset=utf-8");
PrintWriter out = resp.getWriter();
try {
String unid = req.getParameter("unid");
String fields = req.getParameter("fields");
String jsoncallback = req.getParameter("jsoncallback");
String fieldarray[] = null;
if (fields != null) {
fieldarray = fields.split("~");
}
if (jsoncallback != null) {
out.print(jsoncallback + "(");
}
// json start
out.print("\n{");
if (unid != null) {
try {
Database db = ContextInfo.getUserDatabase();
Session session = db.getParent();
Document doc = db.getDocumentByUNID(unid);
boolean firstItem = true;
if (doc != null) {
Item item = null;
if (fieldarray != null) {
for (String field : fieldarray) {
if (doc.hasItem(field)) {
item = doc.getFirstItem(field);
printJsonItem(out, session, item,
firstItem, null);
if (item != null) {
item.recycle();
}
}
firstItem = false;
}
} else {
Iterator<Item> it = doc.getItems().iterator();
while (it.hasNext()) {
item = it.next();
printJsonItem(out, session, item, firstItem,
null);
if (item != null) {
item.recycle();
}
firstItem = false;
}
}
}
doc.recycle();
db.recycle();
session.recycle();
} catch (Exception e) {
// Be quiet.
}
}
// json end
out.print("\n}");
if (jsoncallback != null) {
out.print("\n)");
}
} catch (Exception e) {
// Be quiet.
}
out.flush();
out.close();
}
private void printJsonItem(PrintWriter out, Session session, Item item,
boolean firstItem, String format) {
Vector<?> v = null;
Iterator<?> it = null;
boolean firstValue = true;
try {
if (item != null) {
if (item.getType() == Item.AUTHORS
|| item.getType() == Item.DATETIMES
|| item.getType() == Item.NAMES
|| item.getType() == Item.NUMBERS
|| item.getType() == Item.READERS
|| item.getType() == Item.RICHTEXT
|| item.getType() == Item.TEXT) {
v = item.getValues();
if (v != null) {
out.print((firstItem ? "\n\t" : ",\n\t")
+ item.getName() + ":");
if (v.size() > 1) {
out.print("[");
it = v.iterator();
while (it.hasNext()) {
out.print((firstValue ? "" : ",")
+ "\""
+ getValueString(session, it.next(),
item.getType()) + "\"");
firstValue = false;
}
firstValue = true;
out.print("]");
} else {
out.print("\""
+ getValueString(session, v.get(0), item
.getType()) + "\"");
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
// Be quiet.
}
}
private String getValueString(Session session, Object obj, int type) {
String valueString = "";
DateTime dt = null;
try {
switch (type) {
case Item.RICHTEXT:
valueString = obj.toString();
break;
case Item.DATETIMES:
dt = (DateTime) obj;
valueString = dt.getLocalTime();
dt.recycle();
break;
case Item.NUMBERS:
valueString = obj.toString();
break;
case Item.TEXT:
valueString = obj.toString();
break;
default:
valueString = obj.toString();
}
if (dt != null) {
dt.recycle();
}
} catch (Exception e) {
// Be quiet.
}
return addSlashes(valueString);
}
private static String addSlashes(String text) {
final StringBuffer sb = new StringBuffer(text.length() * 2);
final StringCharacterIterator iterator = new StringCharacterIterator(
text);
char character = iterator.current();
while (character != StringCharacterIterator.DONE) {
if (character == '"')
sb.append("\\\"");
else if (character == '\'')
sb.append("\\\'");
else if (character == '\\')
sb.append("\\\\");
else if (character == '\n')
sb.append("\\n");
else if (character == '{')
sb.append("\\{");
else if (character == '}')
sb.append("\\}");
else
sb.append(character);
character = iterator.next();
}
return sb.toString();
}
}
|
Created 13/10-2011 by Michael Bornholdt Nielsen