[26493] | 1 | package org.greenstone.gsdl3.service;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileWriter;
|
---|
| 5 | import java.io.Serializable;
|
---|
| 6 | import java.util.HashMap;
|
---|
| 7 |
|
---|
| 8 | import javax.xml.transform.Transformer;
|
---|
| 9 | import javax.xml.transform.TransformerFactory;
|
---|
| 10 | import javax.xml.transform.dom.DOMSource;
|
---|
| 11 | import javax.xml.transform.stream.StreamResult;
|
---|
| 12 |
|
---|
| 13 | import org.apache.log4j.Logger;
|
---|
| 14 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 15 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[27023] | 16 | import org.greenstone.util.GlobalProperties;
|
---|
[26493] | 17 | import org.w3c.dom.Document;
|
---|
| 18 | import org.w3c.dom.Element;
|
---|
[26561] | 19 | import org.w3c.dom.NamedNodeMap;
|
---|
| 20 | import org.w3c.dom.Node;
|
---|
[26493] | 21 | import org.w3c.dom.NodeList;
|
---|
| 22 |
|
---|
| 23 | public class DebugService extends ServiceRack
|
---|
| 24 | {
|
---|
| 25 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DebugService.class.getName());
|
---|
| 26 |
|
---|
| 27 | /**********************************************************
|
---|
| 28 | * The list of services the utility service rack supports *
|
---|
| 29 | *********************************************************/
|
---|
| 30 | protected static final String RETRIEVE_TEMPLATE_FROM_XML_FILE = "RetrieveXMLTemplateFromFile";
|
---|
| 31 | protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile";
|
---|
[27054] | 32 | protected static final String GET_GSLIB_ELEMENTS_FROM_FILE = "GetGSLIBElementsFromFile";
|
---|
[26493] | 33 | /*********************************************************/
|
---|
| 34 |
|
---|
[27023] | 35 | String[] services = { RETRIEVE_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_GSLIB_ELEMENTS_FROM_FILE };
|
---|
[26493] | 36 |
|
---|
| 37 | public boolean configure(Element info, Element extra_info)
|
---|
| 38 | {
|
---|
| 39 | if (!super.configure(info, extra_info))
|
---|
| 40 | {
|
---|
| 41 | return false;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | logger.info("Configuring DebugServices...");
|
---|
| 45 | this.config_info = info;
|
---|
| 46 |
|
---|
| 47 | for (int i = 0; i < services.length; i++)
|
---|
| 48 | {
|
---|
| 49 | Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 50 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 51 | service.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
| 52 | this.short_service_info.appendChild(service);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | return true;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
| 59 | {
|
---|
| 60 | for (int i = 0; i < services.length; i++)
|
---|
| 61 | {
|
---|
| 62 | if (service_id.equals(services[i]))
|
---|
| 63 | {
|
---|
| 64 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 65 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 66 | service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
| 67 | return service_elem;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | return null;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | protected Element processRetrieveXMLTemplateFromFile(Element request)
|
---|
| 75 | {
|
---|
| 76 | Element result = GSXML.createBasicResponse(this.doc, RETRIEVE_TEMPLATE_FROM_XML_FILE);
|
---|
| 77 |
|
---|
| 78 | if (request == null)
|
---|
| 79 | {
|
---|
| 80 | GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 81 | return result;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 85 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
| 86 |
|
---|
| 87 | // Get the parameters of the request
|
---|
| 88 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 89 |
|
---|
| 90 | if (param_list == null)
|
---|
| 91 | {
|
---|
| 92 | GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 93 | return result;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 97 |
|
---|
| 98 | String filepath = (String) params.get("filePath");
|
---|
| 99 | String namespace = (String) params.get("namespace");
|
---|
| 100 | String nodeName = (String) params.get("nodename");
|
---|
| 101 | String nameToGet = (String) params.get("name");
|
---|
| 102 | String matchToGet = (String) params.get("match");
|
---|
| 103 |
|
---|
| 104 | String fullNamespace;
|
---|
| 105 | if (namespace.toLowerCase().equals("gsf"))
|
---|
| 106 | {
|
---|
| 107 | fullNamespace = GSXML.GSF_NAMESPACE;
|
---|
| 108 | }
|
---|
| 109 | else if (namespace.toLowerCase().equals("xsl"))
|
---|
| 110 | {
|
---|
| 111 | fullNamespace = GSXML.XSL_NAMESPACE;
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
| 114 | {
|
---|
| 115 | return result;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | File xslFile = new File(filepath);
|
---|
| 119 | if (xslFile.exists())
|
---|
| 120 | {
|
---|
| 121 | XMLConverter converter = new XMLConverter();
|
---|
| 122 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
| 123 |
|
---|
| 124 | NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
|
---|
| 125 |
|
---|
| 126 | if (nameToGet != null && nameToGet.length() != 0)
|
---|
| 127 | {
|
---|
| 128 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 129 | {
|
---|
| 130 | Element template = (Element) templateElems.item(i);
|
---|
| 131 | if (template.getAttribute("name").equals(nameToGet))
|
---|
| 132 | {
|
---|
[26561] | 133 | fixAttributes(template);
|
---|
| 134 |
|
---|
[26556] | 135 | Element requestedTemplate = this.doc.createElement("requestedNameTemplate");
|
---|
[26493] | 136 | requestedTemplate.appendChild(this.doc.importNode(template, true));
|
---|
| 137 | result.appendChild(requestedTemplate);
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
[26556] | 141 |
|
---|
[26493] | 142 | //Maybe should look for highest priority
|
---|
| 143 | if (matchToGet != null && matchToGet.length() != 0)
|
---|
| 144 | {
|
---|
| 145 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 146 | {
|
---|
| 147 | Element template = (Element) templateElems.item(i);
|
---|
| 148 | if (template.getAttribute("match").equals(matchToGet))
|
---|
| 149 | {
|
---|
[26561] | 150 | fixAttributes(template);
|
---|
| 151 |
|
---|
[26556] | 152 | Element requestedTemplate = this.doc.createElement("requestedMatchTemplate");
|
---|
[26493] | 153 | requestedTemplate.appendChild(this.doc.importNode(template, true));
|
---|
| 154 | result.appendChild(requestedTemplate);
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | return result;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[26561] | 163 | protected void fixAttributes(Element template)
|
---|
| 164 | {
|
---|
| 165 | NodeList nodes = template.getElementsByTagName("*");
|
---|
| 166 | for (int j = 0; j < nodes.getLength(); j++)
|
---|
| 167 | {
|
---|
| 168 | Node current = nodes.item(j);
|
---|
| 169 | NamedNodeMap attributes = current.getAttributes();
|
---|
| 170 | for (int k = 0; k < attributes.getLength(); k++)
|
---|
| 171 | {
|
---|
| 172 | Node currentAttr = attributes.item(k);
|
---|
| 173 | String value = currentAttr.getNodeValue();
|
---|
| 174 | if (value.contains("&") || value.contains("<") || value.contains(">"))
|
---|
| 175 | {
|
---|
| 176 | currentAttr.setNodeValue(value.replace("&", "&amp;").replace("<", "<").replace(">", ">"));
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[26493] | 182 | protected Element processSaveXMLTemplateToFile(Element request)
|
---|
| 183 | {
|
---|
| 184 | Element result = GSXML.createBasicResponse(this.doc, SAVE_TEMPLATE_TO_XML_FILE);
|
---|
| 185 |
|
---|
| 186 | if (request == null)
|
---|
| 187 | {
|
---|
| 188 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 189 | return result;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 193 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
| 194 |
|
---|
| 195 | // Get the parameters of the request
|
---|
| 196 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 197 |
|
---|
| 198 | if (param_list == null)
|
---|
| 199 | {
|
---|
| 200 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 201 | return result;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 205 |
|
---|
| 206 | String filepath = (String) params.get("filePath");
|
---|
| 207 | String namespace = (String) params.get("namespace");
|
---|
| 208 | String nodeName = (String) params.get("nodename");
|
---|
| 209 | String nameToSave = (String) params.get("name");
|
---|
| 210 | String matchToSave = (String) params.get("match");
|
---|
| 211 | String xml = (String) params.get("xml");
|
---|
| 212 |
|
---|
| 213 | String fullNamespace;
|
---|
| 214 | if (namespace.toLowerCase().equals("gsf"))
|
---|
| 215 | {
|
---|
| 216 | fullNamespace = GSXML.GSF_NAMESPACE;
|
---|
| 217 | }
|
---|
| 218 | else if (namespace.toLowerCase().equals("xsl"))
|
---|
| 219 | {
|
---|
| 220 | fullNamespace = GSXML.XSL_NAMESPACE;
|
---|
| 221 | }
|
---|
| 222 | else
|
---|
| 223 | {
|
---|
| 224 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": The specified namespace was not valid", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 225 | return result;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | File xslFile = new File(filepath);
|
---|
| 229 | if (xslFile.exists())
|
---|
| 230 | {
|
---|
| 231 | XMLConverter converter = new XMLConverter();
|
---|
| 232 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
| 233 |
|
---|
| 234 | NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
|
---|
| 235 |
|
---|
| 236 | boolean found = false;
|
---|
| 237 | if (nameToSave != null && nameToSave.length() != 0)
|
---|
| 238 | {
|
---|
| 239 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 240 | {
|
---|
| 241 | Element template = (Element) templateElems.item(i);
|
---|
| 242 | if (template.getAttribute("name").equals(nameToSave))
|
---|
| 243 | {
|
---|
| 244 | try
|
---|
| 245 | {
|
---|
| 246 | Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"" + GSXML.XSL_NAMESPACE + "\" xmlns:java=\"" + GSXML.JAVA_NAMESPACE + "\" xmlns:util=\"" + GSXML.UTIL_NAMESPACE + "\" xmlns:gsf=\"" + GSXML.GSF_NAMESPACE + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getElementsByTagNameNS(fullNamespace, nodeName).item(0);
|
---|
| 247 | template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
|
---|
| 248 | found = true;
|
---|
| 249 | }
|
---|
| 250 | catch (Exception ex)
|
---|
| 251 | {
|
---|
| 252 | ex.printStackTrace();
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | //Maybe should look for highest priority match
|
---|
| 258 | if (matchToSave != null && matchToSave.length() != 0)
|
---|
| 259 | {
|
---|
| 260 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 261 | {
|
---|
| 262 | Element template = (Element) templateElems.item(i);
|
---|
| 263 | if (template.getAttribute("match").equals(matchToSave))
|
---|
| 264 | {
|
---|
[26669] | 265 | Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"" + GSXML.XSL_NAMESPACE + "\" xmlns:java=\"" + GSXML.JAVA_NAMESPACE + "\" xmlns:util=\"" + GSXML.UTIL_NAMESPACE + "\" xmlns:gsf=\"" + GSXML.GSF_NAMESPACE + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getElementsByTagNameNS(fullNamespace, nodeName).item(0);
|
---|
[26493] | 266 | template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
|
---|
| 267 | found = true;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | if (!found)
|
---|
| 273 | {
|
---|
| 274 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": Could not save as the specified template could not be found", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 275 | }
|
---|
| 276 | else
|
---|
| 277 | {
|
---|
| 278 |
|
---|
| 279 | try
|
---|
| 280 | {
|
---|
| 281 | Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
---|
| 282 |
|
---|
| 283 | //initialize StreamResult with File object to save to file
|
---|
| 284 | StreamResult sresult = new StreamResult(new FileWriter(xslFile));
|
---|
| 285 | DOMSource source = new DOMSource(xslDoc);
|
---|
| 286 | transformer.transform(source, sresult);
|
---|
| 287 | }
|
---|
| 288 | catch (Exception ex)
|
---|
| 289 | {
|
---|
| 290 | GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": There was an error writing out the XML file", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | return result;
|
---|
| 297 | }
|
---|
[27023] | 298 |
|
---|
| 299 | protected Element processGetGSLIBElementsFromFile(Element request)
|
---|
| 300 | {
|
---|
| 301 | Element result = GSXML.createBasicResponse(this.doc, GET_GSLIB_ELEMENTS_FROM_FILE);
|
---|
[27054] | 302 |
|
---|
[27023] | 303 | if (request == null)
|
---|
| 304 | {
|
---|
| 305 | GSXML.addError(this.doc, result, GET_GSLIB_ELEMENTS_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 306 | return result;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
| 310 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
[27054] | 311 |
|
---|
[27023] | 312 | // Get the parameters of the request
|
---|
| 313 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 314 |
|
---|
| 315 | if (param_list == null)
|
---|
| 316 | {
|
---|
| 317 | GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 318 | return result;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 322 |
|
---|
| 323 | String interfaceName = (String) params.get("interfaceName");
|
---|
| 324 |
|
---|
| 325 | String filePath = GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform" + File.separator + "gslib.xsl";
|
---|
| 326 | File xslFile = new File(filePath);
|
---|
[27054] | 327 |
|
---|
| 328 | if (xslFile.exists())
|
---|
[27023] | 329 | {
|
---|
| 330 | XMLConverter converter = new XMLConverter();
|
---|
| 331 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
| 332 |
|
---|
| 333 | Element templateList = this.doc.createElement("templateList");
|
---|
| 334 | String templateListString = "[";
|
---|
[27054] | 335 |
|
---|
[27023] | 336 | NodeList templateElems = xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
|
---|
[27054] | 337 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
[27023] | 338 | {
|
---|
[27054] | 339 | Element currentElem = (Element) templateElems.item(i);
|
---|
| 340 | if (currentElem.hasAttribute(GSXML.NAME_ATT))
|
---|
[27023] | 341 | {
|
---|
| 342 | templateListString += "\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\"";
|
---|
[27054] | 343 | if (i < templateElems.getLength() - 1)
|
---|
[27023] | 344 | {
|
---|
| 345 | templateListString += ",";
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
[27054] | 349 |
|
---|
[27023] | 350 | templateListString += "]";
|
---|
[27054] | 351 |
|
---|
[27023] | 352 | templateList.setTextContent(templateListString);
|
---|
| 353 | result.appendChild(templateList);
|
---|
| 354 | }
|
---|
[27054] | 355 |
|
---|
[27023] | 356 | return result;
|
---|
| 357 | }
|
---|
[26493] | 358 | } |
---|