1 | /*
|
---|
2 | * ArchiveRetrieve.java
|
---|
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 |
|
---|
23 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
24 | import org.greenstone.gsdl3.util.GSPath;
|
---|
25 | import org.greenstone.gsdl3.util.GSXML;
|
---|
26 | import org.greenstone.gsdl3.util.SimpleCollectionDatabase;
|
---|
27 | import org.greenstone.gsdl3.util.UserContext;
|
---|
28 |
|
---|
29 | import org.w3c.dom.Element;
|
---|
30 |
|
---|
31 | import org.apache.log4j.*;
|
---|
32 |
|
---|
33 | import java.io.File;
|
---|
34 | import java.io.Serializable;
|
---|
35 | import java.util.HashMap;
|
---|
36 | import java.util.Vector;
|
---|
37 |
|
---|
38 | public class ArchiveRetrieve extends ServiceRack
|
---|
39 | {
|
---|
40 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.ArchiveRetrieve.class.getName());
|
---|
41 |
|
---|
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";
|
---|
45 |
|
---|
46 | protected SimpleCollectionDatabase coll_db = null;
|
---|
47 |
|
---|
48 | /** configure this service */
|
---|
49 | public boolean configure(Element info, Element extra_info)
|
---|
50 | {
|
---|
51 | if (!super.configure(info, extra_info))
|
---|
52 | {
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | logger.info("Configuring ArchiveRetrieve...");
|
---|
57 | this.config_info = info;
|
---|
58 |
|
---|
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);
|
---|
63 |
|
---|
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 |
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected Element getServiceDescription(String service_id, String lang, String subset)
|
---|
78 | {
|
---|
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;
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected Element processDocumentFilePathRetrieve(Element request)
|
---|
104 | {
|
---|
105 | // Create a new (empty) result message
|
---|
106 | Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
107 | result.setAttribute(GSXML.FROM_ATT, DOCUMENT_FILE_PATH_RETRIEVE_SERVICE);
|
---|
108 | result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
|
---|
109 |
|
---|
110 | UserContext userContext = new UserContext(request);
|
---|
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) {
|
---|
115 | GSXML.addError(this.doc, result, "DocumentFilePathRetrieve: missing "+ GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER, GSXML.ERROR_TYPE_SYNTAX);
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
119 |
|
---|
120 | String oid = (String) params.get("oid");
|
---|
121 | String collection = (String) params.get("c");
|
---|
122 |
|
---|
123 | String assocFilePath = getAssocFilePathFromDocID(oid, collection, userContext);
|
---|
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";
|
---|
131 |
|
---|
132 | Element metadataList = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
133 | metadataList.appendChild(createMetadataElement("docfilepath", docFilePath));
|
---|
134 | result.appendChild(metadataList);
|
---|
135 |
|
---|
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);
|
---|
145 |
|
---|
146 | UserContext userContext = new UserContext(request);
|
---|
147 |
|
---|
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 | }
|
---|
155 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
156 |
|
---|
157 | String srcFile = (String) params.get("srcfile");
|
---|
158 | String collection = (String) params.get("c");
|
---|
159 |
|
---|
160 | //Find out what kind of database we have
|
---|
161 | String databaseType = getDatabaseTypeFromCollection(collection, userContext);
|
---|
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 |
|
---|
207 | return result;
|
---|
208 | }
|
---|
209 |
|
---|
210 | protected Element processAssociatedImportFilesRetrieve(Element request)
|
---|
211 | {
|
---|
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 |
|
---|
217 | UserContext userContext = new UserContext(request);
|
---|
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;
|
---|
225 | }
|
---|
226 | HashMap<String, Serializable> params = GSXML.extractParams(param_list, false);
|
---|
227 |
|
---|
228 | String oid = (String) params.get("oid");
|
---|
229 | String collection = (String) params.get("c");
|
---|
230 |
|
---|
231 | String databaseType = getDatabaseTypeFromCollection(collection, userContext);
|
---|
232 | if (databaseType == null || databaseType.equals(""))
|
---|
233 | {
|
---|
234 | databaseType = "gdbm"; // the default
|
---|
235 | }
|
---|
236 |
|
---|
237 | String dbExt = null;
|
---|
238 | if (databaseType.equalsIgnoreCase("jdbm"))
|
---|
239 | {
|
---|
240 | dbExt = ".jdb";
|
---|
241 | }
|
---|
242 | else
|
---|
243 | {
|
---|
244 | dbExt = ".gdb"; // assume gdbm
|
---|
245 | }
|
---|
246 |
|
---|
247 | coll_db = new SimpleCollectionDatabase(databaseType);
|
---|
248 | if (!coll_db.databaseOK())
|
---|
249 | {
|
---|
250 | logger.error("Couldn't create the collection database of type "+databaseType);
|
---|
251 | return null;
|
---|
252 | }
|
---|
253 |
|
---|
254 | coll_db.openDatabase
|
---|
255 | (
|
---|
256 | this.site_home + File.separatorChar +
|
---|
257 | "collect" + File.separatorChar +
|
---|
258 | collection + File.separatorChar +
|
---|
259 | "archives" + File.separatorChar +
|
---|
260 | "archiveinf-doc" + dbExt,
|
---|
261 | SimpleCollectionDatabase.READ
|
---|
262 | );
|
---|
263 |
|
---|
264 | DBInfo info = coll_db.getInfo(oid);
|
---|
265 |
|
---|
266 | if (info == null)
|
---|
267 | {
|
---|
268 | return result;
|
---|
269 | }
|
---|
270 |
|
---|
271 | String srcFile = info.getInfo("src-file");
|
---|
272 | Vector<String> data = info.getMultiInfo("assoc-file");
|
---|
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 | {
|
---|
279 | metadataList.appendChild(createMetadataElement("assocfile", data.get(i)));
|
---|
280 | }
|
---|
281 |
|
---|
282 | result.appendChild(metadataList);
|
---|
283 |
|
---|
284 | return result;
|
---|
285 | }
|
---|
286 |
|
---|
287 | public Element createMetadataElement(String name, String value)
|
---|
288 | {
|
---|
289 | Element metaElem = this.doc.createElement(GSXML.METADATA_ELEM);
|
---|
290 | metaElem.setAttribute("name", name);
|
---|
291 | metaElem.setAttribute("value", value);
|
---|
292 | return metaElem;
|
---|
293 | }
|
---|
294 |
|
---|
295 | public String getAssocFilePathFromDocID(String oid, String collection, UserContext userContext)
|
---|
296 | {
|
---|
297 | Element mr_query_message = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
298 | Element mr_query_request = GSXML.createBasicRequest (this.doc, GSXML.REQUEST_TYPE_PAGE, collection + "/DocumentMetadataRetrieve", userContext);
|
---|
299 | mr_query_message.appendChild(mr_query_request);
|
---|
300 |
|
---|
301 | Element paramList = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
|
---|
302 | paramList.appendChild(createMetadataElement("metadata", "assocfilepath"));
|
---|
303 |
|
---|
304 | mr_query_request.appendChild(paramList);
|
---|
305 |
|
---|
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);
|
---|
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 | }
|
---|
322 |
|
---|
323 | public String getDatabaseTypeFromCollection(String collection, UserContext userContext)
|
---|
324 | {
|
---|
325 | //Find out what kind of database we have
|
---|
326 | Element dbTypeMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
327 | Element dbTypeRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_DESCRIBE, collection, userContext);
|
---|
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 | }
|
---|
340 | } |
---|