[3340] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
[4695] | 3 | import java.util.HashMap;
|
---|
[4856] | 4 | import java.util.HashSet;
|
---|
| 5 | import java.util.Iterator;
|
---|
[4695] | 6 |
|
---|
[26055] | 7 | import org.apache.log4j.Logger;
|
---|
| 8 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 9 | import org.greenstone.gsdl3.util.GSConstants;
|
---|
| 10 | import org.greenstone.gsdl3.util.GSParams;
|
---|
| 11 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[27144] | 12 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
[26055] | 13 | import org.greenstone.gsdl3.util.UserContext;
|
---|
| 14 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
| 15 | import org.w3c.dom.Document;
|
---|
| 16 | import org.w3c.dom.Element;
|
---|
| 17 | import org.w3c.dom.Node;
|
---|
| 18 | import org.w3c.dom.NodeList;
|
---|
[13124] | 19 |
|
---|
[3568] | 20 | /** base class for Actions */
|
---|
[24867] | 21 | abstract public class Action
|
---|
| 22 | {
|
---|
[24889] | 23 |
|
---|
[24867] | 24 | /** the system set up variables */
|
---|
[27617] | 25 | protected HashMap<String, Object> config_params = null;
|
---|
[24867] | 26 | /** container Document to create XML Nodes */
|
---|
| 27 | protected Document doc = null;
|
---|
| 28 | /** a converter class to parse XML and create Docs */
|
---|
| 29 | protected XMLConverter converter = null;
|
---|
| 30 | /**
|
---|
| 31 | * a reference to the message router that it must talk to to get info. it
|
---|
| 32 | * may be a communicator acting as a proxy, but it doesn't care about that
|
---|
| 33 | */
|
---|
| 34 | protected ModuleInterface mr = null;
|
---|
[3340] | 35 |
|
---|
[24867] | 36 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.Action.class.getName());
|
---|
[3340] | 37 |
|
---|
[24867] | 38 | public Action()
|
---|
| 39 | {
|
---|
| 40 | this.converter = new XMLConverter();
|
---|
| 41 | this.doc = this.converter.newDOM();
|
---|
| 42 | }
|
---|
[13124] | 43 |
|
---|
[24867] | 44 | /** the config variables must be set before configure is called */
|
---|
[27617] | 45 | public void setConfigParams(HashMap<String, Object> params)
|
---|
[24867] | 46 | {
|
---|
| 47 | this.config_params = params;
|
---|
| 48 | }
|
---|
[20402] | 49 |
|
---|
[24867] | 50 | /** sets the message router */
|
---|
| 51 | public void setMessageRouter(ModuleInterface m)
|
---|
| 52 | {
|
---|
| 53 | this.mr = m;
|
---|
| 54 | }
|
---|
[3893] | 55 |
|
---|
[24867] | 56 | public boolean configure()
|
---|
| 57 | {
|
---|
| 58 | // does nothing yet
|
---|
| 59 | return true;
|
---|
| 60 | }
|
---|
[4980] | 61 |
|
---|
[24867] | 62 | /**
|
---|
| 63 | * process takes an xml representation of cgi args and returns the page of
|
---|
| 64 | * results - may be in html/xml/other depending on the output att of the
|
---|
| 65 | * request
|
---|
| 66 | */
|
---|
| 67 | public String process(String xml_in)
|
---|
| 68 | {
|
---|
[6248] | 69 |
|
---|
[24867] | 70 | Document message_doc = this.converter.getDOM(xml_in);
|
---|
| 71 | if (message_doc == null)
|
---|
| 72 | {
|
---|
| 73 | logger.error("Couldn't parse request");
|
---|
| 74 | logger.error(xml_in);
|
---|
| 75 | return null;
|
---|
| 76 | }
|
---|
| 77 | Node result = process(message_doc);
|
---|
| 78 | return this.converter.getString(result);
|
---|
[24219] | 79 | }
|
---|
[24087] | 80 |
|
---|
[24867] | 81 | /** the main process method - must be implemented in subclass */
|
---|
| 82 | abstract public Node process(Node xml_in);
|
---|
[24087] | 83 |
|
---|
[24867] | 84 | /**
|
---|
| 85 | * tell the param class what its arguments are if an action has its own
|
---|
| 86 | * arguments, this should add them to the params object - particularly
|
---|
| 87 | * important for args that should not be saved
|
---|
| 88 | */
|
---|
[25302] | 89 | public boolean addActionParameters(GSParams params)
|
---|
[24867] | 90 | {
|
---|
| 91 | return true;
|
---|
| 92 | }
|
---|
[24087] | 93 |
|
---|
[25635] | 94 | protected void getRequiredMetadataNames(Element format, HashSet<String> meta_names)
|
---|
[24889] | 95 | {
|
---|
| 96 | extractMetadataNames(format, meta_names);
|
---|
| 97 | addLinkMetadataNames(format, meta_names);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[26087] | 100 | // should change to metadataList?? and use attributes for select rather than
|
---|
| 101 | // prepending parent_ etc
|
---|
| 102 | protected void extractMetadataNames(Element format, HashSet<String> meta_names)
|
---|
[24867] | 103 | {
|
---|
[26491] | 104 |
|
---|
| 105 | NodeList metadata_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "metadata"); // gsf:metadata
|
---|
[26087] | 106 | for (int i = 0; i < metadata_nodes.getLength(); i++)
|
---|
| 107 | {
|
---|
| 108 | Element elem = (Element) metadata_nodes.item(i);
|
---|
| 109 | String name = elem.getAttribute("name");
|
---|
| 110 | String select = elem.getAttribute("select");
|
---|
[26031] | 111 |
|
---|
[26087] | 112 | if (!select.equals("")) {
|
---|
| 113 | name = select+GSConstants.META_RELATION_SEP+name;
|
---|
| 114 | }
|
---|
| 115 | meta_names.add(name);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[26491] | 118 | NodeList foreach_metadata_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "foreach-metadata"); // gsf:foreach-metadata
|
---|
| 119 | for (int i = 0; i < foreach_metadata_nodes.getLength(); i++)
|
---|
| 120 | {
|
---|
| 121 | Element elem = (Element) foreach_metadata_nodes.item(i);
|
---|
| 122 | String name = elem.getAttribute("name");
|
---|
| 123 | String select = elem.getAttribute("select");
|
---|
| 124 |
|
---|
| 125 | if (!select.equals("")) {
|
---|
| 126 | name = select+GSConstants.META_RELATION_SEP+name;
|
---|
| 127 | }
|
---|
| 128 | meta_names.add(name);
|
---|
| 129 | }
|
---|
[24889] | 130 | }
|
---|
[4856] | 131 |
|
---|
[25635] | 132 | protected void addLinkMetadataNames(Element format, HashSet<String> meta_names)
|
---|
[24889] | 133 | {
|
---|
[24867] | 134 | // The XSL tranform for
|
---|
| 135 | // gsf:link type="source"
|
---|
| 136 | // makes use of 'assocfilepath' so need to make sure it's asked for
|
---|
[4856] | 137 |
|
---|
[26038] | 138 | boolean getEquivLinkMeta = false;
|
---|
| 139 |
|
---|
[26055] | 140 | NodeList link_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "link");
|
---|
[24867] | 141 | for (int i = 0; i < link_nodes.getLength(); i++)
|
---|
| 142 | {
|
---|
| 143 | Element elem = (Element) link_nodes.item(i);
|
---|
| 144 | String type = elem.getAttribute("type");
|
---|
| 145 | if (type.equals("source"))
|
---|
| 146 | {
|
---|
| 147 | meta_names.add("assocfilepath");
|
---|
| 148 | meta_names.add("srclinkFile");
|
---|
| 149 | }
|
---|
[26055] | 150 | else if (type.equals("web"))
|
---|
| 151 | {
|
---|
| 152 | meta_names.add("weblink");
|
---|
[26038] | 153 | meta_names.add("webicon");
|
---|
| 154 | meta_names.add("/weblink");
|
---|
| 155 | }
|
---|
[26055] | 156 | else if (type.equals("equivdoc"))
|
---|
| 157 | {
|
---|
[26038] | 158 | getEquivLinkMeta = true;
|
---|
| 159 | }
|
---|
[24867] | 160 | }
|
---|
[19984] | 161 |
|
---|
[24867] | 162 | // get all the metadata necessary for when the user has used "gsf:equivlink"
|
---|
| 163 | // so that we can build up the equivlink from the metadata components it needs
|
---|
[26055] | 164 | link_nodes = format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "equivlinkgs3");
|
---|
[26038] | 165 | if (getEquivLinkMeta || link_nodes.getLength() > 0)
|
---|
[24867] | 166 | {
|
---|
| 167 | String[] equivlink_metanames = { "equivDocIcon", "equivDocLink", "/equivDocLink" };
|
---|
[3340] | 168 |
|
---|
[24867] | 169 | for (int i = 0; i < equivlink_metanames.length; i++)
|
---|
| 170 | {
|
---|
| 171 | StringBuffer metadata = new StringBuffer();
|
---|
| 172 | metadata.append("all"); // this means the attr multiple = true;
|
---|
| 173 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
[3935] | 174 |
|
---|
[24867] | 175 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
| 176 | metadata.append(','); // attr separator = ","
|
---|
| 177 | metadata.append(GSConstants.META_SEPARATOR_SEP);
|
---|
| 178 | metadata.append(GSConstants.META_RELATION_SEP);
|
---|
[3935] | 179 |
|
---|
[24867] | 180 | // the name of the metadata we're retrieving
|
---|
| 181 | metadata.append(equivlink_metanames[i]);
|
---|
| 182 | meta_names.add(metadata.toString());
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
[26026] | 185 |
|
---|
[26055] | 186 | if (format.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "image").getLength() > 0)
|
---|
[24889] | 187 | {
|
---|
[26026] | 188 | meta_names.add("Thumb");
|
---|
| 189 | meta_names.add("Screen");
|
---|
| 190 | meta_names.add("SourceFile");
|
---|
[24889] | 191 | }
|
---|
[24867] | 192 | }
|
---|
[3935] | 193 |
|
---|
[25635] | 194 | protected Element createMetadataParamList(HashSet<String> metadata_names)
|
---|
[24867] | 195 | {
|
---|
| 196 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 197 |
|
---|
| 198 | Element param = null;
|
---|
[25635] | 199 | Iterator<String> i = metadata_names.iterator();
|
---|
[24867] | 200 | while (i.hasNext())
|
---|
| 201 | {
|
---|
[25635] | 202 | String name = i.next();
|
---|
[24867] | 203 | param = this.doc.createElement(GSXML.PARAM_ELEM);
|
---|
| 204 | param_list.appendChild(param);
|
---|
| 205 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
| 206 | param.setAttribute(GSXML.VALUE_ATT, name);
|
---|
| 207 |
|
---|
| 208 | }
|
---|
| 209 | return param_list;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | protected boolean processErrorElements(Element message, Element page)
|
---|
| 213 | {
|
---|
| 214 | NodeList error_nodes = message.getElementsByTagName(GSXML.ERROR_ELEM);
|
---|
| 215 | if (error_nodes.getLength() == 0)
|
---|
| 216 | {
|
---|
| 217 | return false;
|
---|
| 218 | }
|
---|
| 219 | Document owner = page.getOwnerDocument();
|
---|
| 220 | for (int i = 0; i < error_nodes.getLength(); i++)
|
---|
| 221 | {
|
---|
| 222 | page.appendChild(owner.importNode(error_nodes.item(i), true));
|
---|
| 223 | }
|
---|
| 224 | return true;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
| 228 | * Takes an XML element and adds the metadata of the current site to it.
|
---|
| 229 | * Useful for adding the current site's metadata to a response before
|
---|
| 230 | * sending it
|
---|
| 231 | *
|
---|
| 232 | * @param element
|
---|
| 233 | * the element to add site metadata to
|
---|
| 234 | * @param lang
|
---|
| 235 | * the current language
|
---|
| 236 | * @param uid
|
---|
| 237 | * the current user id
|
---|
| 238 | */
|
---|
[24993] | 239 | protected void addSiteMetadata(Element element, UserContext userContext)
|
---|
[24867] | 240 | {
|
---|
| 241 | //ADD SITE METADATA
|
---|
[24993] | 242 | Element metadata_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", userContext);
|
---|
[24867] | 243 | //create a hashmap of params
|
---|
| 244 | HashMap subset_params = new HashMap(1);
|
---|
| 245 | subset_params.put(GSXML.SUBSET_PARAM, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 246 | //create the element to put the params in
|
---|
| 247 | Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 248 | //put them in
|
---|
| 249 | GSXML.addParametersToList(this.doc, param_list, subset_params);
|
---|
| 250 | metadata_request.appendChild(param_list);
|
---|
| 251 | //create the message
|
---|
| 252 | Element metadata_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 253 | metadata_message.appendChild(metadata_request);
|
---|
| 254 | //get response
|
---|
| 255 | Element metadata_response_message = (Element) this.mr.process(metadata_message);
|
---|
| 256 | //drill down to response
|
---|
| 257 | Element metadata_response = (Element) GSXML.getChildByTagName(metadata_response_message, GSXML.RESPONSE_ELEM);
|
---|
| 258 | //merge in metadata
|
---|
| 259 | GSXML.mergeMetadataLists(element, metadata_response);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[25128] | 262 | protected void addInterfaceOptions(Element elem)
|
---|
| 263 | {
|
---|
| 264 | Element documentOptionList = this.doc.createElement("interfaceOptions");
|
---|
[26026] | 265 | for (Object key : this.config_params.keySet())
|
---|
[25128] | 266 | {
|
---|
| 267 | Element option = this.doc.createElement("option");
|
---|
[26026] | 268 | option.setAttribute(GSXML.NAME_ATT, (String) key);
|
---|
[25128] | 269 | option.setAttribute(GSXML.VALUE_ATT, this.config_params.get(key).toString());
|
---|
| 270 | documentOptionList.appendChild(option);
|
---|
| 271 | }
|
---|
| 272 | elem.appendChild(elem.getOwnerDocument().importNode(documentOptionList, true));
|
---|
| 273 | }
|
---|
[27144] | 274 |
|
---|
| 275 | protected Element getFormatInfo(String to, UserContext userContext) {
|
---|
| 276 | Element mr_format_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 277 | Element mr_format_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_FORMAT, to, userContext);
|
---|
| 278 | mr_format_message.appendChild(mr_format_request);
|
---|
| 279 |
|
---|
| 280 | // process the message
|
---|
| 281 | Element mr_response_message = (Element) this.mr.process(mr_format_message);
|
---|
| 282 | // the response
|
---|
| 283 |
|
---|
| 284 | Element format_response = (Element) GSXML.getChildByTagName(mr_response_message, GSXML.RESPONSE_ELEM);
|
---|
| 285 |
|
---|
| 286 | Element format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.FORMAT_ELEM);
|
---|
| 287 | if (format_elem!= null) {
|
---|
| 288 | Element global_format_elem = (Element) GSXML.getChildByTagName(format_response, GSXML.GLOBAL_FORMAT_ELEM);
|
---|
| 289 | if (global_format_elem != null)
|
---|
| 290 | {
|
---|
| 291 | GSXSLT.mergeFormatElements(format_elem, global_format_elem, false);
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 | return format_elem;
|
---|
| 295 | }
|
---|
[24867] | 296 | }
|
---|
[27144] | 297 |
|
---|
| 298 |
|
---|
| 299 |
|
---|