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.*;
|
---|
21 | import org.xml.sax.*;
|
---|
22 | import javax.xml.parsers.SAXParserFactory;
|
---|
23 | import javax.xml.parsers.ParserConfigurationException;
|
---|
24 | import javax.xml.parsers.SAXParser;
|
---|
25 |
|
---|
26 | import java.io.PrintWriter;
|
---|
27 | import java.io.StringWriter;
|
---|
28 |
|
---|
29 | import org.apache.log4j.*;
|
---|
30 |
|
---|
31 | public class SystemAction extends Action {
|
---|
32 |
|
---|
33 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.SystemAction.class.getName());
|
---|
34 |
|
---|
35 | String tempVal = "";
|
---|
36 |
|
---|
37 | /** process a request */
|
---|
38 | public Node process (Node message_node) {
|
---|
39 |
|
---|
40 | Element message = this.converter.nodeToElement(message_node);
|
---|
41 |
|
---|
42 | // assume only one request
|
---|
43 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
44 |
|
---|
45 | String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
46 | String lang = request.getAttribute(GSXML.LANG_ATT);
|
---|
47 | String uid = request.getAttribute(GSXML.USER_ID_ATT);
|
---|
48 | // get the param list
|
---|
49 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
50 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
51 |
|
---|
52 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
53 |
|
---|
54 | String coll = (String)params.get(GSParams.SYSTEM_CLUSTER);
|
---|
55 |
|
---|
56 | String to = "";
|
---|
57 | if (coll!=null && !coll.equals("")) {
|
---|
58 | to = coll;
|
---|
59 | }
|
---|
60 |
|
---|
61 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
62 | Element mr_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_SYSTEM, to, lang, uid);
|
---|
63 | mr_request_message.appendChild(mr_request);
|
---|
64 |
|
---|
65 | Element system = this.doc.createElement(GSXML.SYSTEM_ELEM);
|
---|
66 | mr_request.appendChild(system);
|
---|
67 |
|
---|
68 | // will need to change the following if can do more than one system request at once
|
---|
69 | if (subaction.equals("c")) { // configure
|
---|
70 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_CONFIGURE);
|
---|
71 | String info = (String)params.get(GSParams.SYSTEM_SUBSET);
|
---|
72 | system.setAttribute(GSXML.SYSTEM_SUBSET_ATT, info);
|
---|
73 | }
|
---|
74 | else if(subaction.equals("s")) { // save format statement
|
---|
75 | logger.error("Initiate save");
|
---|
76 | String format_string = (String)params.get("data");
|
---|
77 | logger.error("data="+format_string);
|
---|
78 |
|
---|
79 | //SamParser sam = new SamParser();
|
---|
80 | //String format_statement = sam.parse(input);
|
---|
81 | //logger.error("format string="+format_statement);
|
---|
82 |
|
---|
83 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
84 |
|
---|
85 | Iterator it = params.keySet().iterator();
|
---|
86 | while(it.hasNext())
|
---|
87 | {
|
---|
88 | logger.error("Param: "+it.next());
|
---|
89 | }
|
---|
90 |
|
---|
91 | //Node text = this.doc.createTextNode(format_string);
|
---|
92 | //page_response.appendChild(text);
|
---|
93 |
|
---|
94 |
|
---|
95 | try {
|
---|
96 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
---|
97 | DocumentBuilder builder = factory.newDocumentBuilder();
|
---|
98 | //String input = "<html><head><title></title></head><body>" + format_string + "</body></html>";
|
---|
99 | String input = format_string;
|
---|
100 | InputSource is = new InputSource( new StringReader( input ) );
|
---|
101 | Document d = builder.parse( is );
|
---|
102 | Element e = d.getDocumentElement();
|
---|
103 |
|
---|
104 | page_response.appendChild(this.doc.importNode(e, true));
|
---|
105 | }
|
---|
106 | catch( Exception ex ) {
|
---|
107 | logger.error("There was an exception "+ex);
|
---|
108 |
|
---|
109 | StringWriter sw = new StringWriter();
|
---|
110 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
111 | ex.printStackTrace(pw);
|
---|
112 | pw.flush();
|
---|
113 | sw.flush();
|
---|
114 | logger.error(sw.toString());
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | //Element child = this.doc.createElement("div"); //format_string);
|
---|
119 | //Node text = this.doc.createTextNode(format_string); //"<h1>Hi there and greetings!</h1>");
|
---|
120 | //child.innerHTML = "<h1>Hi there and greetings!</h1>";
|
---|
121 | //child.setNodeValue(format_string);
|
---|
122 | //child.appendChild(text);
|
---|
123 | result.appendChild(page_response);
|
---|
124 | return result;
|
---|
125 | } else {
|
---|
126 | String name = (String)params.get(GSParams.SYSTEM_MODULE_NAME);
|
---|
127 | String type = (String)params.get(GSParams.SYSTEM_MODULE_TYPE);
|
---|
128 |
|
---|
129 | system.setAttribute(GSXML.SYSTEM_MODULE_NAME_ATT, name);
|
---|
130 | system.setAttribute(GSXML.SYSTEM_MODULE_TYPE_ATT, type);
|
---|
131 |
|
---|
132 | if (subaction.equals("d")) { // delete
|
---|
133 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_DEACTIVATE);
|
---|
134 |
|
---|
135 | } else if (subaction.equals("a")) { // add
|
---|
136 | system.setAttribute(GSXML.TYPE_ATT, GSXML.SYSTEM_TYPE_ACTIVATE);
|
---|
137 | } else {
|
---|
138 | // create the default response
|
---|
139 | // for now just have an error
|
---|
140 | logger.error("bad subaction type");
|
---|
141 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
142 | result.appendChild(page_response);
|
---|
143 |
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | Node response_message = this.mr.process(mr_request_message);
|
---|
149 |
|
---|
150 | result.appendChild(GSXML.duplicateWithNewName(this.doc, (Element)GSXML.getChildByTagName(response_message, GSXML.RESPONSE_ELEM), GSXML.RESPONSE_ELEM, true));
|
---|
151 | return result;
|
---|
152 |
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*
|
---|
156 | public void parse (String message) {
|
---|
157 | //get a factory
|
---|
158 | SAXParserFactory spf = SAXParserFactory.newInstance();
|
---|
159 | try {
|
---|
160 |
|
---|
161 | //get a new instance of parser
|
---|
162 | SAXParser sp = spf.newSAXParser();
|
---|
163 | InputSource is = new InputSource( new StringReader( message ) );
|
---|
164 | //parse the file and also register this class for call backs
|
---|
165 | sp.parse(is, new SamParser());
|
---|
166 |
|
---|
167 | }catch(SAXException se) {
|
---|
168 | se.printStackTrace();
|
---|
169 | }catch(ParserConfigurationException pce) {
|
---|
170 | pce.printStackTrace();
|
---|
171 | }catch (IOException ie) {
|
---|
172 | ie.printStackTrace();
|
---|
173 | }
|
---|
174 | }
|
---|
175 | */
|
---|
176 |
|
---|
177 | //Event Handlers
|
---|
178 | /* public void startElement(String uri, String localName, String qName,
|
---|
179 | Attributes attributes) throws SAXException {
|
---|
180 | //reset
|
---|
181 | logger.error("Start Element: "+qName);
|
---|
182 | //if(qName.equalsIgnoreCase("Template")) {
|
---|
183 | //create a new instance of employee
|
---|
184 | //tempEmp = new Employee();
|
---|
185 | //tempEmp.setType(attributes.getValue("type"));
|
---|
186 |
|
---|
187 | //}
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | public void characters(char[] ch, int start, int length) throws SAXException {
|
---|
192 | tempVal = new String(ch,start,length);
|
---|
193 | }
|
---|
194 |
|
---|
195 | public void endElement(String uri, String localName,
|
---|
196 | String qName) throws SAXException {
|
---|
197 |
|
---|
198 | logger.error("Characters: "+tempVal);
|
---|
199 | logger.error("End Element: "+qName);
|
---|
200 | /*
|
---|
201 | if(qName.equalsIgnoreCase("Employee")) {
|
---|
202 | //add it to the list
|
---|
203 | myEmpls.add(tempEmp);
|
---|
204 |
|
---|
205 | }else if (qName.equalsIgnoreCase("Name")) {
|
---|
206 | tempEmp.setName(tempVal);
|
---|
207 | }else if (qName.equalsIgnoreCase("Id")) {
|
---|
208 | tempEmp.setId(Integer.parseInt(tempVal));
|
---|
209 | }else if (qName.equalsIgnoreCase("Age")) {
|
---|
210 | tempEmp.setAge(Integer.parseInt(tempVal));
|
---|
211 | } */
|
---|
212 |
|
---|
213 | //}
|
---|
214 |
|
---|
215 | }
|
---|