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

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

need to check whether group is null before testing its value

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