1 | package org.greenstone.gsdl3.core;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.util.*;
|
---|
4 | import org.greenstone.gsdl3.action.*;
|
---|
5 | import org.greenstone.gsdl3.service.CollectionGroups;
|
---|
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 | // other java classes
|
---|
13 | import java.io.File;
|
---|
14 | import java.io.Serializable;
|
---|
15 | import java.util.HashMap;
|
---|
16 | import java.util.Enumeration;
|
---|
17 |
|
---|
18 | import org.apache.log4j.*;
|
---|
19 |
|
---|
20 | /** The default greenstone receptionist - needs some extra info for each page */
|
---|
21 | public class DefaultReceptionist extends TransformingReceptionist
|
---|
22 | {
|
---|
23 |
|
---|
24 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.DefaultReceptionist.class.getName());
|
---|
25 |
|
---|
26 | // this gets set to the groupInfo service name if there is one.
|
---|
27 | protected String groupInfoServiceName = null;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * add in the collection description to the page, then for each service, add
|
---|
31 | * in the service description
|
---|
32 | */
|
---|
33 | protected void addExtraInfo(Element page)
|
---|
34 | {
|
---|
35 | super.addExtraInfo(page);
|
---|
36 | // the document for the page
|
---|
37 | Document doc = page.getOwnerDocument();
|
---|
38 | // a new document to create messages to send to MR
|
---|
39 | Document msg_doc = XMLConverter.newDOM();
|
---|
40 | Element page_request = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
|
---|
41 | // if it is a system request, then we don't bother with this.
|
---|
42 | String action = page_request.getAttribute(GSXML.ACTION_ATT);
|
---|
43 | if (action.equals("s"))
|
---|
44 | {
|
---|
45 | logger.error("HACK: don't ask for coll info if system action");
|
---|
46 | return;
|
---|
47 | }
|
---|
48 | logger.debug("add extra info, page request=" + this.converter.getString(page_request));
|
---|
49 | // is a collection defined?
|
---|
50 | Element param_list = (Element) GSXML.getChildByTagName(page_request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
51 | if (param_list == null)
|
---|
52 | { // must be the original home page
|
---|
53 | logger.debug(" no param list, assuming home page");
|
---|
54 | return;
|
---|
55 | }
|
---|
56 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
57 | String coll_name = (String)params.get(GSParams.COLLECTION);
|
---|
58 | if (coll_name == null || coll_name.equals(""))
|
---|
59 | {
|
---|
60 | // try p.c
|
---|
61 | HashMap<String,String> prev_params = (HashMap<String,String>)params.get(GSParams.PREVIOUS_PREFIX);
|
---|
62 | if (prev_params != null) {
|
---|
63 | coll_name = prev_params.get(GSParams.COLLECTION);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (coll_name == null || coll_name.equals(""))
|
---|
68 | {
|
---|
69 | logger.debug(" coll/p.c params both empty, not adding extra info");
|
---|
70 | return;
|
---|
71 | }
|
---|
72 |
|
---|
73 | UserContext userContext = new UserContext(page_request);
|
---|
74 |
|
---|
75 | boolean get_service_description = false;
|
---|
76 | Element page_response = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
|
---|
77 |
|
---|
78 | Element coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
|
---|
79 | if (coll_description == null)
|
---|
80 | {
|
---|
81 | // try cluster
|
---|
82 | coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.CLUSTER_ELEM);
|
---|
83 | }
|
---|
84 | if (coll_description == null)
|
---|
85 | {
|
---|
86 | logger.debug("getting the coll description");
|
---|
87 | // we dont have one yet - get it
|
---|
88 |
|
---|
89 | Element coll_about_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
90 | Element coll_about_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
|
---|
91 | coll_about_message.appendChild(coll_about_request);
|
---|
92 |
|
---|
93 | Node coll_about_response_message = this.mr.process(coll_about_message);
|
---|
94 | Element coll_about_response = (Element) GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
|
---|
95 | if (coll_about_response == null)
|
---|
96 | {
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
|
---|
100 | if (coll_description == null)
|
---|
101 | { // may be a cluster
|
---|
102 | coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (coll_description == null)
|
---|
106 | {
|
---|
107 | logger.error(" no collection description, returning");
|
---|
108 | return;
|
---|
109 | }
|
---|
110 | // have found one, append it to the page response
|
---|
111 | coll_description = (Element) doc.importNode(coll_description, true);
|
---|
112 | page_response.appendChild(coll_description);
|
---|
113 | get_service_description = true;
|
---|
114 | }
|
---|
115 |
|
---|
116 | // have we got group param, and pathList??
|
---|
117 | String group = (String)params.get(GSParams.GROUP);
|
---|
118 | if (group != null && !group.equals("")) {
|
---|
119 | // ...yes we do. add group path info if not already present
|
---|
120 | Element path_list = (Element) GSXML.getChildByTagName(page_response, GSXML.PATH_ELEM + GSXML.LIST_MODIFIER);
|
---|
121 | if (path_list == null) {
|
---|
122 | // lets get it now
|
---|
123 |
|
---|
124 | Element group_info_response = getGroupInfo(group, userContext);
|
---|
125 | if (group_info_response != null) {
|
---|
126 | path_list = (Element) GSXML.getChildByTagName(group_info_response,GSXML.PATH_ELEM + GSXML.LIST_MODIFIER);
|
---|
127 | if (path_list != null) {
|
---|
128 | page_response.appendChild(doc.importNode(path_list, true));
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | // have got a coll description
|
---|
135 | // now get the dispay info for the services
|
---|
136 | Element service_list = (Element) GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
|
---|
137 | if (service_list == null)
|
---|
138 | {
|
---|
139 | logger.error(" no service list, returning");
|
---|
140 | // something weird has gone wrong
|
---|
141 | return;
|
---|
142 | }
|
---|
143 |
|
---|
144 | NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
|
---|
145 | if (services.getLength() == 0)
|
---|
146 | {
|
---|
147 | logger.error("DefaultReceoptionist: no services found for colllection/cluster " + coll_name);
|
---|
148 | return;
|
---|
149 | }
|
---|
150 | // check one service for display items
|
---|
151 | if (!get_service_description)
|
---|
152 | {
|
---|
153 | // we dont know yet if we need to get these
|
---|
154 | int i = 1;
|
---|
155 | Element test_s = (Element) services.item(0);
|
---|
156 | while (i < services.getLength() && (test_s.getAttribute(GSXML.TYPE_ATT).equals(GSXML.SERVICE_TYPE_RETRIEVE) || test_s.getAttribute(GSXML.TYPE_ATT).equals(GSXML.SERVICE_TYPE_OAI)))
|
---|
157 | {
|
---|
158 | test_s = (Element) services.item(i);
|
---|
159 | i++;
|
---|
160 | }
|
---|
161 | if (i == services.getLength())
|
---|
162 | {
|
---|
163 | // we have only found retrieve or oai services, so dont need descripitons anyway
|
---|
164 | return;
|
---|
165 | }
|
---|
166 | if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) != null)
|
---|
167 | {
|
---|
168 | // have got descriptions already,
|
---|
169 | return;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | // if get here, we need to get the service descriptions
|
---|
174 |
|
---|
175 | // we will send all the requests in a single message
|
---|
176 | Element info_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
177 | for (int i = 0; i < services.getLength(); i++)
|
---|
178 | {
|
---|
179 | Element c = (Element) services.item(i);
|
---|
180 | String name = c.getAttribute(GSXML.NAME_ATT);
|
---|
181 | String address = GSPath.appendLink(coll_name, name);
|
---|
182 | Element info_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, address, userContext);
|
---|
183 | //Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
184 | //req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
|
---|
185 | //info_request.appendChild(req_param_list);
|
---|
186 | info_message.appendChild(info_request);
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | Element info_response = (Element) this.mr.process(info_message);
|
---|
191 |
|
---|
192 | NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
|
---|
193 | // check that have same number of responses as collections
|
---|
194 | if (services.getLength() != service_responses.getLength())
|
---|
195 | {
|
---|
196 | logger.error(" didn't get a response for each service - somethings gone wrong!");
|
---|
197 | // for now, dont use the metadata
|
---|
198 | }
|
---|
199 | else
|
---|
200 | {
|
---|
201 | for (int i = 0; i < services.getLength(); i++)
|
---|
202 | {
|
---|
203 | Element c1 = (Element) services.item(i);
|
---|
204 | Element c2 = (Element) GSXML.getChildByTagName((Element) service_responses.item(i), GSXML.SERVICE_ELEM);
|
---|
205 | if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT)))
|
---|
206 | {
|
---|
207 | //add the service data into the original response
|
---|
208 | GSXML.mergeElements(c1, c2);
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | logger.debug(" response does not correspond to request!");
|
---|
213 | }
|
---|
214 |
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | // copied from pageaction - put in a single place
|
---|
220 | /** sends a request to GroupCurrentContent service to get group info. null group will give top level info,
|
---|
221 | otherwise will just give info for specified group */
|
---|
222 | protected Element getGroupInfo(String group, UserContext userContext) {
|
---|
223 |
|
---|
224 | if (groupInfoServiceName == null) {
|
---|
225 | // this is the first time we have come here, set this up
|
---|
226 |
|
---|
227 | if (this.mr instanceof MessageRouter && ((MessageRouter)this.mr).pingModule(CollectionGroups.GROUP_CONTENT_SERVICE)) {
|
---|
228 | this.groupInfoServiceName = CollectionGroups.GROUP_CONTENT_SERVICE;
|
---|
229 | } else {
|
---|
230 | this.groupInfoServiceName = "NO_GROUPS";
|
---|
231 | }
|
---|
232 | }
|
---|
233 | if (this.groupInfoServiceName.equals ("NO_GROUPS")) {
|
---|
234 | return null;
|
---|
235 | }
|
---|
236 | // otherwise lets get the group info
|
---|
237 | Document doc = XMLConverter.newDOM();
|
---|
238 | // collections and groups list
|
---|
239 | Element group_info_message = doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
240 | Element group_info_request = GSXML.createBasicRequest(doc, GSXML.TO_ATT, this.groupInfoServiceName, userContext);
|
---|
241 | group_info_message.appendChild(group_info_request);
|
---|
242 | if (group != null) {
|
---|
243 | Element param_list = doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
244 | GSXML.addParameterToList(param_list, GSParams.GROUP, group);
|
---|
245 | group_info_request.appendChild(param_list);
|
---|
246 | }
|
---|
247 | // send off the request
|
---|
248 | Element group_info_response_message = (Element) this.mr.process(group_info_message);
|
---|
249 | Element group_info_response = (Element) GSXML.getChildByTagName(group_info_response_message, GSXML.RESPONSE_ELEM);
|
---|
250 | return group_info_response;
|
---|
251 | }
|
---|
252 |
|
---|
253 | }
|
---|