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

Last change on this file since 29421 was 29421, checked in by kjdon, 9 years ago

adding in debug stuff for a collection, eg so that can run greenbug. Now the user can be all-collections-editor or collname-collection-editor. They are more likely to be one of these and want to use greenbug than they are to be an administrator

  • Property svn:executable set to *
File size: 24.2 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.GSXSLT;
17import org.greenstone.gsdl3.util.UserContext;
18import org.greenstone.gsdl3.util.XMLConverter;
19import org.greenstone.util.GlobalProperties;
20import org.w3c.dom.Document;
21import org.w3c.dom.Element;
22import org.w3c.dom.NamedNodeMap;
23import org.w3c.dom.Node;
24import org.w3c.dom.NodeList;
25
26public 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 *********************************************************/
33 protected static final String GET_TEMPLATE_FROM_XML_FILE = "GetXMLTemplateFromFile";
34 protected static final String SAVE_TEMPLATE_TO_XML_FILE = "SaveXMLTemplateToFile";
35 protected static final String GET_TEMPLATE_LIST_FROM_FILE = "GetTemplateListFromFile";
36 protected static final String GET_XSLT_FILES_FOR_COLLECTION = "GetXSLTFilesForCollection";
37 protected static final String RESOLVE_CALL_TEMPLATE = "ResolveCallTemplate";
38 /*********************************************************/
39
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 };
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 {
54 Element service = this.desc_doc.createElement(GSXML.SERVICE_ELEM);
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
63 protected Element getServiceDescription(Document doc, String service_id, String lang, String subset)
64 {
65 for (int i = 0; i < services.length; i++)
66 {
67 if (service_id.equals(services[i]))
68 {
69 Element service_elem = doc.createElement(GSXML.SERVICE_ELEM);
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
79 protected Element processResolveCallTemplate(Element request)
80 {
81 Document result_doc = XMLConverter.newDOM();
82
83 Element result = GSXML.createBasicResponse(result_doc, RESOLVE_CALL_TEMPLATE);
84
85 if (request == null)
86 {
87 GSXML.addError(result, RESOLVE_CALL_TEMPLATE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
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 {
96 GSXML.addError(result, RESOLVE_CALL_TEMPLATE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
97 return result;
98 }
99
100 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
101
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 }
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
136 Element requestedTemplate = result_doc.createElement("requestedTemplate");
137 requestedTemplate.setTextContent(debugElement.getAttribute("filename"));
138 result.appendChild(requestedTemplate);
139 }
140 }
141
142 return result;
143 }
144
145 protected Element processGetXMLTemplateFromFile(Element request)
146 {
147 Document result_doc = XMLConverter.newDOM();
148 Element result = GSXML.createBasicResponse(result_doc, GET_TEMPLATE_FROM_XML_FILE);
149
150 if (request == null)
151 {
152 GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
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 {
161 GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
162 return result;
163 }
164
165 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
166
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
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");
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");
183 String xPath = (String) params.get("xpath");
184
185
186
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 {
198 GSXML.addError(result, "A valid namespace was not specified.");
199 return result;
200 }
201
202 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
203 if (xslFile.exists())
204 {
205 XMLConverter converter = new XMLConverter();
206 Document xslDoc = converter.getDOM(xslFile, "UTF-8");
207 Element rootElem = xslDoc.getDocumentElement();
208
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 }
233
234 NodeList templateElems = rootElem.getElementsByTagNameNS(fullNamespace, nodeName);
235
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 {
243 fixAttributes(template);
244
245 Element requestedTemplate = result_doc.createElement("requestedNameTemplate");
246 requestedTemplate.appendChild(result_doc.importNode(template, true));
247 result.appendChild(requestedTemplate);
248 }
249 }
250 }
251
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 {
260 fixAttributes(template);
261
262 Element requestedTemplate = result_doc.createElement("requestedMatchTemplate");
263 requestedTemplate.appendChild(result_doc.importNode(template, true));
264 result.appendChild(requestedTemplate);
265 }
266 }
267 }
268 }
269
270 return result;
271 }
272
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();
284 if (value.contains("&") || value.contains("<") || value.contains(">") || value.contains("\""))
285 {
286 currentAttr.setNodeValue(value.replace("&", "&amp;amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;"));
287 }
288 }
289 }
290 }
291
292 protected Element processSaveXMLTemplateToFile(Element request)
293 {
294 Document result_doc = XMLConverter.newDOM();
295 Element result = GSXML.createBasicResponse(result_doc, SAVE_TEMPLATE_TO_XML_FILE);
296
297 if (request == null)
298 {
299 GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
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 {
308 GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
309 return result;
310 }
311
312 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
313
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
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");
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");
329 String xPath = (String) params.get("xpath");
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 {
343 GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": The specified namespace was not valid", GSXML.ERROR_TYPE_SYNTAX);
344 return result;
345 }
346
347 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
348 if (xslFile.exists())
349 {
350 XMLConverter converter = new XMLConverter();
351 Document xslDoc = converter.getDOM(xslFile, "UTF-8");
352
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
380 NodeList templateElems = rootElem.getElementsByTagNameNS(fullNamespace, nodeName);
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 {
392 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);
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 {
411 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);
412 template.getParentNode().replaceChild(xslDoc.importNode(newTemplate, true), template);
413 found = true;
414 }
415 }
416 }
417
418 if (!found)
419 {
420 GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": Could not save as the specified template could not be found", GSXML.ERROR_TYPE_SYNTAX);
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 {
436 GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + ": There was an error writing out the XML file", GSXML.ERROR_TYPE_SYNTAX);
437 }
438
439 }
440 }
441 else
442 {
443 GSXML.addError(result, SAVE_TEMPLATE_TO_XML_FILE + "File: " + xslFile.getAbsolutePath() + " does not exist", GSXML.ERROR_TYPE_SYNTAX);
444 }
445
446 return result;
447 }
448
449 protected Element processGetTemplateListFromFile(Element request)
450 {
451 Document result_doc = XMLConverter.newDOM();
452 Element result = GSXML.createBasicResponse(result_doc, GET_TEMPLATE_LIST_FROM_FILE);
453
454 if (request == null)
455 {
456 GSXML.addError(result, GET_TEMPLATE_LIST_FROM_FILE + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
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 {
465 GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
466 return result;
467 }
468
469 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
470
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 }
477 String locationName = (String) params.get("locationName");
478 String siteName = (String) params.get("siteName");
479 String interfaceName = (String) params.get("interfaceName");
480 String fileName = (String) params.get("fileName");
481
482 fileName.replace("/", File.separator);
483
484 if (locationName == null || locationName.length() == 0)
485 {
486 GSXML.addError(result, "Parameter \"locationName\" was null or empty.");
487 return result;
488 }
489
490 File xslFile = createFileFromLocation(locationName, fileName, interfaceName, siteName, collectionName);
491
492 if (xslFile.exists())
493 {
494 XMLConverter converter = new XMLConverter();
495 Document xslDoc = converter.getDOM(xslFile, "UTF-8");
496 Element xslDocElem = xslDoc.getDocumentElement();
497
498 if (locationName.equals("collectionConfig"))
499 {
500 GSXSLT.modifyCollectionConfigForDebug(xslDocElem);
501 }
502
503 Element templateList = result_doc.createElement("templateList");
504 StringBuilder templateListString = new StringBuilder("[");
505
506 NodeList xslTemplateElems = xslDocElem.getElementsByTagNameNS(GSXML.XSL_NAMESPACE, "template");
507 NodeList gsfTemplateElems = xslDocElem.getElementsByTagNameNS(GSXML.GSF_NAMESPACE, "template");
508 for (int i = 0; i < xslTemplateElems.getLength() + gsfTemplateElems.getLength(); i++)
509 {
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") + "\",");
518 if (currentElem.hasAttribute(GSXML.NAME_ATT))
519 {
520 templateListString.append("\"name\":\"" + currentElem.getAttribute(GSXML.NAME_ATT) + "\",");
521 }
522 else if (currentElem.hasAttribute(GSXML.MATCH_ATT))
523 {
524 templateListString.append("\"match\":\"" + currentElem.getAttribute(GSXML.MATCH_ATT) + "\",");
525 }
526
527 if (currentElem.getUserData("xpath") != null)
528 {
529 templateListString.append("\"xpath\":\"" + currentElem.getUserData("xpath") + "\",");
530 }
531 templateListString.deleteCharAt(templateListString.length() - 1);
532 templateListString.append("},");
533 }
534
535 templateListString.deleteCharAt(templateListString.length() - 1);
536 templateListString.append("]");
537
538 templateList.setTextContent(templateListString.toString());
539 result.appendChild(templateList);
540 }
541 else
542 {
543 GSXML.addError(result, "File: " + xslFile.getAbsolutePath() + " does not exist");
544 return result;
545 }
546
547 return result;
548 }
549
550 protected Element processGetXSLTFilesForCollection(Element request)
551 {
552 Document result_doc = XMLConverter.newDOM();
553 Element result = GSXML.createBasicResponse(result_doc, GET_XSLT_FILES_FOR_COLLECTION);
554
555 if (request == null)
556 {
557 GSXML.addError(result, GET_XSLT_FILES_FOR_COLLECTION + ": Request is null", GSXML.ERROR_TYPE_SYNTAX);
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 {
566 GSXML.addError(result, GET_TEMPLATE_FROM_XML_FILE + ": No param list specified", GSXML.ERROR_TYPE_SYNTAX);
567 return result;
568 }
569
570 HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
571
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 }
578 String interfaceName = (String) params.get("interfaceName");
579 String siteName = (String) params.get("siteName");
580
581 Element fileList = result_doc.createElement("fileListJSON");
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 }
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
Note: See TracBrowser for help on using the repository browser.