source: trunk/gsdl3/src/java/org/greenstone/gsdl3/action/ProcessAction.java@ 4144

Last change on this file since 4144 was 4144, checked in by kjdon, 21 years ago

all stuff for the page generated by teh Receptionist (config and display) is now put into a pageExtra element, so actions only need to append one extra piece to the page

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1package org.greenstone.gsdl3.action;
2
3import org.greenstone.gsdl3.core.ModuleInterface;
4import org.greenstone.gsdl3.util.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11import java.util.HashMap;
12import java.util.Map;
13import java.util.Iterator;
14import java.io.File;
15
16public 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 page extra stuff from message
110 page.appendChild(doc_.importNode(GSXML.getChildByTagName(message, GSXML.PAGE_EXTRA_ELEM), true));
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_response.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 page_request = GSXML.duplicateWithNewName(doc_, request, GSXML.PAGE_REQUEST_ELEM, true);
153 page.appendChild(page_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
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 }
178}
Note: See TracBrowser for help on using the repository browser.