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