source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java@ 4095

Last change on this file since 4095 was 4095, checked in by kjdon, 21 years ago

added in the new system action

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
14import java.util.Enumeration;
15
16/** core class for web interface generation. Receives requests consisting
17 * of an xml representation of cgi args, and returns the page of data - in
18 * html by default. The requests are processed by the appropriate action class
19 *
20 * @see Action
21 */
22public class Receptionist implements ModuleInterface {
23
24 /** the set up variables */
25 protected ConfigVars config_=null;
26
27 /** the cgi args converter */
28 protected GSCGI cgi_ = null;
29
30 /** container Document to create XML Nodes */
31 protected Document doc_=null;
32
33 /** a converter class to parse XML and create Docs */
34 protected XMLConverter converter_=null;
35
36 /** the message router that the actions will talk to */
37 protected ModuleInterface mr_=null;
38
39 /** the list of actions */
40 protected HashMap action_map_=null;
41
42 public Receptionist() {
43 converter_ = new XMLConverter();
44 doc_ = converter_.newDOM();
45 action_map_= new HashMap();
46 cgi_ = new GSCGI();
47 }
48
49 /** the set up vars must be set before configure called*/
50 public void setConfigVars(ConfigVars config) {
51 config_ = config;
52 }
53
54 /** sets the message router - it should already be created and
55 * configured before being passed to the receptionist*/
56 public void setMessageRouter(ModuleInterface m) {
57 mr_ = m;
58 }
59
60 /** configures the receptionist */
61 public boolean configure() {
62
63 if (config_==null) {
64 System.err.println("Receptionist Error: config variables must be set before calling configure");
65 return false;
66 }
67 if (mr_==null) {
68 System.err.println("Receptionist Error: message router must be set before calling configure");
69 return false;
70 }
71
72 // add in the default actions - others can be dynamically
73 // loaded as needed
74 // if you want an action to have a short name it needs to go here
75 Action a = new PageAction();
76 a.setConfigVars(config_);
77 a.setMessageRouter(mr_);
78 a.setCGI(cgi_);
79 a.configure();
80 action_map_.put("p", a);
81
82 a = new QueryAction();
83 a.setConfigVars(config_);
84 a.setCGI(cgi_);
85 a.setMessageRouter(mr_);
86 a.configure();
87 action_map_.put("q", a);
88
89 a = new BrowseAction();
90 a.setConfigVars(config_);
91 a.setCGI(cgi_);
92 a.setMessageRouter(mr_);
93 a.configure();
94 action_map_.put("b", a);
95
96 a = new DocumentAction();
97 a.setConfigVars(config_);
98 a.setCGI(cgi_);
99 a.setMessageRouter(mr_);
100 a.configure();
101 action_map_.put("d", a);
102
103 a = new AppletAction();
104 a.setConfigVars(config_);
105 a.setCGI(cgi_);
106 a.setMessageRouter(mr_);
107 a.configure();
108 action_map_.put("a", a);
109
110 a = new ProcessAction();
111 a.setConfigVars(config_);
112 a.setCGI(cgi_);
113 a.setMessageRouter(mr_);
114 a.configure();
115 action_map_.put("pr", a);
116
117 a = new SystemAction();
118 a.setConfigVars(config_);
119 a.setCGI(cgi_);
120 a.setMessageRouter(mr_);
121 a.configure();
122 action_map_.put("s", a);
123
124
125 return true;
126 }
127 /** process using strings - just calls process using Elements */
128 public String process(String xml_in) {
129
130 Element message = converter_.getDOM(xml_in).getDocumentElement();
131 Element page = process(message);
132 return converter_.getString(page);
133 }
134
135 /** process - produce a page of data in response to a request
136 * if something goes wrong, it returns null -
137 * TODO: return a suitable message to the user */
138 public Element process(Element message) {
139
140 // get the request out of the message - assume that there is only one
141 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
142 if (request == null) {
143 System.err.println("Receptionist Error: message had no request!");
144 return null;
145 }
146 // check that the request type is correct
147 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
148 if (!type.equals(GSXML.REQUEST_TYPE_CGI)) {
149 // now Receptionist forwards non-cgi requests straight to the MR, and returns the responses
150 System.err.println("Receptionist: request type is not '"+GSXML.REQUEST_TYPE_CGI+"', but it is '"+type+"', so forwarding the message to the MR!");
151 // process teh whole message - mr needs <message> tags, and
152 // in this case, there may be more than one request in the message
153 return mr_.process(message);
154 }
155 // work out which action to pass to
156 String action = request.getAttribute(GSXML.ACTION_ATT);
157 if (action.equals("")) {
158 System.err.println("Receptionist Error: no action specified in the request!");
159 return null;
160 }
161 // convert cgi short names to long names in the param list
162 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
163 if (param_list!=null) {
164 if (!cgi_.paramListToLongNames(param_list)) {
165 System.err.println("Receptionist Error: couldn't convert short names to long names in the param list!");
166 return null;
167 }
168 }
169
170 // pass request to appropriate action
171 Action a = (Action)action_map_.get(action);
172 String action_name=null;
173 if (a==null) { // not in the map yet
174 // try to load a new action
175 try {
176 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
177 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
178 ac.setConfigVars(config_);
179 ac.setMessageRouter(mr_);
180 ac.setCGI(cgi_);
181 ac.configure();
182 action_map_.put(action, ac);
183 a = ac;
184 } catch (Exception e) {
185
186 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
187 return null;
188 }
189 }
190
191 // add in the system stuff - display and config
192 Document message_doc = message.getOwnerDocument();
193 String lang = request.getAttribute(GSXML.LANG_ATT);
194 Element display = getDisplayElement(lang);
195 message.appendChild(message_doc.importNode(display, true));
196 message.appendChild(message_doc.importNode(config_.config_xml_, true));
197
198 Element page = a.process(message);
199 return page;
200
201 }
202
203 /** returns an xml element containing all the text strings
204 * needed for an interface
205 * currently only uses the current interface - should add in
206 * needed ones from the default interface if current != default */
207 protected Element getDisplayElement(String lang) {
208
209 Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
210 // looks for properties files based on interface name
211 String resource_name = "interface_"+config_.interface_name_;
212
213 Dictionary dict = new Dictionary(resource_name, lang);
214 Enumeration enum = dict.getKeys();
215 while (enum.hasMoreElements()) {
216 String key = (String)enum.nextElement();
217 String value = dict.get(key);
218 Element e = GSXML.createTextElement(doc_, key, value);
219 display.appendChild(e);
220 }
221 return display;
222 }
223
224}
Note: See TracBrowser for help on using the repository browser.