ChunkingResponder.java :  » Testing » StoryTestIQ » fitnesse » responders » Java Open Source

Java Open Source » Testing » StoryTestIQ 
StoryTestIQ » fitnesse » responders » ChunkingResponder.java
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
package fitnesse.responders;

import fitnesse.*;
import fitnesse.wiki.*;
import fitnesse.http.*;
import java.net.SocketException;

public abstract class ChunkingResponder implements Responder {
  protected WikiPage root;

  public WikiPage page;

  protected WikiPagePath path;

  protected Request request;

  protected ChunkedResponse response;

  protected FitNesseContext context;

  private Thread respondingThread;

  public Response makeResponse(FitNesseContext context, Request request) throws Exception {
    this.context = context;
    this.request = request;
    this.root = context.root;
    response = new ChunkedResponse();

    getRequestedPage(request);
    if (page == null && shouldRespondWith404())
      return pageNotFoundResponse(context, request);

    respondingThread = new Thread(new RespondingRunnable(), getClass() + ": Responding Thread");
    respondingThread.start();

    return response;
  }

  private void getRequestedPage(Request request) throws Exception {
    path = PathParser.parse(request.getResource());
    page = getPageCrawler().getPage(root, path);
  }

  protected PageCrawler getPageCrawler() {
    return root.getPageCrawler();
  }

  private Response pageNotFoundResponse(FitNesseContext context, Request request) throws Exception {
    return new NotFoundResponder().makeResponse(context, request);
  }

  protected boolean shouldRespondWith404() {
    return true;
  }

  public void startSending() {
    try {
      doSending();
    } catch (SocketException e) {
      // normal. someone stoped the request.
    } catch (Exception e) {
      try {
        response.add(ErrorResponder.makeExceptionString(e));
        response.closeAll();
      } catch (Exception e1) {
        // Give me a break!
      }
    }
  }

  protected String getRenderedPath() {
    if (path != null)
      return PathParser.render(path);
    else
      return request.getResource();
  }

  protected class RespondingRunnable implements Runnable {
    public void run() {
      while (!response.isReadyToSend()) {
        try {
          synchronized (response) {
            response.wait();
          }
        } catch (InterruptedException e) {
          // ok
        }
      }
      startSending();
    }
  }

  public void setRequest(Request request) {
    this.request = request;
  }

  protected abstract void doSending() throws Exception;
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.