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

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

Adding in security capabilities for Greenstone 3

  • 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 (meta_list != null)
288 {
289 Element meta_elem = (Element) GSXML.getNamedElement(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 int k;
598 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
599 //String format_string = GSXML.getNodeText(format_element);
600 // Get display tag
601 Element display_format = (Element) format_element.getFirstChild();
602
603 String format_string = GSXML.xmlNodeToString(display_format, false);
604 String collection_config = directory + "collectionConfig.xml";
605 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
606
607 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
608
609 // Get display child
610 if (GSXML.getChildByTagName(current_node, "display") == null)
611 {
612 // well then create a format tag
613 Element display_tag = config.createElement("display");
614 current_node = (Node) current_node.appendChild(display_tag);
615 }
616 else
617 {
618 current_node = GSXML.getChildByTagName(current_node, "display");
619 }
620
621 if (GSXML.getChildByTagName(current_node, "format") == null)
622 {
623 // well then create a format tag
624 Element format_tag = config.createElement("format");
625 current_node.appendChild(format_tag);
626 }
627
628 current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
629
630 String new_config = this.converter.getString(config);
631
632 new_config = StringUtils.replace(new_config, "&lt;", "<");
633 new_config = StringUtils.replace(new_config, "&gt;", ">");
634 new_config = StringUtils.replace(new_config, "&quot;", "\"");
635
636 try
637 {
638 // Write to file (not original! for now)
639 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
640 writer.write(new_config);
641 writer.close();
642 }
643 catch (IOException e)
644 {
645 logger.error("IO Exception " + e);
646 }
647 }
648
649 if (subaction.equals("save"))
650 {
651 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
652 Element format_statement = (Element) format_element.getFirstChild();
653
654 try
655 {
656 // open collectionConfig.xml and read in to w3 Document
657 String collection_config = directory + "collectionConfig.xml";
658 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
659
660 //String tag_name = "";
661 int k;
662 int index;
663 Element elem;
664 // Try importing entire tree to this.doc so we can add and remove children at ease
665 //Node current_node = this.doc.importNode(GSXML.getChildByTagName(config, "CollectionConfig"),true);
666 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
667 NodeList current_node_list;
668
669 if (service.equals("ClassifierBrowse"))
670 {
671 //tag_name = "browse";
672 // if CLX then need to look in <classifier> X then <format>
673 // default is <browse><format>
674
675 current_node = GSXML.getChildByTagName(current_node, "browse");
676
677 // find CLX
678 if (classifier != null)
679 {
680 current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
681 index = Integer.parseInt(classifier.substring(2)) - 1;
682
683 // index should be given by X-1
684 current_node = current_node_list.item(index);
685 // what if classifier does not have a format tag?
686 if (GSXML.getChildByTagName(current_node, "format") == null)
687 {
688 // well then create a format tag
689 Element format_tag = config.createElement("format");
690 current_node.appendChild(format_tag);
691 }
692 }
693 else
694 {
695 // To support all classifiers, set classifier to null? There is the chance here that the format tag does not exist
696 if (GSXML.getChildByTagName(current_node, "format") == null)
697 {
698 // well then create a format tag
699 Element format_tag = config.createElement("format");
700 current_node.appendChild(format_tag);
701 }
702 }
703 }
704 else if (service.equals("AllClassifierBrowse"))
705 {
706 current_node = GSXML.getChildByTagName(current_node, "browse");
707 if (GSXML.getChildByTagName(current_node, "format") == null)
708 {
709 // well then create a format tag
710 Element format_tag = config.createElement("format");
711 current_node.appendChild(format_tag);
712 }
713 }
714 else
715 {
716 // look in <format> with no attributes
717 current_node_list = GSXML.getChildrenByTagName(current_node, "search");
718 for (k = 0; k < current_node_list.getLength(); k++)
719 {
720 current_node = current_node_list.item(k);
721 // if current_node has no attributes then break
722 elem = (Element) current_node;
723 if (elem.hasAttribute("name") == false)
724 break;
725 }
726 }
727
728 current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
729
730 // Now convert config document to string for writing to file
731 String new_config = this.converter.getString(config);
732
733 new_config = StringUtils.replace(new_config, "&lt;", "<");
734 new_config = StringUtils.replace(new_config, "&gt;", ">");
735 new_config = StringUtils.replace(new_config, "&quot;", "\"");
736
737 // Write to file (not original! for now)
738 BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
739 writer.write(new_config);
740 writer.close();
741
742 }
743 catch (Exception ex)
744 {
745 logger.error("There was an exception " + ex);
746
747 StringWriter sw = new StringWriter();
748 PrintWriter pw = new PrintWriter(sw, true);
749 ex.printStackTrace(pw);
750 pw.flush();
751 sw.flush();
752 logger.error(sw.toString());
753 }
754
755 }
756 }
757 else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
758 {
759 String oid = request.getAttribute("oid");
760 ArrayList<String> groups = getPermittedGroups(oid);
761
762 Element groupList = this.doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
763 response.appendChild(groupList);
764
765 for(String groupName : groups)
766 {
767 Element group = this.doc.createElement(GSXML.GROUP_ELEM);
768 groupList.appendChild(group);
769 group.setAttribute(GSXML.NAME_ATT, groupName);
770 }
771 }
772 else
773 { // unknown type
774 return super.processMessage(request);
775
776 }
777 return response;
778 }
779
780 protected ArrayList<String> getPermittedGroups(String oid)
781 {
782 ArrayList<String> groups = new ArrayList<String>();
783
784 if (_securityScopeCollection)
785 {
786 if (_publicAccess)
787 {
788 groups.add("");
789 }
790 else
791 {
792 for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
793 {
794 for (String group : exception.get("groups"))
795 {
796 groups.add(group);
797 }
798 }
799 }
800 }
801 else
802 {
803 if(oid != null && !oid.equals(""))
804 {
805 boolean inSet = false;
806 for (HashMap<String, ArrayList<String>> exception : _securityExceptions)
807 {
808 for (String setName : exception.get("sets"))
809 {
810 if (documentIsInSet(oid, setName))
811 {
812 inSet = true;
813 for (String group : exception.get("groups"))
814 {
815 groups.add(group);
816 }
817 }
818 }
819 }
820
821 if(!inSet && _publicAccess)
822 {
823 groups.add("");
824 }
825 }
826 else
827 {
828 groups.add("");
829 }
830 }
831
832 return groups;
833 }
834
835 protected boolean documentIsInSet(String oid, String setName)
836 {
837 ArrayList<Element> matchStatements = _documentSets.get(setName);
838 if (matchStatements == null || matchStatements.size() == 0)
839 {
840 return false;
841 }
842
843 for (Element currentMatchStatement : matchStatements)
844 {
845 String fieldName = currentMatchStatement.getAttribute(GSXML.FIELD_ATT);
846 if (fieldName == null || fieldName.equals(""))
847 {
848 fieldName = "oid";
849 }
850
851 String type = currentMatchStatement.getAttribute(GSXML.TYPE_ATT);
852 if (type == null || type.equals(""))
853 {
854 type = "match";
855 }
856
857 String fieldValue = "";
858 if (!fieldName.equals("oid"))
859 {
860 fieldValue = getFieldValue(oid, fieldName);
861 if(fieldValue == null)
862 {
863 return false;
864 }
865 }
866 else
867 {
868 fieldValue = oid;
869 }
870
871 String matchValue = GSXML.getNodeText(currentMatchStatement);
872 if (type.equals("match"))
873 {
874 if(matchValue.equals(fieldValue))
875 {
876 return true;
877 }
878 }
879 else if (type.equals("regex"))
880 {
881 if(fieldValue.matches(matchValue))
882 {
883 return true;
884 }
885 }
886 else
887 {
888 logger.warn("Unknown type of match specified in security block of collection " + this.cluster_name + ".");
889 }
890 }
891
892 return false;
893 }
894
895 protected String getFieldValue(String oid, String fieldName)
896 {
897 Element metadataMessage = this.doc.createElement(GSXML.MESSAGE_ELEM);
898 Element metadataRequest = GSXML.createBasicRequest(this.doc, GSXML.REQUEST_TYPE_PROCESS, this.cluster_name + "/DocumentMetadataRetrieve", new UserContext());
899 metadataMessage.appendChild(metadataRequest);
900
901 Element paramList = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
902 metadataRequest.appendChild(paramList);
903
904 Element param = this.doc.createElement(GSXML.PARAM_ELEM);
905 paramList.appendChild(param);
906
907 param.setAttribute(GSXML.NAME_ATT, "metadata");
908 param.setAttribute(GSXML.VALUE_ATT, fieldName);
909
910 Element docList = this.doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
911 metadataRequest.appendChild(docList);
912
913 Element doc = this.doc.createElement(GSXML.DOC_NODE_ELEM);
914 docList.appendChild(doc);
915
916 doc.setAttribute(GSXML.NODE_ID_ATT, oid);
917
918 Element response = (Element) this.router.process(metadataMessage);
919 NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
920
921 if(metadataElems.getLength() > 0)
922 {
923 Element metadata = (Element) metadataElems.item(0);
924 return GSXML.getNodeText(metadata);
925 }
926
927 return null;
928 }
929}
Note: See TracBrowser for help on using the repository browser.