aboutsummaryrefslogtreecommitdiff
path: root/src/uk/org/ury/server/HttpHandler.java
blob: 41b4dc4005145ee1426cc52860664c5e6940ba7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * HttpHandler.java
 * ---------------------
 * 
 * Part of the URY Server Platform
 * 
 * V0.00  2011/03/20
 * 
 * (C) 2011 URY Computing
 * 
 * Based on the HttpCore example code, which is available under the 
 * Apache License, version 2.0; the copyright notice provided with 
 * said code follows.
 * 
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

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;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.json.simple.JSONValue;

import uk.org.ury.protocol.Directive;
import uk.org.ury.protocol.Status;
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;

    /**
     * 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.
     * 
     * @param response
     *            The response that the handler will populate during the
     *            handling of the request.
     * 
     * @param context
     *            The HTTP context.
     */
    @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.");
	}
    }

    /**
     * Handle a HTTP GET request.
     * 
     * @param request
     *            The HTTP request.
     * 
     * @param response
     *            The response that the handler will populate during the
     *            handling of the request.
     * 
     * @param context
     *            The HTTP context.
     * 
     * @throws HandlerNotFoundException
     *             if the client requested a request handler that could not be
     *             found on the class path.
     * 
     * @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.
     * 
     * @throws BadRequestException
     *             if the request was malformed or invalid.
     * 
     * @throws NotAHandlerException
     *             if the class requested to handle the request is not a
     *             handler.
     */
    public void handleGet(HttpRequest request, HttpResponse response,
	    HttpContext context) throws HandlerNotFoundException,
	    HandlerSetupFailureException, HandleFailureException,
	    BadRequestException, NotAHandlerException {
	String path = request.getRequestLine().getUri();

	if (path.equals("/index.html") || path.equals("/")) {
	    // Someone's trying to get the index page!
	    // Humour them.

	    response.setStatusLine(request.getProtocolVersion(),
		    HttpStatus.SC_OK, "OK");

	    StringEntity entity = null;

	    try {
		entity = new StringEntity(Server.DOCTYPE + Server.INDEX_HTML);
	    } catch (UnsupportedEncodingException e) {
		throw new HandlerSetupFailureException("(Index page)", e);
	    }

	    entity.setContentType("text/html");

	    response.setEntity(entity);
	} else {
	    // Convert this into a URL and fan out the various parts of it.

	    URL pathURL = null;

	    try {
		pathURL = new URL("http://localhost" + path);
	    } catch (MalformedURLException e) {
		throw new BadRequestException(e);
	    }

	    String className = "uk.org.ury"
		    + pathURL.getPath().replace('/', '.');
	    System.out.println(className);
	    Class<?> newClass = null;

	    try {
		newClass = Class.forName(className);
	    } catch (ClassNotFoundException e) {
		throw new HandlerNotFoundException(className, e);
	    }

	    if (ApiRequestHandler.class.isAssignableFrom(newClass) == false)
		throw new NotAHandlerException(className);

	    String queryString = pathURL.getQuery();
	    Map<String, String> parameters;

	    try {
		parameters = parseQueryString(queryString);
	    } catch (UnsupportedEncodingException e) {
		throw new HandlerSetupFailureException(className, e);
	    }

	    Map<String, Object> content = null;

	    try {
		ApiRequestHandler srh = ((ApiRequestHandler) newClass
			.newInstance());
		content = srh.handleGetRequest(parameters, server);
	    } catch (InstantiationException e) {
		throw new HandlerSetupFailureException(className, e);
	    } catch (IllegalAccessException e) {
		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(JSONValue.toJSONString(content));
	    } catch (UnsupportedEncodingException e) {
		throw new HandlerSetupFailureException(className, e);
	    }

	    entity.setContentType(HTTP.PLAIN_TEXT_TYPE);
	    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;
    }
}