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 | public static final String PROCESS_ONLY_ARG="p";
|
---|
19 |
|
---|
20 | /** add the action specific args to the GSCGI object */
|
---|
21 | protected void addCGIParams() {
|
---|
22 | cgi_.addStaticParam(PROCESS_ONLY_ARG);
|
---|
23 |
|
---|
24 | }
|
---|
25 | /** process a request */
|
---|
26 | public Element process (Element message) {
|
---|
27 |
|
---|
28 | // assume only one request
|
---|
29 | Element request = (Element)GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
|
---|
30 | String subaction = request.getAttribute(GSXML.SUBACTION_ATT);
|
---|
31 | // get the param list
|
---|
32 | Element cgi_param_list = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
33 | HashMap params = GSXML.extractParams(cgi_param_list);
|
---|
34 | String service_name = (String) params.get(GSCGI.SERVICE_ARG);
|
---|
35 | String cluster_name = (String) params.get(GSCGI.CLUSTER_ARG);
|
---|
36 | String process_p = (String)params.get(PROCESS_ONLY_ARG);
|
---|
37 | boolean process_only = false;
|
---|
38 | if (process_p!=null) {
|
---|
39 | process_only = (((String)params.get(PROCESS_ONLY_ARG)).equals("1")?true:false);
|
---|
40 | }
|
---|
41 | // the return page
|
---|
42 | Element page = doc_.createElement(GSXML.PAGE_ELEM);
|
---|
43 | page.setAttribute(GSXML.LANG_ATT, request.getAttribute(GSXML.LANG_ATT));
|
---|
44 |
|
---|
45 | // what is carried out depends on the subaction
|
---|
46 | // if sa=d, then a describe request is done,
|
---|
47 | // is sa=r, a request and then a describe request is done
|
---|
48 | // if sa=s, a status request is done.
|
---|
49 |
|
---|
50 | // if p=1, then this calls for a process only page - we do the request
|
---|
51 | // (sa should be r or s) and just give the response straight back
|
---|
52 | // without any page processing
|
---|
53 |
|
---|
54 | //the stylesheet
|
---|
55 | String stylesheet = GSFile.stylesheetFile(config_, "process.xsl");
|
---|
56 |
|
---|
57 | if (!process_only) {
|
---|
58 | // check that the stylesheet is present
|
---|
59 | if (stylesheet==null) {
|
---|
60 | System.err.println("ProcessAction Error: build stylesheet not found!");
|
---|
61 | return null;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | // where to send requests
|
---|
66 | String to = cluster_name;
|
---|
67 | to = GSPath.appendLink(to, service_name);
|
---|
68 |
|
---|
69 | if (!subaction.equals("d")) {
|
---|
70 | // if sa=s or sa=r, do the request
|
---|
71 |
|
---|
72 | Element mr_query_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
73 | Element mr_query_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
74 | mr_query_message.appendChild(mr_query_request);
|
---|
75 |
|
---|
76 | if (subaction.equals("s")) { // status
|
---|
77 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
|
---|
78 | } else {
|
---|
79 | mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
80 | }
|
---|
81 | mr_query_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
82 | mr_query_request.setAttribute(GSXML.TO_ATT, to);
|
---|
83 |
|
---|
84 | // add the param list as is
|
---|
85 | Element query_param_list = (Element)doc_.importNode(cgi_param_list, true);
|
---|
86 | mr_query_request.appendChild(query_param_list);
|
---|
87 |
|
---|
88 | Element mr_query_response = (Element)mr_.process(mr_query_message);
|
---|
89 | Element result_response = (Element)GSXML.getChildByTagName(mr_query_response, GSXML.RESPONSE_ELEM);
|
---|
90 |
|
---|
91 | if (process_only) {
|
---|
92 | // just send the reponse as is
|
---|
93 | return result_response;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // else append the response to the page
|
---|
97 | page.appendChild(doc_.importNode(result_response, true));
|
---|
98 | }
|
---|
99 |
|
---|
100 | // add the lang stuff from message
|
---|
101 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.DISPLAY_ELEM), true));
|
---|
102 | // add the system stuff from message
|
---|
103 | page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.CONFIGURATION_ELEM), true));
|
---|
104 |
|
---|
105 |
|
---|
106 | // another part of the page is the service description
|
---|
107 |
|
---|
108 | // request the service info for the selected service - should be cached
|
---|
109 | Element mr_info_message = doc_.createElement(GSXML.MESSAGE_ELEM);
|
---|
110 | Element mr_info_request = doc_.createElement(GSXML.REQUEST_ELEM);
|
---|
111 | mr_info_message.appendChild(mr_info_request);
|
---|
112 | mr_info_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_DESCRIBE);
|
---|
113 | mr_info_request.setAttribute(GSXML.LANG_ATT, page.getAttribute(GSXML.LANG_ATT));
|
---|
114 |
|
---|
115 | mr_info_request.setAttribute(GSXML.TO_ATT, to);
|
---|
116 |
|
---|
117 | Element mr_info_response = (Element) mr_.process(mr_info_message);
|
---|
118 |
|
---|
119 | String path = GSXML.RESPONSE_ELEM;
|
---|
120 | path = GSPath.appendLink(path, GSXML.SERVICE_ELEM);
|
---|
121 | Element description = (Element)doc_.importNode(GSXML.getNodeByPath(mr_info_response, path), true);
|
---|
122 |
|
---|
123 | Element pl = (Element)GSXML.getChildByTagName(description, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
124 |
|
---|
125 | if (pl !=null) {
|
---|
126 | // add short names to the params in the param list
|
---|
127 | cgi_.paramListAddShortNames(pl);
|
---|
128 |
|
---|
129 | // for each param in the description, overwrite teh default value with the currently set value if present
|
---|
130 | Element param = (Element)pl.getFirstChild();
|
---|
131 | while (param !=null) {
|
---|
132 | if (param.getNodeName().equals(GSXML.PARAM_ELEM)) { // just in case
|
---|
133 | String name = param.getAttribute(GSXML.NAME_ATT);
|
---|
134 | String current = (String)params.get(name);
|
---|
135 | if (current !=null && !current.equals("")) {
|
---|
136 | param.setAttribute(GSXML.DEFAULT_ATT, current);
|
---|
137 | }
|
---|
138 | }
|
---|
139 | param = (Element)param.getNextSibling();
|
---|
140 | }
|
---|
141 | }
|
---|
142 | page.appendChild(description);
|
---|
143 |
|
---|
144 | // part of the data for the page is the cgi-params
|
---|
145 | // if we have this here, do we need to do the previous step?
|
---|
146 | Element cgi_request = (Element)doc_.importNode(request, true);
|
---|
147 | page.appendChild(cgi_request);
|
---|
148 |
|
---|
149 | System.out.println("process action, page=");
|
---|
150 | System.out.println(converter_.getString(page));
|
---|
151 | // now process the page and return the result
|
---|
152 | Document style_doc = converter_.getDOM(new File(stylesheet));
|
---|
153 |
|
---|
154 | GSXSLT.absoluteIncludePaths(style_doc, config_);
|
---|
155 | return (Element)transformer_.transform(style_doc, page);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | }
|
---|