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.run;
004:
005: import fitnesse.*;
006: import fitnesse.html.HtmlUtil;
007: import fitnesse.components.*;
008: import fitnesse.wiki.*;
009: import fitnesse.http.*;
010: import java.net.Socket;
011: import java.util.*;
012: import fit.Counts;
013:
014: public class FitClientResponder implements Responder,
015: ResponsePuppeteer, FitClientListener {
016: private FitNesseContext context;
017:
018: private PageCrawler crawler;
019:
020: private String resource;
021:
022: private WikiPage page;
023:
024: private boolean shouldIncludePaths;
025:
026: public Response makeResponse(FitNesseContext context,
027: Request request) throws Exception {
028: this .context = context;
029: this .crawler = context.root.getPageCrawler();
030: this .crawler
031: .setDeadEndStrategy(new VirtualEnabledPageCrawler());
032: this .resource = request.getResource();
033: this .shouldIncludePaths = request.hasInput("includePaths");
034: return new PuppetResponse(this );
035: }
036:
037: public void readyToSend(ResponseSender sender) throws Exception {
038: Socket socket = sender.getSocket();
039: WikiPagePath pagePath = PathParser.parse(this .resource);
040: if (!this .crawler.pageExists(this .context.root, pagePath)) {
041: FitProtocol.writeData(notFoundMessage(), socket
042: .getOutputStream());
043: } else {
044: this .page = this .crawler.getPage(this .context.root,
045: pagePath);
046: PageData data = this .page.getData();
047:
048: if (data.hasAttribute(WikiPage.ACTION_SUITE)) {
049: handleSuitePage(socket, this .page, this .context.root);
050: } else if (data.hasAttribute(WikiPage.ACTION_TEST)) {
051: handleTestPage(socket, data);
052: } else {
053: FitProtocol.writeData(notATestMessage(), socket
054: .getOutputStream());
055: }
056: }
057: sender.close();
058: }
059:
060: private void handleTestPage(Socket socket, PageData data)
061: throws Exception {
062: FitClient client = startClient(socket);
063:
064: if (this .shouldIncludePaths) {
065: String classpath = new ClassPathBuilder()
066: .getClasspath(this .page);
067: client.send(classpath);
068: }
069:
070: sendPage(data, client);
071: closeClient(client);
072: }
073:
074: private void handleSuitePage(Socket socket, WikiPage page,
075: WikiPage root) throws Exception {
076: FitClient client = startClient(socket);
077: List testPages = SuiteResponder.makePageList(page, root);
078:
079: if (this .shouldIncludePaths) {
080: String classpath = SuiteResponder.buildClassPath(testPages,
081: page);
082: client.send(classpath);
083: }
084:
085: for (Iterator iterator = testPages.iterator(); iterator
086: .hasNext();) {
087: WikiPage testPage = (WikiPage) iterator.next();
088: PageData testPageData = testPage.getData();
089: sendPage(testPageData, client);
090: }
091: closeClient(client);
092: }
093:
094: private void sendPage(PageData data, FitClient client)
095: throws Exception {
096: String pageName = this .crawler.getRelativeName(this .page, data
097: .getWikiPage());
098: String testableHtml = HtmlUtil.testableHtml(data);
099: String sendableHtml = pageName + "\n" + testableHtml;
100: client.send(sendableHtml);
101: }
102:
103: private void closeClient(FitClient client) throws Exception {
104: client.done();
105: client.join();
106: }
107:
108: private FitClient startClient(Socket socket) throws Exception {
109: FitClient client = new FitClient(this );
110: client.acceptSocket(socket);
111: return client;
112: }
113:
114: private String notATestMessage() {
115: return this .resource
116: + " is neither a Test page nor a Suite page.";
117: }
118:
119: private String notFoundMessage() {
120: return "The page " + this .resource + " was not found.";
121: }
122:
123: public void acceptOutput(String output) throws Exception {
124: }
125:
126: public void acceptResults(Counts counts) throws Exception {
127: }
128:
129: public void exceptionOccurred(Exception e) {
130: }
131: }
|