Changeset 4258
- Timestamp:
- 2003-05-08T16:15:48+12:00 (21 years ago)
- Location:
- trunk/gsdl3/src/java/org/greenstone/gsdl3/core
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gsdl3/src/java/org/greenstone/gsdl3/core/NZDLReceptionist.java
r4188 r4258 14 14 import java.util.Enumeration; 15 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 * This is a new version of the Receptionist for the nzdl interface 20 * @see Action 16 /** 17 * This is a new version of the Receptionist for the NZDL interface 18 * the interface requires a lot more info for each page 19 * @see DefaultReceptionist 21 20 */ 22 public class NZDLReceptionist extends Receptionist { 21 public class NZDLReceptionist extends DefaultReceptionist { 22 23 protected void addExtraInfo(Element page) { 24 System.out.println("the page before adding extra info is"); 25 System.out.println(this.converter.getPrettyString(page)); 23 26 24 // if we want to use different actions, overwrite this method 25 //public void configure() { 26 //} 27 super.addExtraInfo(page); 27 28 28 /** process - produce a page of data in response to a request 29 * if something goes wrong, it returns null - 30 * TODO: return a suitable message to the user */ 31 public Element process(Element message) { 32 33 // get the request out of the message - assume that there is only one 34 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM); 35 if (request == null) { 36 System.err.println("Receptionist Error: message had no request!"); 37 return null; 38 } 39 // check that the request type is correct 40 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name 41 if (!type.equals(GSXML.REQUEST_TYPE_CGI)) { 42 // now Receptionist forwards non-cgi requests straight to the MR, and returns the responses 43 System.err.println("Receptionist: request type is not '"+GSXML.REQUEST_TYPE_CGI+"', but it is '"+type+"', so forwarding the message to the MR!"); 44 // process teh whole message - mr needs <message> tags, and 45 // in this case, there may be more than one request in the message 46 return mr_.process(message); 47 } 48 // work out which action to pass to 49 String action = request.getAttribute(GSXML.ACTION_ATT); 50 if (action.equals("")) { 51 System.err.println("Receptionist Error: no action specified in the request!"); 52 return null; 53 } 54 // convert cgi short names to long names in the param list 55 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 56 if (param_list!=null) { 57 if (!cgi_.paramListToLongNames(param_list)) { 58 System.err.println("Receptionist Error: couldn't convert short names to long names in the param list!"); 59 return null; 60 } 61 } 62 63 // find the appropriate action - may need to load a new one 64 Action a = (Action)action_map_.get(action); 65 String action_name=null; 66 if (a==null) { // not in the map yet 67 // try to load a new action 68 try { 69 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action"; 70 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance(); 71 ac.setConfigVars(config_); 72 ac.setMessageRouter(mr_); 73 ac.setCGI(cgi_); 74 ac.configure(); 75 action_map_.put(action, ac); 76 a = ac; 77 } catch (Exception e) { 78 79 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage()); 80 return null; 81 } 29 // this gets the collection info 30 Element page_response = (Element)GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM); 31 Element collection_info = (Element)GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM); 32 if (collection_info== null) { 33 return; 82 34 } 83 35 84 // add in the system stuff - display and config - this all goes into 85 // a pageExtra element 86 Document message_doc = message.getOwnerDocument(); 87 Element page_extra = message_doc.createElement(GSXML.PAGE_EXTRA_ELEM); 88 message.appendChild(page_extra); 89 String lang = request.getAttribute(GSXML.LANG_ATT); 90 Element display = getDisplayElement(lang); 91 page_extra.appendChild(message_doc.importNode(display, true)); 92 page_extra.appendChild(message_doc.importNode(config_.config_xml_, true)); 93 94 // create the nzdl page extra 95 // check for collection 96 if (param_list != null) { 97 HashMap params = GSXML.extractParams(param_list, false); 98 String collection = (String)params.get(GSCGI.COLLECTION_ARG); 99 if (collection != null && !collection.equals("")) { 100 addNZDLPageExtra(page_extra, lang, collection); 101 } 102 } 103 //message.appendChild(message_doc.importNode(getPageExtra(lang, collection), true)); 104 105 //} else { 106 //message.appendChild(message_doc.createElement("pageExtra")); 107 // } 108 //} else { 109 // message.appendChild(message_doc.createElement("pageExtra")); 110 //} 111 112 Element page = a.process(message); 113 return page; 114 115 } 116 117 //protected Element getPageExtra(String lang, String collection) { 118 protected void addNZDLPageExtra(Element page_extra, String lang, 119 String collection) { 120 121 Document doc = page_extra.getOwnerDocument(); 122 123 // get the coll description to see whatservices are available 124 Element info_message = doc_.createElement(GSXML.MESSAGE_ELEM); 125 Element info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, collection, lang); 126 Element param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 127 info_request.appendChild(param_list); 128 129 Element param = GSXML.createParameter(doc_,GSXML.SUBSET_PARAM, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER); 130 param_list.appendChild(param); 131 info_message.appendChild(info_request); 132 133 Element info_response = mr_.process(info_message); 134 135 //System.out.println("NZDLrecept: coll response = "+converter_.getPrettyString(info_response)); 136 Element service_list = (Element)GSXML.getNodeByPath(info_response, "response/collection/serviceList"); 137 138 Element phrase_service = GSXML.getNamedElement(service_list, GSXML.SERVICE_ELEM, GSXML.NAME_ATT, "PhindApplet"); 139 if (phrase_service !=null) { 140 page_extra.appendChild(doc.importNode(phrase_service, false)); 36 String collection = collection_info.getAttribute("name"); // should we take this from the request instead?? 37 // look through services and see if classifier one is present - need to 38 // get the list of classifiers 39 Element service_list = (Element)GSXML.getChildByTagName(collection_info, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER); 40 if (service_list == null) { 41 // something weird has gone wrong 42 return; 141 43 } 142 44 … … 145 47 return; // no browsing stuff 146 48 } 147 49 String lang = page.getAttribute(GSXML.LANG_ATT); 148 50 // find the list of classifiers 149 info_message = doc_.createElement(GSXML.MESSAGE_ELEM);51 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM); 150 52 String to = GSPath.appendLink(collection, "ClassifierBrowse"); 151 info_request = GSXML.createBasicRequest(doc_, GSXML.REQUEST_TYPE_DESCRIBE, to, lang);53 Element info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, lang); 152 54 info_message.appendChild(info_request); 153 154 info_response = (Element) mr_.process(info_message);155 156 classifier_service = (Element)GSXML.getNodeByPath(info_response, "response/service");157 55 158 page_extra.appendChild(doc.importNode(classifier_service, true)); 56 Element info_response = (Element) this.mr.process(info_message); 57 String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.SERVICE_ELEM); 58 Element classifier_service_info = (Element)GSXML.getNodeByPath(info_response, path); 59 // add all the children into the classifier service element 60 GSXML.copyAllChildren(classifier_service, classifier_service_info); 61 System.out.println("the final page before transforming is"); 62 System.out.println(this.converter.getPrettyString(page)); 159 63 return; 64 } 160 65 161 162 }163 164 66 } 165 67 -
trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java
r4144 r4258 14 14 import java.util.Enumeration; 15 15 16 /** core class for web interface generation. Receives requests consisting17 * of an xml representation of cgi args, and returns the page of data - in18 * html by default. The requests are processed by the appropriate action class16 /** the most basic Receptionist, used for interface generation. 17 * Receives requests consisting 18 * of an xml representation of cgi args, and returns the page of data. The requests are processed by the appropriate action class 19 19 * 20 20 * @see Action … … 23 23 24 24 /** the set up variables */ 25 protected ConfigVars config_=null; 26 27 /** the cgi args converter */ 28 protected GSCGI cgi_ = null; 25 /// this is web specific - perhaps use an interface so diff types (web/java app) can use diff classes 26 protected ConfigVars config=null; 29 27 30 28 /** container Document to create XML Nodes */ 31 protected Document doc _=null;29 protected Document doc=null; 32 30 33 31 /** a converter class to parse XML and create Docs */ 34 protected XMLConverter converter _=null;35 36 /** the message router that the actions will talk to */37 protected ModuleInterface mr _=null;32 protected XMLConverter converter=null; 33 34 /** the message router that the Receptionist and Actions will talk to */ 35 protected ModuleInterface mr=null; 38 36 39 37 /** the list of actions */ 40 protected HashMap action_map _=null;41 38 protected HashMap action_map=null; 39 42 40 public Receptionist() { 43 converter_ = new XMLConverter(); 44 doc_ = converter_.newDOM(); 45 action_map_= new HashMap(); 46 cgi_ = new GSCGI(); 47 } 48 41 this.converter = new XMLConverter(); 42 this.doc = this.converter.newDOM(); 43 this.action_map= new HashMap(); 44 } 45 49 46 /** the set up vars must be set before configure called*/ 50 47 public void setConfigVars(ConfigVars config) { 51 config_= config;48 this.config = config; 52 49 } 53 50 … … 55 52 * configured before being passed to the receptionist*/ 56 53 public void setMessageRouter(ModuleInterface m) { 57 mr_= m;54 this.mr = m; 58 55 } 59 56 … … 61 58 public boolean configure() { 62 59 63 if ( config_==null) {60 if (this.config==null) { 64 61 System.err.println("Receptionist Error: config variables must be set before calling configure"); 65 62 return false; 66 63 } 67 if ( mr_==null) {64 if (this.mr==null) { 68 65 System.err.println("Receptionist Error: message router must be set before calling configure"); 69 66 return false; 70 67 } 71 68 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 75 Action a = new PageAction(); 76 a.setConfigVars(config_); 77 a.setMessageRouter(mr_); 78 a.setCGI(cgi_); 79 a.configure(); 80 action_map_.put("p", a); 81 82 a = new QueryAction(); 83 a.setConfigVars(config_); 84 a.setCGI(cgi_); 85 a.setMessageRouter(mr_); 86 a.configure(); 87 action_map_.put("q", a); 88 89 a = new BrowseAction(); 90 a.setConfigVars(config_); 91 a.setCGI(cgi_); 92 a.setMessageRouter(mr_); 93 a.configure(); 94 action_map_.put("b", a); 95 96 a = new DocumentAction(); 97 a.setConfigVars(config_); 98 a.setCGI(cgi_); 99 a.setMessageRouter(mr_); 100 a.configure(); 101 action_map_.put("d", a); 102 103 a = new AppletAction(); 104 a.setConfigVars(config_); 105 a.setCGI(cgi_); 106 a.setMessageRouter(mr_); 107 a.configure(); 108 action_map_.put("a", a); 109 110 a = new ProcessAction(); 111 a.setConfigVars(config_); 112 a.setCGI(cgi_); 113 a.setMessageRouter(mr_); 114 a.configure(); 115 action_map_.put("pr", a); 116 117 a = new SystemAction(); 118 a.setConfigVars(config_); 119 a.setCGI(cgi_); 120 a.setMessageRouter(mr_); 121 a.configure(); 122 action_map_.put("s", a); 123 69 // find the config file containing a list of actions 70 File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(this.config.gsdl3_home_, this.config.interface_name_))); 71 if (!interface_config_file.exists()) { 72 System.err.println("Receptionist: interface config file: "+interface_config_file.getPath()+" not found!"); 73 return false; 74 } 75 76 Element config_doc = this.converter.getDOM(interface_config_file).getDocumentElement(); 77 Element action_list = (Element)GSXML.getChildByTagName(config_doc, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER); 78 NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM); 79 80 for (int i=0; i<actions.getLength(); i++) { 81 Element action = (Element) actions.item(i); 82 String class_name = action.getAttribute("class"); 83 String action_name = action.getAttribute("name"); 84 Action ac = null; 85 try { 86 ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance(); 87 } catch (Exception e) { 88 System.out.println("couldn't load in action "+class_name); 89 e.printStackTrace(); 90 continue; 91 } 92 ac.setConfigVars(this.config); 93 ac.setMessageRouter(this.mr); 94 ac.configure(); 95 this.action_map.put(action_name, ac); 96 } 124 97 125 98 return true; … … 128 101 public String process(String xml_in) { 129 102 130 Element message = converter_.getDOM(xml_in).getDocumentElement();103 Element message = this.converter.getDOM(xml_in).getDocumentElement(); 131 104 Element page = process(message); 132 return converter_.getString(page);105 return this.converter.getString(page); 133 106 } 134 107 … … 144 117 return null; 145 118 } 146 // check th at the request type is correct119 // check the request type 147 120 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name 148 if (!type.equals(GSXML.REQUEST_TYPE_CGI)) { 121 if (!type.equals(GSXML.REQUEST_TYPE_CGI)) { // should use a diff name?? 149 122 // now Receptionist forwards non-cgi requests straight to the MR, and returns the responses 150 123 System.err.println("Receptionist: request type is not '"+GSXML.REQUEST_TYPE_CGI+"', but it is '"+type+"', so forwarding the message to the MR!"); 151 // process t ehwhole message - mr needs <message> tags, and124 // process the whole message - mr needs <message> tags, and 152 125 // in this case, there may be more than one request in the message 153 return mr_.process(message);126 return this.mr.process(message); 154 127 } 155 128 // work out which action to pass to … … 159 132 return null; 160 133 } 161 // convert cgi short names to long names in the param list 162 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 163 if (param_list!=null) { 164 if (!cgi_.paramListToLongNames(param_list)) { 165 System.err.println("Receptionist Error: couldn't convert short names to long names in the param list!"); 166 return null; 167 } 168 } 169 170 // pass request to appropriate action 171 Action a = (Action)action_map_.get(action); 134 135 // find the appropriate action 136 Action a = (Action)this.action_map.get(action); 172 137 String action_name=null; 173 138 if (a==null) { // not in the map yet … … 176 141 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action"; 177 142 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance(); 178 ac.setConfigVars(config_); 179 ac.setMessageRouter(mr_); 180 ac.setCGI(cgi_); 143 ac.setConfigVars(this.config); 144 ac.setMessageRouter(this.mr); 181 145 ac.configure(); 182 action_map_.put(action, ac);146 this.action_map.put(action, ac); 183 147 a = ac; 184 148 } catch (Exception e) { … … 189 153 } 190 154 191 // add in the system stuff - display and config - this all goes into 192 // a pageExtra element 193 Document message_doc = message.getOwnerDocument(); 194 Element page_extra = message_doc.createElement(GSXML.PAGE_EXTRA_ELEM); 195 message.appendChild(page_extra); 196 String lang = request.getAttribute(GSXML.LANG_ATT); 197 Element display = getDisplayElement(lang); 198 page_extra.appendChild(message_doc.importNode(display, true)); 199 page_extra.appendChild(message_doc.importNode(config_.config_xml_, true)); 200 201 Element page = a.process(message); 155 // transform the request in some way 156 preProcessRequest(request); 157 // set up the page 158 Element page = this.doc.createElement(GSXML.PAGE_ELEM); 159 // do we need this?? 160 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT)); 161 162 // get the page data from the action 163 Element action_response = a.process(message); 164 165 boolean response_only=false; 166 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER); 167 if (param_list != null) { 168 Element param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "ro"); 169 if (param != null) { 170 String value = param.getAttribute("value"); 171 if (value.equals("1")) { 172 response_only = true; 173 } 174 } 175 } 176 if (response_only) { 177 // only the response from the action is sent back 178 return action_response; 179 } 180 181 // the request is part of the page 182 page.appendChild(GSXML.duplicateWithNewName(this.doc, request, GSXML.PAGE_REQUEST_ELEM, true)); 183 // add the response too 184 Element page_response = GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(action_response, GSXML.RESPONSE_ELEM), GSXML.PAGE_RESPONSE_ELEM, true); 185 page.appendChild(page_response); 186 187 // transform the result in some way 188 Element resulting_page = postProcessPage(page); 189 return resulting_page; 190 191 } 192 193 protected void preProcessRequest(Element request) { 194 return; 195 } 196 197 protected Element postProcessPage(Element page) { 202 198 return page; 203 204 } 205 206 /** returns an xml element containing all the text strings 207 * needed for an interface 208 * currently only uses the current interface - should add in 209 * needed ones from the default interface if current != default */ 210 protected Element getDisplayElement(String lang) { 211 212 Element display = doc_.createElement(GSXML.DISPLAY_ELEM); 213 // looks for properties files based on interface name 214 String resource_name = "interface_"+config_.interface_name_; 215 216 Dictionary dict = new Dictionary(resource_name, lang); 217 Enumeration enum = dict.getKeys(); 218 while (enum.hasMoreElements()) { 219 String key = (String)enum.nextElement(); 220 String value = dict.get(key); 221 Element e = GSXML.createTextElement(doc_, key, value); 222 display.appendChild(e); 223 } 224 return display; 225 } 199 } 200 226 201 227 202 }
Note:
See TracChangeset
for help on using the changeset viewer.