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;
004:
005: import java.lang.reflect.Constructor;
006: import java.lang.reflect.InvocationTargetException;
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: import com.sun.rmi.rmid.ExecOptionPermission;
011:
012: import fitnesse.Responder;
013: import fitnesse.http.Request;
014: import fitnesse.responders.editing.CreateStiqPageResponder;
015: import fitnesse.responders.editing.EditResponder;
016: import fitnesse.responders.editing.PropertiesResponder;
017: import fitnesse.responders.editing.SavePropertiesResponder;
018: import fitnesse.responders.editing.SaveResponder;
019: import fitnesse.responders.editing.SymbolicLinkResponder;
020: import fitnesse.responders.editing.TableWizardResponder;
021: import fitnesse.responders.files.CreateDirectoryResponder;
022: import fitnesse.responders.files.DeleteConfirmationResponder;
023: import fitnesse.responders.files.DeleteFileResponder;
024: import fitnesse.responders.files.RenameFileConfirmationResponder;
025: import fitnesse.responders.files.RenameFileResponder;
026: import fitnesse.responders.files.RepoFileResponder;
027: import fitnesse.responders.files.UploadResponder;
028: import fitnesse.responders.refactoring.DeletePageResponder;
029: import fitnesse.responders.refactoring.MovePageResponder;
030: import fitnesse.responders.refactoring.NewStiqPageResponder;
031: import fitnesse.responders.refactoring.RefactorPageResponder;
032: import fitnesse.responders.refactoring.RenamePageResponder;
033: import fitnesse.responders.run.FitClientResponder;
034: import fitnesse.responders.run.SocketCatchingResponder;
035: import fitnesse.responders.run.TestResultFormattingResponder;
036: import fitnesse.responders.search.SearchFormResponder;
037: import fitnesse.responders.search.SearchResponder;
038: import fitnesse.responders.search.WhereUsedResponder;
039: import fitnesse.responders.versions.RollbackResponder;
040: import fitnesse.responders.versions.VersionResponder;
041: import fitnesse.responders.versions.VersionSelectionResponder;
042: import fitnesse.wiki.WikiPage;
043: import fitnesse.wikitext.widgets.WikiWordWidget;
044:
045: public class ResponderFactory {
046: private String rootPath;
047:
048: private Map responderMap;
049:
050: public ResponderFactory(String rootPath) {
051: this .rootPath = rootPath;
052: responderMap = new HashMap();
053: addResponder("edit", EditResponder.class);
054: addResponder("saveData", SaveResponder.class);
055: addResponder("tableWizard", TableWizardResponder.class);
056: addResponder("search", SearchResponder.class);
057: addResponder("searchForm", SearchFormResponder.class);
058: //addResponder("test", TestResponder.class);
059: //addResponder("suite", SuiteResponder.class);
060: addResponder("proxy", SerializedPageResponder.class);
061: addResponder("versions", VersionSelectionResponder.class);
062: addResponder("viewVersion", VersionResponder.class);
063: addResponder("rollback", RollbackResponder.class);
064: addResponder("names", NameWikiPageResponder.class);
065: addResponder("properties", PropertiesResponder.class);
066: addResponder("saveProperties", SavePropertiesResponder.class);
067: addResponder("whereUsed", WhereUsedResponder.class);
068: addResponder("refactor", RefactorPageResponder.class);
069: addResponder("deletePage", DeletePageResponder.class);
070: addResponder("renamePage", RenamePageResponder.class);
071: addResponder("movePage", MovePageResponder.class);
072: addResponder("pageData", PageDataWikiPageResponder.class);
073: addResponder("createDir", CreateDirectoryResponder.class);
074: addResponder("upload", UploadResponder.class);
075: addResponder("socketCatcher", SocketCatchingResponder.class);
076: addResponder("fitClient", FitClientResponder.class);
077: addResponder("deleteFile", DeleteFileResponder.class);
078: addResponder("renameFile", RenameFileResponder.class);
079: addResponder("deleteConfirmation",
080: DeleteConfirmationResponder.class);
081: addResponder("renameConfirmation",
082: RenameFileConfirmationResponder.class);
083: addResponder("raw", RawContentResponder.class);
084: addResponder("rss", RssResponder.class);
085: addResponder("import", WikiImportingResponder.class);
086: addResponder("files", ClasspathFileResponder.class);
087: addResponder("shutdown", ShutdownResponder.class);
088: addResponder("format", TestResultFormattingResponder.class);
089: addResponder("symlink", SymbolicLinkResponder.class);
090:
091: // STIQ
092: // "Responders" are created using this map when a corresponding Get method parameter name exists
093: addResponder("storytestiq", ClasspathFileResponder.class);
094: addResponder("stiq", ClasspathFileResponder.class);
095: addResponder("startPage", ClasspathFileResponder.class);
096: addResponder(NewStiqPageResponder.RESPONDER_KEY,
097: NewStiqPageResponder.class);
098: addResponder(CreateStiqPageResponder.RESPONDER_KEY,
099: CreateStiqPageResponder.class);
100: addResponder(ExecuteProcessResponder.RESPONDER_KEY,
101: ExecuteProcessResponder.class);
102: }
103:
104: public void addResponder(String key, Class responderClass) {
105: responderMap.put(key, responderClass);
106: }
107:
108: public String getResponderKey(Request request) {
109: if (request.hasInput("responder")) {
110: return (String) request.getInput("responder");
111: } else {
112: if (request.getQueryString() != null) {
113: int nameValueSplit = request.getQueryString().indexOf(
114: '=');
115: if (nameValueSplit != -1) {
116: return request.getQueryString().substring(0,
117: nameValueSplit);
118: }
119: }
120: return request.getQueryString();
121: }
122: }
123:
124: public Responder makeResponder(Request request, WikiPage root)
125: throws Exception {
126: Responder responder = new DefaultResponder();
127: String resource = request.getResource();
128:
129: if ("".equals(resource)) {
130: resource = "FrontPage";
131: }
132:
133: // The first Get method parameter name (after the ?)
134: String responderKey = getResponderKey(request);
135:
136: // Look in responderMap for the Get method parameter name and use its mapped responder
137: if (usingResponderKey(responderKey)) {
138: responder = lookupResponder(responderKey, responder);
139: } else {
140: if (resource.equals("storytestiq/project-extensions.js")) {
141: responder = new ProjectExtensionsResponder();
142: } else if (resource.startsWith("files")
143: || resource.startsWith("storytestiq")
144: || resource.startsWith("stiq")) {
145: responder = new ClasspathFileResponder();
146: } else if (WikiWordWidget.isWikiWord(resource)
147: || "root".equals(resource)) {
148: responder = new WikiPageResponder();
149: } else if (resource.startsWith("SQL")) {
150: responder = new SqlResponder();
151: } else if (resource
152: .startsWith(STIQResultResponder.STIQ_RESULT_URL)) {
153: responder = new STIQResultResponder();
154: } else if (resource
155: .startsWith(RepoFileResponder.RESPONDER_KEY)) {
156: responder = RepoFileResponder.makeResponder(request,
157: rootPath);
158: } else {
159: responder = new NotFoundResponder();
160: }
161: }
162:
163: return responder;
164: }
165:
166: private Responder lookupResponder(String responderKey,
167: Responder responder) throws NoSuchMethodException,
168: InstantiationException, IllegalAccessException,
169: InvocationTargetException {
170: Class responderClass = getResponderClass(responderKey);
171: if (responderClass != null) {
172: try {
173: Constructor constructor = responderClass
174: .getConstructor(new Class[] { String.class });
175: responder = (Responder) constructor
176: .newInstance(new Object[] { rootPath });
177: } catch (NoSuchMethodException e) {
178: Constructor constructor = responderClass
179: .getConstructor(new Class[0]);
180: responder = (Responder) constructor
181: .newInstance(new Object[0]);
182: }
183: }
184: return responder;
185: }
186:
187: public Class getResponderClass(String responderKey) {
188: return (Class) responderMap.get(responderKey);
189: }
190:
191: private boolean usingResponderKey(String responderKey) {
192: boolean usingKey = false;
193: if (!("".equals(responderKey) || responderKey == null)) {
194: if (responderMap.containsKey(responderKey)) {
195: usingKey = true;
196: }
197: }
198: return usingKey;
199: }
200:
201: }
|