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

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

tidying up

  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 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/** 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 */
22public class Receptionist implements ModuleInterface {
23
24 /** the set up variables */
25 protected ConfigVars config_=null;
26
27 /** the cgi args converter */
28 protected GSCGI cgi_ = null;
29
30 /** container Document to create XML Nodes */
31 protected Document doc_=null;
32
33 /** 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;
38
39 /** the list of actions */
40 protected HashMap action_map_=null;
41
42 public Receptionist() {
43 converter_ = new XMLConverter();
44 doc_ = converter_.newDOM();
45 action_map_= new HashMap();
46 cgi_ = new GSCGI();
47 }
48
49 /** the set up vars must be set before configure called*/
50 public void setConfigVars(ConfigVars config) {
51 config_ = config;
52 }
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 mr_ = m;
58 }
59
60 /** configures the receptionist */
61 public boolean configure() {
62
63 if (config_==null) {
64 System.err.println("Receptionist Error: config variables must be set before calling configure");
65 return false;
66 }
67 if (mr_==null) {
68 System.err.println("Receptionist Error: message router must be set before calling configure");
69 return false;
70 }
71
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
118 return true;
119 }
120 /** process using strings - just calls process using Elements */
121 public String process(String xml_in) {
122
123 Element message = converter_.getDOM(xml_in).getDocumentElement();
124 Element page = process(message);
125 return converter_.getString(page);
126 }
127
128 /** process - produce a page of data in response to a request
129 * if something goes wrong, it returns null -
130 * TODO: return a suitable message to the user */
131 public Element process(Element message) {
132
133 // get the request out of the message - assume that there is only one
134 Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
135 if (request == null) {
136 System.err.println("Receptionist Error: message had no request!");
137 return null;
138 }
139 // check that the request type is correct
140 String type = request.getAttribute(GSXML.TYPE_ATT); // returns "" if no att of this name
141 if (!type.equals(GSXML.REQUEST_TYPE_CGI)) {
142 System.err.println("Receptionist Error: request type should be '"+GSXML.REQUEST_TYPE_CGI+"', but it was '"+type+"'!");
143 return null;
144 }
145 // work out which action to pass to
146 String action = request.getAttribute(GSXML.ACTION_ATT);
147 if (action.equals("")) {
148 System.err.println("Receptionist Error: no action specified in the request!");
149 return null;
150 }
151 // convert cgi short names to long names in the param list
152 Element param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
153 if (param_list!=null) {
154 if (!cgi_.paramListToLongNames(param_list)) {
155 System.err.println("Receptionist Error: couldn't convert short names to long names in the param list!");
156 return null;
157 }
158 }
159
160 // pass request to appropriate action
161 Action a = (Action)action_map_.get(action);
162 String action_name=null;
163 if (a==null) { // not in the map yet
164 // try to load a new action
165 try {
166 action_name = action.substring(0,1).toUpperCase()+action.substring(1)+"Action";
167 Action ac = (Action)Class.forName("org.greenstone.gsdl3.action."+action_name).newInstance();
168 ac.setConfigVars(config_);
169 ac.setMessageRouter(mr_);
170 ac.setCGI(cgi_);
171 ac.configure();
172 action_map_.put(action, ac);
173 a = ac;
174 } catch (Exception e) {
175
176 System.err.println("Receptionist Error: a new action ("+action_name+") was specified and it couldn't be created. Error message:"+e.getMessage());
177 return null;
178 }
179 }
180
181 // add in the system stuff - display and config
182 Document message_doc = message.getOwnerDocument();
183 String lang = request.getAttribute(GSXML.LANG_ATT);
184 Element display = getDisplayElement(lang);
185 message.appendChild(message_doc.importNode(display, true));
186 message.appendChild(message_doc.importNode(config_.config_xml_, true));
187
188 Element page = a.process(message);
189 return page;
190
191 }
192
193 /** returns an xml element containing all the text strings
194 * needed for an interface
195 * currently only uses the current interface - should add in
196 * needed ones from the default interface if current != default */
197 protected Element getDisplayElement(String lang) {
198
199 Element display = doc_.createElement(GSXML.DISPLAY_ELEM);
200 // looks for properties files based on interface name
201 String resource_name = "interface_"+config_.interface_name_;
202
203 Dictionary dict = new Dictionary(resource_name, lang);
204 Enumeration enum = dict.getKeys();
205 while (enum.hasMoreElements()) {
206 String key = (String)enum.nextElement();
207 String value = dict.get(key);
208 Element e = GSXML.createTextElement(doc_, key, value);
209 display.appendChild(e);
210 }
211 return display;
212 }
213
214}
Note: See TracBrowser for help on using the repository browser.