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

Last change on this file since 13525 was 13525, checked in by shaoqun, 17 years ago

fixed the bug that failed to get collection details from the remote site

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