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