[3645] | 1 | package org.greenstone.gsdl3.action;
|
---|
| 2 |
|
---|
| 3 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
| 4 | import org.greenstone.gsdl3.util.*;
|
---|
| 5 | // XML classes
|
---|
| 6 | import org.w3c.dom.Node;
|
---|
| 7 | import org.w3c.dom.NodeList;
|
---|
| 8 | import org.w3c.dom.Document;
|
---|
| 9 | import org.w3c.dom.Element;
|
---|
| 10 |
|
---|
| 11 | import java.util.HashMap;
|
---|
| 12 | import java.util.Map;
|
---|
| 13 | import java.util.Iterator;
|
---|
| 14 | import java.io.File;
|
---|
| 15 |
|
---|
| 16 | public class ProcessAction extends Action {
|
---|
| 17 |
|
---|
| 18 | /** process a request */
|
---|
| 19 | public Element process (Element message) {
|
---|
| 20 |
|
---|
| 21 | // assume only one request
|
---|
| 22 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
[3682] | 23 |
|
---|
| 24 | // we ignore the subaction for now - all types are processed by the same method
|
---|
| 25 | //String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
[3645] | 26 | // get the param list
|
---|
| 27 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
[3868] | 28 | HashMap params = GSXML.extractParams(cgi_param_list, false);
|
---|
[3645] | 29 | String service_name = (String) params.get(GSCGI.SERVICE_ARG);
|
---|
| 30 | String cluster_name = (String) params.get(GSCGI.CLUSTER_ARG);
|
---|
[3682] | 31 | String request_only_p = (String)params.get(GSCGI.REQUEST_ONLY_ARG);
|
---|
| 32 | boolean request_only = false;
|
---|
| 33 | if (request_only_p!=null) {
|
---|
| 34 | request_only = (request_only_p.equals("1")?true:false);
|
---|
[3645] | 35 | }
|
---|
[3682] | 36 | String request_type = (String) params.get(GSCGI.REQUEST_TYPE_ARG);
|
---|
[3645] | 37 | // the return page
|
---|
| 38 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
| 39 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
[3987] | 40 | System.out.println("(ProcessAction) Page:\n" + converter_.getPrettyString(page));
|
---|
[3645] | 41 |
|
---|
[3682] | 42 | // what is carried out depends on the request_type
|
---|
| 43 | // if rt=d, then a describe request is done,
|
---|
| 44 | // is rt=r, a request and then a describe request is done
|
---|
| 45 | // if rt=s, a status request is done.
|
---|
[3645] | 46 |
|
---|
[3682] | 47 | // if ro=1, then this calls for a process only page - we do the request
|
---|
| 48 | // (rt should be r or s) and just give the response straight back
|
---|
[3645] | 49 | // without any page processing
|
---|
| 50 |
|
---|
| 51 | //the stylesheet
|
---|
| 52 | String stylesheet = GSFile.stylesheetFile(config_, "process.xsl");
|
---|
| 53 |
|
---|
[3682] | 54 | if (!request_only) {
|
---|
[3645] | 55 | // check that the stylesheet is present
|
---|
| 56 | if (stylesheet==null) {
|
---|
[3682] | 57 | System.err.println("ProcessAction Error: process stylesheet not found!");
|
---|
[3645] | 58 | return null;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // where to send requests
|
---|
| 63 | String to = cluster_name;
|
---|
| 64 | to = GSPath.appendLink(to, service_name);
|
---|
| 65 |
|
---|
[3682] | 66 | if (!request_type.equals("d")) {
|
---|
| 67 | // if rt=s or rt=r, do the request
|
---|
[3645] | 68 |
|
---|
| 69 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 70 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
| 71 | mr_query_message.appendChild(mr_query_request);
|
---|
| 72 |
|
---|
[3966] | 73 | mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
| 74 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
| 75 |
|
---|
| 76 | Element param_list;
|
---|
[3682] | 77 | if (request_type.equals("s")) { // status
|
---|
[3645] | 78 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
|
---|
[3966] | 79 | // only need the handle param
|
---|
| 80 | param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 81 | Element param = doc_.createElement(GSXML.PARAM_ELEM);
|
---|
| 82 | param.setAttribute(GSXML.NAME_ATT, GSCGI.PROCESS_ID_ARG);
|
---|
| 83 | param.setAttribute(GSXML.VALUE_ATT, (String)params.get(GSCGI.PROCESS_ID_ARG));
|
---|
| 84 | param_list.appendChild(param);
|
---|
[3645] | 85 | } else {
|
---|
| 86 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[3966] | 87 | // add in the params - except the ones only used by the action
|
---|
| 88 | param_list = getServiceParamList(cgi_param_list);
|
---|
| 89 |
|
---|
[3645] | 90 | }
|
---|
[3966] | 91 | mr_query_request.appendChild(param_list);
|
---|
| 92 |
|
---|
[3645] | 93 |
|
---|
| 94 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
| 95 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
| 96 |
|
---|
[3682] | 97 | if (request_only) {
|
---|
[3645] | 98 | // just send the reponse as is
|
---|
| 99 | return result_response;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | // else append the response to the page
|
---|
| 103 | page.appendChild(doc_.importNode(result_response, true));
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | // add the lang stuff from message
|
---|
| 107 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
| 108 | // add the system stuff from message
|
---|
| 109 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | // another part of the page is the service description
|
---|
| 113 |
|
---|
| 114 | // request the service info for the selected service - should be cached
|
---|
| 115 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
| 116 | Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
| 117 | mr_info_message.appendChild(mr_info_request);
|
---|
| 118 | mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
| 119 | mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
| 120 |
|
---|
| 121 | mr_info_request.setAttribute(GSXML.TO_ATT, to);
|
---|
| 122 |
|
---|
| 123 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
| 124 |
|
---|
| 125 | String path = GSXML.RESPONSE_ELEM;
|
---|
| 126 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
| 127 | Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
| 128 |
|
---|
| 129 | Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 130 |
|
---|
| 131 | if (pl !=null) {
|
---|
| 132 | // add short names to the params in the param list
|
---|
| 133 | cgi_.paramListAddShortNames(pl);
|
---|
| 134 |
|
---|
| 135 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
| 136 | Element param = (Element)pl.getFirstChild();
|
---|
| 137 | while (param !=null) {
|
---|
| 138 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
| 139 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
| 140 | String current = (String)params.get(name);
|
---|
| 141 | if (current !=null && !current.equals("")) {
|
---|
| 142 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | param = (Element)param.getNextSibling();
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | page.appendChild(description);
|
---|
| 149 |
|
---|
| 150 | // part of the data for the page is the cgi-params
|
---|
| 151 | // if we have this here, do we need to do the previous step?
|
---|
| 152 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
| 153 | page.appendChild(cgi_request);
|
---|
| 154 |
|
---|
| 155 | // now process the page and return the result
|
---|
| 156 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
| 157 |
|
---|
| 158 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
| 159 | return (Element)transformer_.transform(style_doc, page);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[3966] | 162 | protected Element getServiceParamList(Element cgi_param_list) {
|
---|
| 163 |
|
---|
| 164 | Element new_param_list = doc_.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 165 | Element param;
|
---|
| 166 | NodeList cgi_params = cgi_param_list.getChildNodes();
|
---|
| 167 | for (int i=0; i<cgi_params.getLength(); i++) {
|
---|
| 168 | Element p = (Element) cgi_params.item(i);
|
---|
| 169 | String name = p.getAttribute(GSXML.NAME_ATT);
|
---|
| 170 | if (name.equals(GSCGI.SERVICE_ARG) || name.equals(GSCGI.REQUEST_TYPE_ARG) || name.equals(GSCGI.CLUSTER_ARG)) {
|
---|
| 171 | continue;
|
---|
| 172 | }
|
---|
| 173 | // esle add it in to the list
|
---|
| 174 | new_param_list.appendChild(doc_.importNode(p, true));
|
---|
| 175 | }
|
---|
| 176 | return new_param_list;
|
---|
| 177 | }
|
---|
[3645] | 178 | }
|
---|