diff options
author | Matt Windsor <mattwindsor@btinternet.com> | 2011-03-24 14:45:36 +0000 |
---|---|---|
committer | Matt Windsor <mattwindsor@btinternet.com> | 2011-03-24 14:45:36 +0000 |
commit | edf99237651912c729ae89464ca8cc1305c55957 (patch) | |
tree | eb4facbffd59c0d7fc590af412c5a1523c50402d /src/uk | |
parent | cb0a78131286b1a312351308d2cb6e59ed122fef (diff) |
Documentation refresh; classpath update; CODECHANGE: moved content serving in request handling to abstract request handler.
Diffstat (limited to 'src/uk')
4 files changed, 65 insertions, 53 deletions
diff --git a/src/uk/org/ury/backend/handlers/LibraryRequestHandler.java b/src/uk/org/ury/backend/handlers/LibraryRequestHandler.java index b175485..dd992a4 100644 --- a/src/uk/org/ury/backend/handlers/LibraryRequestHandler.java +++ b/src/uk/org/ury/backend/handlers/LibraryRequestHandler.java @@ -1,6 +1,14 @@ -/** +/* + * LibraryRequestHandler.java + * -------------------------- + * + * Part of the URY Backend Platform + * + * V0.00 2011/03/24 * + * (C) 2011 URY Computing */ + package uk.org.ury.backend.handlers; import java.io.UnsupportedEncodingException; @@ -12,9 +20,6 @@ import java.util.Map; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.entity.StringEntity; -import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import uk.org.ury.backend.database.DatabaseDriver; @@ -34,9 +39,6 @@ import uk.org.ury.common.library.LibraryUtils; import uk.org.ury.common.library.exceptions.EmptySearchException; import uk.org.ury.common.library.item.LibraryItem; import uk.org.ury.common.protocol.Directive; -import uk.org.ury.common.protocol.ProtocolUtils; -import uk.org.ury.common.protocol.Status; -import uk.org.ury.common.protocol.exceptions.EncodeFailureException; /** * A request handler for library queries. @@ -74,7 +76,6 @@ public class LibraryRequestHandler extends AbstractRequestHandler { * @throws HandleFailureException * if an error occurs that thwarts the handling of the request. */ - private void doSearch(Map<String, String> parameters, Map<String, Object> response, Server server) throws HandleFailureException { @@ -170,23 +171,7 @@ public class LibraryRequestHandler extends AbstractRequestHandler { } else { throw new UnknownFunctionException(path); } - - 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); + serveContent(request, response, content); } } 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 diff --git a/src/uk/org/ury/backend/server/HttpHandler.java b/src/uk/org/ury/backend/server/HttpHandler.java index 2792cd7..5210c55 100644 --- a/src/uk/org/ury/backend/server/HttpHandler.java +++ b/src/uk/org/ury/backend/server/HttpHandler.java @@ -1,8 +1,8 @@ /* * HttpHandler.java - * --------------------- + * ---------------- * - * Part of the URY Server Platform + * Part of the URY Backend Platform * * V0.00 2011/03/20 * @@ -48,7 +48,6 @@ import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.entity.StringEntity; -import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpRequestHandler; @@ -57,10 +56,6 @@ import uk.org.ury.backend.server.exceptions.HandleFailureException; import uk.org.ury.backend.server.exceptions.HandlerNotFoundException; import uk.org.ury.backend.server.exceptions.HandlerSetupFailureException; import uk.org.ury.backend.server.exceptions.NotAHandlerException; -import uk.org.ury.common.protocol.Directive; -import uk.org.ury.common.protocol.ProtocolUtils; -import uk.org.ury.common.protocol.Status; -import uk.org.ury.common.protocol.exceptions.EncodeFailureException; /** * @author Matt Windsor, Apache Software Foundation @@ -184,25 +179,7 @@ public class HttpHandler extends AbstractRequestHandler implements throw new HandlerSetupFailureException(className, e); } - // Everything seems OK, so make the response. - - 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(className, e); - } catch (EncodeFailureException e) { - throw new HandleFailureException(e); - } - - entity.setContentType(HTTP.PLAIN_TEXT_TYPE); - response.setEntity(entity); + serveContent(request, response, content); } } } diff --git a/src/uk/org/ury/backend/server/Server.java b/src/uk/org/ury/backend/server/Server.java index d1f7384..fe8ca32 100644 --- a/src/uk/org/ury/backend/server/Server.java +++ b/src/uk/org/ury/backend/server/Server.java @@ -2,7 +2,7 @@ * Server.java * ----------- * - * Part of the URY Server Platform + * Part of the URY Backend Platform * * V0.00 2011/03/20 * |