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