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

Last change on this file since 25986 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

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