1 | /*
|
---|
2 | * OAIPMH.java
|
---|
3 | * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.service;
|
---|
20 |
|
---|
21 | // Greenstone classes
|
---|
22 | import org.greenstone.gsdl3.core.GSException;
|
---|
23 | import org.greenstone.gsdl3.util.GSXML;
|
---|
24 | import org.greenstone.gsdl3.util.OAIXML;
|
---|
25 | import org.greenstone.gsdl3.util.OID;
|
---|
26 | import org.greenstone.gsdl3.util.GSFile;
|
---|
27 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
28 |
|
---|
29 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
30 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
31 | // XML classes
|
---|
32 | import org.w3c.dom.Document;
|
---|
33 | import org.w3c.dom.Element;
|
---|
34 | import org.w3c.dom.NodeList;
|
---|
35 |
|
---|
36 | // General Java classes
|
---|
37 | import java.io.File;
|
---|
38 | import java.util.StringTokenizer;
|
---|
39 | import java.util.Vector;
|
---|
40 | import java.util.Set;
|
---|
41 | import java.util.Iterator;
|
---|
42 | import java.util.ArrayList;
|
---|
43 | import java.util.Date;
|
---|
44 | import java.util.HashMap;
|
---|
45 | import java.util.Map.Entry;
|
---|
46 |
|
---|
47 | import org.apache.log4j.Logger;
|
---|
48 |
|
---|
49 | /** Implements the oai metadata retrieval service for GS3 collections.
|
---|
50 | * Dig into each collection's database and retrieve the metadata
|
---|
51 | *
|
---|
52 | * @author <a href="mailto:[email protected]">Xiao</a>
|
---|
53 | */
|
---|
54 |
|
---|
55 | public class OAIPMH extends ServiceRack {
|
---|
56 |
|
---|
57 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.OAIPMH.class.getName());
|
---|
58 |
|
---|
59 | protected SimpleCollectionDatabase coll_db = null;
|
---|
60 |
|
---|
61 | protected String site_name = "";
|
---|
62 | protected String coll_name = "";
|
---|
63 | protected Element coll_config_xml = null;
|
---|
64 |
|
---|
65 | /** constructor */
|
---|
66 | public OAIPMH() {
|
---|
67 |
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void cleanUp() {
|
---|
71 | super.cleanUp();//??
|
---|
72 | this.coll_db.closeDatabase();
|
---|
73 | }
|
---|
74 | /** configure this service */
|
---|
75 | public boolean configure(Element info, Element extra_info) {
|
---|
76 | if (!super.configure(info, extra_info)){
|
---|
77 | logger.info("Configuring ServiceRack.java returns false.");
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | //get the names from ServiceRack.java
|
---|
82 | site_name = this.router.getSiteName();
|
---|
83 | coll_name = this.cluster_name;
|
---|
84 | //get the collection-specific configurations from collectionConfig.xml
|
---|
85 | coll_config_xml = OAIXML.getCollectionConfigXML(site_name, coll_name);
|
---|
86 |
|
---|
87 | logger.info("Configuring OAIPMH...");
|
---|
88 | // this call passes the indexStem in of ServiceRack element in buildConfig.xml to the super class.
|
---|
89 | this.config_info = info;
|
---|
90 |
|
---|
91 | // the index stem is either specified in the buildConfig.xml file or uses the collection name
|
---|
92 | Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
|
---|
93 | String index_stem = null;
|
---|
94 | if (index_stem_elem != null) {
|
---|
95 | index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
|
---|
96 | }
|
---|
97 | if (index_stem == null || index_stem.equals("")) {
|
---|
98 | index_stem = this.cluster_name;
|
---|
99 | }
|
---|
100 |
|
---|
101 | // find out what kind of database we have
|
---|
102 | Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
|
---|
103 | String database_type = null;
|
---|
104 | if (database_type_elem != null) {
|
---|
105 | database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
|
---|
106 | }
|
---|
107 | if (database_type == null || database_type.equals("")) {
|
---|
108 | database_type = "gdbm"; // the default
|
---|
109 | }
|
---|
110 | coll_db = new SimpleCollectionDatabase(database_type);
|
---|
111 | if (coll_db == null) {
|
---|
112 | logger.error("Couldn't create the collection database of type "+database_type);
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 |
|
---|
116 | // Open database for querying
|
---|
117 | String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, index_stem, database_type);
|
---|
118 | if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
|
---|
119 | logger.error("Could not open collection database!");
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 |
|
---|
123 | // the short_service_info is used by the message router to find the method names,
|
---|
124 | //so we just use the doc variable in class ServiceRack to create the xml; but
|
---|
125 | // in each method we will use OAIXML to create the response xml
|
---|
126 | // set up short_service_info_ - just the name
|
---|
127 |
|
---|
128 | Element identify = this.doc.createElement(OAIXML.SERVICE);
|
---|
129 | //add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // why do we need this?
|
---|
130 | identify.setAttribute(OAIXML.NAME, OAIXML.IDENTIFY);
|
---|
131 | this.short_service_info.appendChild(identify);
|
---|
132 |
|
---|
133 | Element list_records = this.doc.createElement(OAIXML.SERVICE);
|
---|
134 | list_records.setAttribute(OAIXML.NAME, OAIXML.LIST_RECORDS);
|
---|
135 | this.short_service_info.appendChild(list_records);
|
---|
136 |
|
---|
137 | Element list_identifiers = this.doc.createElement(OAIXML.SERVICE);
|
---|
138 | list_identifiers.setAttribute(OAIXML.NAME, OAIXML.LIST_IDENTIFIERS);
|
---|
139 | this.short_service_info.appendChild(list_identifiers);
|
---|
140 |
|
---|
141 | Element list_sets = this.doc.createElement(OAIXML.SERVICE);
|
---|
142 | list_sets.setAttribute(OAIXML.NAME, OAIXML.LIST_SETS);
|
---|
143 | this.short_service_info.appendChild(list_sets);
|
---|
144 |
|
---|
145 | Element list_metadata_formats = this.doc.createElement(OAIXML.SERVICE);
|
---|
146 | list_metadata_formats.setAttribute(OAIXML.NAME, OAIXML.LIST_METADATA_FORMATS);
|
---|
147 | this.short_service_info.appendChild(list_metadata_formats);
|
---|
148 |
|
---|
149 | Element get_record = this.doc.createElement(OAIXML.SERVICE);
|
---|
150 | get_record.setAttribute(OAIXML.NAME, OAIXML.GET_RECORD);
|
---|
151 | this.short_service_info.appendChild(get_record);
|
---|
152 |
|
---|
153 | return true;
|
---|
154 | }
|
---|
155 | /** returns a specific service description */
|
---|
156 | public Element getServiceDescription(String service_id, String lang, String subset) {
|
---|
157 |
|
---|
158 | if (service_id.equals(OAIXML.IDENTIFY)) {
|
---|
159 | Element identify = this.doc.createElement(OAIXML.SERVICE);
|
---|
160 | //add_service.setAttribute(GSXML.TYPE_ATT, "gather"); // why do we need this?
|
---|
161 | identify.setAttribute(OAIXML.NAME, OAIXML.IDENTIFY);
|
---|
162 | return identify;
|
---|
163 | }
|
---|
164 | if (service_id.equals(OAIXML.LIST_RECORDS)) {
|
---|
165 | Element list_records = this.doc.createElement(OAIXML.SERVICE);
|
---|
166 | list_records.setAttribute(OAIXML.NAME, OAIXML.LIST_RECORDS);
|
---|
167 | return list_records;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (service_id.equals(OAIXML.LIST_IDENTIFIERS)) {
|
---|
171 | Element list_identifiers = this.doc.createElement(OAIXML.SERVICE);
|
---|
172 | list_identifiers.setAttribute(OAIXML.NAME, OAIXML.LIST_IDENTIFIERS);
|
---|
173 | return list_identifiers;
|
---|
174 | }
|
---|
175 | if (service_id.equals(OAIXML.LIST_SETS)) {
|
---|
176 | Element list_sets = this.doc.createElement(OAIXML.SERVICE);
|
---|
177 | list_sets.setAttribute(OAIXML.NAME, OAIXML.LIST_SETS);
|
---|
178 | return list_sets;
|
---|
179 | }
|
---|
180 | if (service_id.equals(OAIXML.LIST_METADATA_FORMATS)) {
|
---|
181 | Element list_metadata_formats = this.doc.createElement(OAIXML.SERVICE);
|
---|
182 | list_metadata_formats.setAttribute(OAIXML.NAME, OAIXML.LIST_METADATA_FORMATS);
|
---|
183 | return list_metadata_formats;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (service_id.equals(OAIXML.GET_RECORD)) {
|
---|
187 | Element get_record = this.doc.createElement(OAIXML.SERVICE);
|
---|
188 | get_record.setAttribute(OAIXML.NAME, OAIXML.GET_RECORD);
|
---|
189 | return get_record;
|
---|
190 | }
|
---|
191 |
|
---|
192 | return null;
|
---|
193 | }
|
---|
194 | /** return the metadata information about this set of the repository */
|
---|
195 | protected Element processIdentify(Element req) {
|
---|
196 | return null;
|
---|
197 | }
|
---|
198 | /** return the metadata information */
|
---|
199 | protected Element processListSets(Element req) {
|
---|
200 | //This method is never called unless each set in the returned message contain a
|
---|
201 | //'description' element so that we need to ask each collection for their info
|
---|
202 | return null;
|
---|
203 | }
|
---|
204 | /** return the metadata information */
|
---|
205 | protected Element processGetRecord(Element req) {
|
---|
206 | /** arguments:
|
---|
207 | identifier: required
|
---|
208 | metadataPrefix: required
|
---|
209 | * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
|
---|
210 | */
|
---|
211 | NodeList params = GSXML.getChildrenByTagName(req, OAIXML.PARAM);
|
---|
212 | HashMap param_map = OAIXML.getParamMap(params);
|
---|
213 |
|
---|
214 | String prefix = (String)param_map.get(OAIXML.METADATA_PREFIX);
|
---|
215 | if (prefix == null || prefix.equals("")) {
|
---|
216 | //Just a double-check
|
---|
217 | logger.error("the value of metadataPrefix att is not present in the request.");
|
---|
218 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
219 | }
|
---|
220 |
|
---|
221 | Element metadata_format = getMetadataFormatElement(prefix);
|
---|
222 | if(metadata_format == null) {
|
---|
223 | logger.error("metadata prefix is not supported.");
|
---|
224 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
225 | }
|
---|
226 |
|
---|
227 | String oid = (String)param_map.get(OAIXML.OID);
|
---|
228 |
|
---|
229 | //get a DBInfo object of the identifier; if this identifier is not present in the database,
|
---|
230 | // null is returned.
|
---|
231 | DBInfo info = this.coll_db.getInfo(oid);
|
---|
232 | if (info == null) {
|
---|
233 | logger.error("OID: " + oid + " is not present in the database.");
|
---|
234 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.ID_DOES_NOT_EXIST, ""));
|
---|
235 | }
|
---|
236 |
|
---|
237 | ArrayList keys = new ArrayList(info.getKeys());
|
---|
238 | String lastmodified = "";
|
---|
239 | if(keys.contains(OAIXML.LASTMODIFIED)) {
|
---|
240 | lastmodified = info.getInfo(OAIXML.LASTMODIFIED);
|
---|
241 | }
|
---|
242 | lastmodified = OAIXML.getTime(Long.parseLong(lastmodified));
|
---|
243 |
|
---|
244 | Element get_record = OAIXML.createElement(OAIXML.GET_RECORD);
|
---|
245 | Element record = OAIXML.createElement(OAIXML.RECORD);
|
---|
246 | //compose the header element
|
---|
247 | record.appendChild(createHeaderElement(oid, lastmodified));
|
---|
248 | //compose the metadata element
|
---|
249 | record.appendChild(createMetadataElement(prefix, info, metadata_format));
|
---|
250 | get_record.appendChild(record);
|
---|
251 | return OAIXML.getResponse(get_record);
|
---|
252 | }
|
---|
253 | /** return a list of identifiers */
|
---|
254 | protected Element processListIdentifiers(Element req) {
|
---|
255 | /** arguments:
|
---|
256 | metadataPrefix: required
|
---|
257 | * from: optional
|
---|
258 | * until: optional
|
---|
259 | * set: optional
|
---|
260 | * resumptionToken: exclusive and optional (ignored as it has been handled by OAIReceptionist)
|
---|
261 | * Exceptions: badArgument; cannotDisseminateFormat; idDoesNotExist
|
---|
262 | */
|
---|
263 | NodeList params = GSXML.getChildrenByTagName(req, OAIXML.PARAM);
|
---|
264 |
|
---|
265 | if(params.getLength() == 0) {
|
---|
266 | logger.error("must at least have the metadataPrefix parameter, can't be none");
|
---|
267 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.BAD_ARGUMENT, ""));
|
---|
268 | }
|
---|
269 |
|
---|
270 | HashMap param_map = OAIXML.getParamMap(params);
|
---|
271 |
|
---|
272 | String prefix = "";
|
---|
273 | Date from_date = null;
|
---|
274 | Date until_date = null;
|
---|
275 |
|
---|
276 | if(param_map.containsKey(OAIXML.METADATA_PREFIX) == false) {
|
---|
277 | //Just a double-check
|
---|
278 | logger.error("A param element containing the metadataPrefix is not present.");
|
---|
279 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
280 | }
|
---|
281 | prefix = (String)param_map.get(OAIXML.METADATA_PREFIX);
|
---|
282 | if (prefix == null || prefix.equals("")) {
|
---|
283 | //Just a double-check
|
---|
284 | logger.error("the value of metadataPrefix att is not present in the request.");
|
---|
285 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
286 | }
|
---|
287 |
|
---|
288 | if(param_map.containsKey(OAIXML.FROM)) {
|
---|
289 | String from = (String)param_map.get(OAIXML.FROM);
|
---|
290 | from_date = OAIXML.getDate(from);
|
---|
291 | }
|
---|
292 | if(param_map.containsKey(OAIXML.UNTIL)) {
|
---|
293 | String until = (String)param_map.get(OAIXML.UNTIL);
|
---|
294 | until_date = OAIXML.getDate(until);
|
---|
295 | }
|
---|
296 |
|
---|
297 | Element metadata_format = getMetadataFormatElement(prefix);
|
---|
298 | if(metadata_format == null) {
|
---|
299 | logger.error("metadata prefix is not supported.");
|
---|
300 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
301 | }
|
---|
302 | ArrayList oid_list = getChildrenIds(OAIXML.BROWSELIST);
|
---|
303 | if (oid_list == null) {
|
---|
304 | logger.error("No matched records found in collection: browselist is empty");
|
---|
305 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.NO_RECORDS_MATCH, ""));
|
---|
306 | }
|
---|
307 | // all validation is done
|
---|
308 | Element list_identifiers = OAIXML.createElement(OAIXML.LIST_IDENTIFIERS);
|
---|
309 | for(int i=0; i<oid_list.size(); i++) {
|
---|
310 | String oid = (String)oid_list.get(i);
|
---|
311 | DBInfo info = this.coll_db.getInfo(oid);
|
---|
312 | if (info == null) {
|
---|
313 | logger.error("Database does not contains information about oid: " +oid);
|
---|
314 | continue;
|
---|
315 | }
|
---|
316 | ArrayList keys = new ArrayList(info.getKeys());
|
---|
317 | String lastmodified = "";
|
---|
318 | if(keys.contains(OAIXML.LASTMODIFIED)) {
|
---|
319 | lastmodified = info.getInfo(OAIXML.LASTMODIFIED);
|
---|
320 | }
|
---|
321 | lastmodified = OAIXML.getTime(Long.parseLong(lastmodified));
|
---|
322 |
|
---|
323 | Date this_date = OAIXML.getDate(lastmodified);
|
---|
324 | if (from_date != null) {
|
---|
325 | if(this_date.before(from_date)) {
|
---|
326 | continue;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | if (until_date != null) {
|
---|
330 | if (this_date.after(until_date)) {
|
---|
331 | continue;
|
---|
332 | }
|
---|
333 | }
|
---|
334 | //compose the header element and append it
|
---|
335 | list_identifiers.appendChild(createHeaderElement(oid, lastmodified));
|
---|
336 | }//end of for(int i=0; i<oid_list.size(); i++) of doing thru each record
|
---|
337 |
|
---|
338 | return OAIXML.getResponse(list_identifiers);
|
---|
339 | }
|
---|
340 | /** return a list of records */
|
---|
341 | protected Element processListRecords(Element req) {
|
---|
342 | /** the request sent here may contain optional 'from', 'untill', 'metadataPrefix',
|
---|
343 | * and 'resumptionToken' params. see doListSets() in OAIReceptionist.
|
---|
344 | * if the request contains 'resumptionToken' then it should have been handled by the
|
---|
345 | * OAIReceptionist. Therefore, the request sent here must not contain 'resumptionToken'
|
---|
346 | * argument but a 'metadataPrefix' param. The OAIReceptionist makes sure of this.
|
---|
347 | */
|
---|
348 | NodeList params = GSXML.getChildrenByTagName(req, OAIXML.PARAM);
|
---|
349 |
|
---|
350 | if(params.getLength() == 0) {
|
---|
351 | logger.error("must at least have the metadataPrefix parameter, can't be none");
|
---|
352 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.BAD_ARGUMENT, ""));
|
---|
353 | }
|
---|
354 |
|
---|
355 | HashMap param_map = OAIXML.getParamMap(params);
|
---|
356 |
|
---|
357 | String prefix = "";
|
---|
358 | Date from_date = null;
|
---|
359 | Date until_date = null;
|
---|
360 |
|
---|
361 | if(param_map.containsKey(OAIXML.METADATA_PREFIX) == false) {
|
---|
362 | //Just a double-check
|
---|
363 | logger.error("A param element containing the metadataPrefix is not present.");
|
---|
364 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
365 | }
|
---|
366 | prefix = (String)param_map.get(OAIXML.METADATA_PREFIX);
|
---|
367 | if (prefix == null || prefix.equals("")) {
|
---|
368 | //Just a double-check
|
---|
369 | logger.error("the value of metadataPrefix att is not present in the request.");
|
---|
370 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
371 | }
|
---|
372 |
|
---|
373 | if(param_map.containsKey(OAIXML.FROM)) {
|
---|
374 | String from = (String)param_map.get(OAIXML.FROM);
|
---|
375 | from_date = OAIXML.getDate(from);
|
---|
376 | }
|
---|
377 | if(param_map.containsKey(OAIXML.UNTIL)) {
|
---|
378 | String until = (String)param_map.get(OAIXML.UNTIL);
|
---|
379 | until_date = OAIXML.getDate(until);
|
---|
380 | }
|
---|
381 | Element metadata_format = getMetadataFormatElement(prefix);
|
---|
382 | if(metadata_format == null) {
|
---|
383 | logger.error("metadata prefix is not supported.");
|
---|
384 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
385 | }
|
---|
386 | // Another way of doing the same job!
|
---|
387 | // HashMap prefix_map = OAIXML.getChildrenMapByTagName(coll_config_xml, OAIXML.METADATA_PREFIX);
|
---|
388 | // if(!prefix_map.contains(prefix)) {
|
---|
389 | // logger.error("metadata prefix is not supported.");
|
---|
390 | // return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.CANNOT_DISSEMINATE_FORMAT, ""));
|
---|
391 | // }
|
---|
392 |
|
---|
393 | //get a list of identifiers (it contains a list of strings)
|
---|
394 | ArrayList oid_list = getChildrenIds(OAIXML.BROWSELIST);
|
---|
395 | if (oid_list == null) {
|
---|
396 | logger.error("No matched records found in collection: browselist is empty");
|
---|
397 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.NO_RECORDS_MATCH, ""));
|
---|
398 | }
|
---|
399 | // all validation is done
|
---|
400 | Element list_records = OAIXML.createElement(OAIXML.LIST_RECORDS);
|
---|
401 | for(int i=0; i<oid_list.size(); i++) {
|
---|
402 | String oid = (String)oid_list.get(i);
|
---|
403 | DBInfo info = this.coll_db.getInfo(oid);
|
---|
404 | if (info == null) {
|
---|
405 | logger.error("Database does not contains information about oid: " +oid);
|
---|
406 | continue;
|
---|
407 | }
|
---|
408 | ArrayList keys = new ArrayList(info.getKeys());
|
---|
409 | String lastmodified = "";
|
---|
410 | if(keys.contains(OAIXML.LASTMODIFIED)) {
|
---|
411 | lastmodified = info.getInfo(OAIXML.LASTMODIFIED);
|
---|
412 | }
|
---|
413 | lastmodified = OAIXML.getTime(Long.parseLong(lastmodified));
|
---|
414 |
|
---|
415 | Date this_date = OAIXML.getDate(lastmodified);
|
---|
416 | if (from_date != null) {
|
---|
417 | if(this_date.before(from_date)) {
|
---|
418 | continue;
|
---|
419 | }
|
---|
420 | }
|
---|
421 | if (until_date != null) {
|
---|
422 | if (this_date.after(until_date)) {
|
---|
423 | continue;
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | Element record = OAIXML.createElement(OAIXML.RECORD);
|
---|
428 | list_records.appendChild(record);
|
---|
429 | //compose the header element
|
---|
430 | record.appendChild(createHeaderElement(oid, lastmodified));
|
---|
431 | //compose the metadata element
|
---|
432 | record.appendChild(createMetadataElement(prefix, info, metadata_format));
|
---|
433 |
|
---|
434 | }//end of for(int i=0; i<oid_list.size(); i++) of doing thru each record
|
---|
435 |
|
---|
436 | return OAIXML.getResponse(list_records);
|
---|
437 | }
|
---|
438 |
|
---|
439 | /** get the metadataFormat element from the collectionConfig.xml containing the specified metadata prefix.
|
---|
440 | * return null if not found.
|
---|
441 | */
|
---|
442 | private Element getMetadataFormatElement(String prefix) {
|
---|
443 | Element oai = (Element)GSXML.getChildByTagName(this.coll_config_xml, OAIXML.OAI);
|
---|
444 | Element list_meta_format = (Element)GSXML.getChildByTagName(oai, OAIXML.LIST_METADATA_FORMATS);
|
---|
445 | Element metadata_format = GSXML.getNamedElement(list_meta_format, OAIXML.METADATA_FORMAT, OAIXML.METADATA_PREFIX, prefix);
|
---|
446 | return metadata_format;
|
---|
447 | }
|
---|
448 | /** create the metadata element used when processing ListRecords/GetRecord requests
|
---|
449 | */
|
---|
450 | private Element createMetadataElement(String prefix, DBInfo info, Element metadata_format) {
|
---|
451 | //the prefix string is in the form: oai_dc, for example.
|
---|
452 | String prfx_str = "";
|
---|
453 | //the metadata namespace used to retrieve metadata in the repository
|
---|
454 | //For example, if the prefix is like 'oai_ex' then we used 'ex' to get the metadata
|
---|
455 | //Normally we would use 'dc' to find metadata.
|
---|
456 | String meta_ns = "";
|
---|
457 | if(prefix.equals(OAIXML.OAI_DC)) {
|
---|
458 | if(OAIXML.oai_version.equals(OAIXML.OAI_VERSION2)) {
|
---|
459 | prfx_str = prefix + ":" + OAIXML.DC;
|
---|
460 | } else {
|
---|
461 | prfx_str = OAIXML.DC;//oai version 1
|
---|
462 | }
|
---|
463 | meta_ns = OAIXML.DC;
|
---|
464 | } else {
|
---|
465 | prfx_str = prefix.substring(prefix.indexOf("_") + 1);
|
---|
466 | meta_ns = prfx_str;
|
---|
467 | }
|
---|
468 | //create the <metadata> element
|
---|
469 | //OAIXML.oai_version is read from OAIConfig.xml and its default value is "2.0"
|
---|
470 | Element prfx_str_elem = OAIXML.getMetadataPrefixElement(prfx_str, OAIXML.oai_version);
|
---|
471 | String[] metadata_names = getMetadataNames(metadata_format);
|
---|
472 | HashMap meta_map = getInfoByNames(info, metadata_names);
|
---|
473 | ArrayList meta_list = new ArrayList(meta_map.entrySet());
|
---|
474 | for (int j=0; j<meta_list.size(); j++) {
|
---|
475 | Entry men = (Entry)meta_list.get(j);
|
---|
476 | String meta_name = (String)men.getKey();
|
---|
477 | String meta_value = (String)men.getValue();
|
---|
478 | Element e = OAIXML.createElement(meta_name);
|
---|
479 | GSXML.setNodeText(e, meta_value);
|
---|
480 | prfx_str_elem.appendChild(e);
|
---|
481 | }
|
---|
482 | Element metadata = OAIXML.createElement(OAIXML.METADATA);
|
---|
483 | metadata.appendChild(prfx_str_elem);
|
---|
484 | return metadata;
|
---|
485 | }
|
---|
486 | /** create a header element used when processing requests like ListRecords/GetRecord/ListIdentifiers
|
---|
487 | */
|
---|
488 | private Element createHeaderElement(String oid, String lastmodified) {
|
---|
489 | Element header = OAIXML.createElement(OAIXML.HEADER);
|
---|
490 | Element identifier = OAIXML.createElement(OAIXML.IDENTIFIER);
|
---|
491 | GSXML.setNodeText(identifier, site_name + ":" + coll_name + ":" + oid);
|
---|
492 | header.appendChild(identifier);
|
---|
493 | Element set_spec = OAIXML.createElement(OAIXML.SET_SPEC);
|
---|
494 | GSXML.setNodeText(set_spec, site_name + ":" + coll_name);
|
---|
495 | header.appendChild(set_spec);
|
---|
496 | Element datestamp = OAIXML.createElement(OAIXML.DATESTAMP);
|
---|
497 | GSXML.setNodeText(datestamp, lastmodified);
|
---|
498 | header.appendChild(datestamp);
|
---|
499 | return header;
|
---|
500 | }
|
---|
501 | /** return the metadata information */
|
---|
502 | protected Element processListMetadataFormats(Element req) {
|
---|
503 | // the request sent here must contain an OID. see doListMetadataFormats() in OAIReceptionist
|
---|
504 | Element param = GSXML.getNamedElement(req, OAIXML.PARAM, OAIXML.NAME, OAIXML.OID);
|
---|
505 | if (param == null) {
|
---|
506 | logger.error("An element containing the OID attribute not is present.");
|
---|
507 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.ID_DOES_NOT_EXIST, ""));
|
---|
508 | }
|
---|
509 | String oid = param.getAttribute(OAIXML.VALUE);
|
---|
510 | if (oid == null || oid.equals("")) {
|
---|
511 | logger.error("No OID is present in the request.");
|
---|
512 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.ID_DOES_NOT_EXIST, ""));
|
---|
513 | }
|
---|
514 | ArrayList oid_list = getChildrenIds(OAIXML.BROWSELIST);
|
---|
515 | if (oid_list == null || oid_list.contains(oid) == false) {
|
---|
516 | logger.error("OID: " + oid + " is not present in the database.");
|
---|
517 | Element e= OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.ID_DOES_NOT_EXIST, ""));
|
---|
518 | // logger.error((new XMLConverter()).getPrettyString (e));
|
---|
519 | return e;
|
---|
520 | }
|
---|
521 |
|
---|
522 | DBInfo info = null;
|
---|
523 | info = this.coll_db.getInfo(oid);
|
---|
524 | if (info == null) { //just double check
|
---|
525 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.OAI_SERVICE_UNAVAILABLE, ""));
|
---|
526 | }
|
---|
527 |
|
---|
528 | NodeList meta_list = getMetadataFormatList(this.coll_config_xml);
|
---|
529 | if (meta_list == null || meta_list.getLength() == 0) {
|
---|
530 | logger.error("No metadata format is present in collectionConfig.xml");
|
---|
531 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.NO_METADATA_FORMATS, ""));
|
---|
532 | }
|
---|
533 |
|
---|
534 | Element list_metadata_formats = OAIXML.createElement(OAIXML.LIST_METADATA_FORMATS);
|
---|
535 | boolean has_meta_format = false;
|
---|
536 |
|
---|
537 | for (int i=0; i<meta_list.getLength(); i++) {
|
---|
538 | Element metadata_format = (Element)meta_list.item(i);
|
---|
539 | String[] metadata_names = getMetadataNames(metadata_format);
|
---|
540 | if (containsMetadata(info, metadata_names) == true) {
|
---|
541 | has_meta_format = true;
|
---|
542 | Element meta_fmt = OAIXML.createElement(OAIXML.METADATA_FORMAT);
|
---|
543 | OAIXML.copyElement(meta_fmt, metadata_format, OAIXML.METADATA_PREFIX);
|
---|
544 | OAIXML.copyElement(meta_fmt, metadata_format, OAIXML.METADATA_NAMESPACE);
|
---|
545 | OAIXML.copyElement(meta_fmt, metadata_format, OAIXML.SCHEMA);
|
---|
546 | list_metadata_formats.appendChild(meta_fmt);
|
---|
547 | }
|
---|
548 | }//end of for loop
|
---|
549 | if (has_meta_format == false) {
|
---|
550 | logger.error("Specified metadata names are not contained in the database.");
|
---|
551 | return OAIXML.getResponse(OAIXML.createErrorElement(OAIXML.NO_METADATA_FORMATS, ""));
|
---|
552 | } else {
|
---|
553 | return OAIXML.getResponse(list_metadata_formats);
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | /** return the ListMetadataFormats element in collectionConfig.xml
|
---|
558 | * Currently, it will only contain one metadata format: oai_dc
|
---|
559 | */
|
---|
560 | protected NodeList getMetadataFormatList(Element coll_config_xml) {
|
---|
561 | Element oai_elem = (Element)GSXML.getChildByTagName(coll_config_xml, OAIXML.OAI);
|
---|
562 | Element list_meta_formats = (Element)GSXML.getChildByTagName(oai_elem, OAIXML.LIST_METADATA_FORMATS);
|
---|
563 | return GSXML.getChildrenByTagName(list_meta_formats, OAIXML.METADATA_FORMAT);
|
---|
564 | }
|
---|
565 | /** @param metadata_format - the metadataFormat element in collectionConfig.xml
|
---|
566 | */
|
---|
567 | protected String[] getMetadataNames(Element metadata_format) {
|
---|
568 | String[] names = null;
|
---|
569 |
|
---|
570 | //read the mappingList element
|
---|
571 | Element mapping_list = (Element)GSXML.getChildByTagName(metadata_format, OAIXML.MAPPING_LIST);
|
---|
572 | if (mapping_list == null) {
|
---|
573 | logger.info("No metadata mappings are provided in collectionConfig.xml. Use the standard Dublin Core names.");
|
---|
574 | names = OAIXML.getGlobalMetadataMapping(metadata_format.getAttribute(OAIXML.METADATA_PREFIX));
|
---|
575 |
|
---|
576 | return (names != null)? names : OAIXML.getDublinCoreNames();
|
---|
577 | }
|
---|
578 | NodeList mappings = GSXML.getChildrenByTagName(mapping_list, OAIXML.MAPPING);
|
---|
579 | int size = mappings.getLength();
|
---|
580 | if (size == 0) {
|
---|
581 | logger.info("No metadata mappings are provided in collectionConfig.xml. \n Return standard DC names.");
|
---|
582 | // read the standard Dublin Core metadata names
|
---|
583 | return OAIXML.getDublinCoreNames();
|
---|
584 | }
|
---|
585 | names = new String[size];
|
---|
586 | for (int i=0; i<size; i++) {
|
---|
587 | names[i] = GSXML.getNodeText((Element)mappings.item(i)).trim();
|
---|
588 | }
|
---|
589 | return names;
|
---|
590 | }
|
---|
591 |
|
---|
592 | /** returns a list of the child ids in order, null if no children */
|
---|
593 | protected ArrayList getChildrenIds(String node_id) {
|
---|
594 | DBInfo info = this.coll_db.getInfo(node_id);
|
---|
595 | if (info == null) {
|
---|
596 | return null;
|
---|
597 | }
|
---|
598 |
|
---|
599 | String contains = info.getInfo("contains");
|
---|
600 | if (contains.equals("")) {
|
---|
601 | return null;
|
---|
602 | }
|
---|
603 | ArrayList children = new ArrayList();
|
---|
604 | StringTokenizer st = new StringTokenizer(contains, ";");
|
---|
605 | while (st.hasMoreTokens()) {
|
---|
606 | String child_id = st.nextToken().replaceAll("\"", node_id);
|
---|
607 | children.add(child_id);
|
---|
608 | }
|
---|
609 | return children;
|
---|
610 | }
|
---|
611 | /**method to check whether any of the 'metadata_names' is contained in the 'info'.
|
---|
612 | * The name may be in the form: <name>,<mapped name>, in which the mapped name is
|
---|
613 | * optional. The mapped name is looked up in the DBInfo; if not present, use the first
|
---|
614 | * name which is mendatory.
|
---|
615 | */
|
---|
616 | protected boolean containsMetadata(DBInfo info, String[] metadata_names) {
|
---|
617 | if (metadata_names == null) return false;
|
---|
618 | logger.info("checking metadata names in db.");
|
---|
619 | for(int i=0; i<metadata_names.length; i++) {
|
---|
620 | int index = metadata_names[i].indexOf(",");
|
---|
621 | String meta_name = (index == -1) ? metadata_names[i] :
|
---|
622 | metadata_names[i].substring(index + 1);
|
---|
623 |
|
---|
624 | if(info.getInfo(meta_name).equals("") == false) {
|
---|
625 | return true;
|
---|
626 | }
|
---|
627 | }
|
---|
628 | return false;
|
---|
629 | }
|
---|
630 | /** @param keys - contains a list of keys in string format.
|
---|
631 | * Here is a typical record in the collection database, 'keys' contains the values in <...>:
|
---|
632 | *----------------------------------------------------------------------
|
---|
633 | [HASH01a84acb0f1aad2380493b3a]
|
---|
634 | <doctype>doc
|
---|
635 | <hastxt>1
|
---|
636 | <Language>en
|
---|
637 | <Encoding>windows_1252
|
---|
638 | <Plugin>HTMLPlug
|
---|
639 | <FileSize>205093
|
---|
640 | <Source>wb34te.htm
|
---|
641 | <hascover>1
|
---|
642 | <dls.Organization>World Bank
|
---|
643 | <dls.Title>Development in practice: Toward Gender Equality (wb34te)
|
---|
644 | <dls.Language>English
|
---|
645 | <dls.AZList>A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
|
---|
646 | <dls.Subject>Women, gender and development, women's organizations
|
---|
647 | <dls.Keyword>achieve gender equality
|
---|
648 | <URL>http://wb34te/wb34te.htm
|
---|
649 | <Title>Development in practice: Toward Gender Equality
|
---|
650 | <lastmodified>1178245194
|
---|
651 | <assocfilepath>HASH01a8.dir
|
---|
652 | <memberof>CL3
|
---|
653 | <archivedir>HASH01a8.dir
|
---|
654 | <thistype>VList
|
---|
655 | <childtype>VList
|
---|
656 | <contains>".1;".2;".3;".4;".5;".6;".7;".8;".9
|
---|
657 | <docnum>349
|
---|
658 | ----------------------------------------------------------------------
|
---|
659 | */
|
---|
660 | public String[] getMetadata(DBInfo info, String names) {
|
---|
661 | String[] name_value = new String[2];
|
---|
662 | ArrayList keys = new ArrayList(info.getKeys());
|
---|
663 | for (int i=0; i<keys.size(); i++) {
|
---|
664 | String key = (String)keys.get(i);
|
---|
665 | String first_name = "";
|
---|
666 | String second_name = "";
|
---|
667 | int index = names.indexOf(",");
|
---|
668 | if(index != -1) {
|
---|
669 | first_name = names.substring(0, index);
|
---|
670 | second_name = names.substring(index + 1);
|
---|
671 | } else {
|
---|
672 | first_name = second_name = names;
|
---|
673 | }
|
---|
674 | if(key.equals(second_name)) {
|
---|
675 | String meta_value = info.getInfo(key);
|
---|
676 | name_value[0] = first_name;
|
---|
677 | name_value[1] = meta_value;
|
---|
678 | return name_value;
|
---|
679 | }
|
---|
680 | }
|
---|
681 | return null;
|
---|
682 | }
|
---|
683 | protected HashMap getInfoByNames(DBInfo info, String[] metadata_names) {
|
---|
684 | HashMap map = new HashMap();
|
---|
685 | boolean empty_map = true;
|
---|
686 |
|
---|
687 | for(int i=0; i<metadata_names.length; i++) {
|
---|
688 | String[] name_value = getMetadata(info, metadata_names[i]);
|
---|
689 | if(name_value != null) {
|
---|
690 | map.put(name_value[0], name_value[1]);
|
---|
691 | empty_map = false;
|
---|
692 | }
|
---|
693 | }
|
---|
694 | return (empty_map == true) ? null : map;
|
---|
695 | }
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|