1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
4 | import org.greenstone.gsdl3.util.*;
|
---|
5 |
|
---|
6 | // XML classes
|
---|
7 | import org.w3c.dom.Node;
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 | import org.w3c.dom.Document;
|
---|
10 |
|
---|
11 | // other java stuff
|
---|
12 | import java.io.File;
|
---|
13 | import java.util.HashMap;
|
---|
14 | import java.util.*;
|
---|
15 | import java.io.StringReader;
|
---|
16 | import org.xml.sax.InputSource;
|
---|
17 | import javax.xml.parsers.DocumentBuilder;
|
---|
18 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
19 |
|
---|
20 | import java.io.PrintWriter;
|
---|
21 | import java.io.StringWriter;
|
---|
22 |
|
---|
23 | import org.apache.log4j.*;
|
---|
24 |
|
---|
25 | public class SystemAction extends Action {
|
---|
26 |
|
---|
27 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.SystemAction.class.getName());
|
---|
28 |
|
---|
29 | /** process a request */
|
---|
30 | public Node process (Node message_node) {
|
---|
31 |
|
---|
32 | Element message = this.converter.nodeToElement(message_node);
|
---|
33 |
|
---|
34 | // assume only one request
|
---|
35 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
36 |
|
---|
37 | String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
38 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
39 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
40 | // get the param list
|
---|
41 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
42 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
43 |
|
---|
44 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
45 |
|
---|
46 | String coll = (String)params.get(GSParams.SYSTEM_CLUSTER);
|
---|
47 |
|
---|
48 | String to = "";
|
---|
49 | if (coll!=null && !coll.equals("")) {
|
---|
50 | to = coll;
|
---|
51 | }
|
---|
52 |
|
---|
53 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
54 | Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SYSTEM, to, lang, uid);
|
---|
55 | mr_request_message.appendChild(mr_request);
|
---|
56 |
|
---|
57 | Element system = this.doc.createElement(GSXML.SYSTEM_ELEM);
|
---|
58 | mr_request.appendChild(system);
|
---|
59 |
|
---|
60 | // will need to change the following if can do more than one system request at once
|
---|
61 | if (subaction.equals("c")) { // configure
|
---|
62 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_CONFIGURE);
|
---|
63 | String info = (String)params.get(GSParams.SYSTEM_SUBSET);
|
---|
64 | system.setAttribute(GSXML.SYSTEM_SUBSET_ATT, info);
|
---|
65 | }
|
---|
66 | else if(subaction.equals("s")) { // save format statement
|
---|
67 | logger.error("Initiate save");
|
---|
68 | String format_string = (String)params.get("data");
|
---|
69 | logger.error("data="+format_string);
|
---|
70 | Iterator it = params.keySet().iterator();
|
---|
71 | while(it.hasNext())
|
---|
72 | {
|
---|
73 | logger.error("Param: "+it.next());
|
---|
74 | }
|
---|
75 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
76 |
|
---|
77 | try {
|
---|
78 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
79 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
80 | String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
|
---|
81 | InputSource is = new InputSource( new StringReader( input ) );
|
---|
82 | Document d = builder.parse( is );
|
---|
83 | page_response.appendChild(d);
|
---|
84 | }
|
---|
85 | catch( Exception ex ) {
|
---|
86 | logger.error("There was an exception "+ex);
|
---|
87 |
|
---|
88 | StringWriter sw = new StringWriter();
|
---|
89 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
90 | ex.printStackTrace(pw);
|
---|
91 | pw.flush();
|
---|
92 | sw.flush();
|
---|
93 | logger.error(sw.toString());
|
---|
94 | }
|
---|
95 |
|
---|
96 | //Element child = this.doc.createElement("div"); //format_string);
|
---|
97 | //Node text = this.doc.createTextNode(format_string); //"<h1>Hi there and greetings!</h1>");
|
---|
98 | //child.innerHTML = "<h1>Hi there and greetings!</h1>";
|
---|
99 | //child.setNodeValue(format_string);
|
---|
100 | //child.appendChild(text);
|
---|
101 | result.appendChild(page_response);
|
---|
102 | return result;
|
---|
103 | } else {
|
---|
104 | String name = (String)params.get(GSParams.SYSTEM_MODULE_NAME);
|
---|
105 | String type = (String)params.get(GSParams.SYSTEM_MODULE_TYPE);
|
---|
106 |
|
---|
107 | system.setAttribute(GSXML.SYSTEM_MODULE_NAME_ATT, name);
|
---|
108 | system.setAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT, type);
|
---|
109 |
|
---|
110 | if (subaction.equals("d")) { // delete
|
---|
111 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_DEACTIVATE);
|
---|
112 |
|
---|
113 | } else if (subaction.equals("a")) { // add
|
---|
114 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_ACTIVATE);
|
---|
115 | } else {
|
---|
116 | // create the default response
|
---|
117 | // for now just have an error
|
---|
118 | logger.error("bad subaction type");
|
---|
119 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
120 | result.appendChild(page_response);
|
---|
121 |
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | Node response_message = this.mr.process(mr_request_message);
|
---|
127 |
|
---|
128 | result.appendChild(GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true));
|
---|
129 | return result;
|
---|
130 |
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|