Changeset 27109 for main/trunk/greenstone3/src/java
- Timestamp:
- 2013-03-21T16:47:19+13:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DebugService.java
r27076 r27109 4 4 import java.io.FileWriter; 5 5 import java.io.Serializable; 6 import java.util.ArrayList; 6 7 import java.util.HashMap; 7 8 … … 29 30 * The list of services the utility service rack supports * 30 31 *********************************************************/ 31 protected static final String RETRIEVE_TEMPLATE_FROM_XML_FILE = "RetrieveXMLTemplateFromFile";32 protected static final String GET_TEMPLATE_FROM_XML_FILE = "GetXMLTemplateFromFile"; 32 33 protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile"; 33 protected static final String GET_GSLIB_ELEMENTS_FROM_FILE = "GetGSLIBElementsFromFile"; 34 protected static final String GET_TEMPLATE_LIST_FROM_FILE = "GetTemplateListFromFile"; 35 protected static final String GET_XSLT_FILES_FOR_COLLECTION = "GetXSLTFilesForCollection"; 34 36 /*********************************************************/ 35 37 36 String[] services = { RETRIEVE_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_GSLIB_ELEMENTS_FROM_FILE};38 String[] services = { GET_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_TEMPLATE_LIST_FROM_FILE, GET_XSLT_FILES_FOR_COLLECTION }; 37 39 38 40 public boolean configure(Element info, Element extra_info) … … 73 75 } 74 76 75 protected Element process RetrieveXMLTemplateFromFile(Element request)76 { 77 Element result = GSXML.createBasicResponse(this.doc, RETRIEVE_TEMPLATE_FROM_XML_FILE);77 protected Element processGetXMLTemplateFromFile(Element request) 78 { 79 Element result = GSXML.createBasicResponse(this.doc, GET_TEMPLATE_FROM_XML_FILE); 78 80 79 81 if (request == null) 80 82 { 81 GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);83 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX); 82 84 return result; 83 85 } … … 104 106 if (param_list == null) 105 107 { 106 GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);108 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX); 107 109 return result; 108 110 } … … 110 112 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false); 111 113 112 String filepath = (String) params.get("filePath"); 114 String locationName = (String) params.get("locationName"); 115 String interfaceName = (String) params.get("interfaceName"); 116 String siteName = (String) params.get("siteName"); 117 String collectionName = (String) params.get("collectionName"); 118 String fileName = (String) params.get("fileName"); 113 119 String namespace = (String) params.get("namespace"); 114 120 String nodeName = (String) params.get("nodename"); … … 127 133 else 128 134 { 129 return result; 130 } 131 132 File xslFile = new File(filepath); 135 GSXML.addError(this.doc, result, "A valid namespace was not specified."); 136 return result; 137 } 138 139 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName); 133 140 if (xslFile.exists()) 134 141 { … … 186 193 Node currentAttr = attributes.item(k); 187 194 String value = currentAttr.getNodeValue(); 188 if (value.contains("&") || value.contains("<") || value.contains(">") )189 { 190 currentAttr.setNodeValue(value.replace("&", "&amp;").replace("<", "<").replace(">", ">") );195 if (value.contains("&") || value.contains("<") || value.contains(">") || value.contains("\"")) 196 { 197 currentAttr.setNodeValue(value.replace("&", "&amp;").replace("<", "<").replace(">", ">").replace("\"", """)); 191 198 } 192 199 } … … 231 238 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false); 232 239 233 String filepath = (String) params.get("filePath"); 240 String locationName = (String) params.get("locationName"); 241 String fileName = (String) params.get("fileName"); 242 String interfaceName = (String) params.get("interfaceName"); 243 String siteName = (String) params.get("siteName"); 244 String collectionName = (String) params.get("collectionName"); 234 245 String namespace = (String) params.get("namespace"); 235 246 String nodeName = (String) params.get("nodename"); … … 253 264 } 254 265 255 File xslFile = new File(filepath);266 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName); 256 267 if (xslFile.exists()) 257 268 { … … 320 331 } 321 332 } 333 else 334 { 335 GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + "File: " + xslFile.getAbsolutePath() + " does not exist", GSXML.ERROR_TYPE_SYNTAX); 336 } 322 337 323 338 return result; 324 339 } 325 340 326 protected Element processGet GSLIBElementsFromFile(Element request)327 { 328 Element result = GSXML.createBasicResponse(this.doc, GET_ GSLIB_ELEMENTS_FROM_FILE);341 protected Element processGetTemplateListFromFile(Element request) 342 { 343 Element result = GSXML.createBasicResponse(this.doc, GET_TEMPLATE_LIST_FROM_FILE); 329 344 330 345 if (request == null) 331 346 { 332 GSXML.addError(this.doc, result, GET_ GSLIB_ELEMENTS_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);347 GSXML.addError(this.doc, result, GET_TEMPLATE_LIST_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX); 333 348 return result; 334 349 } … … 355 370 if (param_list == null) 356 371 { 357 GSXML.addError(this.doc, result, RETRIEVE_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);372 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX); 358 373 return result; 359 374 } … … 361 376 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false); 362 377 378 String locationName = (String) params.get("locationName"); 379 String siteName = (String) params.get("siteName"); 380 String collectionName = (String) params.get("collectionName"); 363 381 String interfaceName = (String) params.get("interfaceName"); 364 365 String filePath = GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform" + File.separator + "gslib.xsl"; 366 File xslFile = new File(filePath); 382 String fileName = (String) params.get("fileName"); 383 384 fileName.replace("/", File.separator); 385 386 if (locationName == null || locationName.length() == 0) 387 { 388 GSXML.addError(this.doc, result, "Parameter \"locationName\" was null or empty."); 389 return result; 390 } 391 392 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName); 367 393 368 394 if (xslFile.exists()) … … 372 398 373 399 Element templateList = this.doc.createElement("templateList"); 374 String templateListString = "["; 375 376 NodeList templateElems = xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template"); 377 for (int i = 0; i < templateElems.getLength(); i++) 378 { 379 Element currentElem = (Element) templateElems.item(i); 400 StringBuilder templateListString = new StringBuilder("["); 401 402 NodeList xslTemplateElems = xslDoc.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template"); 403 NodeList gsfTemplateElems = xslDoc.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "template"); 404 for (int i = 0; i < xslTemplateElems.getLength() + gsfTemplateElems.getLength(); i++) 405 { 406 Element currentElem = (i < xslTemplateElems.getLength()) ? (Element) xslTemplateElems.item(i) : (Element) gsfTemplateElems.item(i - xslTemplateElems.getLength()); 407 if (!currentElem.hasAttribute(GSXML.NAME_ATT) && !currentElem.hasAttribute(GSXML.MATCH_ATT)) 408 { 409 continue; 410 } 411 412 templateListString.append("{"); 413 templateListString.append("\"namespace\":\"" + ((i < xslTemplateElems.getLength()) ? "xsl" : "gsf") + "\","); 380 414 if (currentElem.hasAttribute(GSXML.NAME_ATT)) 381 415 { 382 templateListString += "\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\""; 383 if (i < templateElems.getLength() - 1) 384 { 385 templateListString += ","; 386 } 387 } 388 } 389 390 templateListString += "]"; 391 392 templateList.setTextContent(templateListString); 416 templateListString.append("\"name\":\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\","); 417 } 418 else if (currentElem.hasAttribute(GSXML.MATCH_ATT)) 419 { 420 templateListString.append("\"match\":\"" + currentElem.getAttribute(GSXML.MATCH_ATT) + "\","); 421 } 422 templateListString.deleteCharAt(templateListString.length() - 1); 423 templateListString.append("},"); 424 } 425 426 templateListString.deleteCharAt(templateListString.length() - 1); 427 templateListString.append("]"); 428 429 templateList.setTextContent(templateListString.toString()); 393 430 result.appendChild(templateList); 394 431 } 432 else 433 { 434 GSXML.addError(this.doc, result, "File: " + xslFile.getAbsolutePath() + " does not exist"); 435 return result; 436 } 395 437 396 438 return result; 397 439 } 440 441 protected Element processGetXSLTFilesForCollection(Element request) 442 { 443 Element result = GSXML.createBasicResponse(this.doc, GET_XSLT_FILES_FOR_COLLECTION); 444 445 if (request == null) 446 { 447 GSXML.addError(this.doc, result, GET_XSLT_FILES_FOR_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX); 448 return result; 449 } 450 451 UserContext context = new UserContext(request); 452 boolean found = false; 453 for (String group : context.getGroups()) 454 { 455 if (group.equals("administrator")) 456 { 457 found = true; 458 } 459 } 460 461 if (!found) 462 { 463 GSXML.addError(this.doc, result, "This user does not have the required permissions to perform this action."); 464 return result; 465 } 466 467 // Get the parameters of the request 468 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER); 469 470 if (param_list == null) 471 { 472 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX); 473 return result; 474 } 475 476 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false); 477 478 String interfaceName = (String) params.get("interfaceName"); 479 String siteName = (String) params.get("siteName"); 480 String collectionName = (String) params.get("collectionName"); 481 482 Element fileList = this.doc.createElement("fileListJSON"); 483 StringBuilder fileListString = new StringBuilder("["); 484 485 String[] placesToCheck = new String[] { GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform", //INTERFACE FILE PATH 486 GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "transform", //SITE FILE PATH 487 GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "transform", //COLLECTION FILE PATH 488 GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "etc" //COLLECTION CONFIG FILE PATH 489 }; 490 491 String[] shortPlaceNames = new String[] { "interface", "site", "collection", "collectionConfig" }; 492 493 for (int i = 0; i < placesToCheck.length; i++) 494 { 495 ArrayList<File> xslFiles = getXSLTFilesFromDirectoryRecursive(new File(placesToCheck[i])); 496 for (File f : xslFiles) 497 { 498 fileListString.append("{\"location\":\"" + shortPlaceNames[i] + "\",\"path\":\"" + f.getAbsolutePath().replace(placesToCheck[i] + File.separator, "") + "\"},"); 499 } 500 } 501 fileListString.deleteCharAt(fileListString.length() - 1); //Remove the last , character 502 fileListString.append("]"); 503 504 fileList.setTextContent(fileListString.toString()); 505 result.appendChild(fileList); 506 507 return result; 508 } 509 510 protected File createFileFromLocation(String location, String filename, String interfaceName, String siteName, String collectionName) 511 { 512 String filePath = ""; 513 if (location.equals("interface")) 514 { 515 filePath = GlobalProperties.getGSDL3Home() + File.separator + "interfaces" + File.separator + interfaceName + File.separator + "transform" + File.separator + filename; 516 } 517 else if (location.equals("site")) 518 { 519 filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "transform" + File.separator + filename; 520 } 521 else if (location.equals("collection")) 522 { 523 filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "transform" + File.separator + filename; 524 } 525 else if (location.equals("collectionConfig")) 526 { 527 filePath = GlobalProperties.getGSDL3Home() + File.separator + "sites" + File.separator + siteName + File.separator + "collect" + File.separator + collectionName + File.separator + "etc" + File.separator + filename; 528 } 529 530 return new File(filePath); 531 } 532 533 protected ArrayList<File> getXSLTFilesFromDirectoryRecursive(File dir) 534 { 535 ArrayList<File> result = new ArrayList<File>(); 536 537 if (dir != null && dir.exists() && dir.isDirectory()) 538 { 539 for (File f : dir.listFiles()) 540 { 541 if (f.isDirectory()) 542 { 543 result.addAll(getXSLTFilesFromDirectoryRecursive(f)); 544 } 545 else if (f.getName().endsWith(".xsl") || f.getName().endsWith(".xml")) 546 { 547 result.add(f); 548 } 549 } 550 } 551 552 return result; 553 } 398 554 }
Note:
See TracChangeset
for help on using the changeset viewer.