source: branches/ant-install-branch/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java@ 9798

Last change on this file since 9798 was 9798, checked in by kjdon, 19 years ago

for all the converter.getDOM calls, now check for null document before using it - hopefully avoid lots of the exceptions that get printed to the screen if something goes wrong

  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 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;
15import java.util.ArrayList;
16
17/** the most basic Receptionist, used for interface generation.
18 * Receives requests consisting
19 * of an xml representation of cgi args, and returns the page of data. The requests are processed by the appropriate action class
20 *
21 * @see Action
22 */
23public class Receptionist implements ModuleInterface {
24
25 /** the set up variables */
26 protected HashMap config_params = null;
27 /** container Document to create XML Nodes */
28 protected Document doc=null;
29
30 /** a converter class to parse XML and create Docs */
31 protected XMLConverter converter=null;
32
33 /** the message router that the Receptionist and Actions will talk to */
34 protected ModuleInterface mr=null;
35
36 /** the list of actions */
37 protected HashMap action_map=null;
38
39 /** the list of params */
40 protected GSParams params=null;
41 protected Element language_list = null;
42
43 /** the list of interfaces this is based on */
44 protected ArrayList base_interfaces = null;
45
46 public Receptionist() {
47 this.converter = new XMLConverter();
48 this.doc = this.converter.newDOM();
49 this.action_map= new HashMap();
50 }
51
52 public void setParams(GSParams params) {
53 this.params = params;
54 }
55 public void setConfigParams(HashMap params) {
56 this.config_params = params;
57 }
58 /** sets the message router - it should already be created and
59 * configured before being passed to the receptionist*/
60 public void setMessageRouter(ModuleInterface m) {
61 this.mr = m;
62 }
63
64 /** configures the receptionist */
65 public boolean configure() {
66
67 if (this.config_params==null) {
68 System.err.println("Receptionist Error: config variables must be set before calling configure");
69 return false;
70 }
71 if (this.mr==null) {
72 System.err.println("Receptionist Error: message router must be set before calling configure");
73 return false;
74 }
75
76 // find the config file containing a list of actions
77 File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome((String)this.config_params.get(GSConstants.GSDL3_HOME), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
78 if (!interface_config_file.exists()) {
79 System.err.println("Receptionist: interface config file: "+interface_config_file.getPath()+" not found!");
80 return false;
81 }
82
83 Document config_doc = this.converter.getDOM(interface_config_file);
84 if (config_doc == null) {
85 System.err.println("Receptionist: could not parse interface config file: "+interface_config_file.getPath());
86 return false;
87 }
88 Element config_elem = config_doc.getDocumentElement();
89 String base_interface = config_elem.getAttribute("baseInterface");
90 setUpBaseInterface(base_interface);
91 setUpInterfaceOptions(config_elem);
92
93 // load up the actions
94 Element action_list = (Element)GSXML.getChildByTagName(config_elem, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER);
95 NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
96
97 for (int i=0; i<actions.getLength(); i++) {
98 Element action = (Element) actions.item(i);
99 String class_name = action.getAttribute("class");
100 String action_name = action.getAttribute("name");
101 Action ac = null;
102 try {
103 ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance();
104 } catch (Exception e) {
105 System.err.println("Receptionist: couldn't load in action "+class_name);
106 e.printStackTrace();
107 continue;
108 }
109 ac.setConfigParams(this.config_params);
110 ac.setMessageRouter(this.mr);
111 ac.configure();
112 ac.getActionParameters(this.params);
113 this.action_map.put(action_name, ac);
114 }
115
116 this.language_list = (Element)GSXML.getChildByTagName(config_elem, "languageList");
117 if (language_list == null) {
118 System.err.println("Receptionist: didn't find a language list in the config file!!");
119 }
120
121 return true;
122 }
123 /** process using strings - just calls process using Elements */
124 public String process(String xml_in) {
125
126 Element message = this.converter.getDOM(xml_in).getDocumentElement();
127 Element page = process(message);
128 return this.converter.getString(page);
129 }
130
131 /** process - produce a page of data in response to a request
132 * if something goes wrong, it returns null -
133 * TODO: return a suitable message to the user */
134 public Element process(Element message) {
135
136 // get the request out of the message - assume that there is only one
137 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
138 if (request == null) {
139 System.err.println("Receptionist Error: message had no request!");
140 return null;
141 }
142 // check the request type
143 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
144 if (!type.equals(GSXML.REQUEST_TYPE_PAGE)) {
145 // now Receptionist forwards non-page requests straight to the MR, and returns the responses
146 System.err.println("Receptionist: request type is not '"+GSXML.REQUEST_TYPE_PAGE+"', but it is '"+type+"', so forwarding the message to the MR!");
147 // process the whole message - mr needs <message> tags, and
148 // in this case, there may be more than one request in the message
149 return this.mr.process(message);
150 }
151 // work out which action to pass to
152 String action = request.getAttribute(GSXML.ACTION_ATT);
153 if (action.equals("")) {
154 System.err.println("Receptionist Error: no action specified in the request!");
155 return null;
156 }
157
158 // find the appropriate action
159 Action a = (Action)this.action_map.get(action);
160 String action_name=null;
161 if (a==null) { // not in the map yet
162 // try to load a new action
163 try {
164 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
165 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
166 ac.setConfigParams(this.config_params);
167 ac.setMessageRouter(this.mr);
168 ac.configure();
169 ac.getActionParameters(this.params);
170 this.action_map.put(action, ac);
171 a = ac;
172 } catch (Exception e) {
173
174 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
175 return null;
176 }
177 }
178
179 // transform the request in some way
180 preProcessRequest(request);
181 // set up the page
182 Element page = this.doc.createElement(GSXML.PAGE_ELEM);
183 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
184
185 // get the page data from the action
186 Element action_response = a.process(message);
187
188 boolean response_only=false;
189 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
190 if (param_list != null) {
191 Element param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "ro");
192 if (param != null) {
193 String value = param.getAttribute("value");
194 if (value.equals("1")) {
195 response_only = true;
196 }
197 }
198 }
199 if (response_only) {
200 // only the response from the action is sent back
201 return action_response;
202 }
203
204 // the request is part of the page
205 page.appendChild(GSXML.duplicateWithNewName(this.doc, request, GSXML.PAGE_REQUEST_ELEM, true));
206 // add the response too
207 Element page_response = GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(action_response, GSXML.RESPONSE_ELEM), GSXML.PAGE_RESPONSE_ELEM, true);
208 page.appendChild(page_response);
209
210 ///ystem.out.println("Receptionist: raw page="+this.converter.getString(page));
211 // transform the result in some way
212 Element resulting_page = postProcessPage(page);
213 return resulting_page;
214
215 }
216
217 protected boolean setUpBaseInterface(String base_interface) {
218 if (base_interface== null || base_interface.equals("")) {
219 // there was no base interface, the list remains null
220 return true;
221 }
222 // foreach base interface
223 while (!base_interface.equals("")) {
224 // find the base interface config file
225 File base_interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome((String)this.config_params.get(GSConstants.GSDL3_HOME), base_interface)));
226 if (!base_interface_config_file.exists()) {
227 System.err.println("Receptionist: base interface config file: "+base_interface_config_file.getPath()+" not found!");
228 return false;
229 }
230 // the interface name is valid, add it to the list
231 if (base_interfaces == null) {
232 base_interfaces = new ArrayList();
233 }
234 base_interfaces.add(base_interface);
235 // now see if this has a base interface
236 Document config_doc = this.converter.getDOM(base_interface_config_file);
237 if (config_doc == null) {
238 System.err.println("Receptionist: could not parse base interface config file: "+base_interface_config_file.getPath());
239 return false;
240 }
241 Element config_elem = config_doc.getDocumentElement();
242 base_interface = config_elem.getAttribute("baseInterface");
243 }
244 return true;
245 }
246
247 protected boolean setUpInterfaceOptions(Element config_elem) {
248 Element option_list = (Element)GSXML.getChildByTagName(config_elem, "optionList");
249 if (option_list != null) {
250 System.err.println("found an option list");
251 // we set any options in the config params
252 NodeList options = option_list.getElementsByTagName("option");
253 for (int i=0; i<options.getLength(); i++) {
254 Element option = (Element)options.item(i);
255 String name = option.getAttribute(GSXML.NAME_ATT);
256 String value = option.getAttribute(GSXML.VALUE_ATT);
257 System.err.println("option: "+name+", "+value);
258 if (!name.equals("") && !value.equals("")) {
259 this.config_params.put(name, value);
260 }
261 }
262 }
263
264 return true;
265 }
266
267 protected void preProcessRequest(Element request) {
268 return;
269 }
270
271 protected Element postProcessPage(Element page) {
272 return page;
273 }
274
275
276}
Note: See TracBrowser for help on using the repository browser.