- Timestamp:
- 2002-11-26T13:59:51+13:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java
r3512 r3570 12 12 import java.io.File; 13 13 import java.util.HashMap; 14 14 import java.util.Enumeration; 15 16 /** core class for web interface generation. Receives requests consisting 17 * of an xml representation of cgi args, and returns the page of data - in 18 * html by default. The requests are processed by the appropriate action class 19 * 20 * @see Action 21 */ 15 22 public class Receptionist implements ModuleInterface { 16 23 … … 23 30 /** container Document to create XML Nodes */ 24 31 protected Document doc_=null; 32 25 33 /** a converter class to parse XML and create Docs */ 26 34 protected XMLConverter converter_=null; 27 35 28 /** the message router that it talksto */36 /** the message router that the actions will talk to */ 29 37 protected ModuleInterface mr_=null; 30 38 … … 32 40 protected HashMap action_map_=null; 33 41 34 protected Translate translation_=null;35 42 public Receptionist() { 36 43 converter_ = new XMLConverter(); 37 44 doc_ = converter_.newDOM(); 38 45 action_map_= new HashMap(); 39 translation_ = new Translate();40 46 cgi_ = new CGIArgConverter(); 41 47 } … … 44 50 public void setConfigVars(ConfigVars config) { 45 51 config_ = config; 46 translation_.setConfigVars(config); 47 } 52 } 53 48 54 /** sets the message router - it should already be created and 49 55 * configured before being passed to the receptionist*/ … … 52 58 } 53 59 60 /** configures the receptionist */ 54 61 public boolean configure() { 55 62 56 63 if (config_==null) { 57 System.err.println(" You must set the config variablesbefore calling configure");64 System.err.println("Receptionist Error: config variables must be set before calling configure"); 58 65 return false; 59 66 } 60 67 if (mr_==null) { 61 System.err.println(" You must set the mrbefore calling configure");68 System.err.println("Receptionist Error: message router must be set before calling configure"); 62 69 return false; 63 70 } 64 71 65 // for now, assume default is en, so load that 66 translation_.setDefaultLanguage("en"); 67 // for now, just statically add in the actions 68 // actions may disappear if xslt is all that is needed 69 // or they should be dynamically loaded 72 // add in the default actions - others can be dynamically 73 // loaded as needed 74 // if you want an action to have a short name it needs to go here 70 75 Action a = new PageAction(); 71 76 a.setConfigVars(config_); … … 82 87 action_map_.put("q", a); 83 88 84 a = new BrowseAction();85 a.setConfigVars(config_);86 a.setCGIConverter(cgi_);87 a.setMessageRouter(mr_);88 a.configure();89 action_map_.put("b", a);89 a = new BrowseAction(); 90 a.setConfigVars(config_); 91 a.setCGIConverter(cgi_); 92 a.setMessageRouter(mr_); 93 a.configure(); 94 action_map_.put("b", a); 90 95 91 96 a = new ResourceAction(); … … 102 107 a.configure(); 103 108 action_map_.put("a", a); 109 110 104 111 return true; 105 112 } 106 113 /** process using strings - just calls process using Elements */ 107 114 public String process(String xml_in) { 108 115 109 Document message_doc = converter_.getDOM(xml_in); 110 Element message = message_doc.getDocumentElement(); 111 // there should only be one request per message 116 Element message = converter_.getDOM(xml_in).getDocumentElement(); 117 Element page = process(message); 118 return converter_.getString(page); 119 } 120 121 /** process - produce a page of data in response to a request 122 * if something goes wrong, it returns null - 123 * TODO: return a suitable message to the user */ 124 public Element process(Element message) { 125 126 // get the request out of the message - assume that there is only one 112 127 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 113 128 if (request == null) { 114 return GSHTML.errorPage("no request in the message"); 115 } 116 129 System.err.println("Receptionist Error: message had no request!"); 130 return null; 131 } 132 // check that the request type is correct 117 133 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name 118 134 if (!type.equals(GSXML.REQUEST_TYPE_ACTION)) { 119 return GSHTML.errorPage("wrong type in request - should be action"); 135 System.err.println("Receptionist Error: request type should be '"+GSXML.REQUEST_TYPE_ACTION+"', but it was '"+type+"'!"); 136 return null; 120 137 } 121 138 // work out which action to pass to 122 139 String action = request.getAttribute(GSXML.ACTION_ATT); 123 140 if (action.equals("")) { 124 return GSHTML.errorPage(" no action specified"); 125 } 126 // pass to appropriate action 141 System.err.println("Receptionist Error: no action specified in the request!"); 142 return null; 143 } 144 // convert cgi short names to long names in the param list 145 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 146 if (!cgi_.toLong(param_list)) { 147 System.err.println("Receptionist Error: couldn't convert short names to long names in the param list!"); 148 return null; 149 } 127 150 151 // pass request to appropriate action 128 152 Action a = (Action)action_map_.get(action); 129 if (a==null) { 153 String action_name=null; 154 if (a==null) { // not in the map yet 130 155 // try to load a new action 131 156 try { 132 String action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action"; 133 System.out.println("new action name="+action_name); 157 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action"; 134 158 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance(); 135 159 ac.setConfigVars(config_); … … 141 165 } catch (Exception e) { 142 166 143 return GSHTML.errorPage("no action for "+action+", and couldn't create a new one: error message:"+e.getMessage()); 167 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage()); 168 return null; 144 169 } 145 170 } 146 171 147 // add in the translation bit - should we do it here? or give the actions access to the translation object? 148 String lang = message.getAttribute(GSXML.LANG_ATT); 149 Element trans = translation_.getLanguageTree(lang); 150 message.appendChild(message_doc.importNode(trans, true)); 151 152 // add in the system vars bit 172 // add in the system stuff - display and config 173 Document message_doc = message.getOwnerDocument(); 174 String lang = request.getAttribute(GSXML.LANG_ATT); 175 Element display = getDisplayElement(lang); 176 message.appendChild(message_doc.importNode(display, true)); 153 177 message.appendChild(message_doc.importNode(config_.config_xml_, true)); 154 178 155 Stringpage = a.process(message);179 Element page = a.process(message); 156 180 return page; 157 } 158 159 public Element process(Element xml_in) { 160 return xml_in; // for now 161 162 } 163 181 182 } 183 184 /** returns an xml element containing all the text strings 185 * needed for an interface 186 * currenlty only uses the current interface - should add in 187 * needed ones from the default interface if current != default */ 188 protected Element getDisplayElement(String lang) { 189 190 Element display = doc_.createElement(GSXML.DISPLAY_ELEM); 191 // looks for properties files based on interface name 192 String resource_name = "interface_"+config_.interface_name_; 193 194 Dictionary dict = new Dictionary(resource_name, lang); 195 Enumeration enum = dict.getKeys(); 196 while (enum.hasMoreElements()) { 197 String key = (String)enum.nextElement(); 198 String value = dict.get(key); 199 Element e = GSXML.createTextElement(doc_, key, value); 200 display.appendChild(e); 201 } 202 return display; 203 } 164 204 165 205 }
Note:
See TracChangeset
for help on using the changeset viewer.