[24211] | 1 | /*
|
---|
[24221] | 2 | * ArchiveRetrieve.java
|
---|
[24211] | 3 | * a base class for retrieval services
|
---|
| 4 |
|
---|
| 5 | * Copyright (C) 2005 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 6 | *
|
---|
| 7 | * This program is free software; you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * This program is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with this program; if not, write to the Free Software
|
---|
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 20 | */
|
---|
| 21 | package org.greenstone.gsdl3.service;
|
---|
| 22 |
|
---|
[24221] | 23 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
[24211] | 24 | import org.greenstone.gsdl3.util.GSPath;
|
---|
| 25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
| 26 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
[24993] | 27 | import org.greenstone.gsdl3.util.UserContext;
|
---|
[24211] | 28 |
|
---|
| 29 | import org.w3c.dom.Element;
|
---|
| 30 |
|
---|
| 31 | import org.apache.log4j.*;
|
---|
| 32 |
|
---|
| 33 | import java.io.File;
|
---|
[25635] | 34 | import java.io.Serializable;
|
---|
[24211] | 35 | import java.util.HashMap;
|
---|
[24221] | 36 | import java.util.Vector;
|
---|
[24211] | 37 |
|
---|
[24221] | 38 | public class ArchiveRetrieve extends ServiceRack
|
---|
| 39 | {
|
---|
[24211] | 40 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveRetrieve.class.getName());
|
---|
| 41 |
|
---|
[24221] | 42 | protected static final String DOCUMENT_FILE_PATH_RETRIEVE_SERVICE = "DocumentFilePathRetrieve";
|
---|
| 43 | protected static final String ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE = "AssociatedImportFilesRetrieve";
|
---|
| 44 | protected static final String SOURCE_FILE_OID_RETRIEVE = "SourceFileOIDRetrieve";
|
---|
[24211] | 45 |
|
---|
| 46 | protected SimpleCollectionDatabase coll_db = null;
|
---|
| 47 |
|
---|
| 48 | /** configure this service */
|
---|
| 49 | public boolean configure(Element info, Element extra_info)
|
---|
| 50 | {
|
---|
[24221] | 51 | if (!super.configure(info, extra_info))
|
---|
| 52 | {
|
---|
[24211] | 53 | return false;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | logger.info("Configuring ArchiveRetrieve...");
|
---|
| 57 | this.config_info = info;
|
---|
| 58 |
|
---|
[24221] | 59 | Element documentFilePathRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 60 | documentFilePathRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 61 | documentFilePathRetrieveService.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
| 62 | this.short_service_info.appendChild(documentFilePathRetrieveService);
|
---|
[24211] | 63 |
|
---|
[24221] | 64 | Element associatedImportFilesRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 65 | associatedImportFilesRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 66 | associatedImportFilesRetrieveService.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
| 67 | this.short_service_info.appendChild(associatedImportFilesRetrieveService);
|
---|
| 68 |
|
---|
| 69 | Element sourceFileDocIDRetrieveService = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 70 | sourceFileDocIDRetrieveService.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 71 | sourceFileDocIDRetrieveService.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
| 72 | this.short_service_info.appendChild(sourceFileDocIDRetrieveService);
|
---|
| 73 |
|
---|
[24211] | 74 | return true;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
| 78 | {
|
---|
[24221] | 79 | if (service_id.equals(DOCUMENT_FILE_PATH_RETRIEVE_SERVICE))
|
---|
| 80 | {
|
---|
| 81 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 82 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 83 | service_elem.setAttribute(GSXML.NAME_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
| 84 | return service_elem;
|
---|
| 85 | }
|
---|
| 86 | else if (service_id.equals(ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE))
|
---|
| 87 | {
|
---|
| 88 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 89 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 90 | service_elem.setAttribute(GSXML.NAME_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
| 91 | return service_elem;
|
---|
| 92 | }
|
---|
| 93 | else if (service_id.equals(SOURCE_FILE_OID_RETRIEVE))
|
---|
| 94 | {
|
---|
| 95 | Element service_elem = this.doc.createElement(GSXML.SERVICE_ELEM);
|
---|
| 96 | service_elem.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE);
|
---|
| 97 | service_elem.setAttribute(GSXML.NAME_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
| 98 | return service_elem;
|
---|
| 99 | }
|
---|
| 100 | return null;
|
---|
[24211] | 101 | }
|
---|
| 102 |
|
---|
[24221] | 103 | protected Element processDocumentFilePathRetrieve(Element request)
|
---|
[24211] | 104 | {
|
---|
| 105 | // Create a new (empty) result message
|
---|
| 106 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
[24221] | 107 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
[24211] | 108 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 109 |
|
---|
[24993] | 110 | UserContext userContext = new UserContext(request);
|
---|
[24211] | 111 |
|
---|
| 112 | // Get the parameters of the request
|
---|
| 113 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 114 | if (param_list == null) {
|
---|
[24221] | 115 | GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
[24211] | 116 | return result;
|
---|
| 117 | }
|
---|
[25635] | 118 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24211] | 119 |
|
---|
[24221] | 120 | String oid = (String) params.get("oid");
|
---|
[24211] | 121 | String collection = (String) params.get("c");
|
---|
| 122 |
|
---|
[24993] | 123 | String assocFilePath = getAssocFilePathFromDocID(oid, collection, userContext);
|
---|
[24211] | 124 |
|
---|
| 125 | String docFilePath = this.site_home + File.separatorChar +
|
---|
| 126 | "collect" + File.separatorChar +
|
---|
| 127 | collection + File.separatorChar +
|
---|
| 128 | "archives" + File.separatorChar +
|
---|
| 129 | assocFilePath + File.separatorChar +
|
---|
| 130 | "doc.xml";
|
---|
[24221] | 131 |
|
---|
| 132 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 133 | metadataList.appendChild(createMetadataElement("docfilepath", docFilePath));
|
---|
| 134 | result.appendChild(metadataList);
|
---|
[24211] | 135 |
|
---|
[24221] | 136 | return result;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | protected Element processSourceFileOIDRetrieveService(Element request)
|
---|
| 140 | {
|
---|
| 141 | //Create a new (empty) result message
|
---|
| 142 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 143 | result.setAttribute(GSXML.FROM_ATT, SOURCE_FILE_OID_RETRIEVE);
|
---|
| 144 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
[24211] | 145 |
|
---|
[24993] | 146 | UserContext userContext = new UserContext(request);
|
---|
[24211] | 147 |
|
---|
[24221] | 148 | // Get the parameters of the request
|
---|
| 149 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 150 | if (param_list == null)
|
---|
| 151 | {
|
---|
| 152 | GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 153 | return result;
|
---|
| 154 | }
|
---|
[25635] | 155 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24211] | 156 |
|
---|
[24221] | 157 | String srcFile = (String) params.get("srcfile");
|
---|
| 158 | String collection = (String) params.get("c");
|
---|
| 159 |
|
---|
| 160 | //Find out what kind of database we have
|
---|
[24993] | 161 | String databaseType = getDatabaseTypeFromCollection(collection, userContext);
|
---|
[24221] | 162 | if (databaseType == null || databaseType.equals(""))
|
---|
| 163 | {
|
---|
| 164 | databaseType = "gdbm"; // the default
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | String dbExt = null;
|
---|
| 168 | if (databaseType.equalsIgnoreCase("jdbm"))
|
---|
| 169 | {
|
---|
| 170 | dbExt = ".jdb";
|
---|
| 171 | }
|
---|
| 172 | else
|
---|
| 173 | {
|
---|
| 174 | dbExt = ".gdb"; // assume gdbm
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
| 178 | if (!coll_db.databaseOK())
|
---|
| 179 | {
|
---|
| 180 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
| 181 | return null;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | coll_db.openDatabase
|
---|
| 185 | (
|
---|
| 186 | this.site_home + File.separatorChar +
|
---|
| 187 | "collect" + File.separatorChar +
|
---|
| 188 | collection + File.separatorChar +
|
---|
| 189 | "archives" + File.separatorChar +
|
---|
| 190 | "archiveinf-src" + dbExt,
|
---|
| 191 | SimpleCollectionDatabase.READ
|
---|
| 192 | );
|
---|
| 193 |
|
---|
| 194 | DBInfo info = coll_db.getInfo(srcFile);
|
---|
| 195 |
|
---|
| 196 | if (info == null)
|
---|
| 197 | {
|
---|
| 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | String oid = info.getInfo("oid");
|
---|
| 202 |
|
---|
| 203 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 204 | metadataList.appendChild(createMetadataElement("oid", oid));
|
---|
| 205 | result.appendChild(metadataList);
|
---|
| 206 |
|
---|
[24211] | 207 | return result;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[24221] | 210 | protected Element processAssociatedImportFilesRetrieve(Element request)
|
---|
[24211] | 211 | {
|
---|
[24221] | 212 | //Create a new (empty) result message
|
---|
| 213 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
| 214 | result.setAttribute(GSXML.FROM_ATT, ASSOCIATED_IMPORT_FILES_RETRIEVE_SERVICE);
|
---|
| 215 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
| 216 |
|
---|
[24993] | 217 | UserContext userContext = new UserContext(request);
|
---|
[24221] | 218 |
|
---|
| 219 | // Get the parameters of the request
|
---|
| 220 | Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 221 | if (param_list == null)
|
---|
| 222 | {
|
---|
| 223 | GSXML.addError(this.doc, result, "AssociatedImportFilesRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
| 224 | return result;
|
---|
[24211] | 225 | }
|
---|
[25635] | 226 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
[24221] | 227 |
|
---|
| 228 | String oid = (String) params.get("oid");
|
---|
| 229 | String collection = (String) params.get("c");
|
---|
| 230 |
|
---|
[24993] | 231 | String databaseType = getDatabaseTypeFromCollection(collection, userContext);
|
---|
[24221] | 232 | if (databaseType == null || databaseType.equals(""))
|
---|
| 233 | {
|
---|
| 234 | databaseType = "gdbm"; // the default
|
---|
[24211] | 235 | }
|
---|
| 236 |
|
---|
[24221] | 237 | String dbExt = null;
|
---|
| 238 | if (databaseType.equalsIgnoreCase("jdbm"))
|
---|
| 239 | {
|
---|
| 240 | dbExt = ".jdb";
|
---|
| 241 | }
|
---|
| 242 | else
|
---|
| 243 | {
|
---|
| 244 | dbExt = ".gdb"; // assume gdbm
|
---|
[24211] | 245 | }
|
---|
| 246 |
|
---|
[24221] | 247 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
| 248 | if (!coll_db.databaseOK())
|
---|
| 249 | {
|
---|
| 250 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
[24211] | 251 | return null;
|
---|
| 252 | }
|
---|
[24221] | 253 |
|
---|
| 254 | coll_db.openDatabase
|
---|
| 255 | (
|
---|
[24211] | 256 | this.site_home + File.separatorChar +
|
---|
| 257 | "collect" + File.separatorChar +
|
---|
| 258 | collection + File.separatorChar +
|
---|
| 259 | "archives" + File.separatorChar +
|
---|
[24221] | 260 | "archiveinf-doc" + dbExt,
|
---|
| 261 | SimpleCollectionDatabase.READ
|
---|
| 262 | );
|
---|
[24211] | 263 |
|
---|
[24221] | 264 | DBInfo info = coll_db.getInfo(oid);
|
---|
[24211] | 265 |
|
---|
[24221] | 266 | if (info == null)
|
---|
| 267 | {
|
---|
| 268 | return result;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | String srcFile = info.getInfo("src-file");
|
---|
[25635] | 272 | Vector<String> data = info.getMultiInfo("assoc-file");
|
---|
[24221] | 273 |
|
---|
| 274 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
| 275 | metadataList.appendChild(createMetadataElement("srcfile", srcFile));
|
---|
| 276 |
|
---|
| 277 | for (int i = 0; i < data.size(); i++)
|
---|
| 278 | {
|
---|
[25635] | 279 | metadataList.appendChild(createMetadataElement("assocfile", data.get(i)));
|
---|
[24221] | 280 | }
|
---|
| 281 |
|
---|
| 282 | result.appendChild(metadataList);
|
---|
| 283 |
|
---|
| 284 | return result;
|
---|
[24211] | 285 | }
|
---|
| 286 |
|
---|
[24221] | 287 | public Element createMetadataElement(String name, String value)
|
---|
[24211] | 288 | {
|
---|
[24221] | 289 | Element metaElem = this.doc.createElement(GSXML.METADATA_ELEM);
|
---|
| 290 | metaElem.setAttribute("name", name);
|
---|
| 291 | metaElem.setAttribute("value", value);
|
---|
| 292 | return metaElem;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[24993] | 295 | public String getAssocFilePathFromDocID(String oid, String collection, UserContext userContext)
|
---|
[24221] | 296 | {
|
---|
[24211] | 297 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 298 | Element mr_query_request = GSXML.createBasicRequest (this.doc, GSXML.REQUEST_TYPE_PAGE, collection + "/DocumentMetadataRetrieve", userContext);
|
---|
[24211] | 299 | mr_query_message.appendChild(mr_query_request);
|
---|
| 300 |
|
---|
| 301 | Element paramList = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
[24221] | 302 | paramList.appendChild(createMetadataElement("metadata", "assocfilepath"));
|
---|
[24211] | 303 |
|
---|
| 304 | mr_query_request.appendChild(paramList);
|
---|
| 305 |
|
---|
[24221] | 306 | Element docListElem = this.doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 307 | Element docElem = this.doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
| 308 | docElem.setAttribute(GSXML.NODE_ID_ATT, oid);
|
---|
| 309 | docListElem.appendChild(docElem);
|
---|
| 310 | mr_query_request.appendChild(docListElem);
|
---|
[24211] | 311 |
|
---|
| 312 | Element response = (Element) this.router.process(mr_query_message);
|
---|
| 313 |
|
---|
| 314 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 315 | path = GSPath.appendLink(path, GSXML.DOC_NODE_ELEM);
|
---|
| 316 | path = GSPath.appendLink(path, GSXML.METADATA_ELEM+GSXML.LIST_MODIFIER);
|
---|
| 317 | Element metadataListElem = (Element) GSXML.getNodeByPath(response, path);
|
---|
| 318 | Element metadataElem = (Element) metadataListElem.getFirstChild();
|
---|
| 319 |
|
---|
| 320 | return metadataElem.getFirstChild().getNodeValue();
|
---|
| 321 | }
|
---|
[24221] | 322 |
|
---|
[24993] | 323 | public String getDatabaseTypeFromCollection(String collection, UserContext userContext)
|
---|
[24221] | 324 | {
|
---|
| 325 | //Find out what kind of database we have
|
---|
| 326 | Element dbTypeMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
[24993] | 327 | Element dbTypeRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, collection, userContext);
|
---|
[24221] | 328 | dbTypeMessage.appendChild(dbTypeRequest);
|
---|
| 329 | Element dbTypeResponse = (Element)this.router.process(dbTypeMessage);
|
---|
| 330 |
|
---|
| 331 | String path = GSPath.appendLink(GSXML.RESPONSE_ELEM, GSXML.COLLECTION_ELEM);
|
---|
| 332 | Element collectionElem = (Element) GSXML.getNodeByPath(dbTypeResponse, path);
|
---|
| 333 |
|
---|
| 334 | if (collectionElem != null)
|
---|
| 335 | {
|
---|
| 336 | return collectionElem.getAttribute(GSXML.DB_TYPE_ATT);
|
---|
| 337 | }
|
---|
| 338 | return null;
|
---|
| 339 | }
|
---|
[24211] | 340 | } |
---|