[26493] | 1 | package org.greenstone.gsdl3.service;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.io.FileWriter;
|
---|
| 5 | import java.io.Serializable;
|
---|
[27109] | 6 | import java.util.ArrayList;
|
---|
[26493] | 7 | import java.util.HashMap;
|
---|
| 8 |
|
---|
| 9 | import javax.xml.transform.Transformer;
|
---|
| 10 | import javax.xml.transform.TransformerFactory;
|
---|
| 11 | import javax.xml.transform.dom.DOMSource;
|
---|
| 12 | import javax.xml.transform.stream.StreamResult;
|
---|
| 13 |
|
---|
| 14 | import org.apache.log4j.Logger;
|
---|
| 15 | import org.greenstone.gsdl3.util.GSXML;
|
---|
[27165] | 16 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
[27076] | 17 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[26493] | 18 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
[27023] | 19 | import org.greenstone.util.GlobalProperties;
|
---|
[26493] | 20 | import org.w3c.dom.Document;
|
---|
| 21 | import org.w3c.dom.Element;
|
---|
[26561] | 22 | import org.w3c.dom.NamedNodeMap;
|
---|
| 23 | import org.w3c.dom.Node;
|
---|
[26493] | 24 | import org.w3c.dom.NodeList;
|
---|
| 25 |
|
---|
| 26 | public class DebugService extends ServiceRack
|
---|
| 27 | {
|
---|
| 28 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DebugService.class.getName());
|
---|
| 29 |
|
---|
| 30 | /**********************************************************
|
---|
| 31 | * The list of services the utility service rack supports *
|
---|
| 32 | *********************************************************/
|
---|
[27109] | 33 | protected static final String GET_TEMPLATE_FROM_XML_FILE = "GetXMLTemplateFromFile";
|
---|
[26493] | 34 | protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile";
|
---|
[27109] | 35 | protected static final String GET_TEMPLATE_LIST_FROM_FILE = "GetTemplateListFromFile";
|
---|
| 36 | protected static final String GET_XSLT_FILES_FOR_COLLECTION = "GetXSLTFilesForCollection";
|
---|
[27165] | 37 | protected static final String RESOLVE_CALL_TEMPLATE = "ResolveCallTemplate";
|
---|
[26493] | 38 | /*********************************************************/
|
---|
| 39 |
|
---|
[27165] | 40 | String[] services = { GET_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_TEMPLATE_LIST_FROM_FILE, GET_XSLT_FILES_FOR_COLLECTION, RESOLVE_CALL_TEMPLATE };
|
---|
[26493] | 41 |
|
---|
| 42 | public boolean configure(Element info, Element extra_info)
|
---|
| 43 | {
|
---|
| 44 | if (!super.configure(info, extra_info))
|
---|
| 45 | {
|
---|
| 46 | return false;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | logger.info("Configuring DebugServices...");
|
---|
| 50 | this.config_info = info;
|
---|
| 51 |
|
---|
| 52 | for (int i = 0; i < services.length; i++)
|
---|
| 53 | {
|
---|
[28966] | 54 | Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[26493] | 55 | service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 56 | service.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
| 57 | this.short_service_info.appendChild(service);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | return true;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[28966] | 63 | protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
|
---|
[26493] | 64 | {
|
---|
| 65 | for (int i = 0; i < services.length; i++)
|
---|
| 66 | {
|
---|
| 67 | if (service_id.equals(services[i]))
|
---|
| 68 | {
|
---|
[28966] | 69 | Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
|
---|
[26493] | 70 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 71 | service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
|
---|
| 72 | return service_elem;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | return null;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[27165] | 79 | protected Element processResolveCallTemplate(Element request)
|
---|
| 80 | {
|
---|
[28966] | 81 | Document result_doc = XMLConverter.newDOM();
|
---|
[27165] | 82 |
|
---|
[28966] | 83 | Element result = GSXML.createBasicResponse(result_doc, RESOLVE_CALL_TEMPLATE);
|
---|
| 84 |
|
---|
[27165] | 85 | if (request == null)
|
---|
| 86 | {
|
---|
[28966] | 87 | GSXML.addError(result, RESOLVE_CALL_TEMPLATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27165] | 88 | return result;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // Get the parameters of the request
|
---|
| 92 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 93 |
|
---|
| 94 | if (param_list == null)
|
---|
| 95 | {
|
---|
[28966] | 96 | GSXML.addError(result, RESOLVE_CALL_TEMPLATE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27165] | 97 | return result;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 101 |
|
---|
[29421] | 102 | String collectionName = (String) params.get("collectionName");
|
---|
| 103 |
|
---|
| 104 | // check permissions
|
---|
| 105 | if (!userHasEditPermissions(collectionName, request)) {
|
---|
| 106 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 107 | return result;
|
---|
| 108 | }
|
---|
[27165] | 109 | String interfaceName = (String) params.get("interfaceName");
|
---|
| 110 | String siteName = (String) params.get("siteName");
|
---|
| 111 | String fileName = (String) params.get("fileName");
|
---|
| 112 | String nameToGet = (String) params.get("templateName");
|
---|
| 113 |
|
---|
| 114 | fileName = fileName.replace("\\", "/");
|
---|
| 115 |
|
---|
| 116 | Document xslDoc = GSXSLT.mergedXSLTDocumentCascade(fileName, siteName, collectionName, interfaceName, new ArrayList<String>(), true);
|
---|
| 117 |
|
---|
| 118 | int sepIndex = fileName.lastIndexOf("/");
|
---|
| 119 | String pathExtra = null;
|
---|
| 120 | if (sepIndex != -1)
|
---|
| 121 | {
|
---|
| 122 | pathExtra = fileName.substring(0, sepIndex + 1);
|
---|
| 123 | fileName = fileName.substring(sepIndex + 1);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | GSXSLT.inlineImportAndIncludeFilesDebug(xslDoc, pathExtra, true, fileName, siteName, collectionName, interfaceName, new ArrayList<String>());
|
---|
| 127 |
|
---|
| 128 | NodeList templateList = xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
|
---|
| 129 | for (int i = 0; i < templateList.getLength(); i++)
|
---|
| 130 | {
|
---|
| 131 | Element current = (Element) templateList.item(i);
|
---|
| 132 | if (current.hasAttribute("name") && current.getAttribute("name").equals(nameToGet))
|
---|
| 133 | {
|
---|
| 134 | Element debugElement = (Element) current.getElementsByTagName("debug").item(0);
|
---|
| 135 |
|
---|
[28966] | 136 | Element requestedTemplate = result_doc.createElement("requestedTemplate");
|
---|
[27165] | 137 | requestedTemplate.setTextContent(debugElement.getAttribute("filename"));
|
---|
| 138 | result.appendChild(requestedTemplate);
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | return result;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[27109] | 145 | protected Element processGetXMLTemplateFromFile(Element request)
|
---|
[26493] | 146 | {
|
---|
[28966] | 147 | Document result_doc = XMLConverter.newDOM();
|
---|
| 148 | Element result = GSXML.createBasicResponse(result_doc, GET_TEMPLATE_FROM_XML_FILE);
|
---|
[26493] | 149 |
|
---|
| 150 | if (request == null)
|
---|
| 151 | {
|
---|
[28966] | 152 | GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 153 | return result;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | // Get the parameters of the request
|
---|
| 157 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 158 |
|
---|
| 159 | if (param_list == null)
|
---|
| 160 | {
|
---|
[28966] | 161 | GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 162 | return result;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 166 |
|
---|
[29421] | 167 |
|
---|
| 168 | String collectionName = (String) params.get("collectionName");
|
---|
| 169 | // check permissions
|
---|
| 170 | if (!userHasEditPermissions(collectionName, request)) {
|
---|
| 171 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 172 | return result;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
[27109] | 175 | String locationName = (String) params.get("locationName");
|
---|
| 176 | String interfaceName = (String) params.get("interfaceName");
|
---|
| 177 | String siteName = (String) params.get("siteName");
|
---|
| 178 | String fileName = (String) params.get("fileName");
|
---|
[26493] | 179 | String namespace = (String) params.get("namespace");
|
---|
| 180 | String nodeName = (String) params.get("nodename");
|
---|
| 181 | String nameToGet = (String) params.get("name");
|
---|
| 182 | String matchToGet = (String) params.get("match");
|
---|
[27718] | 183 | String xPath = (String) params.get("xpath");
|
---|
[26493] | 184 |
|
---|
[29421] | 185 |
|
---|
| 186 |
|
---|
[26493] | 187 | String fullNamespace;
|
---|
| 188 | if (namespace.toLowerCase().equals("gsf"))
|
---|
| 189 | {
|
---|
| 190 | fullNamespace = GSXML.GSF_NAMESPACE;
|
---|
| 191 | }
|
---|
| 192 | else if (namespace.toLowerCase().equals("xsl"))
|
---|
| 193 | {
|
---|
| 194 | fullNamespace = GSXML.XSL_NAMESPACE;
|
---|
| 195 | }
|
---|
| 196 | else
|
---|
| 197 | {
|
---|
[28966] | 198 | GSXML.addError(result, "A valid namespace was not specified.");
|
---|
[26493] | 199 | return result;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[27109] | 202 | File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
|
---|
[26493] | 203 | if (xslFile.exists())
|
---|
| 204 | {
|
---|
| 205 | XMLConverter converter = new XMLConverter();
|
---|
| 206 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
[27718] | 207 | Element rootElem = xslDoc.getDocumentElement();
|
---|
[26493] | 208 |
|
---|
[27718] | 209 | if (xPath != null && xPath.length() > 0)
|
---|
| 210 | {
|
---|
| 211 | String[] pathSegments = xPath.split("/");
|
---|
| 212 | for (int i = 1; i < pathSegments.length; i++)
|
---|
| 213 | {
|
---|
| 214 | String currentSegment = pathSegments[i];
|
---|
| 215 | int count = 1;
|
---|
| 216 | if (currentSegment.contains("["))
|
---|
| 217 | {
|
---|
| 218 | count = Integer.parseInt(currentSegment.substring(currentSegment.indexOf("[") + 1, currentSegment.indexOf("]")));
|
---|
| 219 | currentSegment = currentSegment.substring(0, currentSegment.indexOf("["));
|
---|
| 220 | }
|
---|
| 221 | Node child = rootElem.getFirstChild();
|
---|
| 222 | while (count > 0)
|
---|
| 223 | {
|
---|
| 224 | if (child.getNodeType() == Node.ELEMENT_NODE && ((Element) child).getNodeName().equals(currentSegment))
|
---|
| 225 | {
|
---|
| 226 | rootElem = (Element) child;
|
---|
| 227 | count--;
|
---|
| 228 | }
|
---|
| 229 | child = child.getNextSibling();
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
[26493] | 233 |
|
---|
[27718] | 234 | NodeList templateElems = rootElem.getElementsByTagNameNS(fullNamespace, nodeName);
|
---|
| 235 |
|
---|
[26493] | 236 | if (nameToGet != null && nameToGet.length() != 0)
|
---|
| 237 | {
|
---|
| 238 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 239 | {
|
---|
| 240 | Element template = (Element) templateElems.item(i);
|
---|
| 241 | if (template.getAttribute("name").equals(nameToGet))
|
---|
| 242 | {
|
---|
[26561] | 243 | fixAttributes(template);
|
---|
| 244 |
|
---|
[28966] | 245 | Element requestedTemplate = result_doc.createElement("requestedNameTemplate");
|
---|
| 246 | requestedTemplate.appendChild(result_doc.importNode(template, true));
|
---|
[26493] | 247 | result.appendChild(requestedTemplate);
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
[26556] | 251 |
|
---|
[26493] | 252 | //Maybe should look for highest priority
|
---|
| 253 | if (matchToGet != null && matchToGet.length() != 0)
|
---|
| 254 | {
|
---|
| 255 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 256 | {
|
---|
| 257 | Element template = (Element) templateElems.item(i);
|
---|
| 258 | if (template.getAttribute("match").equals(matchToGet))
|
---|
| 259 | {
|
---|
[26561] | 260 | fixAttributes(template);
|
---|
| 261 |
|
---|
[28966] | 262 | Element requestedTemplate = result_doc.createElement("requestedMatchTemplate");
|
---|
| 263 | requestedTemplate.appendChild(result_doc.importNode(template, true));
|
---|
[26493] | 264 | result.appendChild(requestedTemplate);
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | return result;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[26561] | 273 | protected void fixAttributes(Element template)
|
---|
| 274 | {
|
---|
| 275 | NodeList nodes = template.getElementsByTagName("*");
|
---|
| 276 | for (int j = 0; j < nodes.getLength(); j++)
|
---|
| 277 | {
|
---|
| 278 | Node current = nodes.item(j);
|
---|
| 279 | NamedNodeMap attributes = current.getAttributes();
|
---|
| 280 | for (int k = 0; k < attributes.getLength(); k++)
|
---|
| 281 | {
|
---|
| 282 | Node currentAttr = attributes.item(k);
|
---|
| 283 | String value = currentAttr.getNodeValue();
|
---|
[27109] | 284 | if (value.contains("&") || value.contains("<") || value.contains(">") || value.contains("\""))
|
---|
[26561] | 285 | {
|
---|
[27109] | 286 | currentAttr.setNodeValue(value.replace("&", "&amp;").replace("<", "<").replace(">", ">").replace("\"", """));
|
---|
[26561] | 287 | }
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[26493] | 292 | protected Element processSaveXMLTemplateToFile(Element request)
|
---|
| 293 | {
|
---|
[28966] | 294 | Document result_doc = XMLConverter.newDOM();
|
---|
| 295 | Element result = GSXML.createBasicResponse(result_doc, SAVE_TEMPLATE_TO_XML_FILE);
|
---|
[26493] | 296 |
|
---|
| 297 | if (request == null)
|
---|
| 298 | {
|
---|
[28966] | 299 | GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 300 | return result;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | // Get the parameters of the request
|
---|
| 304 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 305 |
|
---|
| 306 | if (param_list == null)
|
---|
| 307 | {
|
---|
[28966] | 308 | GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 309 | return result;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 313 |
|
---|
[29421] | 314 | String collectionName = (String) params.get("collectionName");
|
---|
| 315 | // check permissions
|
---|
| 316 | if (!userHasEditPermissions(collectionName, request)) {
|
---|
| 317 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 318 | return result;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[27109] | 321 | String locationName = (String) params.get("locationName");
|
---|
| 322 | String fileName = (String) params.get("fileName");
|
---|
| 323 | String interfaceName = (String) params.get("interfaceName");
|
---|
| 324 | String siteName = (String) params.get("siteName");
|
---|
[26493] | 325 | String namespace = (String) params.get("namespace");
|
---|
| 326 | String nodeName = (String) params.get("nodename");
|
---|
| 327 | String nameToSave = (String) params.get("name");
|
---|
| 328 | String matchToSave = (String) params.get("match");
|
---|
[27718] | 329 | String xPath = (String) params.get("xpath");
|
---|
[26493] | 330 | String xml = (String) params.get("xml");
|
---|
| 331 |
|
---|
| 332 | String fullNamespace;
|
---|
| 333 | if (namespace.toLowerCase().equals("gsf"))
|
---|
| 334 | {
|
---|
| 335 | fullNamespace = GSXML.GSF_NAMESPACE;
|
---|
| 336 | }
|
---|
| 337 | else if (namespace.toLowerCase().equals("xsl"))
|
---|
| 338 | {
|
---|
| 339 | fullNamespace = GSXML.XSL_NAMESPACE;
|
---|
| 340 | }
|
---|
| 341 | else
|
---|
| 342 | {
|
---|
[28966] | 343 | GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": The specified namespace was not valid", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 344 | return result;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[27109] | 347 | File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
|
---|
[26493] | 348 | if (xslFile.exists())
|
---|
| 349 | {
|
---|
| 350 | XMLConverter converter = new XMLConverter();
|
---|
| 351 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
| 352 |
|
---|
[27718] | 353 | Element rootElem = xslDoc.getDocumentElement();
|
---|
| 354 |
|
---|
| 355 | if (xPath != null && xPath.length() > 0)
|
---|
| 356 | {
|
---|
| 357 | String[] pathSegments = xPath.split("/");
|
---|
| 358 | for (int i = 1; i < pathSegments.length; i++)
|
---|
| 359 | {
|
---|
| 360 | String currentSegment = pathSegments[i];
|
---|
| 361 | int count = 1;
|
---|
| 362 | if (currentSegment.contains("["))
|
---|
| 363 | {
|
---|
| 364 | count = Integer.parseInt(currentSegment.substring(currentSegment.indexOf("[") + 1, currentSegment.indexOf("]")));
|
---|
| 365 | currentSegment = currentSegment.substring(0, currentSegment.indexOf("["));
|
---|
| 366 | }
|
---|
| 367 | Node child = rootElem.getFirstChild();
|
---|
| 368 | while (count > 0)
|
---|
| 369 | {
|
---|
| 370 | if (child.getNodeType() == Node.ELEMENT_NODE && ((Element) child).getNodeName().equals(currentSegment))
|
---|
| 371 | {
|
---|
| 372 | rootElem = (Element) child;
|
---|
| 373 | count--;
|
---|
| 374 | }
|
---|
| 375 | child = child.getNextSibling();
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 |
|
---|
[28118] | 380 | NodeList templateElems = rootElem.getElementsByTagNameNS(fullNamespace, nodeName);
|
---|
[26493] | 381 |
|
---|
| 382 | boolean found = false;
|
---|
| 383 | if (nameToSave != null && nameToSave.length() != 0)
|
---|
| 384 | {
|
---|
| 385 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 386 | {
|
---|
| 387 | Element template = (Element) templateElems.item(i);
|
---|
| 388 | if (template.getAttribute("name").equals(nameToSave))
|
---|
| 389 | {
|
---|
| 390 | try
|
---|
| 391 | {
|
---|
[30587] | 392 | Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\" "+ GSXML.ALL_NAMESPACES_ATTS + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getElementsByTagNameNS(fullNamespace, nodeName).item(0);
|
---|
[26493] | 393 | template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
|
---|
| 394 | found = true;
|
---|
| 395 | }
|
---|
| 396 | catch (Exception ex)
|
---|
| 397 | {
|
---|
| 398 | ex.printStackTrace();
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 | //Maybe should look for highest priority match
|
---|
| 404 | if (matchToSave != null && matchToSave.length() != 0)
|
---|
| 405 | {
|
---|
| 406 | for (int i = 0; i < templateElems.getLength(); i++)
|
---|
| 407 | {
|
---|
| 408 | Element template = (Element) templateElems.item(i);
|
---|
| 409 | if (template.getAttribute("match").equals(matchToSave))
|
---|
| 410 | {
|
---|
[30587] | 411 | Element newTemplate = (Element) converter.getDOM("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<xsl:stylesheet version=\"1.0\"" + GSXML.ALL_NAMESPACES_ATTS + "\">" + xml + "</xsl:stylesheet>", "UTF-8").getDocumentElement().getElementsByTagNameNS(fullNamespace, nodeName).item(0);
|
---|
[26493] | 412 | template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
|
---|
| 413 | found = true;
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | if (!found)
|
---|
| 419 | {
|
---|
[28966] | 420 | GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": Could not save as the specified template could not be found", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 421 | }
|
---|
| 422 | else
|
---|
| 423 | {
|
---|
| 424 |
|
---|
| 425 | try
|
---|
| 426 | {
|
---|
| 427 | Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
---|
| 428 |
|
---|
| 429 | //initialize StreamResult with File object to save to file
|
---|
| 430 | StreamResult sresult = new StreamResult(new FileWriter(xslFile));
|
---|
| 431 | DOMSource source = new DOMSource(xslDoc);
|
---|
| 432 | transformer.transform(source, sresult);
|
---|
| 433 | }
|
---|
| 434 | catch (Exception ex)
|
---|
| 435 | {
|
---|
[28966] | 436 | GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": There was an error writing out the XML file", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[26493] | 437 | }
|
---|
| 438 |
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
[27109] | 441 | else
|
---|
| 442 | {
|
---|
[28966] | 443 | GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + "File: " + xslFile.getAbsolutePath() + " does not exist", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27109] | 444 | }
|
---|
[26493] | 445 |
|
---|
| 446 | return result;
|
---|
| 447 | }
|
---|
[27023] | 448 |
|
---|
[27109] | 449 | protected Element processGetTemplateListFromFile(Element request)
|
---|
[27023] | 450 | {
|
---|
[28966] | 451 | Document result_doc = XMLConverter.newDOM();
|
---|
| 452 | Element result = GSXML.createBasicResponse(result_doc, GET_TEMPLATE_LIST_FROM_FILE);
|
---|
[27054] | 453 |
|
---|
[27023] | 454 | if (request == null)
|
---|
| 455 | {
|
---|
[28966] | 456 | GSXML.addError(result, GET_TEMPLATE_LIST_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27023] | 457 | return result;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | // Get the parameters of the request
|
---|
| 461 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 462 |
|
---|
| 463 | if (param_list == null)
|
---|
| 464 | {
|
---|
[28966] | 465 | GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27023] | 466 | return result;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 470 |
|
---|
[29421] | 471 | String collectionName = (String) params.get("collectionName");
|
---|
| 472 | // check permissions
|
---|
| 473 | if (!userHasEditPermissions(collectionName, request)) {
|
---|
| 474 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 475 | return result;
|
---|
| 476 | }
|
---|
[27109] | 477 | String locationName = (String) params.get("locationName");
|
---|
| 478 | String siteName = (String) params.get("siteName");
|
---|
[27023] | 479 | String interfaceName = (String) params.get("interfaceName");
|
---|
[27109] | 480 | String fileName = (String) params.get("fileName");
|
---|
[27023] | 481 |
|
---|
[27109] | 482 | fileName.replace("/", File.separator);
|
---|
[27054] | 483 |
|
---|
[27109] | 484 | if (locationName == null || locationName.length() == 0)
|
---|
| 485 | {
|
---|
[28966] | 486 | GSXML.addError(result, "Parameter \"locationName\" was null or empty.");
|
---|
[27109] | 487 | return result;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
|
---|
| 491 |
|
---|
[27054] | 492 | if (xslFile.exists())
|
---|
[27023] | 493 | {
|
---|
| 494 | XMLConverter converter = new XMLConverter();
|
---|
| 495 | Document xslDoc = converter.getDOM(xslFile, "UTF-8");
|
---|
[27718] | 496 | Element xslDocElem = xslDoc.getDocumentElement();
|
---|
[27023] | 497 |
|
---|
[27718] | 498 | if (locationName.equals("collectionConfig"))
|
---|
| 499 | {
|
---|
| 500 | GSXSLT.modifyCollectionConfigForDebug(xslDocElem);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[28966] | 503 | Element templateList = result_doc.createElement("templateList");
|
---|
[27109] | 504 | StringBuilder templateListString = new StringBuilder("[");
|
---|
[27054] | 505 |
|
---|
[27718] | 506 | NodeList xslTemplateElems = xslDocElem.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
|
---|
| 507 | NodeList gsfTemplateElems = xslDocElem.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "template");
|
---|
[27109] | 508 | for (int i = 0; i < xslTemplateElems.getLength() + gsfTemplateElems.getLength(); i++)
|
---|
[27023] | 509 | {
|
---|
[27109] | 510 | Element currentElem = (i < xslTemplateElems.getLength()) ? (Element) xslTemplateElems.item(i) : (Element) gsfTemplateElems.item(i - xslTemplateElems.getLength());
|
---|
| 511 | if (!currentElem.hasAttribute(GSXML.NAME_ATT) && !currentElem.hasAttribute(GSXML.MATCH_ATT))
|
---|
| 512 | {
|
---|
| 513 | continue;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | templateListString.append("{");
|
---|
| 517 | templateListString.append("\"namespace\":\"" + ((i < xslTemplateElems.getLength()) ? "xsl" : "gsf") + "\",");
|
---|
[27054] | 518 | if (currentElem.hasAttribute(GSXML.NAME_ATT))
|
---|
[27023] | 519 | {
|
---|
[27109] | 520 | templateListString.append("\"name\":\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\",");
|
---|
[27023] | 521 | }
|
---|
[27109] | 522 | else if (currentElem.hasAttribute(GSXML.MATCH_ATT))
|
---|
| 523 | {
|
---|
| 524 | templateListString.append("\"match\":\"" + currentElem.getAttribute(GSXML.MATCH_ATT) + "\",");
|
---|
| 525 | }
|
---|
[27718] | 526 |
|
---|
| 527 | if (currentElem.getUserData("xpath") != null)
|
---|
| 528 | {
|
---|
| 529 | templateListString.append("\"xpath\":\"" + currentElem.getUserData("xpath") + "\",");
|
---|
| 530 | }
|
---|
[27109] | 531 | templateListString.deleteCharAt(templateListString.length() - 1);
|
---|
| 532 | templateListString.append("},");
|
---|
[27023] | 533 | }
|
---|
[27054] | 534 |
|
---|
[27109] | 535 | templateListString.deleteCharAt(templateListString.length() - 1);
|
---|
| 536 | templateListString.append("]");
|
---|
[27054] | 537 |
|
---|
[27109] | 538 | templateList.setTextContent(templateListString.toString());
|
---|
[27023] | 539 | result.appendChild(templateList);
|
---|
| 540 | }
|
---|
[27109] | 541 | else
|
---|
| 542 | {
|
---|
[28966] | 543 | GSXML.addError(result, "File: " + xslFile.getAbsolutePath() + " does not exist");
|
---|
[27109] | 544 | return result;
|
---|
| 545 | }
|
---|
[27054] | 546 |
|
---|
[27023] | 547 | return result;
|
---|
| 548 | }
|
---|
[27109] | 549 |
|
---|
| 550 | protected Element processGetXSLTFilesForCollection(Element request)
|
---|
| 551 | {
|
---|
[28966] | 552 | Document result_doc = XMLConverter.newDOM();
|
---|
| 553 | Element result = GSXML.createBasicResponse(result_doc, GET_XSLT_FILES_FOR_COLLECTION);
|
---|
[27109] | 554 |
|
---|
| 555 | if (request == null)
|
---|
| 556 | {
|
---|
[28966] | 557 | GSXML.addError(result, GET_XSLT_FILES_FOR_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27109] | 558 | return result;
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | // Get the parameters of the request
|
---|
| 562 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 563 |
|
---|
| 564 | if (param_list == null)
|
---|
| 565 | {
|
---|
[28966] | 566 | GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
|
---|
[27109] | 567 | return result;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
| 571 |
|
---|
[29421] | 572 | String collectionName = (String) params.get("collectionName");
|
---|
| 573 | // check permissions
|
---|
| 574 | if (!userHasEditPermissions(collectionName, request)) {
|
---|
| 575 | GSXML.addError(result, "This user does not have the required permissions to perform this action.");
|
---|
| 576 | return result;
|
---|
| 577 | }
|
---|
[27109] | 578 | String interfaceName = (String) params.get("interfaceName");
|
---|
| 579 | String siteName = (String) params.get("siteName");
|
---|
| 580 |
|
---|
[28966] | 581 | Element fileList = result_doc.createElement("fileListJSON");
|
---|
[27109] | 582 | StringBuilder fileListString = new StringBuilder("[");
|
---|
| 583 |
|
---|
| 584 | String[] placesToCheck = new String[] { GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform", //INTERFACE FILE PATH
|
---|
| 585 | GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "transform", //SITE FILE PATH
|
---|
| 586 | GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "transform", //COLLECTION FILE PATH
|
---|
| 587 | GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "etc" //COLLECTION CONFIG FILE PATH
|
---|
| 588 | };
|
---|
| 589 |
|
---|
| 590 | String[] shortPlaceNames = new String[] { "interface", "site", "collection", "collectionConfig" };
|
---|
| 591 |
|
---|
| 592 | for (int i = 0; i < placesToCheck.length; i++)
|
---|
| 593 | {
|
---|
| 594 | ArrayList<File> xslFiles = getXSLTFilesFromDirectoryRecursive(new File(placesToCheck[i]));
|
---|
| 595 | for (File f : xslFiles)
|
---|
| 596 | {
|
---|
| 597 | fileListString.append("{\"location\":\"" + shortPlaceNames[i] + "\",\"path\":\"" + f.getAbsolutePath().replace(placesToCheck[i] + File.separator, "") + "\"},");
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | fileListString.deleteCharAt(fileListString.length() - 1); //Remove the last , character
|
---|
| 601 | fileListString.append("]");
|
---|
| 602 |
|
---|
| 603 | fileList.setTextContent(fileListString.toString());
|
---|
| 604 | result.appendChild(fileList);
|
---|
| 605 |
|
---|
| 606 | return result;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 | protected File createFileFromLocation(String location, String filename, String interfaceName, String siteName, String collectionName)
|
---|
| 610 | {
|
---|
| 611 | String filePath = "";
|
---|
| 612 | if (location.equals("interface"))
|
---|
| 613 | {
|
---|
| 614 | filePath = GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform" + File.separator + filename;
|
---|
| 615 | }
|
---|
| 616 | else if (location.equals("site"))
|
---|
| 617 | {
|
---|
| 618 | filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "transform" + File.separator + filename;
|
---|
| 619 | }
|
---|
| 620 | else if (location.equals("collection"))
|
---|
| 621 | {
|
---|
| 622 | filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "transform" + File.separator + filename;
|
---|
| 623 | }
|
---|
| 624 | else if (location.equals("collectionConfig"))
|
---|
| 625 | {
|
---|
| 626 | filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "etc" + File.separator + filename;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | return new File(filePath);
|
---|
| 630 | }
|
---|
| 631 |
|
---|
| 632 | protected ArrayList<File> getXSLTFilesFromDirectoryRecursive(File dir)
|
---|
| 633 | {
|
---|
| 634 | ArrayList<File> result = new ArrayList<File>();
|
---|
| 635 |
|
---|
| 636 | if (dir != null && dir.exists() && dir.isDirectory())
|
---|
| 637 | {
|
---|
| 638 | for (File f : dir.listFiles())
|
---|
| 639 | {
|
---|
| 640 | if (f.isDirectory())
|
---|
| 641 | {
|
---|
| 642 | result.addAll(getXSLTFilesFromDirectoryRecursive(f));
|
---|
| 643 | }
|
---|
| 644 | else if (f.getName().endsWith(".xsl") || f.getName().endsWith(".xml"))
|
---|
| 645 | {
|
---|
| 646 | result.add(f);
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | return result;
|
---|
| 652 | }
|
---|
[29421] | 653 |
|
---|
| 654 | protected boolean userHasEditPermissions(String collection, Element request) {
|
---|
| 655 | UserContext context = new UserContext(request);
|
---|
| 656 | for (String group : context.getGroups()) {
|
---|
| 657 | // administrator always has permission
|
---|
| 658 | if (group.equals("administrator")) {
|
---|
| 659 | return true;
|
---|
| 660 | }
|
---|
| 661 | // all-collections-editor can edit any collection
|
---|
| 662 | if (!collection.equals("")) {
|
---|
| 663 | if (group.equals("all-collections-editor")) {
|
---|
| 664 | return true;
|
---|
| 665 | }
|
---|
| 666 | if (group.equals(collection+"-collection-editor")) {
|
---|
| 667 | return true;
|
---|
| 668 | }
|
---|
| 669 | }
|
---|
| 670 | }
|
---|
| 671 | // haven't found a group with edit permissions
|
---|
| 672 | return false;
|
---|
| 673 |
|
---|
| 674 | }
|
---|
| 675 | }
|
---|
| 676 |
|
---|