source: greenstone3/trunk/src/java/org/greenstone/gsdl3/action/PageAction.java@ 16688

Last change on this file since 16688 was 16688, checked in by davidb, 16 years ago

Changed 'Element process(Element)' in ModuleInterface to 'Node process(Node)'. After some deliberation is was decided this is a more useful (generic) layer of the DOM to pass information around in. Helps with the DocType problem when producing XSL Transformed pages, for example. When this was an Element, it would loose track of its DocType. Supporting method provided in XMLConverter 'Element nodeToElement(Node)' which checks a nodes docType and casts to Element if appropriate, or if a Document, typecasts to that and then extracts the top-level Element. With this fundamental change in ModuleInterface, around 20 files needed to be updated (Actions, Services, etc) that build on top of 'process()' to reflect this change, and use nodeToElement where necessary.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.4 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.io.File;
13
14import org.apache.log4j.*;
15
16public class PageAction extends Action {
17
18 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.action.PageAction.class.getName());
19
20 public static final String HOME_PAGE = "home";
21 public static final String ABOUT_PAGE = "about";
22 public static final String PREFS_PAGE = "pref";
23 public static final String GLI4GS3_PAGE="gli4gs3";
24
25 public Node process (Node message_node) {
26
27 Element message = this.converter.nodeToElement(message_node);
28
29 Element request = (Element) GSXML.getChildByTagName(message, GSXML.REQUEST_ELEM);
30 // the page name is the subaction
31 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
32 if (page_name.equals("")) { // if no page specified, assume home page
33 page_name = HOME_PAGE;
34 }
35 Element result = this.doc.createElement(GSXML.MESSAGE_ELEM);
36 Element response;
37 if (page_name.equals(HOME_PAGE)) {
38 response = homePage(request);
39 //} else if (page_name.equals(ABOUT_PAGE)) {
40 } else if (page_name.equals(ABOUT_PAGE) || page_name.equals(PREFS_PAGE)) {
41 response = aboutPage(request);
42 //}else if (page_name.equals(PREFS_PAGE)) {
43 //response = prefsPage(request);
44 } else if (page_name.equals(GLI4GS3_PAGE)){
45 response = gli4gs3Page(request);
46 }else { // unknown page
47
48 logger.error("unknown page specified!");
49 response = unknownPage(request);
50 }
51
52 result.appendChild(this.doc.importNode(response, true));
53 logger.debug("page action result: "+this.converter.getPrettyString(result));
54 return result;
55 }
56
57
58 protected Element homePage(Element request) {
59
60 String lang = request.getAttribute(GSXML.LANG_ATT);
61 String uid = request.getAttribute(GSXML.USER_ID_ATT);
62 // first, get the message router info
63 Element info_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
64 Element coll_list_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, "", lang, uid);
65 info_message.appendChild(coll_list_request);
66 Element info_response_message = (Element)this.mr.process(info_message);
67 if (info_response_message==null) {
68 logger.error(" couldn't query the message router!");
69 return null;
70 }
71 Element info_response = (Element)GSXML.getChildByTagName(info_response_message, GSXML.RESPONSE_ELEM);
72 if (info_response==null) {
73 logger.error("couldn't query the message router!");
74 return null;
75 }
76
77 // second, get the metadata for each collection - we only want specific
78 // elements but for now, we'll just get it all
79 Element collection_list = (Element)GSXML.getChildByTagName(info_response, GSXML.COLLECTION_ELEM+GSXML.LIST_MODIFIER);
80 logger.info(GSXML.xmlNodeToString(collection_list));
81 if (collection_list != null) {
82 NodeList colls = GSXML.getChildrenByTagName(collection_list, GSXML.COLLECTION_ELEM);
83 if (colls.getLength()>0) {
84 sendMultipleRequests(colls, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
85 }
86 }
87
88 // get metadata for any services
89 Element service_list = (Element)GSXML.getChildByTagName(info_response, GSXML.SERVICE_ELEM+GSXML.LIST_MODIFIER);
90 if (service_list != null) {
91 NodeList services = GSXML.getChildrenByTagName(service_list, GSXML.SERVICE_ELEM);
92 if (services.getLength() > 0) {
93 sendMultipleRequests(services, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
94 }
95 }
96
97 // get metadata for service clusters
98 Element cluster_list = (Element)GSXML.getChildByTagName(info_response, GSXML.CLUSTER_ELEM+GSXML.LIST_MODIFIER);
99 if (cluster_list != null) {
100 NodeList clusters = GSXML.getChildrenByTagName(cluster_list, GSXML.CLUSTER_ELEM);
101 if (clusters.getLength() > 0) {
102 sendMultipleRequests(clusters, null, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
103
104 }
105 }
106
107 // all the components have been merged into info_response
108 return info_response;
109
110 } // homePage
111
112 protected Element aboutPage(Element request) {
113
114 String lang = request.getAttribute(GSXML.LANG_ATT);
115 String uid = request.getAttribute(GSXML.USER_ID_ATT);
116 // extract the params from the cgi-request,
117 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
118 HashMap params = GSXML.extractParams(cgi_paramList, false);
119
120 String coll_name = (String)params.get(GSParams.COLLECTION);
121 if (coll_name == null || coll_name.equals("")) {
122 logger.error("about page requested with no collection or cluster specified!");
123 // return an empty response
124 return this.doc.createElement(GSXML.RESPONSE_ELEM);
125 }
126
127 // get the collection or cluster description
128 Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
129
130 Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
131 coll_about_message.appendChild(coll_about_request);
132
133 Element coll_about_response = (Element)this.mr.process(coll_about_message);
134
135 // add collection type attribute to paramList
136 String col_type = "";
137 NodeList collect_elem = coll_about_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
138 if(collect_elem.getLength() != 0) {
139 for (int i=0; i < collect_elem.getLength(); i++) {
140 Element e = (Element) collect_elem.item(i);
141 col_type = e.getAttribute(GSXML.TYPE_ATT);
142 }
143 } else {
144 logger.error(GSXML.COLLECTION_ELEM + " element is null");
145 }
146
147 NodeList paramList = request.getElementsByTagName(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
148 if(paramList.getLength() != 0) {
149 for (int i=0; i<paramList.getLength(); i++) {
150 Element e = (Element) paramList.item(i);
151 Element ct = GSXML.createParameter(request.getOwnerDocument(), GSParams.COLLECTION_TYPE, col_type.equalsIgnoreCase("mg") ? "0" : "1");
152 e.appendChild(ct);
153 }
154 } else {
155 logger.info("paramList is null!!");
156 }
157
158 if (coll_about_response == null) {
159 return null;
160 }
161
162 // second, get the info for each service - we only want display items
163 // but for now, we'll just get it all
164 NodeList services = coll_about_response.getElementsByTagName(GSXML.SERVICE_ELEM);
165 if (services.getLength() > 0) {
166 sendMultipleRequests(services, coll_name, GSXML.REQUEST_TYPE_DESCRIBE, lang, uid);
167 }
168
169 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
170 return response;
171 }
172
173 //protected Element prefsPage(Element request) {
174
175 // return null;
176 //}
177
178 /** if we dont know the page type, use this method */
179 protected Element unknownPage(Element request) {
180
181 String lang = request.getAttribute(GSXML.LANG_ATT);
182 String uid = request.getAttribute(GSXML.USER_ID_ATT);
183 String page_name = request.getAttribute(GSXML.SUBACTION_ATT);
184
185 // extract the params from the cgi-request,
186 Element cgi_paramList = (Element)GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
187 HashMap params = GSXML.extractParams(cgi_paramList, false);
188
189 String coll_name = (String)params.get(GSParams.COLLECTION);
190 if (coll_name == null || coll_name.equals("")) {
191 // just return an empty response
192 return this.doc.createElement(GSXML.RESPONSE_ELEM);
193 }
194
195 // else get the coll description - actually this is the same as for the about page - should we merge these two methods??
196
197 // if there is a service specified should we get the service description instead??
198 // get the collection or cluster description
199 Element coll_about_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
200
201 Element coll_about_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, lang, uid);
202 coll_about_message.appendChild(coll_about_request);
203
204 Element coll_about_response = (Element)this.mr.process(coll_about_message);
205
206 Element response = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.RESPONSE_ELEM);
207 return response;
208
209 }
210
211
212 protected boolean sendMultipleRequests(NodeList items, String path_prefix, String request_type, String lang, String uid) {
213
214 // we will send all the requests in a single message
215 Element message = this.doc.createElement(GSXML.MESSAGE_ELEM);
216 for (int i=0; i<items.getLength(); i++) {
217 Element c = (Element)items.item(i);
218 String path = c.getAttribute(GSXML.NAME_ATT);
219 if (path_prefix != null) {
220 path = GSPath.appendLink(path_prefix, path);
221 }
222 Element request = GSXML.createBasicRequest(this.doc, request_type, path, lang, uid);
223 message.appendChild(request);
224 }
225
226 Element response_message = (Element)this.mr.process(message);
227
228 NodeList responses = response_message.getElementsByTagName(GSXML.RESPONSE_ELEM);
229 // check that have same number of responses as requests
230 if (items.getLength() != responses.getLength()) {
231 logger.error("didn't get a response for each request - somethings gone wrong!");
232 return false;
233 }
234
235 for (int i=0; i<items.getLength(); i++) {
236 Element c1 = (Element)items.item(i);
237 Element c2 = (Element)GSXML.getChildByTagName((Element)responses.item(i), c1.getTagName());
238 if (c1 != null && c2 !=null && c1.getAttribute(GSXML.NAME_ATT).endsWith(c2.getAttribute(GSXML.NAME_ATT))) {
239 //add the new data into the original element
240 GSXML.mergeElements(c1, c2);
241 } else {
242 logger.error(" response does not correspond to request!");
243 }
244
245 }
246
247 return true;
248
249 }
250
251 protected Element gli4gs3Page(Element request) {
252 String lang = request.getAttribute(GSXML.LANG_ATT);
253 String uid = request.getAttribute(GSXML.USER_ID_ATT);
254
255 Element page_response = this.doc.createElement(GSXML.RESPONSE_ELEM);
256
257 Element applet_elem = this.doc.createElement("Applet");
258 page_response.appendChild(applet_elem);
259 applet_elem.setAttribute("ARCHIVE","SignedGatherer.jar");
260 applet_elem.setAttribute("CODE","org.greenstone.gatherer.GathererApplet4gs3");
261 applet_elem.setAttribute("CODEBASE","applet");
262 applet_elem.setAttribute("HEIGHT","50");
263 applet_elem.setAttribute("WIDTH","380");
264 Element gwcgi_param_elem= this.doc.createElement("PARAM");
265 gwcgi_param_elem.setAttribute("name","gwcgi");
266 String library_name=GlobalProperties.getGSDL3WebAddress();
267 gwcgi_param_elem.setAttribute("value",library_name);
268 applet_elem.appendChild(gwcgi_param_elem);
269
270 Element gsdl3_param_elem= this.doc.createElement("PARAM");
271 gsdl3_param_elem.setAttribute("name","gsdl3");
272 gsdl3_param_elem.setAttribute("value","true");
273 applet_elem.appendChild(gsdl3_param_elem);
274
275 return page_response;
276 }
277}
Note: See TracBrowser for help on using the repository browser.