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

Last change on this file since 28855 was 28855, checked in by kjdon, 10 years ago

changed to use new DOM for response xml rather than the request DOM. And fixed up a place where appending node in wrong document.

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