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