source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/DebugService.java@ 27109

Last change on this file since 27109 was 27109, checked in by sjm84, 11 years ago

Major change to how the files are located. Also added the functionality to retreive all xsl files available to a collection

  • Property svn:executable set to *
File size: 19.5 KB
Line 
1package org.greenstone.gsdl3.service;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.Serializable;
6import java.util.ArrayList;
7import java.util.HashMap;
8
9import javax.xml.transform.Transformer;
10import javax.xml.transform.TransformerFactory;
11import javax.xml.transform.dom.DOMSource;
12import javax.xml.transform.stream.StreamResult;
13
14import org.apache.log4j.Logger;
15import org.greenstone.gsdl3.util.GSXML;
16import org.greenstone.gsdl3.util.UserContext;
17import org.greenstone.gsdl3.util.XMLConverter;
18import org.greenstone.util.GlobalProperties;
19import org.w3c.dom.Document;
20import org.w3c.dom.Element;
21import org.w3c.dom.NamedNodeMap;
22import org.w3c.dom.Node;
23import org.w3c.dom.NodeList;
24
25public class DebugService extends ServiceRack
26{
27 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.DebugService.class.getName());
28
29 /**********************************************************
30 * The list of services the utility service rack supports *
31 *********************************************************/
32 protected static final String GET_TEMPLATE_FROM_XML_FILE = "GetXMLTemplateFromFile";
33 protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile";
34 protected static final String GET_TEMPLATE_LIST_FROM_FILE = "GetTemplateListFromFile";
35 protected static final String GET_XSLT_FILES_FOR_COLLECTION = "GetXSLTFilesForCollection";
36 /*********************************************************/
37
38 String[] services = { GET_TEMPLATE_FROM_XML_FILE, SAVE_TEMPLATE_TO_XML_FILE, GET_TEMPLATE_LIST_FROM_FILE, GET_XSLT_FILES_FOR_COLLECTION };
39
40 public boolean configure(Element info, Element extra_info)
41 {
42 if (!super.configure(info, extra_info))
43 {
44 return false;
45 }
46
47 logger.info("Configuring DebugServices...");
48 this.config_info = info;
49
50 for (int i = 0; i < services.length; i++)
51 {
52 Element service = this.doc.createElement(GSXML.SERVICE_ELEM);
53 service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
54 service.setAttribute(GSXML.NAME_ATT, services[i]);
55 this.short_service_info.appendChild(service);
56 }
57
58 return true;
59 }
60
61 protected Element getServiceDescription(String service_id, String lang, String subset)
62 {
63 for (int i = 0; i < services.length; i++)
64 {
65 if (service_id.equals(services[i]))
66 {
67 Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
68 service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
69 service_elem.setAttribute(GSXML.NAME_ATT, services[i]);
70 return service_elem;
71 }
72 }
73
74 return null;
75 }
76
77 protected Element processGetXMLTemplateFromFile(Element request)
78 {
79 Element result = GSXML.createBasicResponse(this.doc, GET_TEMPLATE_FROM_XML_FILE);
80
81 if (request == null)
82 {
83 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
84 return result;
85 }
86
87 UserContext context = new UserContext(request);
88 boolean found = false;
89 for (String group : context.getGroups())
90 {
91 if (group.equals("administrator"))
92 {
93 found = true;
94 }
95 }
96
97 if (!found)
98 {
99 GSXML.addError(this.doc, result, "This user does not have the required permissions to perform this action.");
100 return result;
101 }
102
103 // Get the parameters of the request
104 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
105
106 if (param_list == null)
107 {
108 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
109 return result;
110 }
111
112 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
113
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");
119 String namespace = (String) params.get("namespace");
120 String nodeName = (String) params.get("nodename");
121 String nameToGet = (String) params.get("name");
122 String matchToGet = (String) params.get("match");
123
124 String fullNamespace;
125 if (namespace.toLowerCase().equals("gsf"))
126 {
127 fullNamespace = GSXML.GSF_NAMESPACE;
128 }
129 else if (namespace.toLowerCase().equals("xsl"))
130 {
131 fullNamespace = GSXML.XSL_NAMESPACE;
132 }
133 else
134 {
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);
140 if (xslFile.exists())
141 {
142 XMLConverter converter = new XMLConverter();
143 Document xslDoc = converter.getDOM(xslFile, "UTF-8");
144
145 NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
146
147 if (nameToGet != null && nameToGet.length() != 0)
148 {
149 for (int i = 0; i < templateElems.getLength(); i++)
150 {
151 Element template = (Element) templateElems.item(i);
152 if (template.getAttribute("name").equals(nameToGet))
153 {
154 fixAttributes(template);
155
156 Element requestedTemplate = this.doc.createElement("requestedNameTemplate");
157 requestedTemplate.appendChild(this.doc.importNode(template, true));
158 result.appendChild(requestedTemplate);
159 }
160 }
161 }
162
163 //Maybe should look for highest priority
164 if (matchToGet != null && matchToGet.length() != 0)
165 {
166 for (int i = 0; i < templateElems.getLength(); i++)
167 {
168 Element template = (Element) templateElems.item(i);
169 if (template.getAttribute("match").equals(matchToGet))
170 {
171 fixAttributes(template);
172
173 Element requestedTemplate = this.doc.createElement("requestedMatchTemplate");
174 requestedTemplate.appendChild(this.doc.importNode(template, true));
175 result.appendChild(requestedTemplate);
176 }
177 }
178 }
179 }
180
181 return result;
182 }
183
184 protected void fixAttributes(Element template)
185 {
186 NodeList nodes = template.getElementsByTagName("*");
187 for (int j = 0; j < nodes.getLength(); j++)
188 {
189 Node current = nodes.item(j);
190 NamedNodeMap attributes = current.getAttributes();
191 for (int k = 0; k < attributes.getLength(); k++)
192 {
193 Node currentAttr = attributes.item(k);
194 String value = currentAttr.getNodeValue();
195 if (value.contains("&") || value.contains("<") || value.contains(">") || value.contains("\""))
196 {
197 currentAttr.setNodeValue(value.replace("&", "&amp;amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"));
198 }
199 }
200 }
201 }
202
203 protected Element processSaveXMLTemplateToFile(Element request)
204 {
205 Element result = GSXML.createBasicResponse(this.doc, SAVE_TEMPLATE_TO_XML_FILE);
206
207 if (request == null)
208 {
209 GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
210 return result;
211 }
212
213 UserContext context = new UserContext(request);
214 boolean foundGroup = false;
215 for (String group : context.getGroups())
216 {
217 if (group.equals("administrator"))
218 {
219 foundGroup = true;
220 }
221 }
222
223 if (!foundGroup)
224 {
225 GSXML.addError(this.doc, result, "This user does not have the required permissions to perform this action.");
226 return result;
227 }
228
229 // Get the parameters of the request
230 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
231
232 if (param_list == null)
233 {
234 GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
235 return result;
236 }
237
238 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
239
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");
245 String namespace = (String) params.get("namespace");
246 String nodeName = (String) params.get("nodename");
247 String nameToSave = (String) params.get("name");
248 String matchToSave = (String) params.get("match");
249 String xml = (String) params.get("xml");
250
251 String fullNamespace;
252 if (namespace.toLowerCase().equals("gsf"))
253 {
254 fullNamespace = GSXML.GSF_NAMESPACE;
255 }
256 else if (namespace.toLowerCase().equals("xsl"))
257 {
258 fullNamespace = GSXML.XSL_NAMESPACE;
259 }
260 else
261 {
262 GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": The specified namespace was not valid", GSXML.ERROR_TYPE_SYNTAX);
263 return result;
264 }
265
266 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
267 if (xslFile.exists())
268 {
269 XMLConverter converter = new XMLConverter();
270 Document xslDoc = converter.getDOM(xslFile, "UTF-8");
271
272 NodeList templateElems = xslDoc.getElementsByTagNameNS(fullNamespace, nodeName);
273
274 boolean found = false;
275 if (nameToSave != null && nameToSave.length() != 0)
276 {
277 for (int i = 0; i < templateElems.getLength(); i++)
278 {
279 Element template = (Element) templateElems.item(i);
280 if (template.getAttribute("name").equals(nameToSave))
281 {
282 try
283 {
284 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);
285 template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
286 found = true;
287 }
288 catch (Exception ex)
289 {
290 ex.printStackTrace();
291 }
292 }
293 }
294 }
295 //Maybe should look for highest priority match
296 if (matchToSave != null && matchToSave.length() != 0)
297 {
298 for (int i = 0; i < templateElems.getLength(); i++)
299 {
300 Element template = (Element) templateElems.item(i);
301 if (template.getAttribute("match").equals(matchToSave))
302 {
303 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);
304 template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
305 found = true;
306 }
307 }
308 }
309
310 if (!found)
311 {
312 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);
313 }
314 else
315 {
316
317 try
318 {
319 Transformer transformer = TransformerFactory.newInstance().newTransformer();
320
321 //initialize StreamResult with File object to save to file
322 StreamResult sresult = new StreamResult(new FileWriter(xslFile));
323 DOMSource source = new DOMSource(xslDoc);
324 transformer.transform(source, sresult);
325 }
326 catch (Exception ex)
327 {
328 GSXML.addError(this.doc, result, SAVE_TEMPLATE_TO_XML_FILE + ": There was an error writing out the XML file", GSXML.ERROR_TYPE_SYNTAX);
329 }
330
331 }
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 }
337
338 return result;
339 }
340
341 protected Element processGetTemplateListFromFile(Element request)
342 {
343 Element result = GSXML.createBasicResponse(this.doc, GET_TEMPLATE_LIST_FROM_FILE);
344
345 if (request == null)
346 {
347 GSXML.addError(this.doc, result, GET_TEMPLATE_LIST_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
348 return result;
349 }
350
351 UserContext context = new UserContext(request);
352 boolean found = false;
353 for (String group : context.getGroups())
354 {
355 if (group.equals("administrator"))
356 {
357 found = true;
358 }
359 }
360
361 if (!found)
362 {
363 GSXML.addError(this.doc, result, "This user does not have the required permissions to perform this action.");
364 return result;
365 }
366
367 // Get the parameters of the request
368 Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
369
370 if (param_list == null)
371 {
372 GSXML.addError(this.doc, result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
373 return result;
374 }
375
376 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
377
378 String locationName = (String) params.get("locationName");
379 String siteName = (String) params.get("siteName");
380 String collectionName = (String) params.get("collectionName");
381 String interfaceName = (String) params.get("interfaceName");
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);
393
394 if (xslFile.exists())
395 {
396 XMLConverter converter = new XMLConverter();
397 Document xslDoc = converter.getDOM(xslFile, "UTF-8");
398
399 Element templateList = this.doc.createElement("templateList");
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") + "\",");
414 if (currentElem.hasAttribute(GSXML.NAME_ATT))
415 {
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());
430 result.appendChild(templateList);
431 }
432 else
433 {
434 GSXML.addError(this.doc, result, "File: " + xslFile.getAbsolutePath() + " does not exist");
435 return result;
436 }
437
438 return result;
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 }
554}
Note: See TracBrowser for help on using the repository browser.