01: package pygmy.handlers;
02:
03: import pygmy.core.*;
04:
05: import java.io.IOException;
06: import java.io.ByteArrayOutputStream;
07: import java.io.PrintWriter;
08:
09: /**
10: * <p>
11: * This Handler prints out the incoming Http Request, and echos it back as plain text. This is great for debugging
12: * request being submitted to the server. Past though this it has little production use. It does not filter
13: * by URL so if it is called it short circuits any other handler down stream. If it is installed at the head of a chain
14: * no other handlers in the chain will be called.
15: * </p>
16: */
17: public class PrintHandler extends AbstractHandler implements Handler {
18:
19: public boolean handle(Request aRequest, Response aResponse)
20: throws IOException {
21: if (aRequest instanceof HttpRequest) {
22: HttpRequest request = (HttpRequest) aRequest;
23: HttpResponse response = (HttpResponse) aResponse;
24: StringBuffer buffer = new StringBuffer();
25:
26: buffer.append(request.toString());
27: buffer.append("\r\n");
28: ByteArrayOutputStream baos = new ByteArrayOutputStream();
29: request.getHeaders().print(new InternetOutputStream(baos));
30: if (request.getPostData() != null) {
31: baos.write(request.getPostData());
32: }
33: buffer.append(baos.toString("UTF-8"));
34: response.setMimeType("text/plain");
35: PrintWriter out = response.getPrintWriter();
36: out.write(buffer.toString());
37: return true;
38: }
39: return false;
40: }
41:
42: }
|