001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/ogcwebservices/csw/CatalogueService.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044:
045: package org.deegree.ogcwebservices.csw;
046:
047: import java.net.URL;
048: import java.util.HashMap;
049: import java.util.Map;
050: import java.util.Stack;
051:
052: import org.deegree.framework.log.ILogger;
053: import org.deegree.framework.log.LoggerFactory;
054: import org.deegree.framework.trigger.TriggerProvider;
055: import org.deegree.i18n.Messages;
056: import org.deegree.ogcwebservices.MissingParameterValueException;
057: import org.deegree.ogcwebservices.OGCWebService;
058: import org.deegree.ogcwebservices.OGCWebServiceException;
059: import org.deegree.ogcwebservices.OGCWebServiceRequest;
060: import org.deegree.ogcwebservices.csw.capabilities.CatalogueGetCapabilities;
061: import org.deegree.ogcwebservices.csw.capabilities.CatalogueOperationsMetadata;
062: import org.deegree.ogcwebservices.csw.configuration.CatalogueConfiguration;
063: import org.deegree.ogcwebservices.csw.configuration.CatalogueConfigurationDocument;
064: import org.deegree.ogcwebservices.csw.discovery.DescribeRecord;
065: import org.deegree.ogcwebservices.csw.discovery.Discovery;
066: import org.deegree.ogcwebservices.csw.discovery.GetDomain;
067: import org.deegree.ogcwebservices.csw.discovery.GetRecordById;
068: import org.deegree.ogcwebservices.csw.discovery.GetRecords;
069: import org.deegree.ogcwebservices.csw.manager.Harvest;
070: import org.deegree.ogcwebservices.csw.manager.Manager;
071: import org.deegree.ogcwebservices.csw.manager.Transaction;
072: import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
073: import org.deegree.ogcwebservices.wfs.RemoteWFService;
074: import org.deegree.ogcwebservices.wfs.WFService;
075: import org.deegree.ogcwebservices.wfs.WFServiceFactory;
076: import org.deegree.ogcwebservices.wfs.XMLFactory;
077: import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
078: import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument;
079: import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
080: import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
081:
082: /**
083: * The Catalogue Service class provides the foundation for an OGC catalogue service. The Catalogue
084: * Service class directly includes only the serviceTypeID attribute. In most cases, this attribute
085: * will not be directly visible to catalogue clients.
086: * <p>
087: * The catalog service is an implementation of the OpenGIS Catalogue Service Specification 2.0.
088: * </p>
089: *
090: * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </a>
091: * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
092: *
093: * @author last edited by: $Author: apoth $
094: *
095: * @version $Revision: 9384 $, $Date: 2008-01-03 05:30:39 -0800 (Thu, 03 Jan 2008) $
096: * @see <a href="http://www.opengis.org/specs/">OGC Specification </a>
097: */
098:
099: public class CatalogueService implements OGCWebService {
100:
101: private static final ILogger LOG = LoggerFactory
102: .getLogger(CatalogueService.class);
103:
104: private static final TriggerProvider TP = TriggerProvider
105: .create(CatalogueService.class);
106:
107: private Discovery discovery;
108:
109: private WFService wfsService;
110:
111: private CatalogueConfiguration serviceConfiguration;
112:
113: private static Map<URL, OGCWebService> wfsMap = new HashMap<URL, OGCWebService>();
114:
115: private static Stack<Manager> managerPool = new Stack<Manager>();
116:
117: /**
118: * Creates a new <code>CatalogService</code> instance.
119: *
120: * @param config
121: *
122: * @return new <code>CatalogService</code> instance.
123: * @throws OGCWebServiceException
124: */
125: public static final CatalogueService create(
126: CatalogueConfiguration config)
127: throws OGCWebServiceException {
128: // get WFS: local or remote
129: OGCWebService wfsResource = null;
130: try {
131: CatalogueConfigurationDocument document = new CatalogueConfigurationDocument();
132: document.setSystemId(config.getSystemId());
133:
134: URL wfsCapabilitiesFileURL = document.resolve(config
135: .getDeegreeParams().getWfsResource().getHref()
136: .toString());
137: if (wfsMap.get(wfsCapabilitiesFileURL) == null) {
138: if (wfsCapabilitiesFileURL.getProtocol().equals("http")) {
139: WFSCapabilitiesDocument capaDoc = new WFSCapabilitiesDocument();
140: capaDoc.load(wfsCapabilitiesFileURL);
141: WFSCapabilities capabilities = (WFSCapabilities) capaDoc
142: .parseCapabilities();
143: LOG
144: .logInfo("Creating remote WFS with capabilities file "
145: + wfsCapabilitiesFileURL);
146: wfsResource = new RemoteWFService(capabilities);
147: } else {
148: WFSConfigurationDocument capaDoc = new WFSConfigurationDocument();
149: capaDoc.load(wfsCapabilitiesFileURL);
150: WFSConfiguration conf = capaDoc.getConfiguration();
151: LOG
152: .logInfo("CS-W service: Creating local WFS with capabilities file "
153: + wfsCapabilitiesFileURL);
154: wfsResource = WFServiceFactory.createInstance(conf);
155: if (LOG.getLevel() == ILogger.LOG_DEBUG) {
156: LOG
157: .logDebug("CS-W service: The localwfs was has been successfully created, it's capabilties are: "
158: + XMLFactory
159: .export(
160: (WFSCapabilities) wfsResource
161: .getCapabilities())
162: .getAsPrettyString());
163: }
164: }
165: wfsMap.put(wfsCapabilitiesFileURL, wfsResource);
166: } else {
167: wfsResource = wfsMap.get(wfsCapabilitiesFileURL);
168: }
169: } catch (Exception e) {
170: LOG.logError("Error creating WFS for CSW", e);
171: String msg = Messages.get("CSW_ERROR_CREATING_WFS", e
172: .getMessage());
173: throw new OGCWebServiceException(CatalogueService.class
174: .getName(), msg);
175: }
176:
177: // initialize manager and discovery
178: return new CatalogueService(config, (WFService) wfsResource);
179: }
180:
181: /**
182: *
183: * @param config
184: * @param wfsService
185: */
186: private CatalogueService(CatalogueConfiguration config,
187: WFService wfsService) {
188: this .serviceConfiguration = config;
189: this .wfsService = wfsService;
190: this .discovery = new Discovery(wfsService, config);
191: }
192:
193: /**
194: * Returns the OGC-capabilities of the service.
195: *
196: * @return the OGC-capabilities of the service.
197: * @todo analyze incoming request! return only requested sections
198: */
199: public OGCCapabilities getCapabilities() {
200: return this .serviceConfiguration;
201: }
202:
203: /**
204: * Returns the service type (CSW).
205: *
206: * @return the service type (CSW).
207: */
208: public String getServiceTypeId() {
209: return this .serviceConfiguration.getServiceIdentification()
210: .getServiceType().getCode();
211: }
212:
213: /**
214: * @return Version
215: */
216: public String getVersion() {
217: return this .serviceConfiguration.getVersion();
218: }
219:
220: /**
221: * Method for event based request processing.
222: *
223: * @param request
224: * request object containing the request
225: * @return an Object which may be one of the following
226: * <ul>
227: * <li>DescribeRecordResult</li>
228: * <li>GetRecordResult</li>
229: * <li>GetRecordByIdResult</li>
230: * <li>TransactionResult</li>
231: * <li>EchoRequest</li>
232: * <li>OGCCapabilities</li>
233: * @throws OGCWebServiceException
234: *
235: * @todo validation of requested version against accepted versions
236: * @todo return type
237: */
238: public Object doService(OGCWebServiceRequest request)
239: throws OGCWebServiceException {
240:
241: request = (OGCWebServiceRequest) TP.doPreTrigger(this , request)[0];
242:
243: Object response = null;
244:
245: if (request instanceof DescribeRecord) {
246: response = this .getDiscovery().describeRecordType(
247: (DescribeRecord) request);
248: } else if (request instanceof GetDomain) {
249: throw new OGCWebServiceException(getClass().getName(),
250: Messages.get("CSW_GETDOMAIN_NOT_IMPLEMENTED"));
251: // TODO is not implemented
252: // response = this.getDiscovery().getDomain( (GetDomain) request );
253: } else if (request instanceof GetRecords) {
254: response = this .getDiscovery().query((GetRecords) request);
255: } else if (request instanceof GetRecordById) {
256: response = this .getDiscovery().query(
257: (GetRecordById) request);
258: } else if (request instanceof Transaction) {
259: Manager manager = this .getManager(request.getVersion());
260: response = manager.transaction((Transaction) request);
261: managerPool.push(manager);
262: } else if (request instanceof Harvest) {
263: Manager manager = this .getManager(request.getVersion());
264: response = manager.harvestRecords((Harvest) request);
265: managerPool.push(manager);
266: } else if (request instanceof CatalogueGetCapabilities) {
267: LOG.logDebug("GetCapabilities for version:"
268: + request.getVersion(), request);
269: response = this .getCapabilities();
270: } else {
271: throw new OGCWebServiceException(Messages.get(
272: "CSW_INVALID_REQUEST_TYPE", request.getClass()
273: .getName()));
274: }
275:
276: return TP.doPostTrigger(this , response)[0];
277: }
278:
279: /**
280: * @return Returns the discovery.
281: *
282: */
283: public Discovery getDiscovery() {
284: return discovery;
285: }
286:
287: /**
288: * @return the manager.
289: * @throws OGCWebServiceException
290: */
291: public synchronized Manager getManager(String version)
292: throws OGCWebServiceException {
293: CatalogueOperationsMetadata com = (CatalogueOperationsMetadata) serviceConfiguration
294: .getOperationsMetadata();
295: Manager manager = null;
296: if (managerPool.size() == 0) {
297: if (com.getHarvest() != null
298: || com.getTransaction() != null) {
299: try {
300: String className = CSWPropertiesAccess
301: .getString("Manager" + version);
302: if (className == null) {
303: String msg = Messages.get(
304: "CSW_UNSUPPORTED_VERSION", version);
305: throw new OGCWebServiceException(getClass()
306: .getName(), msg);
307: }
308: manager = (Manager) Class.forName(className)
309: .newInstance();
310: manager.init(wfsService, serviceConfiguration);
311: managerPool.push(manager);
312: } catch (MissingParameterValueException e) {
313: LOG.logError(e.getMessage(), e);
314: throw new OGCWebServiceException(getClass()
315: .getName(), e.getMessage());
316: } catch (InstantiationException e) {
317: LOG.logError(e.getMessage(), e);
318: throw new OGCWebServiceException(getClass()
319: .getName(), e.getMessage());
320: } catch (IllegalAccessException e) {
321: LOG.logError(e.getMessage(), e);
322: throw new OGCWebServiceException(getClass()
323: .getName(), e.getMessage());
324: } catch (ClassNotFoundException e) {
325: LOG.logError(e.getMessage(), e);
326: throw new OGCWebServiceException(getClass()
327: .getName(), e.getMessage());
328: }
329: } else {
330: String msg = Messages
331: .get("CSW_TRANSCATIONS_ARE_NOT_DEFINED");
332: throw new OGCWebServiceException(getClass().getName(),
333: msg);
334: }
335: } else {
336: manager = managerPool.pop();
337: }
338: return manager;
339: }
340:
341: }
|