source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/core/OAIMessageRouter.java@ 33968

Last change on this file since 33968 was 33968, checked in by kjdon, 4 years ago

pass in oai_config from server, rather than reading it in itself

File size: 6.0 KB
Line 
1/*
2 * OAIMessageRouter.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.core;
20
21import java.io.File;
22import java.net.Authenticator;
23import java.net.PasswordAuthentication;
24import java.util.HashMap;
25import java.util.Map;
26import java.util.Iterator;
27
28import org.apache.commons.lang3.StringUtils;
29import org.apache.log4j.Logger;
30import org.greenstone.gsdl3.collection.OAICollection;
31import org.greenstone.gsdl3.collection.ServiceCluster;
32import org.greenstone.gsdl3.comms.Communicator;
33import org.greenstone.gsdl3.comms.SOAPCommunicator;
34import org.greenstone.gsdl3.service.ServiceRack;
35import org.greenstone.gsdl3.util.GSFile;
36import org.greenstone.gsdl3.util.GSPath;
37import org.greenstone.gsdl3.util.GSXML;
38import org.greenstone.gsdl3.util.OAIXML;
39import org.greenstone.gsdl3.util.UserContext;
40import org.greenstone.gsdl3.util.XMLConverter;
41import org.greenstone.util.GlobalProperties;
42import org.w3c.dom.Document;
43import org.w3c.dom.Element;
44import org.w3c.dom.Node;
45import org.w3c.dom.NodeList;
46
47/**
48 * The hub of a Greenstone OAI server.
49 *
50 * A simplified version of MessageRouter for OAIServer. Only loads up collections that have OAI services.
51 */
52public class OAIMessageRouter extends MessageRouter
53{
54
55 static Logger logger = Logger.getLogger(org.greenstone.gsdl3.core.MessageRouter.class.getName());
56
57 public Element oai_config = null;
58 //***************************************************************
59 // public methods
60 //***************************************************************
61
62 /** constructor */
63 public OAIMessageRouter()
64 {
65 }
66
67 /** this must be called before calling configure() */
68 public void setOAIConfig(Element config) {
69 this.oai_config = config;
70 }
71
72 /**
73 * read thru own site config file - create services and connect to sites
74 */
75 protected boolean configureLocalSite()
76 {
77
78 // this may be a reconfigure, so clean up the old moduleMap
79 cleanUpModuleMapEntire();
80
81 // for oai, we don't do anything with the site config file. But we'll read it in and keep it in case need it later, eg for replace elements when retrieving metadata - which I don't think has been implemented
82 File configFile = new File(GSFile.siteConfigFile(this.site_home));
83
84 if (!configFile.exists())
85 {
86 logger.error(" site config file: " + configFile.getPath() + " not found!");
87 return false;
88 }
89
90 Document config_doc = XMLConverter.getDOM(configFile);
91 if (config_doc == null)
92 {
93 logger.error(" couldn't parse site config file: " + configFile.getPath());
94 return false;
95 }
96
97 this.config_info = config_doc.getDocumentElement();
98
99 Document doc = XMLConverter.newDOM();
100 // load up the collections
101 this.collection_list = doc.createElement(GSXML.COLLECTION_ELEM + GSXML.LIST_MODIFIER);
102 configureCollections();
103
104 return true;
105
106 }
107
108
109 /**
110 * creates and configures a new collection if this is done for a
111 * reconfigure, the collection should be deactivated first.
112 *
113 * @param col_name
114 * the name of the collection
115 * @return true if collection created ok
116 */
117 protected boolean activateCollectionByName(String col_name)
118 {
119
120 logger.info("Activating collection: " + col_name + ".");
121 Document doc = this.collection_list.getOwnerDocument();
122 // use our special OAICollection - this will only load in oai services
123 OAICollection c = new OAICollection();
124
125 c.setCollectionName(col_name);
126 c.setSiteHome(this.site_home);
127 c.setSiteAddress(this.site_http_address);
128 c.setMessageRouter(this);
129 if (!c.configure()) {
130 logger.error("Couldn't configure collection: " + col_name + ".");
131 return false;
132 }
133
134 logger.info("have just configured collection " + col_name);
135 if (!c.hasOAI()) {
136 logger.info ("collection "+col_name+" has no OAI services. Not keeping it loaded");
137 return false;
138 }
139 if (!c.configureOAI(this.oai_config)) {
140 logger.info("couldn't configure the collection : "+col_name +" with the oai config info");
141 return false;
142 }
143 // add to list of collections
144 this.module_map.put(col_name, c);
145 Element e = doc.createElement(GSXML.COLLECTION_ELEM);
146 e.setAttribute(GSXML.NAME_ATT, col_name);
147 e.setAttribute(OAIXML.LASTMODIFIED, "" + c.getLastmodified());
148 e.setAttribute(OAIXML.EARLIEST_DATESTAMP, "" + c.getEarliestDatestamp());
149 e.setAttribute(OAIXML.EARLIEST_OAI_DATESTAMP, "" + c.getEarliestOAIDatestamp());
150 this.collection_list.appendChild(e);
151 return true;
152
153 }
154
155
156
157 //*****************************************************************
158 // auxiliary process methods
159 //*****************************************************************
160
161 /**
162 * handles requests made to the MessageRouter itself
163 *
164 * @param req
165 * - the request Element- <request>
166 * @return the result Element - should be <response>
167 */
168 protected Element processMessage(Element req) {
169 Document doc = XMLConverter.newDOM();
170 String type = req.getAttribute(GSXML.TYPE_ATT);
171 Element response = doc.createElement(GSXML.RESPONSE_ELEM);
172 response.setAttribute(GSXML.FROM_ATT, "");
173
174 if (type.equals(OAIXML.OAI_SET_LIST))
175 {
176 logger.info("oaiSetList request received");
177 //this is the oai receptionist asking for a list of oai-support collections
178 response.setAttribute(GSXML.TYPE_ATT, OAIXML.OAI_SET_LIST);
179 response.appendChild(doc.importNode(this.collection_list, true));
180 return response;
181 }
182 return super.processMessage(req);
183 }
184
185
186}
Note: See TracBrowser for help on using the repository browser.