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

Last change on this file since 14395 was 14395, checked in by anna, 17 years ago

For preference page request, call aboutPage action to get the description of the (search) services supported, to enable the search mode option and the search options on the preference page.

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