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

Last change on this file since 28966 was 28966, checked in by kjdon, 10 years ago

Lots of changes. Mainly to do with removing this.doc from everywhere. Document is not thread safe. Now we tend to create a new Document everytime we are starting a new page/message etc. in service this.desc_doc is available as teh document to create service info stuff. But it should only be used for this and not for other messages. newDOM is now static for XMLConverter. method param changes for some GSXML methods.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 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 if (this.language_list != null)
74 {
75 page_response.appendChild(doc.importNode(this.language_list, true));
76 }
77 Element coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.COLLECTION_ELEM);
78 if (coll_description == null)
79 {
80 // try cluster
81 coll_description = (Element) GSXML.getChildByTagName(page_response, GSXML.CLUSTER_ELEM);
82 }
83 if (coll_description == null)
84 {
85 logger.debug("getting the coll description");
86 // we dont have one yet - get it
87
88 Element coll_about_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
89 Element coll_about_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, coll_name, userContext);
90 coll_about_message.appendChild(coll_about_request);
91
92 Node coll_about_response_message = this.mr.process(coll_about_message);
93 Element coll_about_response = (Element) GSXML.getChildByTagName(coll_about_response_message, GSXML.RESPONSE_ELEM);
94 if (coll_about_response == null)
95 {
96 return;
97 }
98 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.COLLECTION_ELEM);
99 if (coll_description == null)
100 { // may be a cluster
101 coll_description = (Element) GSXML.getChildByTagName(coll_about_response, GSXML.CLUSTER_ELEM);
102 }
103
104 if (coll_description == null)
105 {
106 logger.error(" no collection description, returning");
107 return;
108 }
109 // have found one, append it to the page response
110 coll_description = (Element) doc.importNode(coll_description, true);
111 page_response.appendChild(coll_description);
112 get_service_description = true;
113 }
114
115
116 // have got a coll description
117 // now get the dispay info for the services
118 Element service_list = (Element) GSXML.getChildByTagName(coll_description, GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER);
119 if (service_list == null)
120 {
121 logger.error(" no service list, returning");
122 // something weird has gone wrong
123 return;
124 }
125
126 NodeList services = service_list.getElementsByTagName(GSXML.SERVICE_ELEM);
127 if (services.getLength() == 0)
128 {
129 logger.error("DefaultReceoptionist: no services found for colllection/cluster " + coll_name);
130 return;
131 }
132 // check one service for display items
133 if (!get_service_description)
134 {
135 // we dont know yet if we need to get these
136 int i = 1;
137 Element test_s = (Element) services.item(0);
138 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)))
139 {
140 test_s = (Element) services.item(i);
141 i++;
142 }
143 if (i == services.getLength())
144 {
145 // we have only found retrieve or oai services, so dont need descripitons anyway
146 return;
147 }
148 if (GSXML.getChildByTagName(test_s, GSXML.DISPLAY_TEXT_ELEM) != null)
149 {
150 // have got descriptions already,
151 return;
152 }
153 }
154
155 // if get here, we need to get the service descriptions
156
157 // we will send all the requests in a single message
158 Element info_message = msg_doc.createElement(GSXML.MESSAGE_ELEM);
159 for (int i = 0; i < services.getLength(); i++)
160 {
161 Element c = (Element) services.item(i);
162 String name = c.getAttribute(GSXML.NAME_ATT);
163 String address = GSPath.appendLink(coll_name, name);
164 Element info_request = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_DESCRIBE, address, userContext);
165 //Element req_param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
166 //req_param_list.appendChild(GSXML.createParameter(this.doc, GSXML.SUBSET_PARAM, GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER));
167 //info_request.appendChild(req_param_list);
168 info_message.appendChild(info_request);
169
170 }
171
172 Element info_response = (Element) this.mr.process(info_message);
173
174 NodeList service_responses = info_response.getElementsByTagName(GSXML.RESPONSE_ELEM);
175 // check that have same number of responses as collections
176 if (services.getLength() != service_responses.getLength())
177 {
178 logger.error(" didn't get a response for each service - somethings gone wrong!");
179 // for now, dont use the metadata
180 }
181 else
182 {
183 for (int i = 0; i < services.getLength(); i++)
184 {
185 Element c1 = (Element) services.item(i);
186 Element c2 = (Element) GSXML.getChildByTagName((Element) service_responses.item(i), GSXML.SERVICE_ELEM);
187 if (c1 != null && c2 != null && c1.getAttribute(GSXML.NAME_ATT).equals(c2.getAttribute(GSXML.NAME_ATT)))
188 {
189 //add the service data into the original response
190 GSXML.mergeElements(c1, c2);
191 }
192 else
193 {
194 logger.debug(" response does not correspond to request!");
195 }
196
197 }
198 }
199 }
200}
Note: See TracBrowser for help on using the repository browser.