001    // $HeadURL: /cvsroot/deegree/src/org/deegree/ogcwebservices/wms/WMService.java,v
002    // 1.3 2004/07/12 06:12:11 ap Exp $
003    /*----------------    FILE HEADER  ------------------------------------------
004    
005     This file is part of deegree.
006     Copyright (C) 2001-2006 by:
007     EXSE, Department of Geography, University of Bonn
008     http://www.giub.uni-bonn.de/deegree/
009     lat/lon GmbH
010     http://www.lat-lon.de
011    
012     This library is free software; you can redistribute it and/or
013     modify it under the terms of the GNU Lesser General Public
014     License as published by the Free Software Foundation; either
015     version 2.1 of the License, or (at your option) any later version.
016    
017     This library is distributed in the hope that it will be useful,
018     but WITHOUT ANY WARRANTY; without even the implied warranty of
019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
020     Lesser General Public License for more details.
021    
022     You should have received a copy of the GNU Lesser General Public
023     License along with this library; if not, write to the Free Software
024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
025    
026     Contact:
027    
028     Andreas Poth
029     lat/lon GmbH
030     Aennchenstr. 19
031     53115 Bonn
032     Germany
033     E-Mail: poth@lat-lon.de
034    
035     Prof. Dr. Klaus Greve
036     Department of Geography
037     University of Bonn
038     Meckenheimer Allee 166
039     53115 Bonn
040     Germany
041     E-Mail: greve@giub.uni-bonn.de
042    
043     
044     ---------------------------------------------------------------------------*/
045    package org.deegree.ogcwebservices.wms;
046    
047    import java.lang.reflect.Constructor;
048    import java.lang.reflect.InvocationTargetException;
049    
050    import org.deegree.enterprise.Proxy;
051    import org.deegree.framework.log.ILogger;
052    import org.deegree.framework.log.LoggerFactory;
053    import org.deegree.framework.trigger.TriggerProvider;
054    import org.deegree.ogcwebservices.CurrentUpdateSequenceException;
055    import org.deegree.ogcwebservices.InvalidUpdateSequenceException;
056    import org.deegree.ogcwebservices.OGCWebService;
057    import org.deegree.ogcwebservices.OGCWebServiceException;
058    import org.deegree.ogcwebservices.OGCWebServiceRequest;
059    import org.deegree.ogcwebservices.OGCWebServiceResponse;
060    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
061    import org.deegree.ogcwebservices.wms.configuration.WMSConfigurationType;
062    import org.deegree.ogcwebservices.wms.operation.DescribeLayer;
063    import org.deegree.ogcwebservices.wms.operation.GetFeatureInfo;
064    import org.deegree.ogcwebservices.wms.operation.GetLegendGraphic;
065    import org.deegree.ogcwebservices.wms.operation.GetLegendGraphicResult;
066    import org.deegree.ogcwebservices.wms.operation.GetMap;
067    import org.deegree.ogcwebservices.wms.operation.GetStyles;
068    import org.deegree.ogcwebservices.wms.operation.GetStylesResult;
069    import org.deegree.ogcwebservices.wms.operation.PutStyles;
070    import org.deegree.ogcwebservices.wms.operation.WMSGetCapabilities;
071    import org.deegree.ogcwebservices.wms.operation.WMSGetCapabilitiesResult;
072    import org.deegree.ogcwebservices.wms.operation.WMSProtocolFactory;
073    
074    /**
075     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
076     * @author last edited by: $Author: aschmitz $
077     * 
078     * @version 1.0. $Revision: 6891 $, $Date: 2007-05-08 14:02:55 +0200 (Di, 08 Mai 2007) $
079     * 
080     * @since 1.1
081     * 
082     */
083    
084    public class WMService implements OGCWebService {
085    
086        private static final ILogger LOG = LoggerFactory.getLogger( WMService.class );
087        private static final TriggerProvider TP = TriggerProvider.create( WMService.class );
088    
089        private WMSConfigurationType configuration = null;
090    
091        /**
092         * Creates a new WMService object.
093         * 
094         * @param configuration
095         */
096        public WMService( WMSConfigurationType configuration ) {
097            this.configuration = configuration;
098            Proxy proxy = configuration.getDeegreeParams().getProxy();
099            if ( proxy != null ) {
100                proxy.setProxy( true );
101            }
102        }
103    
104        /**
105         * 
106         */
107        public OGCCapabilities getCapabilities() {
108            // should a class implement the WMSConfigurationType and not be an instance
109            // of OGCCapabilities, here's where it'll fail ;-)
110            return (OGCCapabilities) configuration;
111        }
112    
113        /**
114         * the method performs the handling of the passed OGCWebServiceEvent
115         * directly and returns the result to the calling class/method
116         * 
117         * @param request request to perform
118         * 
119         * @throws OGCWebServiceException
120         */
121        public Object doService( OGCWebServiceRequest request )
122                                throws OGCWebServiceException {
123            
124            request = (OGCWebServiceRequest)TP.doPreTrigger( this, request )[0];
125            
126            Object result = null;
127            if ( request instanceof GetMap ) {
128                GetMapHandler gmh = (GetMapHandler) createHandler( request, GetMap.class,
129                                                                   HandlerMapping.getString( "WMService.GETMAP" ) );
130                result = gmh.performGetMap();
131            } else if ( request instanceof GetFeatureInfo ) {
132                GetFeatureInfoHandler gmh = (GetFeatureInfoHandler) createHandler( request,
133                                                                                   GetFeatureInfo.class,
134                                                                                   HandlerMapping.getString( "WMService.GETFEATUREINFO" ) );
135                result = gmh.performGetFeatureInfo();
136            } else if ( request instanceof WMSGetCapabilities ) {
137                result = handleGetCapabilities( (WMSGetCapabilities) request );
138            } else if ( request instanceof GetStyles ) {
139                handleGetStyles( (GetStyles) request );
140            } else if ( request instanceof PutStyles ) {
141                handlePutStyles( (PutStyles) request );
142            } else if ( request instanceof DescribeLayer ) {
143                handleDescribeLayer( (DescribeLayer) request );
144            } else if ( request instanceof GetLegendGraphic ) {
145                result = handleGetLegendGraphic( (GetLegendGraphic) request );
146            }
147    
148            return TP.doPostTrigger( this, result )[0];
149        }
150    
151        /**
152         * creates a handler class for performing the incomming request. The
153         * instance that will be created depends on the responsible class the for
154         * the submitted request in the WMS capabilities/configuration.
155         * 
156         * @param request
157         *            request to be performed
158         * @param requestClass
159         *            of the request (GetStyles, WMSFeatureInfoRequest etc.)
160         * @param operationType
161         *            type of the operation to perform by the handler
162         */
163        private Object createHandler( OGCWebServiceRequest request, Class<?> requestClass, String className )
164                                throws OGCWebServiceException {
165            LOG.entering();
166    
167            // describes the signature of the required constructor
168            Class[] cl = new Class[2];
169            cl[0] = WMSConfigurationType.class;
170            cl[1] = requestClass;
171    
172            // set parameter to submitt to the constructor
173            Object[] o = new Object[2];
174            o[0] = configuration;
175            o[1] = request;
176    
177            Object handler = null;
178    
179            try {
180                // get constructor
181                Class<?> creator = Class.forName( className );
182                Constructor<?> con = creator.getConstructor( cl );
183    
184                // call constructor and instantiate a new DataStore
185                handler = con.newInstance( o );
186            } catch ( ClassNotFoundException cce ) {
187                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n"
188                                                  + cce.toString() );
189            } catch ( NoSuchMethodException nsme ) {
190                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n"
191                                                  + nsme.toString() );
192            } catch ( InstantiationException ie ) {
193                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n"
194                                                  + ie.toString() );
195            } catch ( IllegalAccessException iae ) {
196                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n"
197                                                  + iae.toString() );
198            } catch ( InvocationTargetException ite ) {
199                // Seems that this does not actually work. Thrown exceptions just get lost.
200                throw (OGCWebServiceException) ite.getCause();
201            } catch ( Exception e ) {
202                throw new OGCWebServiceException( "Couldn't instantiate " + className + "! \n"
203                                                  + e.toString() );
204            }
205    
206            LOG.exiting();
207    
208            return handler;
209        }
210    
211        /**
212         * reads/creates the capabilities of the WMS.
213         * 
214         * @param request
215         *            capabilities request
216         */
217        private WMSGetCapabilitiesResult handleGetCapabilities( WMSGetCapabilities request )
218                                throws OGCWebServiceException {
219            LOG.entering();
220    
221            String rUp = request.getUpdateSequence();
222            String cUp = configuration.getUpdateSequence();
223    
224            if ( ( rUp != null ) && ( cUp != null ) && ( rUp.compareTo( cUp ) == 0 ) ) {
225                throw new CurrentUpdateSequenceException( "request update sequence (" + rUp
226                                                          + ") is equal to capabilities"
227                                                          + " update sequence " + cUp );
228            }
229    
230            if ( ( rUp != null ) && ( cUp != null ) && ( rUp.compareTo( cUp ) > 0 ) ) {
231                throw new InvalidUpdateSequenceException( "request update sequence: " + rUp
232                                                          + " is higher than the "
233                                                          + "capabilities update sequence " + cUp );
234            }
235    
236            WMSGetCapabilitiesResult res = null;
237            res = WMSProtocolFactory.createGetCapabilitiesResponse( request, null, configuration );
238    
239            LOG.exiting();
240            return res;
241        }
242    
243        /**
244         * @param request
245         *            get styles request (WMS 1.1.1 - SLD)
246         */
247        private GetStylesResult handleGetStyles( GetStyles request ) {
248            throw new UnsupportedOperationException( "handleGetStyles(GetStyles) not implemented" );
249        }
250    
251        /**
252         * @param request
253         *            put styles request (WMS 1.1.1 - SLD)
254         */
255        private void handlePutStyles( PutStyles request ) {
256            throw new UnsupportedOperationException( "handlePutStyles(GetStyles) not implemented" );
257        }
258    
259        /**
260         * @param request
261         *            describe layer request (WMS 1.1.1 - SLD)
262         */
263        private void handleDescribeLayer( DescribeLayer request ) {
264            throw new UnsupportedOperationException( "handleDescribeLayer(GetStyles) not implemented" );
265        }
266    
267        /**
268         * @param request
269         * 
270         * @return the response
271         * @throws WebServiceException
272         */
273        private OGCWebServiceResponse handleGetLegendGraphic( GetLegendGraphic request )
274                                throws OGCWebServiceException {
275    
276            LOG.entering();
277    
278            OGCWebServiceResponse result = null;
279    
280            if ( result == null ) {
281                GetLegendGraphicHandler glgh = (GetLegendGraphicHandler) createHandler(
282                                                                                        request,
283                                                                                        GetLegendGraphic.class,
284                                                                                        HandlerMapping.getString( "WMService.GETLEGENDGRAPHIC" ) );
285                result = glgh.performGetLegendGraphic();
286                if ( ( (GetLegendGraphicResult) result ).getLegendGraphic() != null ) {
287                    //cache.push(request, ((GetLegendGraphicResult) result).getLegendGraphic());
288                }
289            }
290    
291            LOG.exiting();
292            return result;
293        }
294    
295       
296    
297    }