1 | package org.greenstone.gsdl3.service;
|
---|
2 |
|
---|
3 |
|
---|
4 | // Greenstone classes
|
---|
5 | import org.greenstone.gsdl3.util.*;
|
---|
6 | import org.greenstone.gsdl3.collection.Collection;
|
---|
7 | import org.greenstone.util.GlobalProperties;
|
---|
8 |
|
---|
9 | // XML classes
|
---|
10 | import org.w3c.dom.Document;
|
---|
11 | import org.w3c.dom.Element;
|
---|
12 | import org.w3c.dom.Node;
|
---|
13 | import org.w3c.dom.Attr;
|
---|
14 | import org.w3c.dom.Text;
|
---|
15 | import org.w3c.dom.NodeList;
|
---|
16 | import org.w3c.dom.NamedNodeMap;
|
---|
17 | import org.w3c.dom.ProcessingInstruction;
|
---|
18 |
|
---|
19 | // General Java classes
|
---|
20 | import java.io.BufferedReader;
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileReader;
|
---|
23 | import java.io.Serializable;
|
---|
24 | import java.util.Vector;
|
---|
25 | import java.util.HashMap;
|
---|
26 | import java.util.Date;
|
---|
27 | import java.text.SimpleDateFormat;
|
---|
28 | import org.apache.log4j.*;
|
---|
29 |
|
---|
30 | public class RSSRetrieve extends ServiceRack {
|
---|
31 |
|
---|
32 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.RSSRetrieve.class.getName());
|
---|
33 | protected static final String RSS_SERVICE = "RSSFeed";
|
---|
34 |
|
---|
35 | public boolean configure(Element info, Element extra_info) {
|
---|
36 | if (!super.configure(info, extra_info)){
|
---|
37 | return false;
|
---|
38 | }
|
---|
39 | logger.info("configuring RSSRetrieve...");
|
---|
40 |
|
---|
41 | // set up short_service_info_ - for now just has name and type
|
---|
42 | Element rss_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
43 | rss_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
44 | rss_service.setAttribute(GSXML.NAME_ATT, RSS_SERVICE);
|
---|
45 | this.short_service_info.appendChild(rss_service);
|
---|
46 |
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 |
|
---|
50 | // this may get called but is not useful in the case of retrieve services
|
---|
51 | protected Element getServiceDescription(String service_id, String lang, String subset) {
|
---|
52 |
|
---|
53 | Element rss_service = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
54 | rss_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
55 | rss_service.setAttribute(GSXML.NAME_ATT, service_id);
|
---|
56 | return rss_service;
|
---|
57 | }
|
---|
58 |
|
---|
59 | // Sends off a collection 'describe' message and returns the <collection> element of the response.
|
---|
60 | // This contains the collection meta from collectionConfig.xml. Used to construct header of RSS feed
|
---|
61 | protected Element getCollMetadata(UserContext userContext) {
|
---|
62 | Element mr_request_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
63 | String to = this.cluster_name;
|
---|
64 | Element meta_request = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, to, userContext);
|
---|
65 | mr_request_message.appendChild(meta_request);
|
---|
66 | Element meta_response = (Element) this.router.process(mr_request_message);
|
---|
67 | meta_response = (Element) GSXML.getChildByTagName(meta_response, GSXML.RESPONSE_ELEM);
|
---|
68 |
|
---|
69 | NodeList nl = meta_response.getElementsByTagName(GSXML.COLLECTION_ELEM);
|
---|
70 | Element collectionEl = (Element) nl.item(0);
|
---|
71 | return collectionEl;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | Generates the RSS feed XML by creating the header and footer with the contents
|
---|
77 | of the the collection's index/rss-items.rdf file embedded in the middle.
|
---|
78 | @return the RSS feed XML.
|
---|
79 | @see http://cyber.law.harvard.edu/rss/rss.html
|
---|
80 | */
|
---|
81 | protected Element processRSSFeed(Element request) {
|
---|
82 |
|
---|
83 | // Ask the MessageRouter for this collection's colConfig metadata
|
---|
84 | // from which the RSS header values will be constructed
|
---|
85 | UserContext userContext = new UserContext(request);
|
---|
86 | Element collMeta = getCollMetadata(userContext);
|
---|
87 |
|
---|
88 | //logger.error("**** collection metadata:");
|
---|
89 | //GSXML.elementToLogAsString(collMeta, true);
|
---|
90 |
|
---|
91 | // work out some commonly used variables such as lang and url_prefix
|
---|
92 | String lang = request.getAttribute("lang");
|
---|
93 | if(lang.equals("")) {
|
---|
94 | lang = "en";
|
---|
95 | }
|
---|
96 |
|
---|
97 | // url_prefix is of the form http://domain/greenstone3/library/collection/_colname_/
|
---|
98 | String url_prefix = GlobalProperties.getFullGSDL3WebAddress()+"/"+this.library_name+"/collection/"+this.cluster_name;
|
---|
99 |
|
---|
100 |
|
---|
101 | // generate the header and footer
|
---|
102 | Document rssDoc = this.doc;
|
---|
103 |
|
---|
104 | Element rssNode = rssDoc.createElement("rss"); // rootnode
|
---|
105 | rssNode.setAttribute("version", "2.0");
|
---|
106 |
|
---|
107 | String namespace_url = "http://www.w3.org/2000/xmlns/";
|
---|
108 | rssNode.setAttributeNS(namespace_url, "xmlns:content", "http://purl.org/rss/1.0/modules/content/");
|
---|
109 | rssNode.setAttributeNS(namespace_url, "xmlns:taxo", "http://purl.org/rss/1.0/modules/taxonomy/");
|
---|
110 | rssNode.setAttributeNS(namespace_url, "xmlns:dc", "http://purl.org/dc/elements/1.1/");
|
---|
111 | rssNode.setAttributeNS(namespace_url, "xmlns:syn", "http://purl.org/rss/1.0/modules/syndication/");
|
---|
112 | rssNode.setAttributeNS(namespace_url, "xmlns:admin", "http://webns.net/mvcb/");
|
---|
113 | rssDoc.appendChild(rssNode);
|
---|
114 |
|
---|
115 | // Setting the preproccessing header line (Utf-8) will be done in web/interfaces' rss.xsl
|
---|
116 | //ProcessingInstruction procInstruction = doc.createProcessingInstruction("xml","version=\"1.0\"");
|
---|
117 | //rssDoc.appendChild(procInstruction);
|
---|
118 |
|
---|
119 | Element channelNode = rssDoc.createElement("channel");
|
---|
120 | rssNode.appendChild(channelNode);
|
---|
121 |
|
---|
122 | Element childNode = rssDoc.createElement("title");
|
---|
123 | GSXML.setNodeText(childNode, this.cluster_name); //_collectionname_
|
---|
124 | channelNode.appendChild(childNode);
|
---|
125 |
|
---|
126 | // _httppageabout_: of form http://domain/greenstone3/library/collection/_colname_/page/about
|
---|
127 | childNode = rssDoc.createElement("link");
|
---|
128 | GSXML.setNodeText(childNode, url_prefix+"/page/about"); // _httppageabout_
|
---|
129 | channelNode.appendChild(childNode);
|
---|
130 |
|
---|
131 | // get the description string for the requested language, else fallback on en description if present
|
---|
132 | childNode = rssDoc.createElement("description");
|
---|
133 | NodeList descriptions = GSXML.getNamedElements(collMeta, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, GSXML.DISPLAY_TEXT_DESCRIPTION);
|
---|
134 | //Element descriptEl = GSXML.getNamedElement(collMeta, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, GSXML.DISPLAY_TEXT_DESCRIPTION);
|
---|
135 | Element descriptEl = null;
|
---|
136 | if(descriptions != null) {
|
---|
137 | for (int i = 0; i < descriptions.getLength(); i++) {
|
---|
138 | Element e = (Element) descriptions.item(i);
|
---|
139 | if(e.getAttribute("lang").equals(lang)) {
|
---|
140 | descriptEl = e;
|
---|
141 | break; // found the description for the requested language, finish loop
|
---|
142 | } else if(e.getAttribute("lang").equals("en")) {
|
---|
143 | descriptEl = e; // at least found english fall-back description, continue loop
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | String description = (descriptEl == null) ? "none" : GSXML.getNodeText(descriptEl);
|
---|
148 | GSXML.setNodeText(childNode, description); //_collectionextra_
|
---|
149 | channelNode.appendChild(childNode);
|
---|
150 |
|
---|
151 | childNode = rssDoc.createElement("language");
|
---|
152 | GSXML.setNodeText(childNode, lang); //_cgiargl_
|
---|
153 | channelNode.appendChild(childNode);
|
---|
154 |
|
---|
155 |
|
---|
156 | // RSS specification: http://cyber.law.harvard.edu/rss/rss.html
|
---|
157 | // pubDate is date of first publication of the item. Use collection.getEarliestDatestamp()
|
---|
158 | // lastBuildDate is the date the item was last modified. Use collection.getLastmodified()
|
---|
159 |
|
---|
160 | //HashMap<String, ModuleInterface> module_map = this.router.getModuleMap();
|
---|
161 | //Collection coll = (Collection)module_map.get(this.cluster_name);
|
---|
162 | Collection coll = (Collection)serviceCluster;
|
---|
163 | SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
|
---|
164 |
|
---|
165 | childNode = rssDoc.createElement("pubDate");
|
---|
166 | Date date = new Date(coll.getEarliestDatestamp()); // "Thu, 23 Aug 1999 07:00:00 GMT"
|
---|
167 | GSXML.setNodeText(childNode, dateFormat.format(date));
|
---|
168 | channelNode.appendChild(childNode);
|
---|
169 |
|
---|
170 | childNode = rssDoc.createElement("lastBuildDate");
|
---|
171 | date = new Date(coll.getLastmodified()); // "Thu, 23 Aug 1999 16:20:26 GMT"
|
---|
172 | GSXML.setNodeText(childNode, dateFormat.format(date));
|
---|
173 | channelNode.appendChild(childNode);
|
---|
174 |
|
---|
175 | childNode = rssDoc.createElement("managingEditor");
|
---|
176 | Element e = GSXML.getNamedElement(collMeta, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, "creator");
|
---|
177 | String value = (e == null) ? "unknown" : GSXML.getNodeText(e);
|
---|
178 | GSXML.setNodeText(childNode, value); //_creator_
|
---|
179 | channelNode.appendChild(childNode);
|
---|
180 |
|
---|
181 | childNode = rssDoc.createElement("webMaster");
|
---|
182 | e = GSXML.getNamedElement(collMeta, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, "maintainer");
|
---|
183 | value = (e == null) ? "unknown" : GSXML.getNodeText(e);
|
---|
184 | GSXML.setNodeText(childNode, value); //_maintainer_
|
---|
185 | channelNode.appendChild(childNode);
|
---|
186 |
|
---|
187 |
|
---|
188 | // <image> child of <channel> has title, url, link and description children
|
---|
189 | Element collIcon = GSXML.getNamedElement(collMeta, GSXML.DISPLAY_TEXT_ELEM, GSXML.NAME_ATT, "icon");
|
---|
190 | if(collIcon != null) { // since there is a collection image, create an imageNode
|
---|
191 |
|
---|
192 | Node imageNode = rssDoc.createElement("image");
|
---|
193 | channelNode.appendChild(imageNode);
|
---|
194 |
|
---|
195 | childNode = rssDoc.createElement("title");
|
---|
196 | GSXML.setNodeText(childNode, this.cluster_name); //_collectionname_
|
---|
197 | imageNode.appendChild(childNode);
|
---|
198 |
|
---|
199 | // need full URL for collection icon. Its name is in <displayItem name="icon_name.ext"/>
|
---|
200 | // URL is of the form domain/servlet/sites/localsite/collect/lucene-jdbm-demo/images/icon_name.ext
|
---|
201 | childNode = rssDoc.createElement("url");
|
---|
202 | String domain = GlobalProperties.getFullGSDL3WebAddress(); // remove servlet as it's included in site_http_address
|
---|
203 | domain = domain.substring(0, domain.lastIndexOf("/"));
|
---|
204 | String image_url = GSXML.getNodeText(collIcon); // name of image file
|
---|
205 | image_url = domain+"/"+this.site_http_address+"/"+"/collect/"+this.cluster_name+"/images/"+image_url;
|
---|
206 | GSXML.setNodeText(childNode, image_url); // _iconcollection_
|
---|
207 | imageNode.appendChild(childNode);
|
---|
208 |
|
---|
209 | childNode = rssDoc.createElement("link");
|
---|
210 | GSXML.setNodeText(childNode, url_prefix+"/page/about"); // _httppageabout_
|
---|
211 | imageNode.appendChild(childNode);
|
---|
212 |
|
---|
213 | childNode = rssDoc.createElement("description");
|
---|
214 | GSXML.setNodeText(childNode, description); //_collectionextra_
|
---|
215 | imageNode.appendChild(childNode);
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | // now add the contents of rss-items.rdf as a child of channel,
|
---|
220 | // passing in url_prefix for url resolution
|
---|
221 | Element rss_raw_data = loadDocument("rss-items.rdf", url_prefix);
|
---|
222 | if(rss_raw_data != null) {
|
---|
223 | NodeList rss_items = rss_raw_data.getElementsByTagName("item");
|
---|
224 | for(int i = 0; i < rss_items.getLength(); i++) {
|
---|
225 | channelNode.appendChild(rssDoc.importNode(rss_items.item(i), true));
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | // generate the GS3 response message containing the RSS xml
|
---|
230 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
231 | result.setAttribute(GSXML.FROM_ATT, RSS_SERVICE);
|
---|
232 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
233 | result.appendChild(rssNode); // body of <response> is simply the <rss> root element of the RSS feed
|
---|
234 | return result;
|
---|
235 |
|
---|
236 | }
|
---|
237 |
|
---|
238 | // load contents of rss-items.rdf file into an XML doc in memory, after doing the url_prefix replacements
|
---|
239 | protected Element loadDocument(String doc_name, String url_prefix) {
|
---|
240 | String document_encoding = "UTF-8";
|
---|
241 |
|
---|
242 | // try to find the document
|
---|
243 | File doc_file = new File(GSFile.collectionIndexDir(this.site_home, this.cluster_name)+File.separator+doc_name);
|
---|
244 |
|
---|
245 | if (!doc_file.exists()) {
|
---|
246 | logger.info("couldn't find file in coll "+this.cluster_name +", file "+doc_name);
|
---|
247 | return null;
|
---|
248 | }
|
---|
249 |
|
---|
250 | // the rss-items.rdf file has no root element, only multiple <item> elements (and subelements)
|
---|
251 | // Without a root element, it can't be read into a DOM object. So we read it into a regular String,
|
---|
252 | // then bookend the contents with temporary <rssrawdata></rssrawdata> elements to provide a root
|
---|
253 | // element and can read in that String as a DOM object.
|
---|
254 |
|
---|
255 | StringBuffer contents = new StringBuffer("<rssrawdata>\n");
|
---|
256 | try {
|
---|
257 | BufferedReader in = new BufferedReader(new FileReader(doc_file));
|
---|
258 | String line = null;
|
---|
259 | while((line = in.readLine()) != null) {
|
---|
260 | //line = line.replace("_httpcollection_", "/"+this.cluster_name);
|
---|
261 | line = line.replace("_httpdomain__httpcollection_", url_prefix);
|
---|
262 | contents.append(line);
|
---|
263 | }
|
---|
264 | contents.append("</rssrawdata>");
|
---|
265 | in.close(); // close the fileread handle
|
---|
266 |
|
---|
267 | } catch (Exception e) {
|
---|
268 | e.printStackTrace();
|
---|
269 | contents.append("couldn't read ");
|
---|
270 | contents.append(doc_file);
|
---|
271 | contents.append("\n</rssrawdata>");
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | Document the_doc = null;
|
---|
276 | try {
|
---|
277 | the_doc = this.converter.getDOM(contents.toString()); // String input converted to DOM
|
---|
278 | } catch (Exception e) {
|
---|
279 | logger.error("couldn't create a DOM from file "+doc_file.getPath());
|
---|
280 | return null;
|
---|
281 | }
|
---|
282 |
|
---|
283 | return the_doc.getDocumentElement();
|
---|
284 |
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | }
|
---|
289 |
|
---|