source: trunk/gsdl3/src/java/org/greenstone/gsdl3/core/Receptionist.java@ 4995

Last change on this file since 4995 was 4988, checked in by kjdon, 21 years ago

the base receptionist class now has a GSParams class - this contains the common interface params, and whether or ot to save them

  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
14import java.util.Enumeration;
15
16/** 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 *
20 * @see Action
21 */
22public class Receptionist implements ModuleInterface {
23
24 /** the set up variables */
25 protected HashMap config_params = null;
26 /** container Document to create XML Nodes */
27 protected Document doc=null;
28
29 /** a converter class to parse XML and create Docs */
30 protected XMLConverter converter=null;
31
32 /** the message router that the Receptionist and Actions will talk to */
33 protected ModuleInterface mr=null;
34
35 /** the list of actions */
36 protected HashMap action_map=null;
37
38 /** the list of params */
39 protected GSParams params=null;
40
41 public Receptionist() {
42 this.converter = new XMLConverter();
43 this.doc = this.converter.newDOM();
44 this.action_map= new HashMap();
45 }
46
47 public void setParams(GSParams params) {
48 this.params = params;
49 }
50 public void setConfigParams(HashMap params) {
51 this.config_params = params;
52 }
53 /** sets the message router - it should already be created and
54 * configured before being passed to the receptionist*/
55 public void setMessageRouter(ModuleInterface m) {
56 this.mr = m;
57 }
58
59 /** configures the receptionist */
60 public boolean configure() {
61
62 if (this.config_params==null) {
63 System.err.println("Receptionist Error: config variables must be set before calling configure");
64 return false;
65 }
66 if (this.mr==null) {
67 System.err.println("Receptionist Error: message router must be set before calling configure");
68 return false;
69 }
70
71 // find the config file containing a list of actions
72 File interface_config_file = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome((String)this.config_params.get(GSConstants.GSDL3_HOME), (String)this.config_params.get(GSConstants.INTERFACE_NAME))));
73 if (!interface_config_file.exists()) {
74 System.err.println("Receptionist: interface config file: "+interface_config_file.getPath()+" not found!");
75 return false;
76 }
77
78 Element config_doc = this.converter.getDOM(interface_config_file).getDocumentElement();
79 Element action_list = (Element)GSXML.getChildByTagName(config_doc, GSXML.ACTION_ELEM+GSXML.LIST_MODIFIER);
80 NodeList actions = action_list.getElementsByTagName(GSXML.ACTION_ELEM);
81
82 for (int i=0; i<actions.getLength(); i++) {
83 Element action = (Element) actions.item(i);
84 String class_name = action.getAttribute("class");
85 String action_name = action.getAttribute("name");
86 Action ac = null;
87 try {
88 ac = (Action)Class.forName("org.greenstone.gsdl3.action."+class_name).newInstance();
89 } catch (Exception e) {
90 System.out.println("couldn't load in action "+class_name);
91 e.printStackTrace();
92 continue;
93 }
94 ac.setConfigParams(this.config_params);
95 ac.setMessageRouter(this.mr);
96 ac.configure();
97 ac.getActionParameters(this.params);
98 this.action_map.put(action_name, ac);
99 }
100
101 return true;
102 }
103 /** process using strings - just calls process using Elements */
104 public String process(String xml_in) {
105
106 Element message = this.converter.getDOM(xml_in).getDocumentElement();
107 Element page = process(message);
108 return this.converter.getString(page);
109 }
110
111 /** process - produce a page of data in response to a request
112 * if something goes wrong, it returns null -
113 * TODO: return a suitable message to the user */
114 public Element process(Element message) {
115
116 // get the request out of the message - assume that there is only one
117 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
118 if (request == null) {
119 System.err.println("Receptionist Error: message had no request!");
120 return null;
121 }
122 // check the request type
123 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
124 if (!type.equals(GSXML.REQUEST_TYPE_PAGE)) {
125 // now Receptionist forwards non-page requests straight to the MR, and returns the responses
126 System.err.println("Receptionist: request type is not '"+GSXML.REQUEST_TYPE_PAGE+"', but it is '"+type+"', so forwarding the message to the MR!");
127 // process the whole message - mr needs <message> tags, and
128 // in this case, there may be more than one request in the message
129 return this.mr.process(message);
130 }
131 // work out which action to pass to
132 String action = request.getAttribute(GSXML.ACTION_ATT);
133 if (action.equals("")) {
134 System.err.println("Receptionist Error: no action specified in the request!");
135 return null;
136 }
137
138 // find the appropriate action
139 Action a = (Action)this.action_map.get(action);
140 String action_name=null;
141 if (a==null) { // not in the map yet
142 // try to load a new action
143 try {
144 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
145 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
146 ac.setConfigParams(this.config_params);
147 ac.setMessageRouter(this.mr);
148 ac.configure();
149 ac.getActionParameters(this.params);
150 this.action_map.put(action, ac);
151 a = ac;
152 } catch (Exception e) {
153
154 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
155 return null;
156 }
157 }
158
159 // transform the request in some way
160 preProcessRequest(request);
161 // set up the page
162 Element page = this.doc.createElement(GSXML.PAGE_ELEM);
163 page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
164
165 // get the page data from the action
166 Element action_response = a.process(message);
167
168 boolean response_only=false;
169 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
170 if (param_list != null) {
171 Element param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "ro");
172 if (param != null) {
173 String value = param.getAttribute("value");
174 if (value.equals("1")) {
175 response_only = true;
176 }
177 }
178 }
179 if (response_only) {
180 // only the response from the action is sent back
181 return action_response;
182 }
183
184 // the request is part of the page
185 page.appendChild(GSXML.duplicateWithNewName(this.doc, request, GSXML.PAGE_REQUEST_ELEM, true));
186 // add the response too
187 Element page_response = GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(action_response, GSXML.RESPONSE_ELEM), GSXML.PAGE_RESPONSE_ELEM, true);
188 page.appendChild(page_response);
189
190 // transform the result in some way
191 Element resulting_page = postProcessPage(page);
192 return resulting_page;
193
194 }
195
196 protected void preProcessRequest(Element request) {
197 return;
198 }
199
200 protected Element postProcessPage(Element page) {
201 return page;
202 }
203
204
205}
Note: See TracBrowser for help on using the repository browser.