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

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

we now keep a language list - read in from teh config file, that lists all teh langs available for the interface - used in preferences page. there may be a better way to do this

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