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