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