source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/PageAction.java@ 32549

Last change on this file since 32549 was 32549, checked in by kjdon, 5 years ago

receptionist now passes languageList (from interfaceConfig) to teh actions, so an action can add it into the page response if it needs to, rather than the receptionist adding it in to every page. its only ever used in prefs page

  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import java.io.Serializable;
4import java.util.HashMap;
5
6import org.apache.log4j.Logger;
7import org.greenstone.gsdl3.util.GSParams;
8import org.greenstone.gsdl3.util.GSPath;
9import org.greenstone.gsdl3.util.GSXML;
10import org.greenstone.gsdl3.util.UserContext;
11import org.greenstone.gsdl3.util.XMLConverter;
12import org.greenstone.util.GlobalProperties;
13import org.w3c.dom.Document;
14import org.w3c.dom.Element;
15import org.w3c.dom.Node;
16import org.w3c.dom.NodeList;
17
18public class PageAction extends Action
19{
20 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName());
21
22 public static final String HOME_PAGE = "home";
23 public static final String ABOUT_PAGE = "about";
24 public static final String PREFS_PAGE = "pref";
25 public static final String GLI4GS3_PAGE = "gli4gs3";
26
27 public Node process(Node message_node)
28
29 {
30 Element message = GSXML.nodeToElement(message_node);
31 Document doc = XMLConverter.newDOM();
32
33 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
34 Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
35 String collection = "";
36 if (paramList != null)
37 {
38 HashMap<String, Serializable> params = GSXML.extractParams(paramList, false);
39 if (params != null && params.get(GSParams.COLLECTION) != null)
40 {
41 collection = (String) params.get(GSParams.COLLECTION);
42 }
43 }
44
45 // the page name is the subaction
46 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
47 if (page_name.equals(""))
48 { // if no page specified, assume home page
49 page_name = HOME_PAGE;
50 }
51 Element result = doc.createElement(GSXML.MESSAGE_ELEM);
52 Element response;
53 if (page_name.equals(HOME_PAGE))
54 {
55 response = homePage(request);
56 //} else if (page_name.equals(ABOUT_PAGE)) {
57 }
58 else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE))
59 {
60 response = aboutOrPrefsPage(request, page_name);
61 //}else if (page_name.equals(PREFS_PAGE)) {
62 //response = prefsPage(request);
63 }
64 else if (page_name.equals(GLI4GS3_PAGE))
65 {
66 response = gli4gs3Page(request);
67 }
68 else
69 { // unknown page
70 response = unknownPage(request);
71 }
72
73 Element formatMessage = doc.createElement(GSXML.MESSAGE_ELEM);
74 Element formatRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, collection, new UserContext(request));
75 formatMessage.appendChild(formatRequest);
76 Element formatResponseMessage = (Element) this.mr.process(formatMessage);
77 Element formatResponse = (Element) GSXML.getChildByTagName(formatResponseMessage, GSXML.RESPONSE_ELEM);
78
79 Element globalFormat = (Element) GSXML.getChildByTagName(formatResponse, GSXML.FORMAT_ELEM);
80 if (globalFormat != null)
81 {
82 response.appendChild(response.getOwnerDocument().importNode(globalFormat, true));
83 }
84
85 result.appendChild(doc.importNode(response, true));
86 logger.debug("page action result: " + this.converter.getPrettyString(result));
87
88 return result;
89 }
90
91 protected Element homePage(Element request)
92 {
93 Document doc = XMLConverter.newDOM();
94
95
96 UserContext userContext = new UserContext(request);
97 // first, get the message router info
98 Element info_message = doc.createElement(GSXML.MESSAGE_ELEM);
99 Element info_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
100 //Create param list
101 Element param_list_element = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
102 info_request.appendChild(param_list_element);
103 //Describe params without collectionlist. Collectionlist provided by CollectionGroup service
104 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
105 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
106 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM, GSXML.SITE_ELEM + GSXML.LIST_MODIFIER);
107 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
108 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
109 info_message.appendChild(info_request);
110 //Send request to message router
111 Element info_response_message = (Element) this.mr.process(info_message);
112 //Check if it is not null
113 if (info_response_message == null)
114 {
115 logger.error(" couldn't query the message router!");
116 return null;
117 }
118 //Check if it is not null
119 Element info_response = (Element) GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
120 if (info_response == null)
121 {
122 logger.error("couldn't query the message router!");
123 return null;
124 }
125
126 Element resp_service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
127
128 if (resp_service_list == null) {
129 logger.error("No services available. Couldn't query the message router!");
130 return null;
131 }
132 Element groupInfoService = GSXML.getNamedElement(resp_service_list, GSXML.SERVICE_ELEM, GSXML.TYPE_ATT,
133 GSXML.SERVICE_TYPE_GROUPINFO);
134 if (groupInfoService != null) {
135 // Prepare request for CollectionGroup service to get current
136 // collections and groups list
137 Element group_info_message = doc.createElement(GSXML.MESSAGE_ELEM);
138 Element group_info_request = GSXML.createBasicRequest(doc, GSXML.TO_ATT,
139 groupInfoService.getAttribute(GSXML.NAME_ATT), userContext);
140 group_info_message.appendChild(group_info_request);
141 //Append group request if exists
142 Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
143 if (paramList != null) {
144 group_info_request.appendChild(doc.importNode(paramList, true));
145 }
146 Element group_info_response_message = (Element) this.mr.process(group_info_message);
147 Element group_info_response = (Element) GSXML.getChildByTagName(group_info_response_message,
148 GSXML.RESPONSE_ELEM);
149 Element collection_list = (Element) GSXML.getChildByTagName(group_info_response,
150 GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
151 // Add Collection List from CollectionGroup Service to response
152 // from message router
153 info_response = (Element) doc.importNode(info_response, true);
154 if (collection_list != null) {
155 info_response.appendChild(doc.importNode(collection_list, true));
156 }
157 Element group_list = (Element) GSXML.getChildByTagName(group_info_response,
158 GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
159 if (group_list != null) {
160 info_response.appendChild(doc.importNode(group_list, true));
161 }
162 Element path_list = (Element) GSXML.getChildByTagName(group_info_response,
163 GSXML.PATH_ELEM + GSXML.LIST_MODIFIER);
164 if (path_list != null) {
165 info_response.appendChild(doc.importNode(path_list, true));
166 }
167 // Send message to groupInfoType Services
168 } else {
169 // If no service with type SERVICE_TYPE_GROUPINFO could be provided
170 // request message router for all available collections
171 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM,
172 GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
173 info_response_message = (Element) this.mr.process(info_message);
174
175 if (info_response_message == null) {
176 logger.error(" couldn't query the message router!");
177 return null;
178 }
179 info_response = (Element) GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
180 if (info_response == null) {
181 logger.error("couldn't query the message router!");
182 return null;
183 }
184 }
185
186 // second, get the metadata for each collection - we only want specific
187 // elements but for now, we'll just get it all
188 Element collection_list = (Element) GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
189 //logger.debug(GSXML.xmlNodeToString(collection_list));
190 if (collection_list != null)
191 {
192 NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM);
193 if (colls.getLength() > 0)
194 {
195 sendMultipleRequests(doc, colls, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
196 }
197 }
198
199 // get metadata for any services
200 Element service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
201 if (service_list != null)
202 {
203 NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM);
204 if (services.getLength() > 0)
205 {
206 sendMultipleRequests(doc, services, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
207 }
208 }
209
210 // get metadata for service clusters
211 Element cluster_list = (Element) GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
212 if (cluster_list != null)
213 {
214 NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM);
215 if (clusters.getLength() > 0)
216 {
217 sendMultipleRequests(doc, clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
218
219 }
220 }
221
222 addSiteMetadata(info_response, userContext);
223 addInterfaceOptions(info_response);
224 // all the components have been merged into info_response
225 return info_response;
226
227 } // homePage
228
229 protected Element aboutOrPrefsPage(Element request, String page_name)
230 {
231 Document doc = XMLConverter.newDOM();
232
233 UserContext userContext = new UserContext(request);
234 // extract the params from the cgi-request,
235 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
236 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
237
238 String coll_name = (String) params.get(GSParams.COLLECTION);
239 if (coll_name == null || coll_name.equals(""))
240 {
241 // return a response with no collection info - must be prefs from home page
242 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
243 addSiteMetadata(response, userContext);
244 addInterfaceOptions(response);
245 if (this.language_list != null) {
246 response.appendChild(doc.importNode(this.language_list, true));
247 }
248 return response;
249 }
250
251 // get the collection or cluster description
252 Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
253
254 Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
255 coll_about_message.appendChild(coll_about_request);
256 Element coll_about_response = (Element) this.mr.process(coll_about_message);
257
258 // add collection type attribute to paramList
259 String col_type = "";
260 NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
261 if (collect_elem.getLength() != 0)
262 {
263 for (int i = 0; i < collect_elem.getLength(); i++)
264 {
265 Element e = (Element) collect_elem.item(i);
266 col_type = e.getAttribute(GSXML.TYPE_ATT);
267 }
268 }
269 else
270 {
271 logger.error(GSXML.COLLECTION_ELEM + " element is null");
272 }
273
274 // adding a ct param to paramlist. only needed for gs2 interface, not default
275 NodeList paramList_list = request.getElementsByTagName(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
276 if (paramList_list.getLength() != 0)
277 {
278 for (int i = 0; i < paramList_list.getLength(); i++)
279 {
280 Element e = (Element) paramList_list.item(i);
281 Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1");
282 e.appendChild(ct);
283 }
284 }
285 else
286 {
287 logger.info("paramList is null!!");
288 }
289
290 if (coll_about_response == null)
291 {
292 return null;
293 }
294
295 // second, get the info for each service - we only want display items
296 // but for now, we'll just get it all
297 NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM);
298 if (services.getLength() > 0)
299 {
300 sendMultipleRequests(doc, services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
301 }
302
303 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
304 //add the site metadata
305 addSiteMetadata(response, userContext);
306 addInterfaceOptions(response);
307 if (page_name.equals(PREFS_PAGE) && this.language_list != null) {
308 response.appendChild(response.getOwnerDocument().importNode(this.language_list, true));
309 }
310 return response;
311 }
312
313 //protected Element prefsPage(Element request) {
314
315 // return null;
316 //}
317
318 /** if we dont know the page type, use this method */
319 protected Element unknownPage(Element request)
320 {
321 Document doc = XMLConverter.newDOM();
322
323 UserContext userContext = new UserContext(request);
324 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
325
326 // extract the params from the cgi-request,
327 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
328 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
329
330 String coll_name = (String) params.get(GSParams.COLLECTION);
331 if (coll_name == null || coll_name.equals(""))
332 {
333 // just return an empty response
334 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
335 addSiteMetadata(response, userContext);
336 addInterfaceOptions(response);
337 return response;
338 }
339
340 // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
341
342 // if there is a service specified should we get the service description instead??
343 // get the collection or cluster description
344 Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
345
346 Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
347 coll_about_message.appendChild(coll_about_request);
348
349 Element coll_about_response = (Element) this.mr.process(coll_about_message);
350
351 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
352
353 //add the site metadata
354 addSiteMetadata(response, userContext);
355 addInterfaceOptions(response);
356
357 return response;
358
359 }
360
361 protected boolean sendMultipleRequests(Document doc, NodeList items, String path_prefix, String request_type, UserContext userContext)
362 {
363 // we will send all the requests in a single message
364 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
365 for (int i = 0; i < items.getLength(); i++)
366 {
367 Element c = (Element) items.item(i);
368 String path = c.getAttribute(GSXML.NAME_ATT);
369 if (path_prefix != null)
370 {
371 path = GSPath.appendLink(path_prefix, path);
372 }
373 Element request = GSXML.createBasicRequest(doc, request_type, path, userContext);
374 message.appendChild(request);
375 }
376
377 Element response_message = (Element) this.mr.process(message);
378
379 NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM);
380 // check that have same number of responses as requests
381 if (items.getLength() != responses.getLength())
382 {
383 logger.error("didn't get a response for each request - somethings gone wrong!");
384 return false;
385 }
386
387 for (int i = 0; i < items.getLength(); i++)
388 {
389 Element c1 = (Element) items.item(i);
390 Element c2 = (Element) GSXML.getChildByTagName((Element) responses.item(i), c1.getTagName());
391 if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT)))
392 {
393 //add the new data into the original element
394 GSXML.mergeElements(c1, c2);
395 }
396 else
397 {
398 logger.debug(" response does not correspond to request!");
399 }
400
401 }
402
403 return true;
404
405 }
406
407 protected Element gli4gs3Page(Element request)
408 {
409 Document doc = XMLConverter.newDOM();
410
411 String lang = request.getAttribute(GSXML.LANG_ATT);
412 String uid = request.getAttribute(GSXML.USER_ID_ATT);
413
414 Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
415
416 Element applet_elem = doc.createElement("Applet");
417 page_response.appendChild(applet_elem);
418 applet_elem.setAttribute("ARCHIVE", "SignedGatherer.jar"); // SignedGatherer.jar should be placed in web/applet.
419 applet_elem.setAttribute("CODE", "org.greenstone.gatherer.WebGatherer");
420 applet_elem.setAttribute("CODEBASE", "applet"); // SignedGatherer.jar is in web/applet. But CODEBASE is the *URL* path to the (jar) file containing the main class, and is relative to documentroot "web".
421 applet_elem.setAttribute("HEIGHT", "50");
422 applet_elem.setAttribute("WIDTH", "380");
423
424 Element gwcgi_param_elem = doc.createElement("PARAM");
425 gwcgi_param_elem.setAttribute("name", "gwcgi");
426 String library_name = GlobalProperties.getGSDL3WebAddress();
427 gwcgi_param_elem.setAttribute("value", library_name);
428 applet_elem.appendChild(gwcgi_param_elem);
429
430 Element gsdl3_param_elem = doc.createElement("PARAM");
431 gsdl3_param_elem.setAttribute("name", "gsdl3");
432 gsdl3_param_elem.setAttribute("value", "true");
433 applet_elem.appendChild(gsdl3_param_elem);
434
435 // When an applet doesn't work in the browser, set the default display text to provide a link to the JNLP file to run with Java Web Start
436 // The display text will be:
437 // Applets don't seem to work in your browser. In place of the GLI Applet, try running its alternative <a href="applet/GLIappWebStart.jnlp">Java Web Start (JNLP) version</a>
438 Node default_text = doc.createTextNode("Applets don't seem to work in your browser. In place of the GLI Applet, try running its alternative ");
439 Element link_to_jnlp = doc.createElement("a");
440 link_to_jnlp.setAttribute("href", "applet/GLIappWebStart.jnlp");
441 Node anchor_text = doc.createTextNode("Java Web Start (JNLP) version");
442 link_to_jnlp.appendChild(anchor_text);
443 applet_elem.appendChild(default_text);
444 applet_elem.appendChild(link_to_jnlp);
445
446 return page_response;
447 }
448}
Note: See TracBrowser for help on using the repository browser.