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.editing;
04:
05: import fitnesse.*;
06: import fitnesse.responders.*;
07: import fitnesse.wiki.*;
08: import fitnesse.http.*;
09:
10: public class SymbolicLinkResponder implements Responder {
11: private Response response;
12:
13: private String resource;
14:
15: private PageCrawler crawler;
16:
17: private FitNesseContext context;
18:
19: public Response makeResponse(FitNesseContext context,
20: Request request) throws Exception {
21: resource = request.getResource();
22: this .context = context;
23: crawler = context.root.getPageCrawler();
24: WikiPage page = crawler.getPage(context.root, PathParser
25: .parse(resource));
26: if (page == null)
27: return new NotFoundResponder().makeResponse(context,
28: request);
29:
30: response = new SimpleResponse();
31: if (request.hasInput("removal"))
32: removeSymbolicLink(request, page);
33: else
34: addSymbolicLink(request, page);
35:
36: return response;
37: }
38:
39: private void setRedirect(String resource) {
40: response.redirect(resource + "?properties");
41: }
42:
43: private void removeSymbolicLink(Request request, WikiPage page)
44: throws Exception {
45: String linkToRemove = (String) request.getInput("removal");
46:
47: PageData data = page.getData();
48: data.getProperties().removeSymbolicLink(linkToRemove);
49: page.commit(data);
50: setRedirect(resource);
51: }
52:
53: private void addSymbolicLink(Request request, WikiPage page)
54: throws Exception {
55: String linkName = (String) request.getInput("linkName");
56: String linkPath = (String) request.getInput("linkPath");
57:
58: if (!crawler.pageExists(context.root, PathParser
59: .parse(linkPath))) {
60: response = new ErrorResponder(
61: "The page to which you are attemting to link, "
62: + linkPath + ", doesn't exist.")
63: .makeResponse(context, null);
64: response.setStatus(404);
65: } else if (page.hasChildPage(linkName)) {
66: response = new ErrorResponder(resource
67: + " already has a child named " + linkName + ".")
68: .makeResponse(context, null);
69: response.setStatus(412);
70: } else {
71: PageData data = page.getData();
72: data.getProperties().addSymbolicLink(linkName,
73: PathParser.parse(linkPath));
74: page.commit(data);
75: setRedirect(resource);
76: }
77: }
78: }
|