[3344] | 1 | package org.greenstone.gsdl3.core;
|
---|
| 2 |
|
---|
[26055] | 3 | import java.io.File;
|
---|
| 4 | import java.util.ArrayList;
|
---|
| 5 | import java.util.HashMap;
|
---|
| 6 |
|
---|
| 7 | import org.apache.log4j.Logger;
|
---|
| 8 | import org.greenstone.gsdl3.action.Action;
|
---|
| 9 | import org.greenstone.gsdl3.util.GSConstants;
|
---|
| 10 | import org.greenstone.gsdl3.util.GSFile;
|
---|
| 11 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 12 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 13 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[22085] | 14 | import org.greenstone.util.GlobalProperties;
|
---|
[26055] | 15 | import org.w3c.dom.Document;
|
---|
| 16 | import org.w3c.dom.Element;
|
---|
[25090] | 17 | import org.w3c.dom.Node;
|
---|
| 18 | import org.w3c.dom.NodeList;
|
---|
[32366] | 19 | import org.w3c.dom.NamedNodeMap;
|
---|
[3344] | 20 |
|
---|
[25090] | 21 | /**
|
---|
| 22 | * the most basic Receptionist, used for interface generation. Receives requests
|
---|
| 23 | * consisting of an xml representation of cgi args, and returns the page of
|
---|
| 24 | * data. The requests are processed by the appropriate action class
|
---|
| 25 | *
|
---|
[3570] | 26 | * @see Action
|
---|
| 27 | */
|
---|
[25090] | 28 | public class Receptionist implements ModuleInterface
|
---|
| 29 | {
|
---|
[3344] | 30 |
|
---|
[25090] | 31 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.Receptionist.class.getName());
|
---|
[13124] | 32 |
|
---|
[25090] | 33 | /** the set up variables */
|
---|
[27617] | 34 | protected HashMap<String, Object> config_params = null;
|
---|
[25090] | 35 | /** container Document to create XML Nodes */
|
---|
[28966] | 36 | //protected Document doc = null;
|
---|
[3344] | 37 |
|
---|
[25090] | 38 | /** a converter class to parse XML and create Docs */
|
---|
| 39 | protected XMLConverter converter = null;
|
---|
[3344] | 40 |
|
---|
[25090] | 41 | /** the message router that the Receptionist and Actions will talk to */
|
---|
| 42 | protected ModuleInterface mr = null;
|
---|
[5144] | 43 |
|
---|
[25090] | 44 | /** the list of actions */
|
---|
[25635] | 45 | protected HashMap<String, Action> action_map = null;
|
---|
[3344] | 46 |
|
---|
[25090] | 47 | /** the list of params */
|
---|
| 48 | protected GSParams params = null;
|
---|
| 49 | protected Element language_list = null;
|
---|
[3344] | 50 |
|
---|
[25090] | 51 | /** the list of interfaces this is based on */
|
---|
[25635] | 52 | protected ArrayList<String> base_interfaces = null;
|
---|
[25090] | 53 |
|
---|
| 54 | public Receptionist()
|
---|
| 55 | {
|
---|
| 56 | this.converter = new XMLConverter();
|
---|
[28966] | 57 | //this.doc = XMLConverter.newDOM();
|
---|
[25635] | 58 | this.action_map = new HashMap<String, Action>();
|
---|
[3344] | 59 | }
|
---|
[25090] | 60 |
|
---|
| 61 | public void cleanUp()
|
---|
| 62 | {
|
---|
[25760] | 63 | if (this.mr != null)
|
---|
| 64 | {
|
---|
| 65 | this.mr.cleanUp();
|
---|
| 66 | }
|
---|
[3344] | 67 | }
|
---|
| 68 |
|
---|
[25090] | 69 | public void setParams(GSParams params)
|
---|
| 70 | {
|
---|
| 71 | this.params = params;
|
---|
[4258] | 72 | }
|
---|
[25090] | 73 |
|
---|
[27617] | 74 | public void setConfigParams(HashMap<String, Object> params)
|
---|
[25090] | 75 | {
|
---|
| 76 | this.config_params = params;
|
---|
[9874] | 77 | }
|
---|
[8831] | 78 |
|
---|
[27617] | 79 | public HashMap<String, Object> getConfigParams()
|
---|
[25090] | 80 | {
|
---|
| 81 | return this.config_params;
|
---|
[4258] | 82 | }
|
---|
[3570] | 83 |
|
---|
[25090] | 84 | /**
|
---|
| 85 | * sets the message router - it should already be created and configured
|
---|
| 86 | * before being passed to the receptionist
|
---|
| 87 | */
|
---|
| 88 | public void setMessageRouter(ModuleInterface m)
|
---|
| 89 | {
|
---|
| 90 | this.mr = m;
|
---|
[5144] | 91 | }
|
---|
[25255] | 92 |
|
---|
[25254] | 93 | /**
|
---|
| 94 | * gets the message router
|
---|
| 95 | */
|
---|
| 96 | public ModuleInterface getMessageRouter()
|
---|
| 97 | {
|
---|
| 98 | return this.mr;
|
---|
| 99 | }
|
---|
[8831] | 100 |
|
---|
[25090] | 101 | /** configures the receptionist */
|
---|
| 102 | public boolean configure()
|
---|
| 103 | {
|
---|
[16374] | 104 |
|
---|
[25090] | 105 | if (this.config_params == null)
|
---|
| 106 | {
|
---|
| 107 | logger.error(" config variables must be set before calling configure");
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
| 110 | if (this.mr == null)
|
---|
| 111 | {
|
---|
| 112 | logger.error(" message router must be set before calling configure");
|
---|
| 113 | return false;
|
---|
| 114 | }
|
---|
[3344] | 115 |
|
---|
[25090] | 116 | // find the config file containing a list of actions
|
---|
| 117 | File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String) this.config_params.get(GSConstants.INTERFACE_NAME))));
|
---|
| 118 | if (!interface_config_file.exists())
|
---|
| 119 | {
|
---|
| 120 | logger.error(" interface config file: " + interface_config_file.getPath() + " not found!");
|
---|
| 121 | return false;
|
---|
| 122 | }
|
---|
[3570] | 123 |
|
---|
[25090] | 124 | Document config_doc = this.converter.getDOM(interface_config_file);
|
---|
| 125 | if (config_doc == null)
|
---|
| 126 | {
|
---|
| 127 | logger.error(" could not parse interface config file: " + interface_config_file.getPath());
|
---|
| 128 | return false;
|
---|
| 129 | }
|
---|
| 130 | Element config_elem = config_doc.getDocumentElement();
|
---|
| 131 | String base_interface = config_elem.getAttribute("baseInterface");
|
---|
| 132 | setUpBaseInterface(base_interface);
|
---|
| 133 | setUpInterfaceOptions(config_elem);
|
---|
[16374] | 134 |
|
---|
[32549] | 135 | this.language_list = (Element) GSXML.getChildByTagName(config_elem, "languageList");
|
---|
| 136 | if (this.language_list == null)
|
---|
| 137 | {
|
---|
| 138 | logger.error(" didn't find a language list in the config file!!");
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[25090] | 141 | // load up the actions
|
---|
| 142 | Element action_list = (Element) GSXML.getChildByTagName(config_elem, GSXML.ACTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 143 | NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
|
---|
[16374] | 144 |
|
---|
[25090] | 145 | for (int i = 0; i < actions.getLength(); i++)
|
---|
| 146 | {
|
---|
| 147 | Element action = (Element) actions.item(i);
|
---|
| 148 | String class_name = action.getAttribute("class");
|
---|
| 149 | String action_name = action.getAttribute("name");
|
---|
| 150 | Action ac = null;
|
---|
| 151 | try
|
---|
| 152 | {
|
---|
| 153 | ac = (Action) Class.forName("org.greenstone.gsdl3.action." + class_name).newInstance();
|
---|
| 154 | }
|
---|
| 155 | catch (Exception e)
|
---|
| 156 | {
|
---|
| 157 | logger.error(" couldn't load in action " + class_name);
|
---|
| 158 | e.printStackTrace();
|
---|
| 159 | continue;
|
---|
| 160 | }
|
---|
| 161 | ac.setConfigParams(this.config_params);
|
---|
| 162 | ac.setMessageRouter(this.mr);
|
---|
| 163 | ac.configure();
|
---|
[25301] | 164 | ac.addActionParameters(this.params);
|
---|
[32549] | 165 | ac.setLanguageList(this.language_list);
|
---|
[25090] | 166 | this.action_map.put(action_name, ac);
|
---|
| 167 | }
|
---|
[16374] | 168 |
|
---|
[3570] | 169 |
|
---|
[25090] | 170 | return true;
|
---|
| 171 | }
|
---|
[16688] | 172 |
|
---|
[25090] | 173 | public String process(String xml_in)
|
---|
| 174 | {
|
---|
| 175 |
|
---|
| 176 | Node message_node = this.converter.getDOM(xml_in);
|
---|
| 177 | Node page = process(message_node);
|
---|
| 178 | return this.converter.getString(page);
|
---|
[3344] | 179 | }
|
---|
[3363] | 180 |
|
---|
[25090] | 181 | /**
|
---|
| 182 | * process - produce a page of data in response to a request if something
|
---|
| 183 | * goes wrong, it returns null - TODO: return a suitable message to the user
|
---|
| 184 | */
|
---|
| 185 | public Node process(Node message_node)
|
---|
| 186 | {
|
---|
[28966] | 187 | Element message = GSXML.nodeToElement(message_node);
|
---|
[25090] | 188 |
|
---|
| 189 | // get the request out of the message - assume that there is only one
|
---|
| 190 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
| 191 | if (request == null)
|
---|
| 192 | {
|
---|
| 193 | logger.error(" message had no request!");
|
---|
| 194 | return null;
|
---|
[4258] | 195 | }
|
---|
[25090] | 196 | // check the request type
|
---|
| 197 | String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
|
---|
[25255] | 198 | if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
|
---|
[25092] | 199 | {
|
---|
| 200 | return this.mr.process(message);
|
---|
| 201 | }
|
---|
[25255] | 202 |
|
---|
[25090] | 203 | if (!type.equals(GSXML.REQUEST_TYPE_PAGE))
|
---|
| 204 | {
|
---|
| 205 | // now Receptionist forwards non-page requests straight to the MR, and returns the responses
|
---|
[28248] | 206 | logger.debug(" request type is not '" + GSXML.REQUEST_TYPE_PAGE + "', but it is '" + type + "', so forwarding the message to the MR!");
|
---|
[25090] | 207 | // process the whole message - mr needs <message> tags, and
|
---|
| 208 | // in this case, there may be more than one request in the message
|
---|
| 209 | return this.mr.process(message);
|
---|
| 210 | }
|
---|
| 211 | // work out which action to pass to
|
---|
| 212 | String action = request.getAttribute(GSXML.ACTION_ATT);
|
---|
| 213 | if (action.equals(""))
|
---|
| 214 | {
|
---|
| 215 | logger.error(" no action specified in the request!");
|
---|
| 216 | return null;
|
---|
| 217 | }
|
---|
[4258] | 218 |
|
---|
[25090] | 219 | // find the appropriate action
|
---|
[25635] | 220 | Action a = this.action_map.get(action);
|
---|
[16374] | 221 |
|
---|
[25090] | 222 | String action_name = null;
|
---|
| 223 | if (a == null)
|
---|
| 224 | { // not in the map yet
|
---|
| 225 | // try to load a new action
|
---|
| 226 | try
|
---|
| 227 | {
|
---|
| 228 | action_name = action.substring(0, 1).toUpperCase() + action.substring(1) + "Action";
|
---|
| 229 | Action ac = (Action) Class.forName("org.greenstone.gsdl3.action." + action_name).newInstance();
|
---|
| 230 | ac.setConfigParams(this.config_params);
|
---|
| 231 | ac.setMessageRouter(this.mr);
|
---|
| 232 | ac.configure();
|
---|
[25301] | 233 | ac.addActionParameters(this.params);
|
---|
[32549] | 234 | ac.setLanguageList(this.language_list);
|
---|
[25090] | 235 | this.action_map.put(action, ac);
|
---|
| 236 | a = ac;
|
---|
| 237 | }
|
---|
| 238 | catch (Exception e)
|
---|
| 239 | {
|
---|
[16374] | 240 |
|
---|
[25090] | 241 | logger.error(" a new action (" + action_name + ") was specified and it couldn't be created. Error message:" + e.getMessage());
|
---|
| 242 | return null;
|
---|
| 243 | }
|
---|
| 244 | }
|
---|
| 245 | // transform the request in some way -- does nothing!
|
---|
| 246 | preProcessRequest(request);
|
---|
| 247 | // set up the page
|
---|
[28966] | 248 | Document doc = XMLConverter.newDOM();
|
---|
| 249 | Element page = doc.createElement(GSXML.PAGE_ELEM);
|
---|
[25090] | 250 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
| 251 | // just in case these namespaces end up in the page and we want to display the XML
|
---|
[30586] | 252 | GSXML.addGSNamespaces(page, false);
|
---|
| 253 |
|
---|
[25090] | 254 | //logger.info(a+" mesa=" + this.converter.getPrettyString(message));
|
---|
| 255 | // get the page data from the action
|
---|
[25092] | 256 |
|
---|
[25090] | 257 | Node action_response = a.process(message);
|
---|
| 258 | boolean response_only = false;
|
---|
| 259 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 260 | if (param_list != null)
|
---|
| 261 | {
|
---|
| 262 | Element param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "ro");
|
---|
| 263 | if (param != null)
|
---|
| 264 | {
|
---|
| 265 | String value = param.getAttribute("value");
|
---|
| 266 | if (value.equals("1"))
|
---|
| 267 | {
|
---|
| 268 | response_only = true;
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | if (response_only)
|
---|
| 273 | {
|
---|
| 274 | // only the response from the action is sent back
|
---|
| 275 | return action_response;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | // the request is part of the page
|
---|
[32366] | 279 | page.appendChild(duplicateRequest(doc, request));
|
---|
[25090] | 280 | // add the response too
|
---|
[28966] | 281 | Element page_response = GSXML.duplicateWithNewName(doc, (Element) GSXML.getChildByTagName(action_response, GSXML.RESPONSE_ELEM), GSXML.PAGE_RESPONSE_ELEM, true);
|
---|
[25090] | 282 | page.appendChild(page_response);
|
---|
| 283 |
|
---|
| 284 | //logger.info(" raw page="+this.converter.getString(page));
|
---|
| 285 | // transform the result in some way
|
---|
| 286 | //Element resulting_page = postProcessPage(page);
|
---|
| 287 |
|
---|
| 288 | Node resulting_page = postProcessPage(page);
|
---|
| 289 |
|
---|
[25871] | 290 | //logger.debug("receptionist returned response");
|
---|
[25090] | 291 | //logger.error("receptionist returned response");
|
---|
[25871] | 292 | //logger.debug(this.converter.getString(resulting_page));
|
---|
[25090] | 293 | //logger.error(this.converter.getString(resulting_page));
|
---|
| 294 | // logger.info("receptionist returned response");
|
---|
| 295 | // logger.info(this.converter.getString(resulting_page));
|
---|
| 296 | return resulting_page;
|
---|
[8562] | 297 | }
|
---|
[25090] | 298 |
|
---|
[32366] | 299 | // This is pretty much a straight copy of the Element, with a new top root node name, but also, removing any NOSAVE parameters.
|
---|
| 300 | protected Element duplicateRequest(Document owner, Element request) {
|
---|
| 301 |
|
---|
| 302 | Element duplicate;
|
---|
| 303 | duplicate = owner.createElement(GSXML.PAGE_REQUEST_ELEM);
|
---|
| 304 | // Copy element attributes
|
---|
| 305 | NamedNodeMap attributes = request.getAttributes();
|
---|
| 306 | for (int i = 0; i < attributes.getLength(); i++)
|
---|
| 307 | {
|
---|
| 308 | Node attribute = attributes.item(i);
|
---|
| 309 | duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
|
---|
| 310 | }
|
---|
| 311 | // Copy element children
|
---|
| 312 | NodeList children = request.getChildNodes();
|
---|
| 313 | for (int i = 0; i < children.getLength(); i++)
|
---|
| 314 | {
|
---|
| 315 | Node child = children.item(i);
|
---|
| 316 | if (child.getNodeName().equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER)) {
|
---|
| 317 | Element param_list = (Element)owner.importNode(child, true);
|
---|
| 318 | NodeList params = param_list.getElementsByTagName(GSXML.PARAM_ELEM);
|
---|
| 319 | for (int j=params.getLength()-1; j>=0; j--) {
|
---|
| 320 | Element p = (Element)params.item(j);
|
---|
| 321 | if (!p.getAttribute(GSXML.SENSITIVE_ATT).equals("")) {
|
---|
[32549] | 322 | logger.debug("removing param "+p.getAttribute("name"));
|
---|
[32366] | 323 | param_list.removeChild(p);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 | duplicate.appendChild(param_list);
|
---|
| 327 | } else {
|
---|
| 328 | duplicate.appendChild(owner.importNode(child, true));
|
---|
| 329 | }
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 | return duplicate;
|
---|
| 334 |
|
---|
| 335 |
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | }
|
---|
[25090] | 339 | protected boolean setUpBaseInterface(String base_interface)
|
---|
| 340 | {
|
---|
| 341 | if (base_interface == null || base_interface.equals(""))
|
---|
| 342 | {
|
---|
| 343 | // there was no base interface, the list remains null
|
---|
| 344 | return true;
|
---|
| 345 | }
|
---|
| 346 | // foreach base interface
|
---|
| 347 | while (!base_interface.equals(""))
|
---|
| 348 | {
|
---|
| 349 | // find the base interface config file
|
---|
| 350 | File base_interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), base_interface)));
|
---|
| 351 | if (!base_interface_config_file.exists())
|
---|
| 352 | {
|
---|
| 353 | logger.error(" base interface config file: " + base_interface_config_file.getPath() + " not found!");
|
---|
| 354 | return false;
|
---|
| 355 | }
|
---|
| 356 | // the interface name is valid, add it to the list
|
---|
| 357 | if (base_interfaces == null)
|
---|
| 358 | {
|
---|
[25635] | 359 | base_interfaces = new ArrayList<String>();
|
---|
[25090] | 360 | }
|
---|
| 361 | base_interfaces.add(base_interface);
|
---|
[27617] | 362 | this.config_params.put(GSConstants.BASE_INTERFACES, base_interfaces);
|
---|
[25090] | 363 | // now see if this has a base interface
|
---|
| 364 | Document config_doc = this.converter.getDOM(base_interface_config_file);
|
---|
| 365 | if (config_doc == null)
|
---|
| 366 | {
|
---|
| 367 | logger.error(" could not parse base interface config file: " + base_interface_config_file.getPath());
|
---|
| 368 | return false;
|
---|
| 369 | }
|
---|
| 370 | Element config_elem = config_doc.getDocumentElement();
|
---|
| 371 | base_interface = config_elem.getAttribute("baseInterface");
|
---|
| 372 | }
|
---|
| 373 | return true;
|
---|
[8562] | 374 | }
|
---|
[8921] | 375 |
|
---|
[25090] | 376 | protected boolean setUpInterfaceOptions(Element config_elem)
|
---|
| 377 | {
|
---|
| 378 | Element option_list = (Element) GSXML.getChildByTagName(config_elem, "optionList");
|
---|
| 379 | if (option_list != null)
|
---|
| 380 | {
|
---|
| 381 | logger.debug("found an interface optionList");
|
---|
| 382 | // we set any options in the config params
|
---|
| 383 | NodeList options = option_list.getElementsByTagName("option");
|
---|
| 384 | for (int i = 0; i < options.getLength(); i++)
|
---|
| 385 | {
|
---|
| 386 | Element option = (Element) options.item(i);
|
---|
| 387 | String name = option.getAttribute(GSXML.NAME_ATT);
|
---|
| 388 | String value = option.getAttribute(GSXML.VALUE_ATT);
|
---|
| 389 | logger.debug("option: " + name + ", " + value);
|
---|
| 390 | if (!name.equals("") && !value.equals(""))
|
---|
| 391 | {
|
---|
| 392 | this.config_params.put(name, value);
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
[8921] | 395 | }
|
---|
[25090] | 396 |
|
---|
| 397 | return true;
|
---|
[8921] | 398 | }
|
---|
| 399 |
|
---|
[25090] | 400 | protected void preProcessRequest(Element request)
|
---|
| 401 | {
|
---|
| 402 | return;
|
---|
| 403 | }
|
---|
[8921] | 404 |
|
---|
[25090] | 405 | protected Node postProcessPage(Element page)
|
---|
| 406 | {
|
---|
| 407 | return page;
|
---|
| 408 | }
|
---|
[3344] | 409 |
|
---|
| 410 | }
|
---|