source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/action/GeneralAction.java@ 32448

Last change on this file since 32448 was 32448, checked in by kjdon, 6 years ago

params class changed, now returns false by default for shouldsave. so don't need to add any that we don't want saving in the session. turned hard coded strings into static string variables

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