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

Last change on this file since 25635 was 25635, checked in by sjm84, 12 years ago

Fixing Greenstone 3's use (or lack thereof) of generics, this was done automatically so we may want to change it over time. This change will also auto-format any files that have not already been formatted.

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