1 | /*
|
---|
2 | * Collection.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.collection;
|
---|
20 |
|
---|
21 | import java.io.BufferedReader;
|
---|
22 | import java.io.BufferedWriter;
|
---|
23 | import java.io.File;
|
---|
24 | import java.io.FileReader;
|
---|
25 | import java.io.FileWriter;
|
---|
26 | import java.io.IOException;
|
---|
27 | import java.io.PrintWriter;
|
---|
28 | import java.io.StringWriter;
|
---|
29 | import java.util.ArrayList;
|
---|
30 | import java.util.HashMap;
|
---|
31 |
|
---|
32 | import org.apache.commons.lang3.StringUtils;
|
---|
33 | import org.apache.log4j.Logger;
|
---|
34 | import org.greenstone.gsdl3.core.ModuleInterface;
|
---|
35 | import org.greenstone.gsdl3.util.GSFile;
|
---|
36 | import org.greenstone.gsdl3.util.GSXML;
|
---|
37 | import org.greenstone.gsdl3.util.GSXSLT;
|
---|
38 | import org.greenstone.gsdl3.util.OAIXML;
|
---|
39 | import org.greenstone.gsdl3.util.SimpleMacroResolver;
|
---|
40 | import org.greenstone.gsdl3.util.UserContext;
|
---|
41 | import org.greenstone.gsdl3.util.XMLConverter;
|
---|
42 | import org.greenstone.gsdl3.util.XMLTransformer;
|
---|
43 | import org.w3c.dom.Document;
|
---|
44 | import org.w3c.dom.Element;
|
---|
45 | import org.w3c.dom.Node;
|
---|
46 | import org.w3c.dom.NodeList;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Represents a collection in Greenstone. A collection is an extension of a
|
---|
50 | * ServiceCluster - it has local data that the services use.
|
---|
51 | *
|
---|
52 | * @author Katherine Don
|
---|
53 | * @see ModuleInterface
|
---|
54 | */
|
---|
55 | public class Collection extends ServiceCluster
|
---|
56 | {
|
---|
57 |
|
---|
58 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
|
---|
59 |
|
---|
60 | /** is this collection being tidied and therefore can support realistic book view? */
|
---|
61 | protected boolean useBook = false;
|
---|
62 | /**
|
---|
63 | * is this collection public or private - public collections will
|
---|
64 | * appear on the home page, whereas private collections won't
|
---|
65 | */
|
---|
66 | protected boolean is_public = true;
|
---|
67 | /** collection type : mg, mgpp or lucene */
|
---|
68 | protected String col_type = "";
|
---|
69 | /** database type : gdbm, jdbm or sqlite */
|
---|
70 | protected String db_type = "";
|
---|
71 |
|
---|
72 | /** does this collection provide the OAI service */
|
---|
73 | protected boolean has_oai = false;
|
---|
74 | /** time when this collection was built */
|
---|
75 | protected long lastmodified = 0;
|
---|
76 | /** earliestDatestamp of this collection. Necessary for OAI */
|
---|
77 | protected long earliestDatestamp = 0;
|
---|
78 |
|
---|
79 | /** Stores the default accessibility of guest users */
|
---|
80 | protected boolean _publicAccess = true;
|
---|
81 | /** Stores the scope of any security rules (either collection or document) */
|
---|
82 | protected boolean _securityScopeCollection = true;
|
---|
83 |
|
---|
84 | protected HashMap<String, ArrayList<Element>> _documentSets = new HashMap<String, ArrayList<Element>>();
|
---|
85 | protected ArrayList<HashMap<String, ArrayList<String>>> _securityExceptions = new ArrayList<HashMap<String, ArrayList<String>>>();
|
---|
86 |
|
---|
87 | protected XMLTransformer transformer = null;
|
---|
88 |
|
---|
89 | /** same as setClusterName */
|
---|
90 | public void setCollectionName(String name)
|
---|
91 | {
|
---|
92 | setClusterName(name);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public Collection()
|
---|
96 | {
|
---|
97 | super();
|
---|
98 | this.description = this.desc_doc.createElement(GSXML.COLLECTION_ELEM);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Configures the collection.
|
---|
103 | *
|
---|
104 | * gsdlHome and collectionName must be set before configure is called.
|
---|
105 | *
|
---|
106 | * the file buildcfg.xml is located in gsdlHome/collect/collectionName
|
---|
107 | * collection metadata is obtained, and services loaded.
|
---|
108 | *
|
---|
109 | * @return true/false on success/fail
|
---|
110 | */
|
---|
111 | public boolean configure()
|
---|
112 | {
|
---|
113 | if (this.site_home == null || this.cluster_name == null)
|
---|
114 | {
|
---|
115 | logger.error("Collection: site_home and collection_name must be set before configure called!");
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
|
---|
120 |
|
---|
121 | Element coll_config_xml = loadCollConfigFile();
|
---|
122 | GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
|
---|
123 | Element build_config_xml = loadBuildConfigFile();
|
---|
124 |
|
---|
125 | if (coll_config_xml == null || build_config_xml == null)
|
---|
126 | {
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 |
|
---|
130 | // get the collection type attribute
|
---|
131 | Element search = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SEARCH_ELEM);
|
---|
132 | if (search != null)
|
---|
133 | {
|
---|
134 | col_type = search.getAttribute(GSXML.TYPE_ATT);
|
---|
135 | }
|
---|
136 |
|
---|
137 | Element browse = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.INFODB_ELEM);
|
---|
138 | if (browse != null)
|
---|
139 | {
|
---|
140 | db_type = browse.getAttribute(GSXML.TYPE_ATT);
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | db_type = "gdbm"; //Default database type
|
---|
145 | }
|
---|
146 |
|
---|
147 | this.description.setAttribute(GSXML.TYPE_ATT, col_type);
|
---|
148 | this.description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
|
---|
149 |
|
---|
150 | _globalFormat = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.FORMAT_ELEM);
|
---|
151 | // process the metadata and display items and default library params
|
---|
152 | super.configureLocalData(coll_config_xml);
|
---|
153 | super.configureLocalData(build_config_xml);
|
---|
154 | // get extra collection specific stuff
|
---|
155 | findAndLoadInfo(coll_config_xml, build_config_xml);
|
---|
156 |
|
---|
157 | loadSecurityInformation(coll_config_xml);
|
---|
158 |
|
---|
159 | // now do the services
|
---|
160 | configureServiceRacks(coll_config_xml, build_config_xml);
|
---|
161 |
|
---|
162 | return true;
|
---|
163 |
|
---|
164 | }
|
---|
165 |
|
---|
166 | public boolean useBook()
|
---|
167 | {
|
---|
168 | return useBook;
|
---|
169 | }
|
---|
170 |
|
---|
171 | public boolean isPublic()
|
---|
172 | {
|
---|
173 | return is_public;
|
---|
174 | }
|
---|
175 |
|
---|
176 | // Used by OAI Receptionist to determine whether to expire a resumption token - if a token was issued before a collection is rebuilt, then the token must be expired.
|
---|
177 | public long getLastmodified()
|
---|
178 | {
|
---|
179 | return lastmodified;
|
---|
180 | }
|
---|
181 |
|
---|
182 | // used by the OAIReceptionist to find out the earliest datestamp amongst all oai collections in the repository
|
---|
183 | public long getEarliestDatestamp()
|
---|
184 | {
|
---|
185 | return earliestDatestamp;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * whether the service_map in ServiceCluster.java contains the service
|
---|
190 | * 'OAIPMH' 11/06/2007 xiao
|
---|
191 | */
|
---|
192 | public boolean hasOAI()
|
---|
193 | {
|
---|
194 | return has_oai;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * load in the collection config file into a DOM Element
|
---|
199 | */
|
---|
200 | protected Element loadCollConfigFile()
|
---|
201 | {
|
---|
202 |
|
---|
203 | File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
|
---|
204 |
|
---|
205 | if (!coll_config_file.exists())
|
---|
206 | {
|
---|
207 | logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + coll_config_file + " does not exist");
|
---|
208 | return null;
|
---|
209 | }
|
---|
210 | // get the xml
|
---|
211 | Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
|
---|
212 | Element coll_config_elem = null;
|
---|
213 | if (coll_config_doc != null)
|
---|
214 | {
|
---|
215 | coll_config_elem = coll_config_doc.getDocumentElement();
|
---|
216 | }
|
---|
217 | return coll_config_elem;
|
---|
218 |
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * load in the collection build config file into a DOM Element
|
---|
223 | */
|
---|
224 | protected Element loadBuildConfigFile()
|
---|
225 | {
|
---|
226 | File build_config_file = new File(GSFile.collectionBuildConfigFile(this.site_home, this.cluster_name));
|
---|
227 | if (!build_config_file.exists())
|
---|
228 | {
|
---|
229 | logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + build_config_file + " does not exist");
|
---|
230 | return null;
|
---|
231 | }
|
---|
232 | Document build_config_doc = this.converter.getDOM(build_config_file, CONFIG_ENCODING);
|
---|
233 | Element build_config_elem = null;
|
---|
234 | if (build_config_doc != null)
|
---|
235 | {
|
---|
236 | build_config_elem = build_config_doc.getDocumentElement();
|
---|
237 | }
|
---|
238 |
|
---|
239 | lastmodified = build_config_file.lastModified();
|
---|
240 |
|
---|
241 | return build_config_elem;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * find the metadata and display elems from the two config files and add it
|
---|
246 | * to the appropriate lists
|
---|
247 | */
|
---|
248 | protected boolean findAndLoadInfo(Element coll_config_xml, Element build_config_xml)
|
---|
249 | {
|
---|
250 | // Element meta_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
251 | // addMetadata(meta_list);
|
---|
252 | // meta_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
253 | // addMetadata(meta_list);
|
---|
254 | addMetadata("httpPath", this.site_http_address + "/collect/" + this.cluster_name);
|
---|
255 |
|
---|
256 | // display stuff
|
---|
257 | // Element display_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
|
---|
258 | // if (display_list != null)
|
---|
259 | // {
|
---|
260 | // resolveMacros(display_list);
|
---|
261 | // addDisplayItems(display_list);
|
---|
262 | // }
|
---|
263 |
|
---|
264 | //check whether the html are tidy or not
|
---|
265 | Element import_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.IMPORT_ELEM);
|
---|
266 | if (import_list != null)
|
---|
267 | {
|
---|
268 | Element plugin_list = (Element) GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
|
---|
269 | //addPlugins(plugin_list);
|
---|
270 | if (plugin_list != null)
|
---|
271 | {
|
---|
272 | Element plugin_elem = (Element) GSXML.getNamedElement(plugin_list, GSXML.PLUGIN_ELEM, GSXML.NAME_ATT, "HTMLPlugin");
|
---|
273 | if (plugin_elem != null)
|
---|
274 | {
|
---|
275 | //get the option
|
---|
276 | Element option_elem = (Element) GSXML.getNamedElement(plugin_elem, GSXML.PARAM_OPTION_ELEM, GSXML.NAME_ATT, "-use_realistic_book");
|
---|
277 | if (option_elem != null)
|
---|
278 | {
|
---|
279 | useBook = true;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | }
|
---|
284 | String tidy = (useBook == true ? "tidy" : "untidy");
|
---|
285 | addMetadata("tidyoption", tidy);
|
---|
286 |
|
---|
287 | // check whether we are public or not
|
---|
288 | if (this.metadata_list != null)
|
---|
289 | {
|
---|
290 | Element meta_elem = (Element) GSXML.getNamedElement(this.metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
|
---|
291 | if (meta_elem != null)
|
---|
292 | {
|
---|
293 | String value = GSXML.getValue(meta_elem).toLowerCase().trim();
|
---|
294 | if (value.equals("false"))
|
---|
295 | {
|
---|
296 | is_public = false;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 | return true;
|
---|
301 | }
|
---|
302 |
|
---|
303 | protected void loadSecurityInformation(Element coll_config_xml)
|
---|
304 | {
|
---|
305 | Element securityBlock = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SECURITY_ELEM);
|
---|
306 |
|
---|
307 | if (securityBlock == null)
|
---|
308 | {
|
---|
309 | return;
|
---|
310 | }
|
---|
311 |
|
---|
312 | String scope = securityBlock.getAttribute(GSXML.SCOPE_ATT);
|
---|
313 | String defaultAccess = securityBlock.getAttribute(GSXML.DEFAULT_ACCESS_ATT);
|
---|
314 |
|
---|
315 | if (defaultAccess.toLowerCase().equals("public"))
|
---|
316 | {
|
---|
317 | _publicAccess = true;
|
---|
318 | }
|
---|
319 | else if (defaultAccess.toLowerCase().equals("private"))
|
---|
320 | {
|
---|
321 | _publicAccess = false;
|
---|
322 | }
|
---|
323 | else
|
---|
324 | {
|
---|
325 | logger.warn("Default access for collection " + this.cluster_name + " is neither public or private, assuming public");
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (scope.toLowerCase().equals("collection"))
|
---|
329 | {
|
---|
330 | _securityScopeCollection = true;
|
---|
331 | }
|
---|
332 | else if (scope.toLowerCase().equals("documents") || scope.toLowerCase().equals("document"))
|
---|
333 | {
|
---|
334 | _securityScopeCollection = false;
|
---|
335 | }
|
---|
336 | else
|
---|
337 | {
|
---|
338 | logger.warn("Security scope is neither collection or document, assuming collection");
|
---|
339 | }
|
---|
340 |
|
---|
341 | NodeList exceptions = GSXML.getChildrenByTagName(securityBlock, GSXML.EXCEPTION_ELEM);
|
---|
342 |
|
---|
343 | if (exceptions.getLength() > 0)
|
---|
344 | {
|
---|
345 | if (!_securityScopeCollection)
|
---|
346 | {
|
---|
347 | NodeList documentSetElems = GSXML.getChildrenByTagName(securityBlock, GSXML.DOCUMENT_SET_ELEM);
|
---|
348 | for (int i = 0; i < documentSetElems.getLength(); i++)
|
---|
349 | {
|
---|
350 | Element documentSet = (Element) documentSetElems.item(i);
|
---|
351 | String setName = documentSet.getAttribute(GSXML.NAME_ATT);
|
---|
352 | NodeList matchStatements = GSXML.getChildrenByTagName(documentSet, GSXML.MATCH_ELEM);
|
---|
353 | ArrayList<Element> matchStatementList = new ArrayList<Element>();
|
---|
354 | for (int j = 0; j < matchStatements.getLength(); j++)
|
---|
355 | {
|
---|
356 | matchStatementList.add((Element) matchStatements.item(j));
|
---|
357 | }
|
---|
358 | _documentSets.put(setName, matchStatementList);
|
---|
359 | }
|
---|
360 | }
|
---|
361 |
|
---|
362 | for (int i = 0; i < exceptions.getLength(); i++)
|
---|
363 | {
|
---|
364 | HashMap<String, ArrayList<String>> securityException = new HashMap<String, ArrayList<String>>();
|
---|
365 | ArrayList<String> exceptionGroups = new ArrayList<String>();
|
---|
366 | ArrayList<String> exceptionSets = new ArrayList<String>();
|
---|
367 |
|
---|
368 | Element exception = (Element) exceptions.item(i);
|
---|
369 | NodeList groups = GSXML.getChildrenByTagName(exception, GSXML.GROUP_ELEM);
|
---|
370 | for (int j = 0; j < groups.getLength(); j++)
|
---|
371 | {
|
---|
372 | Element group = (Element) groups.item(j);
|
---|
373 | String groupName = group.getAttribute(GSXML.NAME_ATT);
|
---|
374 | exceptionGroups.add(groupName);
|
---|
375 | }
|
---|
376 | NodeList docSets = GSXML.getChildrenByTagName(exception, GSXML.DOCUMENT_SET_ELEM);
|
---|
377 | for (int j = 0; j < docSets.getLength(); j++)
|
---|
378 | {
|
---|
379 | Element docSet = (Element) docSets.item(j);
|
---|
380 | String docSetName = docSet.getAttribute(GSXML.NAME_ATT);
|
---|
381 | exceptionSets.add(docSetName);
|
---|
382 | }
|
---|
383 |
|
---|
384 | securityException.put("groups", exceptionGroups);
|
---|
385 | securityException.put("sets", exceptionSets);
|
---|
386 | _securityExceptions.add(securityException);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | protected boolean configureServiceRacks(Element coll_config_xml, Element build_config_xml)
|
---|
392 | {
|
---|
393 | clearServices();
|
---|
394 | Element service_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
|
---|
395 | Element oai_service_rack = null;
|
---|
396 | if (service_list != null)
|
---|
397 | {
|
---|
398 | configureServiceRackList(service_list, coll_config_xml);
|
---|
399 | oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, OAIXML.OAIPMH);
|
---|
400 | }
|
---|
401 | // collection Config may also contain manually added service racks
|
---|
402 | service_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
|
---|
403 | if (service_list != null)
|
---|
404 | {
|
---|
405 | configureServiceRackList(service_list, build_config_xml);
|
---|
406 | // this oai used in preference to one in buildConfig.xml
|
---|
407 | oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, GSXML.NAME_ATT, OAIXML.OAIPMH);
|
---|
408 | }
|
---|
409 | // Check for oai
|
---|
410 |
|
---|
411 | if (oai_service_rack != null)
|
---|
412 | {
|
---|
413 | has_oai = true;
|
---|
414 | logger.info(this.cluster_name + " has OAI services");
|
---|
415 | // extract earliestDatestamp from the buildconfig.xml for OAI
|
---|
416 | Element metadata_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
|
---|
417 |
|
---|
418 | if (metadata_list != null)
|
---|
419 | {
|
---|
420 | NodeList children = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
|
---|
421 | // can't do getChildNodes(), because whitespace, such as newlines, creates Text nodes
|
---|
422 | for (int i = 0; i < children.getLength(); i++)
|
---|
423 | {
|
---|
424 | Element metadata = (Element) children.item(i);
|
---|
425 | if (metadata.getAttribute(GSXML.NAME_ATT).equals(OAIXML.EARLIEST_DATESTAMP))
|
---|
426 | {
|
---|
427 | String earliestDatestampStr = GSXML.getValue(metadata);
|
---|
428 | if (!earliestDatestampStr.equals(""))
|
---|
429 | {
|
---|
430 | earliestDatestamp = Long.parseLong(earliestDatestampStr) * 1000; // stored in seconds, convert to milliseconds
|
---|
431 |
|
---|
432 | }
|
---|
433 | break; // found a metadata element with name=earliestDatestamp in buildconfig
|
---|
434 | }
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | // If at the end of this, there is no value for earliestDatestamp, print out a warning
|
---|
439 | logger.warn("No earliestDatestamp in buildConfig.xml for collection: " + this.cluster_name + ". Defaulting to 0.");
|
---|
440 |
|
---|
441 | } // if oai_service_rack != null
|
---|
442 |
|
---|
443 | return true;
|
---|
444 | }
|
---|
445 |
|
---|
446 | /**
|
---|
447 | * do a configure on only part of the collection
|
---|
448 | */
|
---|
449 | protected boolean configureSubset(String subset)
|
---|
450 | {
|
---|
451 |
|
---|
452 | // need the coll config files
|
---|
453 | Element coll_config_elem = loadCollConfigFile();
|
---|
454 | Element build_config_elem = loadBuildConfigFile();
|
---|
455 | if (coll_config_elem == null || build_config_elem == null)
|
---|
456 | {
|
---|
457 | // wont be able to do any of the requests
|
---|
458 | return false;
|
---|
459 | }
|
---|
460 |
|
---|
461 | if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
|
---|
462 | {
|
---|
463 | return configureServiceRacks(coll_config_elem, build_config_elem);
|
---|
464 | }
|
---|
465 |
|
---|
466 | if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.LIBRARY_PARAM_ELEM+GSXML.LIST_MODIFIER))
|
---|
467 | {
|
---|
468 | configureLocalData(coll_config_elem);
|
---|
469 | configureLocalData(build_config_elem);
|
---|
470 | return findAndLoadInfo(coll_config_elem, build_config_elem);
|
---|
471 |
|
---|
472 | }
|
---|
473 |
|
---|
474 | logger.error("Collection: cant process system request, configure " + subset);
|
---|
475 | return false;
|
---|
476 | }
|
---|
477 |
|
---|
478 | /**
|
---|
479 | * handles requests made to the ServiceCluster itself
|
---|
480 | *
|
---|
481 | * @param req
|
---|
482 | * - the request Element- <request>
|
---|
483 | * @return the result Element - should be <response>
|
---|
484 | */
|
---|
485 | protected Element processMessage(Document result_doc, Element request)
|
---|
486 | {
|
---|
487 | String type = request.getAttribute(GSXML.TYPE_ATT);
|
---|
488 | if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
|
---|
489 | {
|
---|
490 | return processFormatStringRequest(result_doc, request);
|
---|
491 | }
|
---|
492 | else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
|
---|
493 | {
|
---|
494 | return processSecurityRequest(result_doc, request);
|
---|
495 | }
|
---|
496 | else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
|
---|
497 | {
|
---|
498 |
|
---|
499 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
500 | response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
|
---|
501 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
|
---|
502 | if (_globalFormat != null)
|
---|
503 | {
|
---|
504 | response.appendChild(result_doc.importNode(_globalFormat, true));
|
---|
505 | }
|
---|
506 | return response;
|
---|
507 | }
|
---|
508 | // unknown type
|
---|
509 | return super.processMessage(result_doc, request);
|
---|
510 |
|
---|
511 | }
|
---|
512 |
|
---|
513 | protected Element processSecurityRequest(Document result_doc, Element request)
|
---|
514 | {
|
---|
515 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
516 | response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
|
---|
517 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SECURITY);
|
---|
518 |
|
---|
519 | String oid = request.getAttribute("oid");
|
---|
520 | if (oid.contains("."))
|
---|
521 | {
|
---|
522 | oid = oid.substring(0, oid.indexOf("."));
|
---|
523 | }
|
---|
524 |
|
---|
525 | ArrayList<String> groups = getPermittedGroups(oid);
|
---|
526 |
|
---|
527 | Element groupList = result_doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
|
---|
528 | response.appendChild(groupList);
|
---|
529 |
|
---|
530 | for (String groupName : groups)
|
---|
531 | {
|
---|
532 | Element group = result_doc.createElement(GSXML.GROUP_ELEM);
|
---|
533 | groupList.appendChild(group);
|
---|
534 | group.setAttribute(GSXML.NAME_ATT, groupName);
|
---|
535 | }
|
---|
536 | return response;
|
---|
537 | }
|
---|
538 |
|
---|
539 | protected ArrayList<String> getPermittedGroups(String oid)
|
---|
540 | {
|
---|
541 | ArrayList<String> groups = new ArrayList<String>();
|
---|
542 |
|
---|
543 | if (_securityScopeCollection)
|
---|
544 | {
|
---|
545 | if (_publicAccess)
|
---|
546 | {
|
---|
547 | groups.add("");
|
---|
548 | }
|
---|
549 | else
|
---|
550 | {
|
---|
551 | for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
|
---|
552 | {
|
---|
553 | for (String group : exception.get("groups"))
|
---|
554 | {
|
---|
555 | groups.add(group);
|
---|
556 | }
|
---|
557 | }
|
---|
558 | }
|
---|
559 | }
|
---|
560 | else
|
---|
561 | {
|
---|
562 | if (oid != null && !oid.equals(""))
|
---|
563 | {
|
---|
564 | boolean inSet = false;
|
---|
565 | for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
|
---|
566 | {
|
---|
567 | for (String setName : exception.get("sets"))
|
---|
568 | {
|
---|
569 | if (documentIsInSet(oid, setName))
|
---|
570 | {
|
---|
571 | inSet = true;
|
---|
572 | for (String group : exception.get("groups"))
|
---|
573 | {
|
---|
574 | groups.add(group);
|
---|
575 | }
|
---|
576 | }
|
---|
577 | }
|
---|
578 | }
|
---|
579 |
|
---|
580 | if (!inSet && _publicAccess)
|
---|
581 | {
|
---|
582 | groups.add("");
|
---|
583 | }
|
---|
584 | }
|
---|
585 | else
|
---|
586 | {
|
---|
587 | groups.add("");
|
---|
588 | }
|
---|
589 | }
|
---|
590 |
|
---|
591 | return groups;
|
---|
592 | }
|
---|
593 |
|
---|
594 | protected boolean documentIsInSet(String oid, String setName)
|
---|
595 | {
|
---|
596 | ArrayList<Element> matchStatements = _documentSets.get(setName);
|
---|
597 | if (matchStatements == null || matchStatements.size() == 0)
|
---|
598 | {
|
---|
599 | return false;
|
---|
600 | }
|
---|
601 |
|
---|
602 | for (Element currentMatchStatement : matchStatements)
|
---|
603 | {
|
---|
604 | String fieldName = currentMatchStatement.getAttribute(GSXML.FIELD_ATT);
|
---|
605 | if (fieldName == null || fieldName.equals(""))
|
---|
606 | {
|
---|
607 | fieldName = "oid";
|
---|
608 | }
|
---|
609 |
|
---|
610 | String type = currentMatchStatement.getAttribute(GSXML.TYPE_ATT);
|
---|
611 | if (type == null || type.equals(""))
|
---|
612 | {
|
---|
613 | type = "match";
|
---|
614 | }
|
---|
615 |
|
---|
616 | String fieldValue = "";
|
---|
617 | if (!fieldName.equals("oid"))
|
---|
618 | {
|
---|
619 | fieldValue = getFieldValue(oid, fieldName);
|
---|
620 | if (fieldValue == null)
|
---|
621 | {
|
---|
622 | return false;
|
---|
623 | }
|
---|
624 | }
|
---|
625 | else
|
---|
626 | {
|
---|
627 | fieldValue = oid;
|
---|
628 | }
|
---|
629 |
|
---|
630 | String matchValue = GSXML.getNodeText(currentMatchStatement);
|
---|
631 | if (type.equals("match"))
|
---|
632 | {
|
---|
633 | if (matchValue.equals(fieldValue))
|
---|
634 | {
|
---|
635 | return true;
|
---|
636 | }
|
---|
637 | }
|
---|
638 | else if (type.equals("regex"))
|
---|
639 | {
|
---|
640 | if (fieldValue.matches(matchValue))
|
---|
641 | {
|
---|
642 | return true;
|
---|
643 | }
|
---|
644 | }
|
---|
645 | else
|
---|
646 | {
|
---|
647 | logger.warn("Unknown type of match specified in security block of collection " + this.cluster_name + ".");
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | return false;
|
---|
652 | }
|
---|
653 |
|
---|
654 | protected String getFieldValue(String oid, String fieldName)
|
---|
655 | {
|
---|
656 | Document msg_doc = XMLConverter.newDOM();
|
---|
657 | Element metadataMessage = msg_doc.createElement(GSXML.MESSAGE_ELEM);
|
---|
658 | Element metadataRequest = GSXML.createBasicRequest(msg_doc, GSXML.REQUEST_TYPE_PROCESS, this.cluster_name + "/DocumentMetadataRetrieve", new UserContext());
|
---|
659 | metadataMessage.appendChild(metadataRequest);
|
---|
660 |
|
---|
661 | Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
|
---|
662 | metadataRequest.appendChild(paramList);
|
---|
663 |
|
---|
664 | Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
|
---|
665 | paramList.appendChild(param);
|
---|
666 |
|
---|
667 | param.setAttribute(GSXML.NAME_ATT, "metadata");
|
---|
668 | param.setAttribute(GSXML.VALUE_ATT, fieldName);
|
---|
669 |
|
---|
670 | Element docList = msg_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
|
---|
671 | metadataRequest.appendChild(docList);
|
---|
672 |
|
---|
673 | Element doc = msg_doc.createElement(GSXML.DOC_NODE_ELEM);
|
---|
674 | docList.appendChild(doc);
|
---|
675 |
|
---|
676 | doc.setAttribute(GSXML.NODE_ID_ATT, oid);
|
---|
677 |
|
---|
678 | Element response = (Element) this.router.process(metadataMessage);
|
---|
679 | NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
|
---|
680 |
|
---|
681 | if (metadataElems.getLength() > 0)
|
---|
682 | {
|
---|
683 | Element metadata = (Element) metadataElems.item(0);
|
---|
684 | return GSXML.getNodeText(metadata);
|
---|
685 | }
|
---|
686 |
|
---|
687 | return null;
|
---|
688 | }
|
---|
689 |
|
---|
690 | protected Element processFormatStringRequest(Document result_doc, Element request)
|
---|
691 | {
|
---|
692 | Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
|
---|
693 | response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT_STRING);
|
---|
694 | response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
|
---|
695 |
|
---|
696 | String subaction = request.getAttribute("subaction");
|
---|
697 | String service = request.getAttribute("service");
|
---|
698 |
|
---|
699 | String classifier = null;
|
---|
700 | if (service.equals("ClassifierBrowse"))
|
---|
701 | {
|
---|
702 | classifier = request.getAttribute("classifier");
|
---|
703 | }
|
---|
704 |
|
---|
705 | // check for version file
|
---|
706 | String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
|
---|
707 |
|
---|
708 | String version_filename = "";
|
---|
709 | if (service.equals("ClassifierBrowse"))
|
---|
710 | version_filename = directory + "browse_" + classifier + "_format_statement_version.txt";
|
---|
711 | else
|
---|
712 | version_filename = directory + "query_format_statement_version.txt";
|
---|
713 |
|
---|
714 | File version_file = new File(version_filename);
|
---|
715 |
|
---|
716 | if (subaction.equals("update"))
|
---|
717 | {
|
---|
718 | Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
|
---|
719 | //String format_string = GSXML.getNodeText(format_element);
|
---|
720 | Element format_statement = (Element) format_element.getFirstChild();
|
---|
721 |
|
---|
722 | String version_number = "1";
|
---|
723 | BufferedWriter writer;
|
---|
724 |
|
---|
725 | try
|
---|
726 | {
|
---|
727 |
|
---|
728 | if (version_file.exists())
|
---|
729 | {
|
---|
730 | // Read version
|
---|
731 | BufferedReader reader = new BufferedReader(new FileReader(version_filename));
|
---|
732 | version_number = reader.readLine();
|
---|
733 | int aInt = Integer.parseInt(version_number) + 1;
|
---|
734 | version_number = Integer.toString(aInt);
|
---|
735 | reader.close();
|
---|
736 | }
|
---|
737 | else
|
---|
738 | {
|
---|
739 | // Create
|
---|
740 | version_file.createNewFile();
|
---|
741 | writer = new BufferedWriter(new FileWriter(version_filename));
|
---|
742 | writer.write(version_number);
|
---|
743 | writer.close();
|
---|
744 | }
|
---|
745 |
|
---|
746 | // Write version file
|
---|
747 | String format_statement_filename = "";
|
---|
748 |
|
---|
749 | if (service.equals("ClassifierBrowse"))
|
---|
750 | format_statement_filename = directory + "browse_" + classifier + "_format_statement_v" + version_number + ".txt";
|
---|
751 | else
|
---|
752 | format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
|
---|
753 |
|
---|
754 | // Write format statement
|
---|
755 | String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
|
---|
756 | writer = new BufferedWriter(new FileWriter(format_statement_filename));
|
---|
757 | writer.write(format_string);
|
---|
758 | writer.close();
|
---|
759 |
|
---|
760 | // Update version number
|
---|
761 | writer = new BufferedWriter(new FileWriter(version_filename));
|
---|
762 | writer.write(version_number);
|
---|
763 | writer.close();
|
---|
764 |
|
---|
765 | }
|
---|
766 | catch (IOException e)
|
---|
767 | {
|
---|
768 | logger.error("IO Exception " + e);
|
---|
769 | }
|
---|
770 | }
|
---|
771 |
|
---|
772 | if (subaction.equals("saveDocument"))
|
---|
773 | {
|
---|
774 | Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
|
---|
775 | //String format_string = GSXML.getNodeText(format_element);
|
---|
776 | // Get display tag
|
---|
777 | Element display_format = (Element) format_element.getFirstChild();
|
---|
778 |
|
---|
779 | String collection_config = directory + "collectionConfig.xml";
|
---|
780 | Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
|
---|
781 |
|
---|
782 | Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
|
---|
783 |
|
---|
784 | // Get display child
|
---|
785 | if (GSXML.getChildByTagName(current_node, "display") == null)
|
---|
786 | {
|
---|
787 | // well then create a format tag
|
---|
788 | Element display_tag = config.createElement("display");
|
---|
789 | current_node = (Node) current_node.appendChild(display_tag);
|
---|
790 | }
|
---|
791 | else
|
---|
792 | {
|
---|
793 | current_node = GSXML.getChildByTagName(current_node, "display");
|
---|
794 | }
|
---|
795 |
|
---|
796 | if (GSXML.getChildByTagName(current_node, "format") == null)
|
---|
797 | {
|
---|
798 | // well then create a format tag
|
---|
799 | Element format_tag = config.createElement("format");
|
---|
800 | current_node.appendChild(format_tag);
|
---|
801 | }
|
---|
802 |
|
---|
803 | current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
|
---|
804 |
|
---|
805 | String new_config = this.converter.getString(config);
|
---|
806 |
|
---|
807 | new_config = StringUtils.replace(new_config, "<", "<");
|
---|
808 | new_config = StringUtils.replace(new_config, ">", ">");
|
---|
809 | new_config = StringUtils.replace(new_config, """, "\"");
|
---|
810 |
|
---|
811 | try
|
---|
812 | {
|
---|
813 | // Write to file (not original! for now)
|
---|
814 | BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
|
---|
815 | writer.write(new_config);
|
---|
816 | writer.close();
|
---|
817 | }
|
---|
818 | catch (IOException e)
|
---|
819 | {
|
---|
820 | logger.error("IO Exception " + e);
|
---|
821 | }
|
---|
822 | }
|
---|
823 |
|
---|
824 | if (subaction.equals("save"))
|
---|
825 | {
|
---|
826 | Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
|
---|
827 | Element format_statement = (Element) format_element.getFirstChild();
|
---|
828 |
|
---|
829 | try
|
---|
830 | {
|
---|
831 | // open collectionConfig.xml and read in to w3 Document
|
---|
832 | String collection_config = directory + "collectionConfig.xml";
|
---|
833 | Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
|
---|
834 |
|
---|
835 | //String tag_name = "";
|
---|
836 | int k;
|
---|
837 | int index;
|
---|
838 | Element elem;
|
---|
839 | Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
|
---|
840 | NodeList current_node_list;
|
---|
841 |
|
---|
842 | if (service.equals("ClassifierBrowse"))
|
---|
843 | {
|
---|
844 | //tag_name = "browse";
|
---|
845 | // if CLX then need to look in <classifier> X then <format>
|
---|
846 | // default is <browse><format>
|
---|
847 |
|
---|
848 | current_node = GSXML.getChildByTagName(current_node, "browse");
|
---|
849 |
|
---|
850 | // find CLX
|
---|
851 | if (classifier != null)
|
---|
852 | {
|
---|
853 | current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
|
---|
854 | index = Integer.parseInt(classifier.substring(2)) - 1;
|
---|
855 |
|
---|
856 | // index should be given by X-1
|
---|
857 | current_node = current_node_list.item(index);
|
---|
858 | // what if classifier does not have a format tag?
|
---|
859 | if (GSXML.getChildByTagName(current_node, "format") == null)
|
---|
860 | {
|
---|
861 | // well then create a format tag
|
---|
862 | Element format_tag = config.createElement("format");
|
---|
863 | current_node.appendChild(format_tag);
|
---|
864 | }
|
---|
865 | }
|
---|
866 | else
|
---|
867 | {
|
---|
868 | // To support all classifiers, set classifier to null? There is the chance here that the format tag does not exist
|
---|
869 | if (GSXML.getChildByTagName(current_node, "format") == null)
|
---|
870 | {
|
---|
871 | // well then create a format tag
|
---|
872 | Element format_tag = config.createElement("format");
|
---|
873 | current_node.appendChild(format_tag);
|
---|
874 | }
|
---|
875 | }
|
---|
876 | }
|
---|
877 | else if (service.equals("AllClassifierBrowse"))
|
---|
878 | {
|
---|
879 | current_node = GSXML.getChildByTagName(current_node, "browse");
|
---|
880 | if (GSXML.getChildByTagName(current_node, "format") == null)
|
---|
881 | {
|
---|
882 | // well then create a format tag
|
---|
883 | Element format_tag = config.createElement("format");
|
---|
884 | current_node.appendChild(format_tag);
|
---|
885 | }
|
---|
886 | }
|
---|
887 | else
|
---|
888 | {
|
---|
889 | // look in <format> with no attributes
|
---|
890 | current_node_list = GSXML.getChildrenByTagName(current_node, "search");
|
---|
891 | for (k = 0; k < current_node_list.getLength(); k++)
|
---|
892 | {
|
---|
893 | current_node = current_node_list.item(k);
|
---|
894 | // if current_node has no attributes then break
|
---|
895 | elem = (Element) current_node;
|
---|
896 | if (elem.hasAttribute("name") == false)
|
---|
897 | break;
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
|
---|
902 |
|
---|
903 | // Now convert config document to string for writing to file
|
---|
904 | String new_config = this.converter.getString(config);
|
---|
905 |
|
---|
906 | new_config = StringUtils.replace(new_config, "<", "<");
|
---|
907 | new_config = StringUtils.replace(new_config, ">", ">");
|
---|
908 | new_config = StringUtils.replace(new_config, """, "\"");
|
---|
909 |
|
---|
910 | // Write to file (not original! for now)
|
---|
911 | BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
|
---|
912 | writer.write(new_config);
|
---|
913 | writer.close();
|
---|
914 |
|
---|
915 | }
|
---|
916 | catch (Exception ex)
|
---|
917 | {
|
---|
918 | logger.error("There was an exception " + ex);
|
---|
919 |
|
---|
920 | StringWriter sw = new StringWriter();
|
---|
921 | PrintWriter pw = new PrintWriter(sw, true);
|
---|
922 | ex.printStackTrace(pw);
|
---|
923 | pw.flush();
|
---|
924 | sw.flush();
|
---|
925 | logger.error(sw.toString());
|
---|
926 | }
|
---|
927 |
|
---|
928 | }
|
---|
929 |
|
---|
930 | return response;
|
---|
931 | }
|
---|
932 | }
|
---|