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