01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.responders;
04:
05: import fitnesse.*;
06: import fitnesse.wiki.*;
07: import fitnesse.http.*;
08: import java.net.SocketException;
09:
10: public abstract class ChunkingResponder implements Responder {
11: protected WikiPage root;
12: public WikiPage page;
13: protected WikiPagePath path;
14: protected Request request;
15: protected ChunkedResponse response;
16: protected FitNesseContext context;
17:
18: public Response makeResponse(FitNesseContext context,
19: Request request) throws Exception {
20: this .context = context;
21: this .request = request;
22: this .root = context.root;
23: response = new ChunkedResponse();
24:
25: getRequestedPage(request);
26: if (page == null && shouldRespondWith404())
27: return pageNotFoundResponse(context, request);
28:
29: Thread respondingThread = new Thread(new RespondingRunnable(),
30: getClass() + ": Responding Thread");
31: respondingThread.start();
32:
33: return response;
34: }
35:
36: private void getRequestedPage(Request request) throws Exception {
37: path = PathParser.parse(request.getResource());
38: page = getPageCrawler().getPage(root, path);
39: }
40:
41: protected PageCrawler getPageCrawler() {
42: return root.getPageCrawler();
43: }
44:
45: private Response pageNotFoundResponse(FitNesseContext context,
46: Request request) throws Exception {
47: return new NotFoundResponder().makeResponse(context, request);
48: }
49:
50: protected boolean shouldRespondWith404() {
51: return true;
52: }
53:
54: private void startSending() {
55: try {
56: doSending();
57: } catch (SocketException e) {
58: // normal. someone stoped the request.
59: } catch (Exception e) {
60: try {
61: response.add(ErrorResponder.makeExceptionString(e));
62: response.closeAll();
63: } catch (Exception e1) {
64: //Give me a break!
65: }
66: }
67: }
68:
69: protected String getRenderedPath() {
70: if (path != null)
71: return PathParser.render(path);
72: else
73: return request.getResource();
74: }
75:
76: protected class RespondingRunnable implements Runnable {
77: public void run() {
78: while (!response.isReadyToSend()) {
79: try {
80: synchronized (response) {
81: response.wait();
82: }
83: } catch (InterruptedException e) {
84: //ok
85: }
86: }
87: startSending();
88: }
89: }
90:
91: public void setRequest(Request request) {
92: this .request = request;
93: }
94:
95: protected abstract void doSending() throws Exception;
96: }
|