// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.responders;

import fitnesse.FitNesseContext;
import fitnesse.authentication.SecureOperation;
import fitnesse.authentication.SecureReadOperation;
import fitnesse.authentication.SecureResponder;
import fitnesse.html.HtmlUtil;
import fitnesse.http.Request;
import fitnesse.http.Response;
import fitnesse.http.SimpleResponse;
import fitnesse.responders.editing.EditResponder;
import fitnesse.responders.templateUtilities.HtmlPage;
import fitnesse.responders.templateUtilities.PageTitle;
import fitnesse.testsystems.TestPage;
import fitnesse.testsystems.TestPageWithSuiteSetUpAndTearDown;
import fitnesse.wiki.*;

public class WikiPageResponder implements SecureResponder {

  public Response makeResponse(FitNesseContext context, Request request) {
    WikiPage page = loadPage(context, request.getResource());
    if (page == null)
      return notFoundResponse(context, request);
    else
      return makePageResponse(context, page);
  }

  protected WikiPage loadPage(FitNesseContext context, String pageName) {
    WikiPagePath path = PathParser.parse(pageName);
    WikiPage page;
    if (RecentChanges.RECENT_CHANGES.equals(path)) {
      page = context.recentChanges.toWikiPage(context.root);
    } else {
      PageCrawler crawler = context.root.getPageCrawler();
      page = crawler.getPage(context.root, path);
    }
    return page;
  }

  private Response notFoundResponse(FitNesseContext context, Request request) {
    if (dontCreateNonExistentPage(request))
      return new NotFoundResponder().makeResponse(context, request);
    return new EditResponder().makeResponseForNonExistentPage(context, request);
  }

  private boolean dontCreateNonExistentPage(Request request) {
    String dontCreate = (String) request.getInput("dontCreatePage");
    return dontCreate != null && (dontCreate.length() == 0 || Boolean.parseBoolean(dontCreate));
  }

  private SimpleResponse makePageResponse(FitNesseContext context, WikiPage page) {
      String html = makeHtml(context, page);

      SimpleResponse response = new SimpleResponse();
      response.setMaxAge(0);
      response.setContent(html);
      return response;
  }

  public String makeHtml(FitNesseContext context, WikiPage page) {
    PageData pageData = page.getData();
    HtmlPage html = context.pageFactory.newPage();
    WikiPagePath fullPath = page.getPageCrawler().getFullPath(page);
    String fullPathName = PathParser.render(fullPath);
    PageTitle pt = new PageTitle(fullPath);
    
    String tags = "";
    if (pageData != null) {
      tags = pageData.getAttribute(PageData.PropertySUITES);
    }
    pt.setPageTags(tags);
    
    html.setTitle(fullPathName);
    html.setPageTitle(pt.notLinked());
    
    html.setNavTemplate("wikiNav.vm");
    html.put("actions", new WikiPageActions(page));
    html.put("helpText", pageData.getProperties().get(PageData.PropertyHELP));

    if (TestPage.isTestPage(pageData)) {
      TestPage testPage = new TestPageWithSuiteSetUpAndTearDown(page);
      html.put("content", new WikiPageRenderer(testPage.getDecoratedData()));
    } else {
      html.put("content", new WikiPageRenderer(page.getData()));
    }

    html.setMainTemplate("wikiPage");
    html.setFooterTemplate("wikiFooter");
    html.put("footerContent", new WikiPageFooterRenderer(pageData));
    handleSpecialProperties(html, page);
    return html.html();
  }

  private void handleSpecialProperties(HtmlPage html, WikiPage page) {
    WikiImportProperty.handleImportProperties(html, page);
  }

  public SecureOperation getSecureOperation() {
    return new SecureReadOperation();
  }

  public class WikiPageRenderer {
    private PageData data;

    WikiPageRenderer(PageData data) {
      this.data = data;
    }

    public String render() {
        return HtmlUtil.makePageHtml(data);
    }
  }

  public class WikiPageFooterRenderer {
    private PageData data;

    WikiPageFooterRenderer(PageData data) {
      this.data = data;
    }

    public String render() {
        return HtmlUtil.makePageFooterHtml(data);
    }
  }

}

@@ -1172,60 +1172,8 @@
 ) %7B%0A
-    WikiPagePath path = PathParser.parse(pageName);%0A
     
@@ -1233,18 +1233,22 @@
 quals(pa
-th
+geName
 )) %7B%0A   
@@ -1314,24 +1314,78 @@
    %7D else %7B%0A
+      WikiPagePath path = PathParser.parse(pageName);%0A
       PageCr

Fix issue in WikiPageResponder. Now RecentChanges handler is picked.