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.SecureResponder;
07: import fitnesse.authentication.*;
08: import fitnesse.components.*;
09: import fitnesse.http.*;
10: import fitnesse.wiki.*;
11:
12: public class SaveResponder implements SecureResponder {
13: private String user;
14:
15: private long ticketId;
16:
17: private String savedContent;
18:
19: private PageData data;
20:
21: public Response makeResponse(FitNesseContext context,
22: Request request) throws Exception {
23: String resource = request.getResource();
24: WikiPage page = getPage(resource, context);
25: data = page.getData();
26: user = request.getAuthorizationUsername();
27:
28: if (editsNeedMerge(request))
29: return new MergeResponder(request).makeResponse(context,
30: request);
31: else {
32: return saveEdits(request, page);
33: }
34: }
35:
36: private Response saveEdits(Request request, WikiPage page)
37: throws Exception {
38: Response response = new SimpleResponse();
39: savedContent = (String) request
40: .getInput(EditResponder.CONTENT_INPUT_NAME);
41: setData();
42: VersionInfo commitRecord = page.commit(data);
43: response.addHeader("Previous-Version", commitRecord.getName());
44: RecentChanges.updateRecentChanges(data);
45: TagManager.INSTANCE.recordTagsForPage(page);
46: response.redirect(request.getResource());
47: return response;
48: }
49:
50: private boolean editsNeedMerge(Request request) throws Exception {
51: String saveIdString = (String) request
52: .getInput(EditResponder.SAVE_ID);
53: long saveId = Long.parseLong(saveIdString);
54:
55: String ticketIdString = (String) request
56: .getInput(EditResponder.TICKET_ID);
57: ticketId = Long.parseLong(ticketIdString);
58:
59: boolean shouldMerge = SaveRecorder.changesShouldBeMerged(
60: saveId, ticketId, data);
61: return shouldMerge;
62: }
63:
64: private WikiPage getPage(String resource, FitNesseContext context)
65: throws Exception {
66: WikiPagePath path = PathParser.parse(resource);
67: PageCrawler pageCrawler = context.root.getPageCrawler();
68: WikiPage page = pageCrawler.getPage(context.root, path);
69: if (page == null)
70: page = pageCrawler.addPage(context.root, PathParser
71: .parse(resource));
72: return page;
73: }
74:
75: private void setData() throws Exception {
76: data.setContent(savedContent);
77: data.setAttribute(EditResponder.TICKET_ID, ticketId + "");
78: SaveRecorder.pageSaved(data);
79: if (user != null)
80: data.setAttribute(WikiPage.LAST_MODIFYING_USER, user);
81: else
82: data.removeAttribute(WikiPage.LAST_MODIFYING_USER);
83: }
84:
85: public SecureOperation getSecureOperation() {
86: return new SecureWriteOperation();
87: }
88: }
|