aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/backend/server/AbstractRequestHandler.java
diff options
context:
space:
mode:
authorMatt Windsor <mattwindsor@btinternet.com>2011-03-24 14:45:36 +0000
committerMatt Windsor <mattwindsor@btinternet.com>2011-03-24 14:45:36 +0000
commitedf99237651912c729ae89464ca8cc1305c55957 (patch)
treeeb4facbffd59c0d7fc590af412c5a1523c50402d /src/uk/org/ury/backend/server/AbstractRequestHandler.java
parentcb0a78131286b1a312351308d2cb6e59ed122fef (diff)
Documentation refresh; classpath update; CODECHANGE: moved content serving in request handling to abstract request handler.
Diffstat (limited to 'src/uk/org/ury/backend/server/AbstractRequestHandler.java')
-rw-r--r--src/uk/org/ury/backend/server/AbstractRequestHandler.java52
1 files changed, 51 insertions, 1 deletions
diff --git a/src/uk/org/ury/backend/server/AbstractRequestHandler.java b/src/uk/org/ury/backend/server/AbstractRequestHandler.java
index e545651..cb38603 100644
--- a/src/uk/org/ury/backend/server/AbstractRequestHandler.java
+++ b/src/uk/org/ury/backend/server/AbstractRequestHandler.java
@@ -1,6 +1,14 @@
-/**
+/*
+ * AbstractRequestHandler.java
+ * ---------------------------
+ *
+ * Part of the URY Backend Platform
*
+ * V0.00 2011/03/24
+ *
+ * (C) 2011 URY Computing
*/
+
package uk.org.ury.backend.server;
import java.io.IOException;
@@ -98,6 +106,48 @@ public abstract class AbstractRequestHandler implements HttpRequestHandler {
}
/**
+ * Serves a key-value map as a protocol-encoded response.
+ *
+ * @param request
+ * The request that is being responded to.
+ *
+ * @param response
+ * The response to populate with the contents provided.
+ *
+ * @param contents
+ * The key-value map of contents to be encoded and served.
+ *
+ * @throws HandlerSetupFailureException
+ * if the handler was found but could not be set up (eg does not
+ * implement appropriate interface or cannot be instantiated).
+ *
+ * @throws HandleFailureException
+ * if an appropriate handler was contacted, but it failed to
+ * process the request.
+ */
+ protected final void serveContent(HttpRequest request,
+ HttpResponse response, Map<String, Object> content)
+ throws HandlerSetupFailureException, HandleFailureException {
+ response.setStatusLine(request.getProtocolVersion(), HttpStatus.SC_OK,
+ "OK");
+
+ content.put(Directive.STATUS.toString(), Status.OK.toString());
+
+ StringEntity entity = null;
+
+ try {
+ entity = new StringEntity(ProtocolUtils.encode(content));
+ } catch (UnsupportedEncodingException e) {
+ throw new HandlerSetupFailureException(getClass().getName(), e);
+ } catch (EncodeFailureException e) {
+ throw new HandleFailureException(e);
+ }
+
+ entity.setContentType(HTTP.PLAIN_TEXT_TYPE);
+ response.setEntity(entity);
+ }
+
+ /**
* Serves a HTTP plain-text error as the HTTP response for a request.
*
* @param request