[8800] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.util.*;
|
---|
[25128] | 4 | import org.greenstone.util.GlobalProperties;
|
---|
[8800] | 5 | // XML classes
|
---|
[25128] | 6 | import org.w3c.dom.Document;
|
---|
[24865] | 7 | import org.w3c.dom.Node;
|
---|
| 8 | import org.w3c.dom.Element;
|
---|
[25128] | 9 | import org.w3c.dom.NodeList;
|
---|
[8800] | 10 |
|
---|
[25128] | 11 | import java.io.File;
|
---|
[25635] | 12 | import java.io.Serializable;
|
---|
[8800] | 13 | import java.util.HashMap;
|
---|
| 14 |
|
---|
[25128] | 15 | import javax.xml.parsers.DocumentBuilder;
|
---|
| 16 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
| 17 | import javax.xml.transform.Result;
|
---|
| 18 | import javax.xml.transform.Transformer;
|
---|
| 19 | import javax.xml.transform.TransformerFactory;
|
---|
| 20 | import javax.xml.transform.dom.DOMSource;
|
---|
| 21 | import javax.xml.transform.stream.StreamResult;
|
---|
| 22 |
|
---|
[24865] | 23 | public class GeneralAction extends Action
|
---|
| 24 | {
|
---|
[24866] | 25 |
|
---|
[24865] | 26 | /** process a request */
|
---|
| 27 | public Node process(Node message_node)
|
---|
| 28 | {
|
---|
| 29 | Element message = this.converter.nodeToElement(message_node);
|
---|
[8800] | 30 |
|
---|
[24865] | 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);
|
---|
[16688] | 35 |
|
---|
[24865] | 36 | // assume only one request
|
---|
| 37 | Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
| 38 | logger.debug(" request=" + this.converter.getString(request));
|
---|
[8800] | 39 |
|
---|
[24993] | 40 | UserContext userContext = new UserContext(request);
|
---|
[19984] | 41 |
|
---|
[24865] | 42 | // get the param list
|
---|
| 43 | Element cgi_param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
[25635] | 44 | HashMap<String, Serializable> params = GSXML.extractParams(cgi_param_list, false);
|
---|
[25128] | 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");
|
---|
[25260] | 50 |
|
---|
[25128] | 51 | changeConfig(optionName, optionValue);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[24865] | 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.
|
---|
[19984] | 67 |
|
---|
[24865] | 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
|
---|
[8800] | 71 |
|
---|
[24865] | 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 | }
|
---|
[8800] | 82 |
|
---|
[24866] | 83 | if (request_type.equals("r") || request_type.equals("s"))
|
---|
[24865] | 84 | {
|
---|
| 85 | //do the request
|
---|
| 86 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 87 | Element mr_query_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, to, userContext);
|
---|
[25128] | 88 |
|
---|
| 89 | if (request_type.equals("s"))
|
---|
[24866] | 90 | {
|
---|
| 91 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
|
---|
| 92 | }
|
---|
[8800] | 93 |
|
---|
[24865] | 94 | mr_query_message.appendChild(mr_query_request);
|
---|
[8800] | 95 |
|
---|
[24865] | 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 | }
|
---|
[8800] | 105 |
|
---|
[25260] | 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 |
|
---|
[24865] | 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);
|
---|
[8800] | 115 |
|
---|
[24865] | 116 | if (response_only)
|
---|
| 117 | {
|
---|
| 118 | // just send the reponse as is
|
---|
[24993] | 119 | addSiteMetadata(result_response, userContext);
|
---|
[25128] | 120 | addInterfaceOptions(result_response);
|
---|
[24865] | 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 | }
|
---|
[8800] | 129 |
|
---|
[24865] | 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);
|
---|
[24993] | 134 | Element mr_info_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
[24865] | 135 | mr_info_message.appendChild(mr_info_request);
|
---|
| 136 | Element mr_info_response = (Element) this.mr.process(mr_info_message);
|
---|
[25128] | 137 |
|
---|
[24865] | 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 |
|
---|
[24993] | 147 | addSiteMetadata(page_response, userContext);
|
---|
[25128] | 148 | addInterfaceOptions(page_response);
|
---|
| 149 |
|
---|
[24865] | 150 | return result;
|
---|
[13960] | 151 | }
|
---|
[25260] | 152 |
|
---|
| 153 | protected void changeConfig(String optionName, String optionValue)
|
---|
[25128] | 154 | {
|
---|
[25260] | 155 | if (this.config_params.get(optionName) != null)
|
---|
[25128] | 156 | {
|
---|
| 157 | this.config_params.put(optionName, optionValue);
|
---|
[25260] | 158 |
|
---|
| 159 | File interfaceConfigFile = new File(GSFile.interfaceConfigFile(GSFile.interfaceHome(GlobalProperties.getGSDL3Home(), (String) this.config_params.get("interface_name"))));
|
---|
| 160 |
|
---|
[25128] | 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");
|
---|
[8800] | 169 |
|
---|
[25128] | 170 | NodeList optionList = optionListElem.getElementsByTagName("option");
|
---|
[25260] | 171 |
|
---|
| 172 | for (int i = 0; i < optionList.getLength(); i++)
|
---|
[25128] | 173 | {
|
---|
[25260] | 174 | Element currentOption = (Element) optionList.item(i);
|
---|
| 175 | if (currentOption.getAttribute(GSXML.NAME_ATT) != null && currentOption.getAttribute(GSXML.NAME_ATT).equals(optionName))
|
---|
[25128] | 176 | {
|
---|
| 177 | currentOption.setAttribute(GSXML.VALUE_ATT, optionValue);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
[25260] | 180 |
|
---|
[25128] | 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 | }
|
---|
[25260] | 187 | catch (Exception ex)
|
---|
[25128] | 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 | }
|
---|
[8800] | 197 | }
|
---|