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

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

no longer use ConfigVars, use a HashMap instead. request type cgi is now request type page

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