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

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

Making sure the remoteAddress is passed into any services called by the General action. This is used in the Authentication service.

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