[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
[27617] | 3 | import java.io.Serializable;
|
---|
| 4 | import java.util.HashMap;
|
---|
| 5 |
|
---|
| 6 | import org.apache.log4j.Logger;
|
---|
| 7 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 8 | import org.greenstone.gsdl3.util.GSPath;
|
---|
| 9 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 10 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[28964] | 11 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[22085] | 12 | import org.greenstone.util.GlobalProperties;
|
---|
[28382] | 13 | import org.w3c.dom.Document;
|
---|
[27617] | 14 | import org.w3c.dom.Element;
|
---|
[24989] | 15 | import org.w3c.dom.Node;
|
---|
[19984] | 16 | import org.w3c.dom.NodeList;
|
---|
[3340] | 17 |
|
---|
[24989] | 18 | public 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)
|
---|
| 28 | {
|
---|
[28964] | 29 | Element message = GSXML.nodeToElement(message_node);
|
---|
| 30 | Document doc = XMLConverter.newDOM();
|
---|
[28382] | 31 |
|
---|
[25991] | 32 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
| 33 | Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 34 | String collection = "";
|
---|
[27617] | 35 | if (paramList != null)
|
---|
[25991] | 36 | {
|
---|
| 37 | HashMap<String, Serializable> params = GSXML.extractParams(paramList, false);
|
---|
| 38 | if (params != null && params.get(GSParams.COLLECTION) != null)
|
---|
| 39 | {
|
---|
| 40 | collection = (String) params.get(GSParams.COLLECTION);
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
[16688] | 43 |
|
---|
[24989] | 44 | // the page name is the subaction
|
---|
| 45 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
| 46 | if (page_name.equals(""))
|
---|
| 47 | { // if no page specified, assume home page
|
---|
| 48 | page_name = HOME_PAGE;
|
---|
| 49 | }
|
---|
[28382] | 50 | Element result = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24989] | 51 | Element response;
|
---|
| 52 | if (page_name.equals(HOME_PAGE))
|
---|
| 53 | {
|
---|
[28855] | 54 | response = homePage(request);
|
---|
[24989] | 55 | //} else if (page_name.equals(ABOUT_PAGE)) {
|
---|
| 56 | }
|
---|
| 57 | else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE))
|
---|
| 58 | {
|
---|
[28855] | 59 | response = aboutPage(request);
|
---|
[24989] | 60 | //}else if (page_name.equals(PREFS_PAGE)) {
|
---|
| 61 | //response = prefsPage(request);
|
---|
| 62 | }
|
---|
| 63 | else if (page_name.equals(GLI4GS3_PAGE))
|
---|
| 64 | {
|
---|
[28855] | 65 | response = gli4gs3Page(request);
|
---|
[24989] | 66 | }
|
---|
| 67 | else
|
---|
| 68 | { // unknown page
|
---|
[28855] | 69 | response = unknownPage(request);
|
---|
[24989] | 70 | }
|
---|
| 71 |
|
---|
[28382] | 72 | Element formatMessage = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 73 | Element formatRequest = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_FORMAT, collection, new UserContext(request));
|
---|
[25991] | 74 | formatMessage.appendChild(formatRequest);
|
---|
| 75 | Element formatResponseMessage = (Element) this.mr.process(formatMessage);
|
---|
| 76 | Element formatResponse = (Element) GSXML.getChildByTagName(formatResponseMessage, GSXML.RESPONSE_ELEM);
|
---|
| 77 |
|
---|
| 78 | Element globalFormat = (Element) GSXML.getChildByTagName(formatResponse, GSXML.FORMAT_ELEM);
|
---|
| 79 | if (globalFormat != null)
|
---|
| 80 | {
|
---|
[28855] | 81 | response.appendChild(response.getOwnerDocument().importNode(globalFormat, true));
|
---|
[25991] | 82 | }
|
---|
| 83 |
|
---|
[28382] | 84 | result.appendChild(doc.importNode(response, true));
|
---|
[24989] | 85 | logger.debug("page action result: " + this.converter.getPrettyString(result));
|
---|
[27617] | 86 |
|
---|
[24989] | 87 | return result;
|
---|
[11230] | 88 | }
|
---|
[3568] | 89 |
|
---|
[28855] | 90 | protected Element homePage(Element request)
|
---|
[24989] | 91 | {
|
---|
[28964] | 92 | Document doc = XMLConverter.newDOM();
|
---|
[28382] | 93 |
|
---|
[24993] | 94 | UserContext userContext = new UserContext(request);
|
---|
[14395] | 95 | // first, get the message router info
|
---|
[28382] | 96 | Element info_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 97 | Element coll_list_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
|
---|
[14395] | 98 | info_message.appendChild(coll_list_request);
|
---|
[24989] | 99 | Element info_response_message = (Element) this.mr.process(info_message);
|
---|
| 100 | if (info_response_message == null)
|
---|
| 101 | {
|
---|
[14395] | 102 | logger.error(" couldn't query the message router!");
|
---|
| 103 | return null;
|
---|
| 104 | }
|
---|
[24989] | 105 | Element info_response = (Element) GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
|
---|
| 106 | if (info_response == null)
|
---|
| 107 | {
|
---|
[14395] | 108 | logger.error("couldn't query the message router!");
|
---|
| 109 | return null;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | // second, get the metadata for each collection - we only want specific
|
---|
| 113 | // elements but for now, we'll just get it all
|
---|
[24989] | 114 | Element collection_list = (Element) GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25651] | 115 | logger.debug(GSXML.xmlNodeToString(collection_list));
|
---|
[24989] | 116 | if (collection_list != null)
|
---|
| 117 | {
|
---|
[14395] | 118 | NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM);
|
---|
[24989] | 119 | if (colls.getLength() > 0)
|
---|
| 120 | {
|
---|
[28382] | 121 | sendMultipleRequests(doc, colls, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
|
---|
[14395] | 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | // get metadata for any services
|
---|
[24989] | 126 | Element service_list = (Element) GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 127 | if (service_list != null)
|
---|
| 128 | {
|
---|
[14395] | 129 | NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM);
|
---|
[24989] | 130 | if (services.getLength() > 0)
|
---|
| 131 | {
|
---|
[28382] | 132 | sendMultipleRequests(doc, services, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
|
---|
[14395] | 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | // get metadata for service clusters
|
---|
[24989] | 137 | Element cluster_list = (Element) GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 138 | if (cluster_list != null)
|
---|
| 139 | {
|
---|
[14395] | 140 | NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM);
|
---|
[24989] | 141 | if (clusters.getLength() > 0)
|
---|
| 142 | {
|
---|
[28382] | 143 | sendMultipleRequests(doc, clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
|
---|
[14395] | 144 |
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[27143] | 148 | addSiteMetadata(info_response, userContext);
|
---|
| 149 | addInterfaceOptions(info_response);
|
---|
[14395] | 150 | // all the components have been merged into info_response
|
---|
| 151 | return info_response;
|
---|
| 152 |
|
---|
| 153 | } // homePage
|
---|
| 154 |
|
---|
[24989] | 155 | protected Element aboutPage(Element request)
|
---|
| 156 | {
|
---|
[28964] | 157 | Document doc = XMLConverter.newDOM();
|
---|
[28382] | 158 |
|
---|
[24993] | 159 | UserContext userContext = new UserContext(request);
|
---|
[14395] | 160 | // extract the params from the cgi-request,
|
---|
[24989] | 161 | Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 162 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
|
---|
[24989] | 163 |
|
---|
| 164 | String coll_name = (String) params.get(GSParams.COLLECTION);
|
---|
| 165 | if (coll_name == null || coll_name.equals(""))
|
---|
| 166 | {
|
---|
[14395] | 167 | logger.error("about page requested with no collection or cluster specified!");
|
---|
| 168 | // return an empty response
|
---|
[28382] | 169 | Element response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24993] | 170 | addSiteMetadata(response, userContext);
|
---|
[25128] | 171 | addInterfaceOptions(response);
|
---|
[19984] | 172 | return response;
|
---|
[14395] | 173 | }
|
---|
[3340] | 174 |
|
---|
[14395] | 175 | // get the collection or cluster description
|
---|
[28382] | 176 | Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[3340] | 177 |
|
---|
[28382] | 178 | Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
|
---|
[14395] | 179 | coll_about_message.appendChild(coll_about_request);
|
---|
[24989] | 180 | Element coll_about_response = (Element) this.mr.process(coll_about_message);
|
---|
[14395] | 181 |
|
---|
[14642] | 182 | // add collection type attribute to paramList
|
---|
| 183 | String col_type = "";
|
---|
[24989] | 184 | NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
| 185 | if (collect_elem.getLength() != 0)
|
---|
| 186 | {
|
---|
| 187 | for (int i = 0; i < collect_elem.getLength(); i++)
|
---|
| 188 | {
|
---|
| 189 | Element e = (Element) collect_elem.item(i);
|
---|
| 190 | col_type = e.getAttribute(GSXML.TYPE_ATT);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | else
|
---|
| 194 | {
|
---|
[14642] | 195 | logger.error(GSXML.COLLECTION_ELEM + " element is null");
|
---|
| 196 | }
|
---|
[24989] | 197 |
|
---|
| 198 | NodeList paramList = request.getElementsByTagName(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 199 | if (paramList.getLength() != 0)
|
---|
| 200 | {
|
---|
| 201 | for (int i = 0; i < paramList.getLength(); i++)
|
---|
| 202 | {
|
---|
| 203 | Element e = (Element) paramList.item(i);
|
---|
| 204 | Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1");
|
---|
| 205 | e.appendChild(ct);
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | else
|
---|
| 209 | {
|
---|
[14642] | 210 | logger.info("paramList is null!!");
|
---|
[24989] | 211 | }
|
---|
| 212 |
|
---|
| 213 | if (coll_about_response == null)
|
---|
| 214 | {
|
---|
[14395] | 215 | return null;
|
---|
| 216 | }
|
---|
[19984] | 217 |
|
---|
[14395] | 218 | // second, get the info for each service - we only want display items
|
---|
| 219 | // but for now, we'll just get it all
|
---|
| 220 | NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
[24989] | 221 | if (services.getLength() > 0)
|
---|
| 222 | {
|
---|
[28382] | 223 | sendMultipleRequests(doc, services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, userContext);
|
---|
[14395] | 224 | }
|
---|
| 225 |
|
---|
| 226 | Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
|
---|
[19984] | 227 | //add the site metadata
|
---|
[24993] | 228 | addSiteMetadata(response, userContext);
|
---|
[25128] | 229 | addInterfaceOptions(response);
|
---|
[14395] | 230 | return response;
|
---|
[3467] | 231 | }
|
---|
[24989] | 232 |
|
---|
[14395] | 233 | //protected Element prefsPage(Element request) {
|
---|
[24989] | 234 |
|
---|
[14395] | 235 | // return null;
|
---|
| 236 | //}
|
---|
[24989] | 237 |
|
---|
[14395] | 238 | /** if we dont know the page type, use this method */
|
---|
[24989] | 239 | protected Element unknownPage(Element request)
|
---|
| 240 | {
|
---|
[28964] | 241 | Document doc = XMLConverter.newDOM();
|
---|
[28382] | 242 |
|
---|
[24993] | 243 | UserContext userContext = new UserContext(request);
|
---|
[14395] | 244 | String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
[4155] | 245 |
|
---|
[14395] | 246 | // extract the params from the cgi-request,
|
---|
[24989] | 247 | Element cgi_paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 248 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_paramList, false);
|
---|
[4950] | 249 |
|
---|
[24989] | 250 | String coll_name = (String) params.get(GSParams.COLLECTION);
|
---|
| 251 | if (coll_name == null || coll_name.equals(""))
|
---|
| 252 | {
|
---|
[14395] | 253 | // just return an empty response
|
---|
[28382] | 254 | Element response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24993] | 255 | addSiteMetadata(response, userContext);
|
---|
[25128] | 256 | addInterfaceOptions(response);
|
---|
[19984] | 257 | return response;
|
---|
[14395] | 258 | }
|
---|
[5147] | 259 |
|
---|
[14395] | 260 | // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
|
---|
[4155] | 261 |
|
---|
[14395] | 262 | // if there is a service specified should we get the service description instead??
|
---|
| 263 | // get the collection or cluster description
|
---|
[28382] | 264 | Element coll_about_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[14395] | 265 |
|
---|
[28382] | 266 | Element coll_about_request = GSXML.createBasicRequest(doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
|
---|
[14395] | 267 | coll_about_message.appendChild(coll_about_request);
|
---|
| 268 |
|
---|
[24989] | 269 | Element coll_about_response = (Element) this.mr.process(coll_about_message);
|
---|
| 270 |
|
---|
[14395] | 271 | Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
|
---|
[19984] | 272 |
|
---|
| 273 | //add the site metadata
|
---|
[24993] | 274 | addSiteMetadata(response, userContext);
|
---|
[25128] | 275 | addInterfaceOptions(response);
|
---|
[19984] | 276 |
|
---|
[14395] | 277 | return response;
|
---|
| 278 |
|
---|
[4257] | 279 | }
|
---|
[4155] | 280 |
|
---|
[28382] | 281 | protected boolean sendMultipleRequests(Document doc, NodeList items, String path_prefix, String request_type, UserContext userContext)
|
---|
[24989] | 282 | {
|
---|
[14395] | 283 | // we will send all the requests in a single message
|
---|
[28382] | 284 | Element message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24989] | 285 | for (int i = 0; i < items.getLength(); i++)
|
---|
| 286 | {
|
---|
| 287 | Element c = (Element) items.item(i);
|
---|
| 288 | String path = c.getAttribute(GSXML.NAME_ATT);
|
---|
| 289 | if (path_prefix != null)
|
---|
| 290 | {
|
---|
[14395] | 291 | path = GSPath.appendLink(path_prefix, path);
|
---|
| 292 | }
|
---|
[28382] | 293 | Element request = GSXML.createBasicRequest(doc, request_type, path, userContext);
|
---|
[14395] | 294 | message.appendChild(request);
|
---|
| 295 | }
|
---|
[11230] | 296 |
|
---|
[24989] | 297 | Element response_message = (Element) this.mr.process(message);
|
---|
| 298 |
|
---|
[14395] | 299 | NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
| 300 | // check that have same number of responses as requests
|
---|
[24989] | 301 | if (items.getLength() != responses.getLength())
|
---|
| 302 | {
|
---|
[14395] | 303 | logger.error("didn't get a response for each request - somethings gone wrong!");
|
---|
| 304 | return false;
|
---|
[24989] | 305 | }
|
---|
[14395] | 306 |
|
---|
[24989] | 307 | for (int i = 0; i < items.getLength(); i++)
|
---|
| 308 | {
|
---|
| 309 | Element c1 = (Element) items.item(i);
|
---|
| 310 | Element c2 = (Element) GSXML.getChildByTagName((Element) responses.item(i), c1.getTagName());
|
---|
| 311 | if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT)))
|
---|
| 312 | {
|
---|
[14395] | 313 | //add the new data into the original element
|
---|
| 314 | GSXML.mergeElements(c1, c2);
|
---|
[24989] | 315 | }
|
---|
| 316 | else
|
---|
| 317 | {
|
---|
[19641] | 318 | logger.debug(" response does not correspond to request!");
|
---|
[24989] | 319 | }
|
---|
| 320 |
|
---|
[14395] | 321 | }
|
---|
| 322 |
|
---|
| 323 | return true;
|
---|
| 324 |
|
---|
[11230] | 325 | }
|
---|
[14311] | 326 |
|
---|
[24989] | 327 | protected Element gli4gs3Page(Element request)
|
---|
| 328 | {
|
---|
[28964] | 329 | Document doc = XMLConverter.newDOM();
|
---|
[28382] | 330 |
|
---|
[14395] | 331 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 332 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
[14311] | 333 |
|
---|
[28382] | 334 | Element page_response = doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[14311] | 335 |
|
---|
[28382] | 336 | Element applet_elem = doc.createElement("Applet");
|
---|
[14395] | 337 | page_response.appendChild(applet_elem);
|
---|
[24989] | 338 | applet_elem.setAttribute("ARCHIVE", "SignedGatherer.jar");
|
---|
| 339 | applet_elem.setAttribute("CODE", "org.greenstone.gatherer.GathererApplet");
|
---|
| 340 | applet_elem.setAttribute("CODEBASE", "applet");
|
---|
| 341 | applet_elem.setAttribute("HEIGHT", "50");
|
---|
| 342 | applet_elem.setAttribute("WIDTH", "380");
|
---|
[28382] | 343 | Element gwcgi_param_elem = doc.createElement("PARAM");
|
---|
[24989] | 344 | gwcgi_param_elem.setAttribute("name", "gwcgi");
|
---|
| 345 | String library_name = GlobalProperties.getGSDL3WebAddress();
|
---|
| 346 | gwcgi_param_elem.setAttribute("value", library_name);
|
---|
[14395] | 347 | applet_elem.appendChild(gwcgi_param_elem);
|
---|
| 348 |
|
---|
[28382] | 349 | Element gsdl3_param_elem = doc.createElement("PARAM");
|
---|
[24989] | 350 | gsdl3_param_elem.setAttribute("name", "gsdl3");
|
---|
| 351 | gsdl3_param_elem.setAttribute("value", "true");
|
---|
[14395] | 352 | applet_elem.appendChild(gsdl3_param_elem);
|
---|
| 353 |
|
---|
| 354 | return page_response;
|
---|
| 355 | }
|
---|
[3340] | 356 | }
|
---|