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

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

just deleted some commented out code

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