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

Last change on this file was 37614, checked in by kjdon, 13 months ago

now looks for a ccServices element (cross collection) - and stores its format elements. if the coll gets a format request come in, with a service attribute, it looks up that service name to see if there is a format element for it, and returns it if there is.

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