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

Last change on this file since 16944 was 16944, checked in by kjdon, 16 years ago

add xmlns:xsl into the page element as well, in case we are doing o=xml and the format statement has xsl elements

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