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

Last change on this file since 14311 was 14311, checked in by qq6, 17 years ago

added subaction 'gli4gs3' for a applet gli

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