All blog posts, code samples and downloads licensed under Apache License 2.0.

Creating your own simple REST service - without ExtLib

Oliver Busse on 04.04.2013 17:21:27 CEDT, filed under Java ExtLib 

Jeff Byrd posted a cool way to create your own REST service to access any view of your application. His code needs the Extension Library from OpenNTF or IBM Notes/Domino 9 though, so applications running on older releases or without the ExtLib cannot use it.

This post shows a way to use REST without the ExtLib.


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lotus.domino.Database;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.ViewColumn;
import lotus.domino.ViewEntry;
import lotus.domino.ViewNavigator;


public class REST {

	String viewName = null;

	public REST() {


	public String invoke(HttpServletRequest request,
			HttpServletResponse response, String viewName) {
		this.viewName = viewName;
		return doPost(request, response);

	public String doGet(HttpServletRequest request, HttpServletResponse response) {
		return doPost(request, response);

	public String doPost(HttpServletRequest request,
			HttpServletResponse response) {
		String strValue = "";

		Database database = null;
		View view = null;
		ViewNavigator nav = null;
		int count = 0;
		int totalCount = 0;
		try {

			// Get the paging parameters
			int start = Integer.parseInt(request.getParameter("start"));
			int limit = Integer.parseInt(request.getParameter("limit"));

			// Set the response headers
			response.setHeader("Cache-Control", "no-cache");

			database = getCurrentSession().getCurrentDatabase();
			view = database.getView(this.viewName);

			if (view != null) {
				// get the total number of entries in the view
				totalCount = view.getEntryCount();
				nav = view.createViewNav();
				// No need to buffer any more entries than we actually need
				// try to skip <start> entries and return the # of entries
				// actually skipped
				int skippedEntries = nav.skip(start);

				if (skippedEntries == start) {

					// Get the View Column Names ( or titles )
					Map<Integer, String> columnNameMap = new HashMap<Integer, String>();
					for (ViewColumn col : (List<ViewColumn>) view.getColumns()) {
						if (col.getColumnValuesIndex() < 65535) {
							// We can return the Column Names or Column Items
							// columnNameMap.put(col.getColumnValuesIndex(),
							// col.getTitle());
							columnNameMap.put(col.getColumnValuesIndex(), col

					// This list of employee information will be turned into
					// JSON.
					List emplData = new ArrayList();

					// read the current entry after the skip operation
					ViewEntry entry = nav.getCurrent();

					while (entry != null && count <= (limit - 1)) {
						if (!entry.isCategory()) {
							// Get a list of the column values
							List<Object> columnValues = entry.getColumnValues();
							// Create a map of column:value pairs
							HashMap<String, String> entryMap = new HashMap<String, String>();
							entryMap.put("@UNID", entry.getUniversalID());
							entryMap.put("@position", entry.getPosition('.'));
							for (Integer index : columnNameMap.keySet())

							// Add the entry map to the data variable that was
							// passed in
						ViewEntry tmpentry = nav.getNext(entry);
						entry = tmpentry;
					} // while

					// Create a JSON object to wrap the employee Json array and
					// provide the root element items
					JsonJavaObject returnJSON = new JsonJavaObject();
					// set success to true
					returnJSON.put("success", true);
					// set the total number of Items
					returnJSON.put("total", totalCount);
					// set the data element to the employee JSON list.
					returnJSON.put("data", emplData);
					// Return a JSON string generated from our JsonJavaObject
					strValue = JsonGenerator.toJson(JsonJavaFactory.instanceEx,



			database = null;

		} catch (NotesException e) {
			strValue = "{\"success\":false,\"message\":\"" + e.getMessage()
					+ "\"}";
		} catch (Exception e) {
			strValue = "{\"success\":false,\"message\":\"" + e.getMessage()
					+ "\"}";
		return strValue;

	public static Session getCurrentSession() {
		FacesContext context = FacesContext.getCurrentInstance();
		return (Session) context.getApplication().getVariableResolver()
				.resolveVariable(context, "session");

Notice the method getCurrentSession. It is described here and combines the cool variable resolver with the great example from Jeff.

Original code by Jeff Byrd.

Tagged with rest extlib json view