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

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

fixed up the receptionist option handling stuff

  • Property svn:keywords set to Author Date Id Revision
File size: 9.4 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 Element config_doc = this.converter.getDOM(interface_config_file).getDocumentElement();
84 String base_interface = config_doc.getAttribute("baseInterface");
85 setUpBaseInterface(base_interface);
86 setUpInterfaceOptions(config_doc);
87
88 // load up the actions
89 Element action_list = (Element)GSXML.getChildByTagName(config_doc, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER);
90 NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
91
92 for (int i=0; i<actions.getLength(); i++) {
93 Element action = (Element) actions.item(i);
94 String class_name = action.getAttribute("class");
95 String action_name = action.getAttribute("name");
96 Action ac = null;
97 try {
98 ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance();
99 } catch (Exception e) {
100 System.err.println("Receptionist: couldn't load in action "+class_name);
101 e.printStackTrace();
102 continue;
103 }
104 ac.setConfigParams(this.config_params);
105 ac.setMessageRouter(this.mr);
106 ac.configure();
107 ac.getActionParameters(this.params);
108 this.action_map.put(action_name, ac);
109 }
110
111 this.language_list = (Element)GSXML.getChildByTagName(config_doc, "languageList");
112 if (language_list == null) {
113 System.err.println("Receptionist: didn't find a language list in the config file!!");
114 }
115
116 return true;
117 }
118 /** process using strings - just calls process using Elements */
119 public String process(String xml_in) {
120
121 Element message = this.converter.getDOM(xml_in).getDocumentElement();
122 Element page = process(message);
123 return this.converter.getString(page);
124 }
125
126 /** process - produce a page of data in response to a request
127 * if something goes wrong, it returns null -
128 * TODO: return a suitable message to the user */
129 public Element process(Element message) {
130
131 // get the request out of the message - assume that there is only one
132 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
133 if (request == null) {
134 System.err.println("Receptionist Error: message had no request!");
135 return null;
136 }
137 // check the request type
138 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
139 if (!type.equals(GSXML.REQUEST_TYPE_PAGE)) {
140 // now Receptionist forwards non-page requests straight to the MR, and returns the responses
141 System.err.println("Receptionist: request type is not '"+GSXML.REQUEST_TYPE_PAGE+"', but it is '"+type+"', so forwarding the message to the MR!");
142 // process the whole message - mr needs <message> tags, and
143 // in this case, there may be more than one request in the message
144 return this.mr.process(message);
145 }
146 // work out which action to pass to
147 String action = request.getAttribute(GSXML.ACTION_ATT);
148 if (action.equals("")) {
149 System.err.println("Receptionist Error: no action specified in the request!");
150 return null;
151 }
152
153 // find the appropriate action
154 Action a = (Action)this.action_map.get(action);
155 String action_name=null;
156 if (a==null) { // not in the map yet
157 // try to load a new action
158 try {
159 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
160 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
161 ac.setConfigParams(this.config_params);
162 ac.setMessageRouter(this.mr);
163 ac.configure();
164 ac.getActionParameters(this.params);
165 this.action_map.put(action, ac);
166 a = ac;
167 } catch (Exception e) {
168
169 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
170 return null;
171 }
172 }
173
174 // transform the request in some way
175 preProcessRequest(request);
176 // set up the page
177 Element page = this.doc.createElement(GSXML.PAGE_ELEM);
178 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
179
180 // get the page data from the action
181 Element action_response = a.process(message);
182
183 boolean response_only=false;
184 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
185 if (param_list != null) {
186 Element param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "ro");
187 if (param != null) {
188 String value = param.getAttribute("value");
189 if (value.equals("1")) {
190 response_only = true;
191 }
192 }
193 }
194 if (response_only) {
195 // only the response from the action is sent back
196 return action_response;
197 }
198
199 // the request is part of the page
200 page.appendChild(GSXML.duplicateWithNewName(this.doc, request, GSXML.PAGE_REQUEST_ELEM, true));
201 // add the response too
202 Element page_response = GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(action_response, GSXML.RESPONSE_ELEM), GSXML.PAGE_RESPONSE_ELEM, true);
203 page.appendChild(page_response);
204
205 ///ystem.out.println("Receptionist: raw page="+this.converter.getString(page));
206 // transform the result in some way
207 Element resulting_page = postProcessPage(page);
208 return resulting_page;
209
210 }
211
212 protected boolean setUpBaseInterface(String base_interface) {
213 if (base_interface== null || base_interface.equals("")) {
214 // there was no base interface, the list remains null
215 return true;
216 }
217 // foreach base interface
218 while (!base_interface.equals("")) {
219 // find the base interface config file
220 File base_interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome((String)this.config_params.get(GSConstants.GSDL3_HOME), base_interface)));
221 if (!base_interface_config_file.exists()) {
222 System.err.println("Receptionist: base interface config file: "+base_interface_config_file.getPath()+" not found!");
223 return false;
224 }
225 // the interface name is valid, add it to the list
226 if (base_interfaces == null) {
227 base_interfaces = new ArrayList();
228 }
229 base_interfaces.add(base_interface);
230 // now see if this has a base interface
231 Element config_doc = this.converter.getDOM(base_interface_config_file).getDocumentElement();
232 base_interface = config_doc.getAttribute("baseInterface");
233 }
234 return true;
235 }
236
237 protected boolean setUpInterfaceOptions(Element config_doc) {
238 Element option_list = (Element)GSXML.getChildByTagName(config_doc, "optionList");
239 if (option_list != null) {
240 System.err.println("found an option list");
241 // we set any options in the config params
242 NodeList options = option_list.getElementsByTagName("option");
243 for (int i=0; i<options.getLength(); i++) {
244 Element option = (Element)options.item(i);
245 String name = option.getAttribute(GSXML.NAME_ATT);
246 String value = option.getAttribute(GSXML.VALUE_ATT);
247 System.err.println("option: "+name+", "+value);
248 if (!name.equals("") && !value.equals("")) {
249 this.config_params.put(name, value);
250 }
251 }
252 }
253
254 return true;
255 }
256
257 protected void preProcessRequest(Element request) {
258 return;
259 }
260
261 protected Element postProcessPage(Element page) {
262 return page;
263 }
264
265
266}
Note: See TracBrowser for help on using the repository browser.