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

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

Second round of changes adding in the login ability, also interface options are now returned whenever site metadata is returned

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 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
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 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
87 Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
88 Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
89
90 if (request_type.equals("s"))
91 {
92 mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
93 }
94
95 mr_query_message.appendChild(mr_query_request);
96
97 Element param_list = null;
98 // add in the service params - except the ones only used by the action
99 HashMap service_params = (HashMap) params.get("s1");
100 if (service_params != null)
101 {
102 param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
103 GSXML.addParametersToList(this.doc, param_list, service_params);
104 mr_query_request.appendChild(param_list);
105 }
106
107 Element mr_query_response = (Element) this.mr.process(mr_query_message);
108 Element result_response = (Element) GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
109
110 if (response_only)
111 {
112 // just send the reponse as is
113 addSiteMetadata(result_response, userContext);
114 addInterfaceOptions(result_response);
115 return result_response;
116 }
117 if (result_response != null)
118 {
119 // else append the contents of the response to the page
120 GSXML.copyAllChildren(page_response, result_response);
121 }
122 }
123
124 // another part of the page is the service description
125
126 // request the service info for the selected service - should be cached
127 Element mr_info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
128 Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
129 mr_info_message.appendChild(mr_info_request);
130 Element mr_info_response = (Element) this.mr.process(mr_info_message);
131
132 String path = GSXML.RESPONSE_ELEM;
133 path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
134
135 Node desNode = GSXML.getNodeByPath(mr_info_response, path);
136 if (desNode != null)
137 {
138 page_response.appendChild((Element) this.doc.importNode(desNode, true));
139 }
140
141 addSiteMetadata(page_response, userContext);
142 addInterfaceOptions(page_response);
143
144 return result;
145 }
146
147 protected void changeConfig (String optionName, String optionValue)
148 {
149 if(this.config_params.get(optionName) != null)
150 {
151 this.config_params.put(optionName, optionValue);
152
153 File interfaceConfigFile = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String)this.config_params.get("interface_name"))));
154
155 Document interfaceXML = null;
156 try
157 {
158 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
159 DocumentBuilder db = dbf.newDocumentBuilder();
160 interfaceXML = db.parse(interfaceConfigFile);
161 Element topElement = interfaceXML.getDocumentElement();
162 Element optionListElem = (Element) GSXML.getChildByTagName(topElement, "optionList");
163
164 NodeList optionList = optionListElem.getElementsByTagName("option");
165
166 for(int i = 0; i < optionList.getLength(); i++)
167 {
168 Element currentOption = (Element)optionList.item(i);
169 if(currentOption.getAttribute(GSXML.NAME_ATT) != null && currentOption.getAttribute(GSXML.NAME_ATT).equals(optionName))
170 {
171 currentOption.setAttribute(GSXML.VALUE_ATT, optionValue);
172 }
173 }
174
175 DOMSource source = new DOMSource(interfaceXML);
176 Result xmlresult = new StreamResult(interfaceConfigFile);
177
178 Transformer transformer = TransformerFactory.newInstance().newTransformer();
179 transformer.transform(source, xmlresult);
180 }
181 catch(Exception ex)
182 {
183 ex.printStackTrace();
184 }
185 }
186 else
187 {
188 logger.error("Could not set param \"" + optionName + "\" to \"" + optionValue + "\" because that option does not exist.");
189 }
190 }
191}
Note: See TracBrowser for help on using the repository browser.