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
RevLine 
[3340]1package org.greenstone.gsdl3.action;
2
[27617]3import java.io.Serializable;
4import java.util.HashMap;
5
6import org.apache.log4j.Logger;
[32649]7import org.greenstone.gsdl3.service.CollectionGroups;
[27617]8import org.greenstone.gsdl3.util.GSParams;
9import org.greenstone.gsdl3.util.GSPath;
10import org.greenstone.gsdl3.util.GSXML;
11import org.greenstone.gsdl3.util.UserContext;
[28964]12import org.greenstone.gsdl3.util.XMLConverter;
[22085]13import org.greenstone.util.GlobalProperties;
[28382]14import org.w3c.dom.Document;
[27617]15import org.w3c.dom.Element;
[24989]16import org.w3c.dom.Node;
[19984]17import org.w3c.dom.NodeList;
[3340]18
[24989]19public class PageAction extends Action
20{
[14395]21 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName());
[14311]22
[14395]23 public static final String HOME_PAGE = "home";
24 public static final String ABOUT_PAGE = "about";
25 public static final String PREFS_PAGE = "pref";
[24989]26 public static final String GLI4GS3_PAGE = "gli4gs3";
[3987]27
[32649]28 // this gets set to the groupInfo service name if there is one.
29 protected String groupInfoServiceName = null;
30
[24989]31 public Node process(Node message_node)
[30540]32
[24989]33 {
[28964]34 Element message = GSXML.nodeToElement(message_node);
35 Document doc = XMLConverter.newDOM();
[28382]36
[25991]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 = "";
[27617]40 if (paramList != null)
[25991]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 }
[16688]48
[24989]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 }
[28382]55 Element result = doc.createElement(GSXML.MESSAGE_ELEM);
[24989]56 Element response;
57 if (page_name.equals(HOME_PAGE))
58 {
[28855]59 response = homePage(request);
[24989]60 //} else if (page_name.equals(ABOUT_PAGE)) {
61 }
62 else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE))
63 {
[32549]64 response = aboutOrPrefsPage(request, page_name);
[24989]65 }
66 else if (page_name.equals(GLI4GS3_PAGE))
67 {
[28855]68 response = gli4gs3Page(request);
[24989]69 }
70 else
71 { // unknown page
[28855]72 response = unknownPage(request);
[24989]73 }
74
[28382]75 Element formatMessage = doc.createElement(GSXML.MESSAGE_ELEM);
76 Element formatRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, collection, new UserContext(request));
[25991]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 {
[28855]84 response.appendChild(response.getOwnerDocument().importNode(globalFormat, true));
[25991]85 }
86
[28382]87 result.appendChild(doc.importNode(response, true));
[24989]88 logger.debug("page action result: " + this.converter.getPrettyString(result));
[27617]89
[24989]90 return result;
[11230]91 }
[3568]92
[28855]93 protected Element homePage(Element request)
[24989]94 {
[28964]95 Document doc = XMLConverter.newDOM();
[28382]96
[30540]97
[24993]98 UserContext userContext = new UserContext(request);
[14395]99 // first, get the message router info
[28382]100 Element info_message = doc.createElement(GSXML.MESSAGE_ELEM);
[30540]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);
[30836]110 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
[30540]111 info_message.appendChild(info_request);
112 //Send request to message router
[24989]113 Element info_response_message = (Element) this.mr.process(info_message);
[30540]114 //Check if it is not null
[24989]115 if (info_response_message == null)
116 {
[14395]117 logger.error(" couldn't query the message router!");
118 return null;
119 }
[30540]120 //Check if it is not null
[24989]121 Element info_response = (Element) GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
122 if (info_response == null)
123 {
[14395]124 logger.error("couldn't query the message router!");
125 return null;
126 }
[32649]127
128 // import it into our current doc.
129 info_response = (Element) doc.importNode(info_response, true);
[30540]130
131 Element resp_service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
[32649]132
133 // TODO - is this true? can we run with no MR services? can't we still query for the collections/MR info?
[30540]134 if (resp_service_list == null) {
135 logger.error("No services available. Couldn't query the message router!");
136 return null;
137 }
[32649]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);
[32768]159 if (group != null && group.equals("")) {
[32649]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
[30540]184 } else {
[32649]185
[30540]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);
[14395]191
[30540]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
[14395]203 // second, get the metadata for each collection - we only want specific
204 // elements but for now, we'll just get it all
[24989]205 Element collection_list = (Element) GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
206 if (collection_list != null)
207 {
[14395]208 NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM);
[24989]209 if (colls.getLength() > 0)
210 {
[28382]211 sendMultipleRequests(doc, colls, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
[14395]212 }
213 }
214
215 // get metadata for any services
[24989]216 Element service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
217 if (service_list != null)
218 {
[14395]219 NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM);
[24989]220 if (services.getLength() > 0)
221 {
[28382]222 sendMultipleRequests(doc, services, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
[14395]223 }
224 }
225
226 // get metadata for service clusters
[24989]227 Element cluster_list = (Element) GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
228 if (cluster_list != null)
229 {
[14395]230 NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM);
[24989]231 if (clusters.getLength() > 0)
232 {
[28382]233 sendMultipleRequests(doc, clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
[14395]234
235 }
236 }
237
[27143]238 addSiteMetadata(info_response, userContext);
239 addInterfaceOptions(info_response);
[14395]240 // all the components have been merged into info_response
241 return info_response;
242
243 } // homePage
244
[32649]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
[32549]266 protected Element aboutOrPrefsPage(Element request, String page_name)
[24989]267 {
[28964]268 Document doc = XMLConverter.newDOM();
[28382]269
[24993]270 UserContext userContext = new UserContext(request);
[14395]271 // extract the params from the cgi-request,
[24989]272 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
[25635]273 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
[24989]274
275 String coll_name = (String) params.get(GSParams.COLLECTION);
276 if (coll_name == null || coll_name.equals(""))
277 {
[32549]278 // return a response with no collection info - must be prefs from home page
[28382]279 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
[24993]280 addSiteMetadata(response, userContext);
[25128]281 addInterfaceOptions(response);
[32549]282 if (this.language_list != null) {
283 response.appendChild(doc.importNode(this.language_list, true));
284 }
[19984]285 return response;
[14395]286 }
[3340]287
[14395]288 // get the collection or cluster description
[28382]289 Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
[3340]290
[28382]291 Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
[14395]292 coll_about_message.appendChild(coll_about_request);
[24989]293 Element coll_about_response = (Element) this.mr.process(coll_about_message);
[14395]294
[14642]295 // add collection type attribute to paramList
296 String col_type = "";
[24989]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 {
[14642]308 logger.error(GSXML.COLLECTION_ELEM + " element is null");
309 }
[24989]310
[31872]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)
[24989]314 {
[31872]315 for (int i = 0; i < paramList_list.getLength(); i++)
[24989]316 {
[31872]317 Element e = (Element) paramList_list.item(i);
[24989]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 {
[14642]324 logger.info("paramList is null!!");
[24989]325 }
326
327 if (coll_about_response == null)
328 {
[14395]329 return null;
330 }
[19984]331
[14395]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);
[24989]335 if (services.getLength() > 0)
336 {
[28382]337 sendMultipleRequests(doc, services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
[14395]338 }
339
340 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
[32649]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
[19984]354 //add the site metadata
[24993]355 addSiteMetadata(response, userContext);
[25128]356 addInterfaceOptions(response);
[32549]357 if (page_name.equals(PREFS_PAGE) && this.language_list != null) {
358 response.appendChild(response.getOwnerDocument().importNode(this.language_list, true));
359 }
[14395]360 return response;
[3467]361 }
[24989]362
363
[14395]364 /** if we dont know the page type, use this method */
[24989]365 protected Element unknownPage(Element request)
366 {
[28964]367 Document doc = XMLConverter.newDOM();
[28382]368
[24993]369 UserContext userContext = new UserContext(request);
[14395]370 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
[4155]371
[14395]372 // extract the params from the cgi-request,
[24989]373 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
[25635]374 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
[4950]375
[24989]376 String coll_name = (String) params.get(GSParams.COLLECTION);
377 if (coll_name == null || coll_name.equals(""))
378 {
[14395]379 // just return an empty response
[28382]380 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
[24993]381 addSiteMetadata(response, userContext);
[25128]382 addInterfaceOptions(response);
[19984]383 return response;
[14395]384 }
[5147]385
[14395]386 // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
[4155]387
[14395]388 // if there is a service specified should we get the service description instead??
389 // get the collection or cluster description
[28382]390 Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
[14395]391
[28382]392 Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
[14395]393 coll_about_message.appendChild(coll_about_request);
394
[24989]395 Element coll_about_response = (Element) this.mr.process(coll_about_message);
396
[14395]397 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
[19984]398
399 //add the site metadata
[24993]400 addSiteMetadata(response, userContext);
[25128]401 addInterfaceOptions(response);
[19984]402
[14395]403 return response;
404
[4257]405 }
[4155]406
[28382]407 protected boolean sendMultipleRequests(Document doc, NodeList items, String path_prefix, String request_type, UserContext userContext)
[24989]408 {
[14395]409 // we will send all the requests in a single message
[28382]410 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
[24989]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 {
[14395]417 path = GSPath.appendLink(path_prefix, path);
418 }
[28382]419 Element request = GSXML.createBasicRequest(doc, request_type, path, userContext);
[14395]420 message.appendChild(request);
421 }
[11230]422
[24989]423 Element response_message = (Element) this.mr.process(message);
424
[14395]425 NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM);
426 // check that have same number of responses as requests
[24989]427 if (items.getLength() != responses.getLength())
428 {
[14395]429 logger.error("didn't get a response for each request - somethings gone wrong!");
430 return false;
[24989]431 }
[14395]432
[24989]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 {
[14395]439 //add the new data into the original element
440 GSXML.mergeElements(c1, c2);
[24989]441 }
442 else
443 {
[19641]444 logger.debug(" response does not correspond to request!");
[24989]445 }
446
[14395]447 }
448
449 return true;
450
[11230]451 }
[14311]452
[24989]453 protected Element gli4gs3Page(Element request)
454 {
[28964]455 Document doc = XMLConverter.newDOM();
[28382]456
[14395]457 String lang = request.getAttribute(GSXML.LANG_ATT);
458 String uid = request.getAttribute(GSXML.USER_ID_ATT);
[14311]459
[28382]460 Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
[14311]461
[28382]462 Element applet_elem = doc.createElement("Applet");
[14395]463 page_response.appendChild(applet_elem);
[32127]464 applet_elem.setAttribute("ARCHIVE", "SignedGatherer.jar"); // SignedGatherer.jar should be placed in web/applet.
[32138]465 applet_elem.setAttribute("CODE", "org.greenstone.gatherer.WebGatherer");
[32127]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".
[24989]467 applet_elem.setAttribute("HEIGHT", "50");
468 applet_elem.setAttribute("WIDTH", "380");
[32127]469
[28382]470 Element gwcgi_param_elem = doc.createElement("PARAM");
[24989]471 gwcgi_param_elem.setAttribute("name", "gwcgi");
472 String library_name = GlobalProperties.getGSDL3WebAddress();
473 gwcgi_param_elem.setAttribute("value", library_name);
[14395]474 applet_elem.appendChild(gwcgi_param_elem);
475
[28382]476 Element gsdl3_param_elem = doc.createElement("PARAM");
[24989]477 gsdl3_param_elem.setAttribute("name", "gsdl3");
478 gsdl3_param_elem.setAttribute("value", "true");
[14395]479 applet_elem.appendChild(gsdl3_param_elem);
[32136]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:
[32141]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>
[32136]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");
[32141]486 link_to_jnlp.setAttribute("href", "applet/GLIappWebStart.jnlp");
[32136]487 Node anchor_text = doc.createTextNode("Java Web Start (JNLP) version");
488 link_to_jnlp.appendChild(anchor_text);
[32127]489 applet_elem.appendChild(default_text);
[32136]490 applet_elem.appendChild(link_to_jnlp);
[32127]491
[14395]492 return page_response;
493 }
[3340]494}
Note: See TracBrowser for help on using the repository browser.