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

Last change on this file since 36976 was 34154, checked in by ak19, 4 years ago

Useful debugging statement. Would have helped me solve a bug sooner by detecting the real issue sooner (wrong site name was in use, because the library that was invoked on the actual site was incorrect)

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