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

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

Tidying imports

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