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