source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/DefaultReceptionist.java@ 32549

Last change on this file since 32549 was 32549, checked in by kjdon, 5 years ago

receptionist now passes languageList (from interfaceConfig) to teh actions, so an action can add it into the page response if it needs to, rather than the receptionist adding it in to every page. its only ever used in prefs page

  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1package org.greenstone.gsdl3.core;
2
3import org.greenstone.gsdl3.util.*;
4import org.greenstone.gsdl3.action.*;
5// XML classes
6import org.w3c.dom.Node;
7import org.w3c.dom.NodeList;
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10
11// other java classes
12import java.io.File;
13import java.util.HashMap;
14import java.util.Enumeration;
15
16import org.apache.log4j.*;
17
18/** The default greenstone receptionist - needs some extra info for each page */
19public class DefaultReceptionist extends TransformingReceptionist
20{
21
22 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.DefaultReceptionist.class.getName());
23
24 /**
25 * add in the collection description to the page, then for each service, add
26 * in the service description
27 */
28 protected void addExtraInfo(Element page)
29 {
30 super.addExtraInfo(page);
31 // the document for the page
32 Document doc = page.getOwnerDocument();
33 // a new document to create messages to send to MR
34 Document msg_doc = XMLConverter.newDOM();
35 Element page_request = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_REQUEST_ELEM);
36 // if it is a system request, then we don't bother with this.
37 String action = page_request.getAttribute(GSXML.ACTION_ATT);
38 if (action.equals("s"))
39 {
40 logger.error("HACK: don't ask for coll info if system action");
41 return;
42 }
43 logger.debug("add extra info, page request=" + this.converter.getString(page_request));
44 // is a collection defined?
45 Element param_list = (Element) GSXML.getChildByTagName(page_request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
46 if (param_list == null)
47 { // must be the original home page
48 logger.debug(" no param list, assuming home page");
49 return;
50 }
51 Element coll_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, GSParams.COLLECTION);
52 if (coll_param == null)
53 {
54 logger.debug(" coll param is null, returning");
55 return;
56 }
57
58 // see if the collection/cluster element is already there
59 String coll_name = coll_param.getAttribute(GSXML.VALUE_ATT);
60 UserContext userContext = new UserContext(page_request);
61
62 if (coll_name.equals(""))
63 {
64 Element pc_param = GSXML.getNamedElement(param_list, GSXML.PARAM_ELEM, GSXML.NAME_ATT, "p.c");
65 if (pc_param != null)
66 {
67 coll_name = pc_param.getAttribute(GSXML.VALUE_ATT);
68 }
69 }
70
71 boolean get_service_description = false;
72 Element page_response = (Element) GSXML.getChildByTagName(page, GSXML.PAGE_RESPONSE_ELEM);
73
74 Element coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
75 if (coll_description == null)
76 {
77 // try cluster
78 coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.CLUSTER_ELEM);
79 }
80 if (coll_description == null)
81 {
82 logger.debug("getting the coll description");
83 // we dont have one yet - get it
84
85 Element coll_about_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
86 Element coll_about_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
87 coll_about_message.appendChild(coll_about_request);
88
89 Node coll_about_response_message = this.mr.process(coll_about_message);
90 Element coll_about_response = (Element) GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
91 if (coll_about_response == null)
92 {
93 return;
94 }
95 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
96 if (coll_description == null)
97 { // may be a cluster
98 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
99 }
100
101 if (coll_description == null)
102 {
103 logger.error(" no collection description, returning");
104 return;
105 }
106 // have found one, append it to the page response
107 coll_description = (Element) doc.importNode(coll_description, true);
108 page_response.appendChild(coll_description);
109 get_service_description = true;
110 }
111
112
113 // have got a coll description
114 // now get the dispay info for the services
115 Element service_list = (Element) GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
116 if (service_list == null)
117 {
118 logger.error(" no service list, returning");
119 // something weird has gone wrong
120 return;
121 }
122
123 NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
124 if (services.getLength() == 0)
125 {
126 logger.error("DefaultReceoptionist: no services found for colllection/cluster " + coll_name);
127 return;
128 }
129 // check one service for display items
130 if (!get_service_description)
131 {
132 // we dont know yet if we need to get these
133 int i = 1;
134 Element test_s = (Element) services.item(0);
135 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)))
136 {
137 test_s = (Element) services.item(i);
138 i++;
139 }
140 if (i == services.getLength())
141 {
142 // we have only found retrieve or oai services, so dont need descripitons anyway
143 return;
144 }
145 if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) != null)
146 {
147 // have got descriptions already,
148 return;
149 }
150 }
151
152 // if get here, we need to get the service descriptions
153
154 // we will send all the requests in a single message
155 Element info_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
156 for (int i = 0; i < services.getLength(); i++)
157 {
158 Element c = (Element) services.item(i);
159 String name = c.getAttribute(GSXML.NAME_ATT);
160 String address = GSPath.appendLink(coll_name, name);
161 Element info_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, address, userContext);
162 //Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
163 //req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
164 //info_request.appendChild(req_param_list);
165 info_message.appendChild(info_request);
166
167 }
168
169 Element info_response = (Element) this.mr.process(info_message);
170
171 NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
172 // check that have same number of responses as collections
173 if (services.getLength() != service_responses.getLength())
174 {
175 logger.error(" didn't get a response for each service - somethings gone wrong!");
176 // for now, dont use the metadata
177 }
178 else
179 {
180 for (int i = 0; i < services.getLength(); i++)
181 {
182 Element c1 = (Element) services.item(i);
183 Element c2 = (Element) GSXML.getChildByTagName((Element) service_responses.item(i), GSXML.SERVICE_ELEM);
184 if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT)))
185 {
186 //add the service data into the original response
187 GSXML.mergeElements(c1, c2);
188 }
189 else
190 {
191 logger.debug(" response does not correspond to request!");
192 }
193
194 }
195 }
196 }
197}
Note: See TracBrowser for help on using the repository browser.