001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/sos/SOService.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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     Aennchenstraße 19  
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     lat/lon GmbH
036     Aennchenstraße 19
037     53177 Bonn
038     Germany
039     E-Mail: fitzke@lat-lon.de
040    
041     ---------------------------------------------------------------------------*/
042    package org.deegree.ogcwebservices.sos;
043    
044    import java.io.IOException;
045    
046    import javax.xml.transform.TransformerException;
047    
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.framework.log.LoggerFactory;
050    import org.deegree.framework.trigger.TriggerProvider;
051    import org.deegree.framework.xml.XMLParsingException;
052    import org.deegree.ogcwebservices.InvalidParameterValueException;
053    import org.deegree.ogcwebservices.OGCWebService;
054    import org.deegree.ogcwebservices.OGCWebServiceException;
055    import org.deegree.ogcwebservices.OGCWebServiceRequest;
056    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
057    import org.deegree.ogcwebservices.sos.capabilities.SOSGetCapabilities;
058    import org.deegree.ogcwebservices.sos.configuration.SOSConfiguration;
059    import org.deegree.ogcwebservices.sos.configuration.SOSDeegreeParams;
060    import org.deegree.ogcwebservices.sos.describeplatform.DescribePlatformRequest;
061    import org.deegree.ogcwebservices.sos.describeplatform.DescribePlatformResult;
062    import org.deegree.ogcwebservices.sos.describeplatform.PlatformDescriptionDocument;
063    import org.deegree.ogcwebservices.sos.describeplatform.PlatformMetadata;
064    import org.deegree.ogcwebservices.sos.describesensor.DescribeSensorRequest;
065    import org.deegree.ogcwebservices.sos.describesensor.DescribeSensorResult;
066    import org.deegree.ogcwebservices.sos.describesensor.SensorDescriptionDocument;
067    import org.deegree.ogcwebservices.sos.describesensor.SensorMetadata;
068    import org.deegree.ogcwebservices.sos.getobservation.GetObservationDocument;
069    import org.deegree.ogcwebservices.sos.getobservation.GetObservationRequest;
070    import org.deegree.ogcwebservices.sos.getobservation.GetObservationResult;
071    import org.deegree.ogcwebservices.sos.om.ObservationArray;
072    import org.xml.sax.SAXException;
073    
074    /**
075     * @author <a href="mailto:mkulbe@lat-lon.de">Matthias Kulbe </a>
076     * @author last edited by: $Author: bezema $
077     * 
078     * @version $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
079     */
080    public class SOService implements OGCWebService {
081    
082        private SOSConfiguration serviceConfiguration;
083    
084        private static final ILogger LOG = LoggerFactory.getLogger( SOService.class );
085    
086        private static final TriggerProvider TP = TriggerProvider.create( SOService.class );
087    
088        /**
089         * static create method to create a new instance of the SCService
090         * 
091         * @param scsConfiguration
092         * 
093         */
094        public static SOService create( SOSConfiguration scsConfiguration ) {
095            return new SOService( scsConfiguration );
096    
097        }
098    
099        /**
100         * privater constructor to construct a new SCService instance with the configuration as the only
101         * parameter
102         * 
103         * @param scsConfiguration
104         * 
105         */
106        private SOService( SOSConfiguration scsConfiguration ) {
107            this.serviceConfiguration = scsConfiguration;
108        }
109    
110        /**
111         * returns the ServiceTypeId from the ServiceIdentification section of the configuration
112         * 
113         * @return the ServiceTypeId from the ServiceIdentification section of the configuration
114         */
115        public String getServiceTypeId() {
116            return this.serviceConfiguration.getServiceIdentification().getServiceType().getCode();
117    
118        }
119    
120        /**
121         * returns the Version from the configuration
122         * 
123         * @return the Version from the configuration
124         */
125        public String getVersion() {
126            return serviceConfiguration.getVersion();
127        }
128    
129        /**
130         * returns the serviceConfiguration
131         * 
132         * @return the serviceConfiguration
133         */
134        public OGCCapabilities getCapabilities() {
135            return this.serviceConfiguration;
136        }
137    
138        /**
139         * checks the request and do service
140         * 
141         * @throws OGCWebServiceException
142         */
143        public Object doService( OGCWebServiceRequest request )
144                                throws OGCWebServiceException {
145    
146            request = (OGCWebServiceRequest) TP.doPreTrigger( this, request )[0];
147    
148            Object response = null;
149            try {
150                if ( request instanceof SOSGetCapabilities ) {
151                    LOG.logDebug( "-> GetCapabilities received" );
152                    response = this.getCapabilities();
153                } else if ( request instanceof DescribePlatformRequest ) {
154                    LOG.logDebug( "-> DescribePlatform received" );
155                    response = this.describePlatform( (DescribePlatformRequest) request );
156                } else if ( request instanceof DescribeSensorRequest ) {
157                    LOG.logDebug( "-> DescribeSensor received" );
158                    response = this.describeSensor( (DescribeSensorRequest) request );
159                } else if ( request instanceof GetObservationRequest ) {
160                    LOG.logDebug( "-> GetObservation received" );
161                    response = this.getObservation( (GetObservationRequest) request );
162                } else {
163                    throw new InvalidParameterValueException( "not a valid scs request" );
164                }
165            } catch ( Exception e ) {
166                LOG.logError( e.getMessage(), e );
167                throw new OGCWebServiceException( "SOS Failure\n" + e.getMessage() );
168            }
169    
170            return TP.doPostTrigger( this, response )[0];
171        }
172    
173        /**
174         * returns a DescribePlatformResult
175         * 
176         * @param request
177         * @return a DescribePlatformResult
178         * @throws InvalidParameterValueException
179         * @throws OGCWebServiceException
180         */
181        private DescribePlatformResult describePlatform( DescribePlatformRequest request )
182                                throws InvalidParameterValueException, OGCWebServiceException {
183    
184            // validate the requested platforms
185            String[] types = request.getTypeNames();
186    
187            for ( int i = 0; i < types.length; i++ ) {
188                if ( serviceConfiguration.getSOSDeegreeParams().getPlatformConfiguration( types[i] ) == null ) {
189                    throw new OGCWebServiceException( "InvalidParameterValueException: Platform '"
190                                                      + types[i] + "' not found" );
191                }
192            }
193    
194            PlatformDescriptionDocument pdd = new PlatformDescriptionDocument();
195            PlatformMetadata[] pm = pdd.getPlatform( serviceConfiguration.getSOSDeegreeParams(), types );
196    
197            return new DescribePlatformResult( request, pm );
198        }
199    
200        /**
201         * returns a DescribeSensorResult
202         * 
203         * @param request
204         * @return a DescribeSensorResult
205         * @throws InvalidParameterValueException
206         * @throws OGCWebServiceException
207         */
208        private DescribeSensorResult describeSensor( DescribeSensorRequest request )
209                                throws InvalidParameterValueException, OGCWebServiceException {
210    
211            // validate the requested sensors
212            String[] types = request.getTypeNames();
213    
214            for ( int i = 0; i < types.length; i++ ) {
215                if ( serviceConfiguration.getSOSDeegreeParams().getSensorConfiguration( types[i] ) == null ) {
216                    throw new OGCWebServiceException( "InvalidParameterValueException: Sensor '"
217                                                      + types[i] + "' not found" );
218                }
219            }
220    
221            SensorDescriptionDocument sdd = new SensorDescriptionDocument();
222            SensorMetadata[] sensors = sdd.getSensor( serviceConfiguration.getSOSDeegreeParams(),
223                                                      request.getTypeNames() );
224            return new DescribeSensorResult( request, sensors );
225    
226        }
227    
228        /**
229         * returns a GetObservationResult
230         * 
231         * @param request
232         * @return a GetObservationResult
233         * @throws OGCWebServiceException
234         * @throws XMLParsingException
235         * @throws IOException
236         * @throws SAXException
237         * @throws TransformerException
238         */
239        private GetObservationResult getObservation( GetObservationRequest request )
240                                throws OGCWebServiceException, XMLParsingException,
241                                TransformerException, IOException, SAXException {
242    
243            SOSDeegreeParams dp = serviceConfiguration.getSOSDeegreeParams();
244    
245            // validate the requested platforms
246            String[] platforms = request.getPlatforms();
247            for ( int i = 0; i < platforms.length; i++ ) {
248                if ( dp.getPlatformConfiguration( platforms[i] ) == null ) {
249                    throw new OGCWebServiceException( "InvalidParameterValueException: Platform '"
250                                                      + platforms[i] + "' not found" );
251                }
252            }
253    
254            // validate the requested sensors
255            String[] sensors = request.getSensors();
256            for ( int i = 0; i < sensors.length; i++ ) {
257                if ( dp.getSensorConfiguration( sensors[i] ) == null ) {
258                    throw new OGCWebServiceException( "InvalidParameterValueException: Sensor '"
259                                                      + sensors[i] + "' not found" );
260                }
261            }
262    
263            GetObservationDocument god = new GetObservationDocument();
264            ObservationArray[] obAr = god.getObservations( serviceConfiguration.getSOSDeegreeParams(),
265                                                           request );
266            return new GetObservationResult( request, obAr );
267        }
268    }
269    /***************************************************************************************************
270     * <code>
271     Changes to this class. What the people have been up to:
272     
273     $Log$
274     Revision 1.17  2007/02/12 11:52:05  wanhoff
275     fixed Javadoc @return tag and footer
276    
277     Revision 1.16  2006/10/18 17:00:56  poth
278     made DefaultOGCWebServiceResponse base type for all webservice responses
279    
280     Revision 1.15  2006/10/01 11:15:43  poth
281     trigger points for doService methods defined
282    
283     Revision 1.14  2006/08/24 06:42:16  poth
284     File header corrected
285    
286     Revision 1.13  2006/07/12 14:46:18  poth
287     comment footer added
288    
289     </code>
290     **************************************************************************************************/