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

Last change on this file since 3987 was 3987, checked in by mdewsnip, 21 years ago

Improvements to page element structure, for better image handling.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 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 System.out.println("(ProcessAction) Page:\n" + converter_.getPrettyString(page));
41
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.
46
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
49 // without any page processing
50
51 //the stylesheet
52 String stylesheet = GSFile.stylesheetFile(config_, "process.xsl");
53
54 if (!request_only) {
55 // check that the stylesheet is present
56 if (stylesheet==null) {
57 System.err.println("ProcessAction Error: process stylesheet not found!");
58 return null;
59 }
60 }
61
62 // where to send requests
63 String to = cluster_name;
64 to = GSPath.appendLink(to, service_name);
65
66 if (!request_type.equals("d")) {
67 // if rt=s or rt=r, do the request
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
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;
77 if (request_type.equals("s")) { // status
78 mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_STATUS);
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);
85 } else {
86 mr_query_request.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
87 // add in the params - except the ones only used by the action
88 param_list = getServiceParamList(cgi_param_list);
89
90 }
91 mr_query_request.appendChild(param_list);
92
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
97 if (request_only) {
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
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.