001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders;
004:
005: import fitnesse.*;
006: import fitnesse.wiki.*;
007: import fitnesse.http.*;
008: import java.net.SocketException;
009:
010: public abstract class ChunkingResponder implements Responder {
011: protected WikiPage root;
012:
013: public WikiPage page;
014:
015: protected WikiPagePath path;
016:
017: protected Request request;
018:
019: protected ChunkedResponse response;
020:
021: protected FitNesseContext context;
022:
023: private Thread respondingThread;
024:
025: public Response makeResponse(FitNesseContext context,
026: Request request) throws Exception {
027: this .context = context;
028: this .request = request;
029: this .root = context.root;
030: response = new ChunkedResponse();
031:
032: getRequestedPage(request);
033: if (page == null && shouldRespondWith404())
034: return pageNotFoundResponse(context, request);
035:
036: respondingThread = new Thread(new RespondingRunnable(),
037: getClass() + ": Responding Thread");
038: respondingThread.start();
039:
040: return response;
041: }
042:
043: private void getRequestedPage(Request request) throws Exception {
044: path = PathParser.parse(request.getResource());
045: page = getPageCrawler().getPage(root, path);
046: }
047:
048: protected PageCrawler getPageCrawler() {
049: return root.getPageCrawler();
050: }
051:
052: private Response pageNotFoundResponse(FitNesseContext context,
053: Request request) throws Exception {
054: return new NotFoundResponder().makeResponse(context, request);
055: }
056:
057: protected boolean shouldRespondWith404() {
058: return true;
059: }
060:
061: public void startSending() {
062: try {
063: doSending();
064: } catch (SocketException e) {
065: // normal. someone stoped the request.
066: } catch (Exception e) {
067: try {
068: response.add(ErrorResponder.makeExceptionString(e));
069: response.closeAll();
070: } catch (Exception e1) {
071: // Give me a break!
072: }
073: }
074: }
075:
076: protected String getRenderedPath() {
077: if (path != null)
078: return PathParser.render(path);
079: else
080: return request.getResource();
081: }
082:
083: protected class RespondingRunnable implements Runnable {
084: public void run() {
085: while (!response.isReadyToSend()) {
086: try {
087: synchronized (response) {
088: response.wait();
089: }
090: } catch (InterruptedException e) {
091: // ok
092: }
093: }
094: startSending();
095: }
096: }
097:
098: public void setRequest(Request request) {
099: this .request = request;
100: }
101:
102: protected abstract void doSending() throws Exception;
103: }
|