source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java@ 25647

Last change on this file since 25647 was 25647, checked in by sjm84, 12 years ago

More reformatting and minor fixes

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