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

Last change on this file since 32989 was 32989, checked in by kjdon, 5 years ago

added a warning message

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