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