Skip to content

Instantly share code, notes, and snippets.

@lpar
Last active July 21, 2023 20:16
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lpar/5904196 to your computer and use it in GitHub Desktop.
Save lpar/5904196 to your computer and use it in GitHub Desktop.
Minimal example of a 4-verb REST service in IBM Domino XPages, with no external dependencies.
<?xml version="1.0" encoding="UTF-8"?>
<!-- XPages source for REST service. -->
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse><![CDATA[#{javascript:
com.ath0.xrest.Service.dispatch();
}]]></xp:this.afterRenderResponse>
</xp:view>
/**
* Copyright 2013 IBM Corporation.
* Distributed under the terms of the Apache License, Version 2.0.
* http://www.apache.org/licenses/LICENSE-2.0.
*/
package com.ath0.xrest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Example code showing a minimal implementation of a REST service for
* IBM Domino XPages.
*
* Note that URL parameters in params are still processed by the XPages
* runtime, and not the raw servlet runtime. This leads to some limitations
* on allowed syntax, and means you can end up with parameters like
* 'OpenXPage' in the map.
*
* @author meta@pobox.com
*/
public class Service {
// These are the standard external contexts you would access in
// server-side JavaScript.
private static FacesContext faccon;
private static ExternalContext extcon;
private static HttpServletRequest request;
private static HttpServletResponse response;
// This holds web form HTTP POST parameters, if there are any.
private static Map<String, String[]> params;
// HTTP requires that lines be terminated with CRLF, even if the server
// is Linux, so I use this to end lines instead of writeln.
private static final String CRLF = "\r\n";
// The MIME type used for form data.
private static final String FORM_DATA = "application/x-www-form-urlencoded";
private static final String HTTP_POST = "POST";
private static final String HTTP_GET = "GET";
private static final String HTTP_PUT = "PUT";
private static final String HTTP_DELETE = "DELETE";
public Service() {
faccon = FacesContext.getCurrentInstance();
extcon = faccon.getExternalContext();
request = (HttpServletRequest) extcon.getRequest();
response = (HttpServletResponse) extcon.getResponse();
}
@SuppressWarnings("unchecked")
public void run() {
String ctype = request.getContentType();
String method = request.getMethod();
try {
if (HTTP_POST.equalsIgnoreCase(method) && FORM_DATA.equalsIgnoreCase(ctype)) {
params = request.getParameterMap();
this.post();
} else if (HTTP_POST.equalsIgnoreCase(method)) {
this.post();
} else if (HTTP_GET.equalsIgnoreCase(method)) {
this.get();
} else if (HTTP_PUT.equalsIgnoreCase(method)) {
this.put();
} else if (HTTP_DELETE.equalsIgnoreCase(method)) {
this.delete();
}
} catch (Exception ex) {
try {
faccon.getResponseWriter().write(ex.getMessage());
} catch (IOException ioe) {
// Not much we can do
}
response.setStatus(501);
}
}
/**
* Override or alter this to implement your GET operation.
* @throws IOException
*/
public void get() throws IOException {
dump();
}
/**
* Override or alter this to implement your GET operation.
* @throws IOException
*/
public void post() throws IOException {
dump();
}
/**
* Override or alter this to implement your GET operation.
* @throws IOException
*/
public void put() throws IOException {
dump();
}
/**
* Override or alter this to implement your GET operation.
* @throws IOException
*/
public void delete() throws IOException {
dump();
}
/**
* Sample code which dumps out the REST request received.
* @throws IOException
*/
static public void dump() throws IOException {
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("utf-8");
response.setStatus(200); // HTTP OK
ResponseWriter writer = faccon.getResponseWriter();
writer.write("Received HTTP " + request.getMethod() + "." + CRLF);
writer.write("Request content-type = " + request.getContentType() + CRLF);
writer.flush();
BufferedReader r = null;
try {
r = request.getReader();
if (r.ready()) {
String line = r.readLine();
writer.write("First line of body data:");
writer.write(line);
writer.write(CRLF);
}
} catch (Exception e) {
writer.write("No body data readable: " + e.getMessage() + "." + CRLF);
} finally {
if (r != null) {
r.close();
}
}
if (params != null) {
for (Entry<String, String[]> parm: params.entrySet()) {
writer.write("POST parameter ");
writer.write(parm.getKey());
writer.write(" = ");
writer.write(parm.getValue()[0]);
writer.write(CRLF);
}
} else {
writer.write("Parameter map was null." + CRLF);
}
// Here's how you end your output:
writer.endDocument();
faccon.responseComplete();
}
public static final void dispatch() {
Service me = new Service();
me.run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment