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