1 | package org.greenstone.gsdl3.action;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.Serializable;
|
---|
5 | import java.util.HashMap;
|
---|
6 |
|
---|
7 | import javax.xml.parsers.DocumentBuilder;
|
---|
8 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
9 | import javax.xml.transform.Result;
|
---|
10 | import javax.xml.transform.Transformer;
|
---|
11 | import javax.xml.transform.TransformerFactory;
|
---|
12 | import javax.xml.transform.dom.DOMSource;
|
---|
13 | import javax.xml.transform.stream.StreamResult;
|
---|
14 |
|
---|
15 | import org.greenstone.gsdl3.util.GSFile;
|
---|
16 | import org.greenstone.gsdl3.util.GSParams;
|
---|
17 | import org.greenstone.gsdl3.util.GSPath;
|
---|
18 | import org.greenstone.gsdl3.util.GSXML;
|
---|
19 | import org.greenstone.gsdl3.util.UserContext;
|
---|
20 | import org.greenstone.util.GlobalProperties;
|
---|
21 | import org.w3c.dom.Document;
|
---|
22 | import org.w3c.dom.Element;
|
---|
23 | import org.w3c.dom.Node;
|
---|
24 | import org.w3c.dom.NodeList;
|
---|
25 |
|
---|
26 | public class GeneralAction extends Action
|
---|
27 | {
|
---|
28 |
|
---|
29 | /** process a request */
|
---|
30 | public Node process(Node message_node)
|
---|
31 | {
|
---|
32 | Element message = this.converter.nodeToElement(message_node);
|
---|
33 |
|
---|
34 | // the result
|
---|
35 | Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
36 | Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
37 | result.appendChild(page_response);
|
---|
38 |
|
---|
39 | // assume only one request
|
---|
40 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
41 | logger.debug(" request=" + this.converter.getString(request));
|
---|
42 |
|
---|
43 | UserContext userContext = new UserContext(request);
|
---|
44 |
|
---|
45 | // get the param list
|
---|
46 | Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
47 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_param_list, false);
|
---|
48 |
|
---|
49 | if (params.get("configChangeName") != null && params.get("configChangeValue") != null)
|
---|
50 | {
|
---|
51 | String optionName = (String) params.get("configChangeName");
|
---|
52 | String optionValue = (String) params.get("configChangeValue");
|
---|
53 |
|
---|
54 | changeConfig(optionName, optionValue);
|
---|
55 | }
|
---|
56 |
|
---|
57 | String service_name = (String) params.get(GSParams.SERVICE);
|
---|
58 | String cluster_name = (String) params.get(GSParams.CLUSTER);
|
---|
59 | String response_only_p = (String) params.get(GSParams.RESPONSE_ONLY);
|
---|
60 | boolean response_only = false;
|
---|
61 | if (response_only_p != null)
|
---|
62 | {
|
---|
63 | response_only = (response_only_p.equals("1") ? true : false);
|
---|
64 | }
|
---|
65 | String request_type = (String) params.get(GSParams.REQUEST_TYPE);
|
---|
66 | // what is carried out depends on the request_type
|
---|
67 | // if rt=d, then a describe request is done,
|
---|
68 | // is rt=r, a request and then a describe request is done
|
---|
69 | // if rt=s, a status request is done.
|
---|
70 |
|
---|
71 | // if ro=1, then this calls for a process only page - we do the request
|
---|
72 | // (rt should be r or s) and just give the response straight back
|
---|
73 | // without any page processing
|
---|
74 |
|
---|
75 | // where to send requests
|
---|
76 | String to;
|
---|
77 | if (cluster_name != null)
|
---|
78 | {
|
---|
79 | to = GSPath.appendLink(cluster_name, service_name);
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | to = service_name;
|
---|
84 | }
|
---|
85 |
|
---|
86 | if (request_type.equals("r") || request_type.equals("s") || request_type.equals("ro"))
|
---|
87 | {
|
---|
88 | //do the request
|
---|
89 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
90 | Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
91 |
|
---|
92 | if (request_type.equals("s"))
|
---|
93 | {
|
---|
94 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
|
---|
95 | }
|
---|
96 |
|
---|
97 | mr_query_message.appendChild(mr_query_request);
|
---|
98 |
|
---|
99 | Element param_list = null;
|
---|
100 | // add in the service params - except the ones only used by the action
|
---|
101 | HashMap service_params = (HashMap) params.get("s1");
|
---|
102 | if (service_params != null)
|
---|
103 | {
|
---|
104 | param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
105 | GSXML.addParametersToList(this.doc, param_list, service_params);
|
---|
106 | mr_query_request.appendChild(param_list);
|
---|
107 | }
|
---|
108 |
|
---|
109 | Element userInformation = (Element) GSXML.getChildByTagName(request, GSXML.USER_INFORMATION_ELEM);
|
---|
110 | if (userInformation != null)
|
---|
111 | {
|
---|
112 | mr_query_request.appendChild(this.doc.importNode(userInformation, true));
|
---|
113 | }
|
---|
114 | mr_query_request.setAttribute("remoteAddress", request.getAttribute("remoteAddress"));
|
---|
115 |
|
---|
116 | Element mr_query_response = (Element) this.mr.process(mr_query_message);
|
---|
117 | Element result_response = (Element) GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
118 |
|
---|
119 | if (response_only)
|
---|
120 | {
|
---|
121 | // just send the reponse as is
|
---|
122 | addSiteMetadata(result_response, userContext);
|
---|
123 | addInterfaceOptions(result_response);
|
---|
124 | return result_response;
|
---|
125 | }
|
---|
126 | if (result_response != null)
|
---|
127 | {
|
---|
128 | // else append the contents of the response to the page
|
---|
129 | GSXML.copyAllChildren(page_response, result_response);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | // another part of the page is the service description
|
---|
134 |
|
---|
135 | // request the service info for the selected service - should be cached
|
---|
136 | Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
137 | Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
138 | mr_info_message.appendChild(mr_info_request);
|
---|
139 | Element mr_info_response = (Element) this.mr.process(mr_info_message);
|
---|
140 |
|
---|
141 | String path = GSXML.RESPONSE_ELEM;
|
---|
142 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
143 |
|
---|
144 | Node desNode = GSXML.getNodeByPath(mr_info_response, path);
|
---|
145 | if (desNode != null)
|
---|
146 | {
|
---|
147 | page_response.appendChild((Element) this.doc.importNode(desNode, true));
|
---|
148 | }
|
---|
149 |
|
---|
150 | addSiteMetadata(page_response, userContext);
|
---|
151 | addInterfaceOptions(page_response);
|
---|
152 |
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 |
|
---|
156 | protected void changeConfig(String optionName, String optionValue)
|
---|
157 | {
|
---|
158 | if (this.config_params.get(optionName) != null)
|
---|
159 | {
|
---|
160 | this.config_params.put(optionName, optionValue);
|
---|
161 |
|
---|
162 | File interfaceConfigFile = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String) this.config_params.get("interface_name"))));
|
---|
163 |
|
---|
164 | Document interfaceXML = null;
|
---|
165 | try
|
---|
166 | {
|
---|
167 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
---|
168 | DocumentBuilder db = dbf.newDocumentBuilder();
|
---|
169 | interfaceXML = db.parse(interfaceConfigFile);
|
---|
170 | Element topElement = interfaceXML.getDocumentElement();
|
---|
171 | Element optionListElem = (Element) GSXML.getChildByTagName(topElement, "optionList");
|
---|
172 |
|
---|
173 | NodeList optionList = optionListElem.getElementsByTagName("option");
|
---|
174 |
|
---|
175 | for (int i = 0; i < optionList.getLength(); i++)
|
---|
176 | {
|
---|
177 | Element currentOption = (Element) optionList.item(i);
|
---|
178 | if (currentOption.getAttribute(GSXML.NAME_ATT) != null && currentOption.getAttribute(GSXML.NAME_ATT).equals(optionName))
|
---|
179 | {
|
---|
180 | currentOption.setAttribute(GSXML.VALUE_ATT, optionValue);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | DOMSource source = new DOMSource(interfaceXML);
|
---|
185 | Result xmlresult = new StreamResult(interfaceConfigFile);
|
---|
186 |
|
---|
187 | Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
---|
188 | transformer.transform(source, xmlresult);
|
---|
189 | }
|
---|
190 | catch (Exception ex)
|
---|
191 | {
|
---|
192 | ex.printStackTrace();
|
---|
193 | }
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | logger.error("Could not set param \"" + optionName + "\" to \"" + optionValue + "\" because that option does not exist.");
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|