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
RevLine 
[3235]1/*
[24236]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 */
[3222]19package org.greenstone.gsdl3.collection;
20
[25751]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;
[3222]31
[25751]32import org.apache.commons.lang3.StringUtils;
33import org.apache.log4j.Logger;
34import org.greenstone.gsdl3.core.ModuleInterface;
[32985]35import org.greenstone.gsdl3.service.Authentication;
[30838]36import org.greenstone.gsdl3.util.CustomClassLoader;
[30670]37import org.greenstone.gsdl3.util.Dictionary;
[25751]38import org.greenstone.gsdl3.util.GSFile;
39import org.greenstone.gsdl3.util.GSXML;
[27716]40import org.greenstone.gsdl3.util.GSXSLT;
[25751]41import org.greenstone.gsdl3.util.OAIXML;
[27716]42import org.greenstone.gsdl3.util.SimpleMacroResolver;
[25751]43import org.greenstone.gsdl3.util.UserContext;
[28966]44import org.greenstone.gsdl3.util.XMLConverter;
[25751]45import org.greenstone.gsdl3.util.XMLTransformer;
[24393]46import org.w3c.dom.Document;
[25751]47import org.w3c.dom.Element;
[24393]48import org.w3c.dom.Node;
49import org.w3c.dom.NodeList;
[3222]50
51/**
[24393]52 * Represents a collection in Greenstone. A collection is an extension of a
53 * ServiceCluster - it has local data that the services use.
54 *
[25727]55 * @author Katherine Don
[24236]56 * @see ModuleInterface
57 */
[24393]58public class Collection extends ServiceCluster
59{
[3222]60
[24393]61 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
[13123]62
[27705]63 /** is this collection being tidied and therefore can support realistic book view? */
[24393]64 protected boolean useBook = false;
[27705]65 /**
66 * is this collection public or private - public collections will
67 * appear on the home page, whereas private collections won't
68 */
[24393]69 protected boolean is_public = true;
[26446]70 /** collection type : mg, mgpp or lucene */
71 protected String col_type = "";
72 /** database type : gdbm, jdbm or sqlite */
73 protected String db_type = "";
[28984]74 /** time when this collection was built Used by RSS */
[24393]75 protected long lastmodified = 0;
[31916]76 /** earliestDatestamp of this collection. Used by RSS. No longer used as fallback by OAI */
[28984]77 protected long earliestDatestamp = 0;
[23938]78
[25092]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;
[33347]83 protected String _humanVerify = null;
[32985]84 protected boolean _useRecaptcha = false; // for human verify
85 protected String _siteKey = null; // for recaptcha
86 protected String _secretKey = null; // for recaptcha
87
[25092]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
[24393]91 protected XMLTransformer transformer = null;
[24236]92
[24393]93 /** same as setClusterName */
94 public void setCollectionName(String name)
95 {
96 setClusterName(name);
[3222]97 }
[24393]98
99 public Collection()
100 {
101 super();
[28966]102 this.description = this.desc_doc.createElement(GSXML.COLLECTION_ELEM);
[4097]103 }
[14208]104
[24393]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 }
[30670]122 // set up the class loader
[30838]123 this.class_loader = new CustomClassLoader(this.getClass().getClassLoader(), GSFile.collectionResourceDir(this.site_home, this.cluster_name));
[27705]124
[26446]125 macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
[24236]126
[24393]127 Element coll_config_xml = loadCollConfigFile();
[29164]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 }
[24393]133 Element build_config_xml = loadBuildConfigFile();
[24236]134
[29164]135 if (build_config_xml == null)
[24393]136 {
[29164]137 logger.error("Collection: couldn't configure collection: " + this.cluster_name + ", " + "Couldn't load build config file");
138
139 return false;
[24393]140 }
141
[29164]142 GSXSLT.modifyCollectionConfigForDebug(coll_config_xml);
[24393]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 }
[30563]149
[24393]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
[26446]160 this.description.setAttribute(GSXML.TYPE_ATT, col_type);
161 this.description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
[27705]162
[25974]163 _globalFormat = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.FORMAT_ELEM);
[26446]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
[24393]168 findAndLoadInfo(coll_config_xml, build_config_xml);
169
[25092]170 loadSecurityInformation(coll_config_xml);
171
[32475]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 }
[24393]177 // now do the services
178 configureServiceRacks(coll_config_xml, build_config_xml);
179
180 return true;
181
[24221]182 }
[24393]183
184 public boolean useBook()
185 {
186 return useBook;
[24221]187 }
[13860]188
[24393]189 public boolean isPublic()
190 {
191 return is_public;
[24221]192 }
[24393]193
[31916]194 // Used by RSSRetrieve. No longer used by OAI Receptionist (as second fallback)
[24393]195 public long getLastmodified()
196 {
197 return lastmodified;
[24236]198 }
[23938]199
[31916]200 // used by RSSRetrieve, no longer used as fallback by the OAIReceptionist
[24393]201 public long getEarliestDatestamp()
202 {
203 return earliestDatestamp;
204 }
[24236]205
[24393]206 /**
207 * load in the collection config file into a DOM Element
208 */
209 protected Element loadCollConfigFile()
210 {
[24236]211
[24393]212 File coll_config_file = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name));
[24236]213
[24393]214 if (!coll_config_file.exists())
215 {
[34154]216 logger.error("@@@ File " + coll_config_file + " did not exist. Can't load it in");
[24393]217 return null;
[24221]218 }
[26446]219 // get the xml
[24393]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
[24221]228 }
[4097]229
[24393]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;
[3847]251 }
[23938]252
[24393]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 {
[28984]259 addMetadata("httpPath", this.site_http_address + "/collect/" + this.cluster_name);
[24393]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);
[26446]267 //addPlugins(plugin_list);
[24393]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 }
[26446]282 String tidy = (useBook == true ? "tidy" : "untidy");
283 addMetadata("tidyoption", tidy);
[24393]284
[28984]285
[25466]286 if (this.metadata_list != null)
[24393]287 {
[28984]288 // check whether we are public or not
[25466]289 Element meta_elem = (Element) GSXML.getNamedElement(this.metadata_list, GSXML.METADATA_ELEM, GSXML.NAME_ATT, "public");
[24393]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 }
[28984]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
[24393]307 }
308 return true;
[24221]309 }
[4097]310
[25092]311 protected void loadSecurityInformation(Element coll_config_xml)
312 {
313 Element securityBlock = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SECURITY_ELEM);
314
[25647]315 if (securityBlock == null)
[25092]316 {
317 return;
318 }
[25647]319
[29310]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 }
[25092]326 String scope = securityBlock.getAttribute(GSXML.SCOPE_ATT);
327 String defaultAccess = securityBlock.getAttribute(GSXML.DEFAULT_ACCESS_ATT);
328
[33176]329 if (defaultAccess.toLowerCase().equals(GSXML.ACCESS_PUBLIC))
[25092]330 {
331 _publicAccess = true;
332 }
[33176]333 else if (defaultAccess.toLowerCase().equals(GSXML.ACCESS_PRIVATE))
[25092]334 {
335 _publicAccess = false;
336 }
337 else
338 {
[33113]339 logger.warn("Default access for collection " + this.cluster_name + " is neither public nor private, assuming public");
[25092]340 }
341
[33176]342 String humanVerify = securityBlock.getAttribute(GSXML.VERIFY_ATT);
[33347]343 if (!humanVerify.equals("") && !humanVerify.equals("false")) {
344 _humanVerify = humanVerify;
[32985]345 }
346
[33176]347 String useRecaptcha = securityBlock.getAttribute(GSXML.USE_RECAPTCHA_ATT);
[32985]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;
[32989]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!");
[32990]361 }
[32985]362 }
363 }
[33176]364 if (scope.toLowerCase().equals(GSXML.SCOPE_COLLECTION))
[25092]365 {
366 _securityScopeCollection = true;
367 }
[33176]368 else if (scope.toLowerCase().equals(GSXML.SCOPE_DOCUMENT))
[25092]369 {
370 _securityScopeCollection = false;
371 }
372 else
373 {
[33113]374 logger.warn("Security scope is neither collection nor document, assuming collection");
[25092]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 }
[29310]419 if (_securityScopeCollection) {
420 // we don't add in any exceptions that have document sets
421 if (!exceptionSets.isEmpty()) {
422 continue;
423 }
424 }
[25092]425 securityException.put("groups", exceptionGroups);
426 securityException.put("sets", exceptionSets);
427 _securityExceptions.add(securityException);
428 }
429 }
430 }
431
[24393]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);
[27705]436 if (service_list != null)
437 {
438 configureServiceRackList(service_list, coll_config_xml);
[26446]439 }
[24393]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 {
[27705]444 configureServiceRackList(service_list, build_config_xml);
[24393]445 }
446 return true;
447 }
[23489]448
[24393]449 /**
450 * do a configure on only part of the collection
451 */
452 protected boolean configureSubset(String subset)
453 {
[24203]454
[24393]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 }
[23489]463
[24393]464 if (subset.equals(GSXML.SERVICE_ELEM + GSXML.LIST_MODIFIER))
465 {
466 return configureServiceRacks(coll_config_elem, build_config_elem);
467 }
[23489]468
[28966]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))
[24393]470 {
[27705]471 configureLocalData(coll_config_elem);
472 configureLocalData(build_config_elem);
[24393]473 return findAndLoadInfo(coll_config_elem, build_config_elem);
[23489]474
[24393]475 }
[23489]476
[24393]477 logger.error("Collection: cant process system request, configure " + subset);
478 return false;
479 }
[23489]480
[24393]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 */
[28966]488 protected Element processMessage(Document result_doc, Element request)
[24393]489 {
[27705]490 String type = request.getAttribute(GSXML.TYPE_ATT);
[24393]491 if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
492 {
[28966]493 return processFormatStringRequest(result_doc, request);
[24393]494 }
[25092]495 else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
496 {
[28966]497 return processSecurityRequest(result_doc, request);
[25092]498 }
[25989]499 else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
500 {
[28966]501
502 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
[27705]503 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
504 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
505 if (_globalFormat != null)
[25989]506 {
[28966]507 response.appendChild(result_doc.importNode(_globalFormat, true));
[25989]508 }
[26446]509 return response;
[25989]510 }
[26446]511 // unknown type
[28966]512 return super.processMessage(result_doc, request);
[24393]513
514 }
515
[28966]516 protected Element processSecurityRequest(Document result_doc, Element request)
[27705]517 {
[28966]518 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
[27705]519 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
520 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SECURITY);
[26446]521
[33347]522 if (_humanVerify != null) {
[34019]523 response.setAttribute(GSXML.VERIFY_ATT, _humanVerify);
[32985]524 if (_useRecaptcha) {
[34019]525 response.setAttribute(GSXML.SITE_KEY_ATT, _siteKey);
526 response.setAttribute(GSXML.SECRET_KEY_ATT, _secretKey);
[32985]527 }
528 }
[27705]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
[28966]537 Element groupList = result_doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
[27705]538 response.appendChild(groupList);
539
540 for (String groupName : groups)
541 {
[28966]542 Element group = result_doc.createElement(GSXML.GROUP_ELEM);
[27705]543 groupList.appendChild(group);
544 group.setAttribute(GSXML.NAME_ATT, groupName);
545 }
546 return response;
547 }
548
[25092]549 protected ArrayList<String> getPermittedGroups(String oid)
550 {
551 ArrayList<String> groups = new ArrayList<String>();
[25647]552
[25092]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 {
[25647]572 if (oid != null && !oid.equals(""))
[25092]573 {
574 boolean inSet = false;
[29049]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"))
[25092]587 {
[29049]588 if (documentIsInSet(oid, setName))
589 {
590 inSet = true;
591 for (String group : exception.get("groups"))
[25092]592 {
[29049]593 groups.add(group);
[25092]594 }
[29049]595 break;
596 }
[25092]597 }
[29049]598 }
[25092]599 }
[29049]600
601
602
[25647]603 if (!inSet && _publicAccess)
[29049]604 {// our doc was not part of any exception, so it must be public
[25092]605 groups.add("");
606 }
607 }
[29049]608 else // if we are not doing a request with an oid, then free to access
[25092]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
[33113]639 //String fieldValue = "";
640 String[] fieldValues = null;
[25092]641 if (!fieldName.equals("oid"))
642 {
[33113]643 //fieldValue = getFieldValue(oid, fieldName);
644 fieldValues = getFieldValues(oid, fieldName);
645 if (fieldValues == null)
[25092]646 {
647 return false;
648 }
649 }
650 else
651 {
[33113]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.
[25092]656 }
657
658 String matchValue = GSXML.getNodeText(currentMatchStatement);
659 if (type.equals("match"))
[25647]660 {
[33113]661
662 for(int i = 0; i < fieldValues.length; i++) {
663 String fieldValue = fieldValues[i];
[25647]664 if (matchValue.equals(fieldValue))
[25092]665 {
666 return true;
667 }
[33113]668 }
[25092]669 }
670 else if (type.equals("regex"))
671 {
[33113]672 for(int i = 0; i < fieldValues.length; i++) {
673 String fieldValue = fieldValues[i];
[25647]674 if (fieldValue.matches(matchValue))
[25092]675 {
676 return true;
677 }
[33113]678 }
[25092]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
[33113]689 protected String old_getFieldValue(String oid, String fieldName)
[25092]690 {
[28966]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());
[25092]694 metadataMessage.appendChild(metadataRequest);
695
[28966]696 Element paramList = msg_doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
[25092]697 metadataRequest.appendChild(paramList);
[25647]698
[28966]699 Element param = msg_doc.createElement(GSXML.PARAM_ELEM);
[25092]700 paramList.appendChild(param);
[25647]701
[25092]702 param.setAttribute(GSXML.NAME_ATT, "metadata");
703 param.setAttribute(GSXML.VALUE_ATT, fieldName);
[25647]704
[28966]705 Element docList = msg_doc.createElement(GSXML.DOC_NODE_ELEM + GSXML.LIST_MODIFIER);
[25092]706 metadataRequest.appendChild(docList);
[25647]707
[28966]708 Element doc = msg_doc.createElement(GSXML.DOC_NODE_ELEM);
[25092]709 docList.appendChild(doc);
[25647]710
[25092]711 doc.setAttribute(GSXML.NODE_ID_ATT, oid);
[25647]712
[25092]713 Element response = (Element) this.router.process(metadataMessage);
714 NodeList metadataElems = response.getElementsByTagName(GSXML.METADATA_ELEM);
[25647]715
716 if (metadataElems.getLength() > 0)
[25092]717 {
718 Element metadata = (Element) metadataElems.item(0);
719 return GSXML.getNodeText(metadata);
720 }
[25647]721
[25092]722 return null;
723 }
[26446]724
[33113]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
[28966]767 protected Element processFormatStringRequest(Document result_doc, Element request)
[27705]768 {
[28966]769 Element response = result_doc.createElement(GSXML.RESPONSE_ELEM);
[27705]770 response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT_STRING);
771 response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
[26446]772
[27705]773 String subaction = request.getAttribute("subaction");
774 String service = request.getAttribute("service");
[26446]775
[27705]776 String classifier = null;
777 if (service.equals("ClassifierBrowse"))
778 {
779 classifier = request.getAttribute("classifier");
780 }
[26446]781
[27705]782 // check for version file
783 String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
[26446]784
[27705]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";
[26446]790
[27705]791 File version_file = new File(version_filename);
[26446]792
[27705]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();
[26446]798
[27705]799 String version_number = "1";
800 BufferedWriter writer;
[26446]801
[27705]802 try
803 {
[26446]804
[27705]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 }
[26446]822
[27705]823 // Write version file
824 String format_statement_filename = "";
[26446]825
[27705]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";
[26446]830
[27705]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();
[26446]836
[27705]837 // Update version number
838 writer = new BufferedWriter(new FileWriter(version_filename));
839 writer.write(version_number);
840 writer.close();
[26446]841
[27705]842 }
843 catch (IOException e)
844 {
845 logger.error("IO Exception " + e);
846 }
847 }
[26446]848
[27705]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();
[26446]855
[27705]856 String collection_config = directory + "collectionConfig.xml";
857 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
[26446]858
[27705]859 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
[26446]860
[27705]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 }
[26446]872
[27705]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 }
[26446]879
[27705]880 current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
[26446]881
[27705]882 String new_config = this.converter.getString(config);
[26446]883
[27705]884 new_config = StringUtils.replace(new_config, "&lt;", "<");
885 new_config = StringUtils.replace(new_config, "&gt;", ">");
886 new_config = StringUtils.replace(new_config, "&quot;", "\"");
[26446]887
[27705]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 }
[26446]900
[27705]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();
[26446]905
[27705]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");
[26446]911
[27705]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;
[26446]918
[27705]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>
[26446]924
[27705]925 current_node = GSXML.getChildByTagName(current_node, "browse");
[26446]926
[27705]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;
[26446]932
[27705]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 }
[26446]977
[27705]978 current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
[26446]979
[27705]980 // Now convert config document to string for writing to file
981 String new_config = this.converter.getString(config);
[26446]982
[27705]983 new_config = StringUtils.replace(new_config, "&lt;", "<");
984 new_config = StringUtils.replace(new_config, "&gt;", ">");
985 new_config = StringUtils.replace(new_config, "&quot;", "\"");
[26446]986
[27705]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();
[26446]991
[27705]992 }
993 catch (Exception ex)
994 {
995 logger.error("There was an exception " + ex);
[26446]996
[27705]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 }
[26446]1004
[27705]1005 }
1006
1007 return response;
1008 }
[30670]1009
1010
[24393]1011}
Note: See TracBrowser for help on using the repository browser.