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

Last change on this file since 30836 was 30836, checked in by kjdon, 8 years ago

along with site metadata we now need to get site displayItems

  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 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 = aboutPage(request);
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 // Send message to groupInfoType Services
163 } else {
164 // If no service with type SERVICE_TYPE_GROUPINFO could be provided
165 // request message router for all available collections
166 GSXML.addParameterToList(param_list_element, GSXML.SUBSET_PARAM,
167 GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
168 info_response_message = (Element) this.mr.process(info_message);
169
170 if (info_response_message == null) {
171 logger.error(" couldn't query the message router!");
172 return null;
173 }
174 info_response = (Element) GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
175 if (info_response == null) {
176 logger.error("couldn't query the message router!");
177 return null;
178 }
179 }
180
181 // second, get the metadata for each collection - we only want specific
182 // elements but for now, we'll just get it all
183 Element collection_list = (Element) GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
184 //logger.debug(GSXML.xmlNodeToString(collection_list));
185 if (collection_list != null)
186 {
187 NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM);
188 if (colls.getLength() > 0)
189 {
190 sendMultipleRequests(doc, colls, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
191 }
192 }
193
194 // get metadata for any services
195 Element service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
196 if (service_list != null)
197 {
198 NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM);
199 if (services.getLength() > 0)
200 {
201 sendMultipleRequests(doc, services, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
202 }
203 }
204
205 // get metadata for service clusters
206 Element cluster_list = (Element) GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
207 if (cluster_list != null)
208 {
209 NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM);
210 if (clusters.getLength() > 0)
211 {
212 sendMultipleRequests(doc, clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
213
214 }
215 }
216
217 addSiteMetadata(info_response, userContext);
218 addInterfaceOptions(info_response);
219 // all the components have been merged into info_response
220 return info_response;
221
222 } // homePage
223
224 protected Element aboutPage(Element request)
225 {
226 Document doc = XMLConverter.newDOM();
227
228 UserContext userContext = new UserContext(request);
229 // extract the params from the cgi-request,
230 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
231 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
232
233 String coll_name = (String) params.get(GSParams.COLLECTION);
234 if (coll_name == null || coll_name.equals(""))
235 {
236 logger.error("about page requested with no collection or cluster specified!");
237 // return an empty response
238 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
239 addSiteMetadata(response, userContext);
240 addInterfaceOptions(response);
241 return response;
242 }
243
244 // get the collection or cluster description
245 Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
246
247 Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
248 coll_about_message.appendChild(coll_about_request);
249 Element coll_about_response = (Element) this.mr.process(coll_about_message);
250
251 // add collection type attribute to paramList
252 String col_type = "";
253 NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
254 if (collect_elem.getLength() != 0)
255 {
256 for (int i = 0; i < collect_elem.getLength(); i++)
257 {
258 Element e = (Element) collect_elem.item(i);
259 col_type = e.getAttribute(GSXML.TYPE_ATT);
260 }
261 }
262 else
263 {
264 logger.error(GSXML.COLLECTION_ELEM + " element is null");
265 }
266
267 NodeList paramList = request.getElementsByTagName(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
268 if (paramList.getLength() != 0)
269 {
270 for (int i = 0; i < paramList.getLength(); i++)
271 {
272 Element e = (Element) paramList.item(i);
273 Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1");
274 e.appendChild(ct);
275 }
276 }
277 else
278 {
279 logger.info("paramList is null!!");
280 }
281
282 if (coll_about_response == null)
283 {
284 return null;
285 }
286
287 // second, get the info for each service - we only want display items
288 // but for now, we'll just get it all
289 NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM);
290 if (services.getLength() > 0)
291 {
292 sendMultipleRequests(doc, services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
293 }
294
295 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
296 //add the site metadata
297 addSiteMetadata(response, userContext);
298 addInterfaceOptions(response);
299 return response;
300 }
301
302 //protected Element prefsPage(Element request) {
303
304 // return null;
305 //}
306
307 /** if we dont know the page type, use this method */
308 protected Element unknownPage(Element request)
309 {
310 Document doc = XMLConverter.newDOM();
311
312 UserContext userContext = new UserContext(request);
313 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
314
315 // extract the params from the cgi-request,
316 Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
317 HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
318
319 String coll_name = (String) params.get(GSParams.COLLECTION);
320 if (coll_name == null || coll_name.equals(""))
321 {
322 // just return an empty response
323 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
324 addSiteMetadata(response, userContext);
325 addInterfaceOptions(response);
326 return response;
327 }
328
329 // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
330
331 // if there is a service specified should we get the service description instead??
332 // get the collection or cluster description
333 Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
334
335 Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
336 coll_about_message.appendChild(coll_about_request);
337
338 Element coll_about_response = (Element) this.mr.process(coll_about_message);
339
340 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
341
342 //add the site metadata
343 addSiteMetadata(response, userContext);
344 addInterfaceOptions(response);
345
346 return response;
347
348 }
349
350 protected boolean sendMultipleRequests(Document doc, NodeList items, String path_prefix, String request_type, UserContext userContext)
351 {
352 // we will send all the requests in a single message
353 Element message = doc.createElement(GSXML.MESSAGE_ELEM);
354 for (int i = 0; i < items.getLength(); i++)
355 {
356 Element c = (Element) items.item(i);
357 String path = c.getAttribute(GSXML.NAME_ATT);
358 if (path_prefix != null)
359 {
360 path = GSPath.appendLink(path_prefix, path);
361 }
362 Element request = GSXML.createBasicRequest(doc, request_type, path, userContext);
363 message.appendChild(request);
364 }
365
366 Element response_message = (Element) this.mr.process(message);
367
368 NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM);
369 // check that have same number of responses as requests
370 if (items.getLength() != responses.getLength())
371 {
372 logger.error("didn't get a response for each request - somethings gone wrong!");
373 return false;
374 }
375
376 for (int i = 0; i < items.getLength(); i++)
377 {
378 Element c1 = (Element) items.item(i);
379 Element c2 = (Element) GSXML.getChildByTagName((Element) responses.item(i), c1.getTagName());
380 if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT)))
381 {
382 //add the new data into the original element
383 GSXML.mergeElements(c1, c2);
384 }
385 else
386 {
387 logger.debug(" response does not correspond to request!");
388 }
389
390 }
391
392 return true;
393
394 }
395
396 protected Element gli4gs3Page(Element request)
397 {
398 Document doc = XMLConverter.newDOM();
399
400 String lang = request.getAttribute(GSXML.LANG_ATT);
401 String uid = request.getAttribute(GSXML.USER_ID_ATT);
402
403 Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
404
405 Element applet_elem = doc.createElement("Applet");
406 page_response.appendChild(applet_elem);
407 applet_elem.setAttribute("ARCHIVE", "SignedGatherer.jar");
408 applet_elem.setAttribute("CODE", "org.greenstone.gatherer.GathererApplet");
409 applet_elem.setAttribute("CODEBASE", "applet");
410 applet_elem.setAttribute("HEIGHT", "50");
411 applet_elem.setAttribute("WIDTH", "380");
412 Element gwcgi_param_elem = doc.createElement("PARAM");
413 gwcgi_param_elem.setAttribute("name", "gwcgi");
414 String library_name = GlobalProperties.getGSDL3WebAddress();
415 gwcgi_param_elem.setAttribute("value", library_name);
416 applet_elem.appendChild(gwcgi_param_elem);
417
418 Element gsdl3_param_elem = doc.createElement("PARAM");
419 gsdl3_param_elem.setAttribute("name", "gsdl3");
420 gsdl3_param_elem.setAttribute("value", "true");
421 applet_elem.appendChild(gsdl3_param_elem);
422
423 return page_response;
424 }
425}
Note: See TracBrowser for help on using the repository browser.