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

Last change on this file since 30540 was 30540, checked in by Georgiy Litvinov, 8 years ago

Added collection groups. Config file in web/sites/localsite/groupConfig.xml
PageAction now send request to CollectionGroups service to retrieve information about collections and groups.

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