aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/server/HttpHandler.java
diff options
context:
space:
mode:
authorMatt Windsor <mattwindsor@btinternet.com>2011-03-20 17:09:11 +0000
committerMatt Windsor <mattwindsor@btinternet.com>2011-03-20 17:09:11 +0000
commit0645ec66e1618ce4b52e56212c79196dd7c5c608 (patch)
tree95b103c7577b4039eabe18cfda7ee8e7ca80e61b /src/uk/org/ury/server/HttpHandler.java
parentcee53b021632c95f1b4882664a31ca639a9b0700 (diff)
Begin migration to HttpCore request handler pattern. Delete protocol duplicates left over from last commit.
Diffstat (limited to 'src/uk/org/ury/server/HttpHandler.java')
-rw-r--r--src/uk/org/ury/server/HttpHandler.java154
1 files changed, 8 insertions, 146 deletions
diff --git a/src/uk/org/ury/server/HttpHandler.java b/src/uk/org/ury/server/HttpHandler.java
index 41b4dc4..948e03a 100644
--- a/src/uk/org/ury/server/HttpHandler.java
+++ b/src/uk/org/ury/server/HttpHandler.java
@@ -39,16 +39,11 @@
package uk.org.ury.server;
-import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
-import java.net.URLDecoder;
-import java.util.HashMap;
-import java.util.Locale;
import java.util.Map;
-import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
@@ -64,64 +59,25 @@ import uk.org.ury.server.exceptions.BadRequestException;
import uk.org.ury.server.exceptions.HandleFailureException;
import uk.org.ury.server.exceptions.HandlerNotFoundException;
import uk.org.ury.server.exceptions.HandlerSetupFailureException;
-import uk.org.ury.server.exceptions.HandlingException;
import uk.org.ury.server.exceptions.NotAHandlerException;
/**
* @author Matt Windsor, Apache Software Foundation
*/
-public class HttpHandler implements HttpRequestHandler {
- private Server server;
+public class HttpHandler extends AbstractRequestHandler implements
+ HttpRequestHandler {
/**
* Construct a new HttpHandler.
*
* @param server
- * The server whose HTTP requests are to be handled by this
- * handler.
- */
- public HttpHandler(Server server) {
- this.server = server;
- }
-
- /**
- * Handle a HTTP request.
- *
- * @param request
- * The HTTP request.
+ * The instance of the URY server responsible for the request.
*
- * @param response
- * The response that the handler will populate during the
- * handling of the request.
- *
- * @param context
- * The HTTP context.
+ * @param mount
+ * The directory to which this handler is to be mounted.
*/
- @Override
- public void handle(HttpRequest request, HttpResponse response,
- HttpContext context) throws HttpException, IOException {
- String method = request.getRequestLine().getMethod()
- .toUpperCase(Locale.ENGLISH);
-
- if (method.equals("GET")) {
- try {
- handleGet(request, response, context);
- } catch (HandlerNotFoundException e) {
- // TODO: log
- serveError(request, response, HttpStatus.SC_NOT_FOUND,
- e.getMessage());
- } catch (BadRequestException e) {
- // TODO: log
- serveError(request, response, HttpStatus.SC_BAD_REQUEST,
- e.getMessage());
- } catch (HandlingException e) {
- serveError(request, response,
- HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
- }
- } else {
- serveError(request, response, HttpStatus.SC_NOT_IMPLEMENTED,
- "Method not implemented.");
- }
+ public HttpHandler(Server server, String mount) {
+ super(server, mount);
}
/**
@@ -156,6 +112,7 @@ public class HttpHandler implements HttpRequestHandler {
* if the class requested to handle the request is not a
* handler.
*/
+ @Override
public void handleGet(HttpRequest request, HttpResponse response,
HttpContext context) throws HandlerNotFoundException,
HandlerSetupFailureException, HandleFailureException,
@@ -245,99 +202,4 @@ public class HttpHandler implements HttpRequestHandler {
response.setEntity(entity);
}
}
-
- /**
- * Serve a HTTP plain-text error as the HTTP response for a request.
- *
- * @param request
- * The request that is being responded to.
- *
- * @param response
- * The response to populate with the error message.
- *
- * @param code
- * HTTP status code to use.
- *
- * @param reason
- * The reason to display to the client.
- */
- private void serveError(HttpRequest request, HttpResponse response,
- int code, String reason) {
- // Get the reason string to put in the error response.
- String statusReason = "";
-
- switch (code) {
- case HttpStatus.SC_BAD_REQUEST:
- statusReason = "Bad Request";
- break;
- case HttpStatus.SC_NOT_FOUND:
- statusReason = "Not Found";
- break;
- default:
- case HttpStatus.SC_INTERNAL_SERVER_ERROR:
- statusReason = "Internal Server Error";
- break;
- }
-
- response.setStatusLine(request.getProtocolVersion(), code, statusReason);
- StringEntity entity = null;
-
- try {
- Map<String, Object> content = new HashMap<String, Object>();
-
- content.put(Directive.STATUS.toString(), Status.ERROR.toString());
- content.put(Directive.REASON.toString(), reason);
-
- entity = new StringEntity(JSONValue.toJSONString(content));
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- if (entity != null) {
- entity.setContentType(HTTP.PLAIN_TEXT_TYPE);
- response.setEntity(entity);
- }
- }
-
- /**
- * Parse a query string, populating a key-value map of the URL-unescaped
- * results.
- *
- * @param query
- * The query string to parse.
- *
- * @return A map associating parameter keys and values.
- *
- * @throws UnsupportedEncodingException
- * if the URL decoder fails.
- */
- public Map<String, String> parseQueryString(String query)
- throws UnsupportedEncodingException {
- Map<String, String> params = new HashMap<String, String>();
-
- // At least one parameter
- if (query != null && query.endsWith("&") == false) {
- String[] qsplit = { query };
-
- // More than one parameter - split the query.
- if (query.contains("&"))
- qsplit = query.split("&");
-
- for (String param : qsplit) {
- // Has a value
- if (param.contains("=") && param.endsWith("=") == false) {
- String[] paramsplit = param.split("=");
- params.put(URLDecoder.decode(paramsplit[0], "UTF-8"),
- URLDecoder.decode(paramsplit[1], "UTF-8"));
- }
- // Doesn't have a value
- else if (param.contains("=") == false) {
- params.put(URLDecoder.decode(param, "UTF-8"), null);
- }
- }
- }
-
- return params;
- }
}