    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/ogcwebservices/sos/SOService.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.ogcwebservices.sos;
    037
    038    import java.io.IOException;
    039
    040    import javax.xml.transform.TransformerException;
    041
    042    import org.deegree.framework.log.ILogger;
    043    import org.deegree.framework.log.LoggerFactory;
    044    import org.deegree.framework.trigger.TriggerProvider;
    045    import org.deegree.framework.xml.XMLParsingException;
    046    import org.deegree.ogcwebservices.InvalidParameterValueException;
    047    import org.deegree.ogcwebservices.OGCWebService;
    048    import org.deegree.ogcwebservices.OGCWebServiceException;
    049    import org.deegree.ogcwebservices.OGCWebServiceRequest;
    050    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
    051    import org.deegree.ogcwebservices.sos.capabilities.SOSGetCapabilities;
    052    import org.deegree.ogcwebservices.sos.configuration.SOSConfiguration;
    053    import org.deegree.ogcwebservices.sos.configuration.SOSDeegreeParams;
    054    import org.deegree.ogcwebservices.sos.describeplatform.DescribePlatformRequest;
    055    import org.deegree.ogcwebservices.sos.describeplatform.DescribePlatformResult;
    056    import org.deegree.ogcwebservices.sos.describeplatform.PlatformDescriptionDocument;
    057    import org.deegree.ogcwebservices.sos.describeplatform.PlatformMetadata;
    058    import org.deegree.ogcwebservices.sos.describesensor.DescribeSensorRequest;
    059    import org.deegree.ogcwebservices.sos.describesensor.DescribeSensorResult;
    060    import org.deegree.ogcwebservices.sos.describesensor.SensorDescriptionDocument;
    061    import org.deegree.ogcwebservices.sos.describesensor.SensorMetadata;
    062    import org.deegree.ogcwebservices.sos.getobservation.GetObservationDocument;
    063    import org.deegree.ogcwebservices.sos.getobservation.GetObservationRequest;
    064    import org.deegree.ogcwebservices.sos.getobservation.GetObservationResult;
    065    import org.deegree.ogcwebservices.sos.om.ObservationArray;
    066    import org.xml.sax.SAXException;
    067
    068    /**
    069     * @author <a href="mailto:mkulbe@lat-lon.de">Matthias Kulbe </a>
    070     * @author last edited by: $Author: mschneider $
    071     *
    072     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    073     */
    074    public class SOService implements OGCWebService {
    075
    076        private SOSConfiguration serviceConfiguration;
    077
    078        private static final ILogger LOG = LoggerFactory.getLogger( SOService.class );
    079
    080        private static final TriggerProvider TP = TriggerProvider.create( SOService.class );
    081
    082        /**
    083         * static create method to create a new instance of the SCService
    084         *
    085         * @param scsConfiguration
    086         * @return the new instance
    087         *
    088         */
    089        public static SOService create( SOSConfiguration scsConfiguration ) {
    090            return new SOService( scsConfiguration );
    091
    092        }
    093
    094        /**
    095         * privater constructor to construct a new SCService instance with the configuration as the only
    096         * parameter
    097         *
    098         * @param scsConfiguration
    099         *
    100         */
    101        private SOService( SOSConfiguration scsConfiguration ) {
    102            this.serviceConfiguration = scsConfiguration;
    103        }
    104
    105        /**
    106         * returns the ServiceTypeId from the ServiceIdentification section of the configuration
    107         *
    108         * @return the ServiceTypeId from the ServiceIdentification section of the configuration
    109         */
    110        public String getServiceTypeId() {
    111            return this.serviceConfiguration.getServiceIdentification().getServiceType().getCode();
    112
    113        }
    114
    115        /**
    116         * returns the Version from the configuration
    117         *
    118         * @return the Version from the configuration
    119         */
    120        public String getVersion() {
    121            return serviceConfiguration.getVersion();
    122        }
    123
    124        /**
    125         * returns the serviceConfiguration
    126         *
    127         * @return the serviceConfiguration
    128         */
    129        public OGCCapabilities getCapabilities() {
    130            return this.serviceConfiguration;
    131        }
    132
    133        /**
    134         * checks the request and do service
    135         *
    136         * @throws OGCWebServiceException
    137         */
    138        public Object doService( OGCWebServiceRequest request )
    139                                throws OGCWebServiceException {
    140
    141            request = (OGCWebServiceRequest) TP.doPreTrigger( this, request )[0];
    142
    143            Object response = null;
    144            try {
    145                if ( request instanceof SOSGetCapabilities ) {
    146                    LOG.logDebug( "-> GetCapabilities received" );
    147                    response = this.getCapabilities();
    148                } else if ( request instanceof DescribePlatformRequest ) {
    149                    LOG.logDebug( "-> DescribePlatform received" );
    150                    response = this.describePlatform( (DescribePlatformRequest) request );
    151                } else if ( request instanceof DescribeSensorRequest ) {
    152                    LOG.logDebug( "-> DescribeSensor received" );
    153                    response = this.describeSensor( (DescribeSensorRequest) request );
    154                } else if ( request instanceof GetObservationRequest ) {
    155                    LOG.logDebug( "-> GetObservation received" );
    156                    response = this.getObservation( (GetObservationRequest) request );
    157                } else {
    158                    throw new InvalidParameterValueException( "not a valid scs request" );
    159                }
    160            } catch ( Exception e ) {
    161                LOG.logError( e.getMessage(), e );
    162                throw new OGCWebServiceException( "SOS Failure\n" + e.getMessage() );
    163            }
    164
    165            return TP.doPostTrigger( this, response )[0];
    166        }
    167
    168        /**
    169         * returns a DescribePlatformResult
    170         *
    171         * @param request
    172         * @return a DescribePlatformResult
    173         * @throws InvalidParameterValueException
    174         * @throws OGCWebServiceException
    175         */
    176        private DescribePlatformResult describePlatform( DescribePlatformRequest request )
    177                                throws InvalidParameterValueException, OGCWebServiceException {
    178
    179            // validate the requested platforms
    180            String[] types = request.getTypeNames();
    181
    182            for ( int i = 0; i < types.length; i++ ) {
    183                if ( serviceConfiguration.getSOSDeegreeParams().getPlatformConfiguration( types[i] ) == null ) {
    184                    throw new OGCWebServiceException( "InvalidParameterValueException: Platform '" + types[i]
    185                                                      + "' not found" );
    186                }
    187            }
    188
    189            PlatformDescriptionDocument pdd = new PlatformDescriptionDocument();
    190            PlatformMetadata[] pm = pdd.getPlatform( serviceConfiguration.getSOSDeegreeParams(), types );
    191
    192            return new DescribePlatformResult( request, pm );
    193        }
    194
    195        /**
    196         * returns a DescribeSensorResult
    197         *
    198         * @param request
    199         * @return a DescribeSensorResult
    200         * @throws InvalidParameterValueException
    201         * @throws OGCWebServiceException
    202         */
    203        private DescribeSensorResult describeSensor( DescribeSensorRequest request )
    204                                throws InvalidParameterValueException, OGCWebServiceException {
    205
    206            // validate the requested sensors
    207            String[] types = request.getTypeNames();
    208
    209            for ( int i = 0; i < types.length; i++ ) {
    210                if ( serviceConfiguration.getSOSDeegreeParams().getSensorConfiguration( types[i] ) == null ) {
    211                    throw new OGCWebServiceException( "InvalidParameterValueException: Sensor '" + types[i] + "' not found" );
    212                }
    213            }
    214
    215            SensorDescriptionDocument sdd = new SensorDescriptionDocument();
    216            SensorMetadata[] sensors = sdd.getSensor( serviceConfiguration.getSOSDeegreeParams(), request.getTypeNames() );
    217            return new DescribeSensorResult( request, sensors );
    218
    219        }
    220
    221        /**
    222         * returns a GetObservationResult
    223         *
    224         * @param request
    225         * @return a GetObservationResult
    226         * @throws OGCWebServiceException
    227         * @throws XMLParsingException
    228         * @throws IOException
    229         * @throws SAXException
    230         * @throws TransformerException
    231         */
    232        private GetObservationResult getObservation( GetObservationRequest request )
    233                                throws OGCWebServiceException, XMLParsingException, TransformerException, IOException,
    234                                SAXException {
    235
    236            SOSDeegreeParams dp = serviceConfiguration.getSOSDeegreeParams();
    237
    238            // validate the requested platforms
    239            String[] platforms = request.getPlatforms();
    240            for ( int i = 0; i < platforms.length; i++ ) {
    241                if ( dp.getPlatformConfiguration( platforms[i] ) == null ) {
    242                    throw new OGCWebServiceException( "InvalidParameterValueException: Platform '" + platforms[i]
    243                                                      + "' not found" );
    244                }
    245            }
    246
    247            // validate the requested sensors
    248            String[] sensors = request.getSensors();
    249            for ( int i = 0; i < sensors.length; i++ ) {
    250                if ( dp.getSensorConfiguration( sensors[i] ) == null ) {
    251                    throw new OGCWebServiceException( "InvalidParameterValueException: Sensor '" + sensors[i]
    252                                                      + "' not found" );
    253                }
    254            }
    255
    256            GetObservationDocument god = new GetObservationDocument();
    257            ObservationArray[] obAr = god.getObservations( serviceConfiguration.getSOSDeegreeParams(), request );
    258            return new GetObservationResult( request, obAr );
    259        }
    260    }