001    // $HeadURL: /cvsroot/deegree/src/org/deegree/ogcwebservices/wms/WMPService.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.wmps;
038    
039    import java.lang.reflect.Constructor;
040    
041    import org.deegree.enterprise.Proxy;
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.i18n.Messages;
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.getcapabilities.OGCCapabilities;
052    import org.deegree.ogcwebservices.wmps.configuration.WMPSConfiguration;
053    import org.deegree.ogcwebservices.wmps.configuration.WMPSDeegreeParams;
054    import org.deegree.ogcwebservices.wmps.operation.PrintMap;
055    import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilities;
056    import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilitiesResult;
057    import org.deegree.ogcwebservices.wmps.operation.WMPSProtocolFactory;
058    
059    /**
060     * Handles saving the PrintMap request to the databank and generating the initial response to be
061     * sent to the user.
062     *
063     * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a>
064     * @version 2.0
065     */
066    public class WMPService implements OGCWebService {
067    
068        private static final ILogger LOG = LoggerFactory.getLogger( WMPService.class );
069    
070        private static final TriggerProvider TP = TriggerProvider.create( WMPService.class );
071    
072        private PrintMapHandler printMapHandler;
073    
074        private WMPSConfiguration configuration;
075    
076        /**
077         * Creates a new WMPService object.
078         *
079         * @param configuration
080         */
081        public WMPService( WMPSConfiguration configuration ) {
082            this.configuration = configuration;
083            this.printMapHandler = new PrintMapHandler( configuration );
084            Proxy proxy = this.configuration.getDeegreeParams().getProxy();
085            if ( proxy != null ) {
086                proxy.setProxy( true );
087            }
088        }
089    
090        /**
091         * Return the OGCCapabilities.
092         *
093         * @return OGCCapabilities
094         */
095        public OGCCapabilities getCapabilities() {
096            return this.configuration;
097        }
098    
099        /**
100         * the method performs the handling of the passed OGCWebServiceEvent directly and returns the
101         * result to the calling class/method
102         *
103         * @param request
104         *            request to perform
105         * @return Object
106         * @throws OGCWebServiceException
107         */
108        public Object doService( OGCWebServiceRequest request )
109                                throws OGCWebServiceException {
110    
111            request = (OGCWebServiceRequest) TP.doPreTrigger( this, request )[0];
112    
113            Object result = null;
114            if ( request instanceof PrintMap ) {
115    
116                String template = ( (PrintMap) request ).getTemplate();
117                WMPSDeegreeParams params = this.configuration.getDeegreeParams();
118                boolean isSynchronous = params.getSynchronousTemplates().contains( template );
119    
120                String handler = HandlerMapping.getString( "WMPService.PRINTMAP" );
121                RequestManager rqManager = (RequestManager) createHandler( request, PrintMap.class, handler );
122    
123                try {
124                    rqManager.saveRequestToDB();
125    
126                    result = rqManager.createInitialResponse( Messages.getMessage( "WMPS_INIT_RESPONSE" ) );
127                } catch ( OGCWebServiceException e ) {
128    
129                    throw new OGCWebServiceException( "Error saving the PrintMap request " + "to the DB. " + e.getMessage() );
130                }
131    
132                try {
133                    if ( isSynchronous ) {
134                        result = this.printMapHandler.runSynchronous( (PrintMap) request );
135                    } else {
136                        Thread printMap = new Thread( this.printMapHandler );
137                        printMap.start();
138                    }
139                } catch ( Exception e ) {
140                    LOG.logError( e.getMessage(), e );
141                    throw new OGCWebServiceException( "Error performing the PrintMap request. " + e.getMessage() );
142                }
143            } else if ( request instanceof WMPSGetCapabilities ) {
144                result = handleGetCapabilities( (WMPSGetCapabilities) request );
145            }
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
152         * created depends on the responsible class the for the submitted request in the WMPS
153         * capabilities/configuration.
154         *
155         * @param request
156         *            request to be performed
157         * @param requestClass
158         *            of the request (GetStyles, WMSFeatureInfoRequest etc.)
159         * @param className
160         *            type of the operation to perform by the handler
161         * @return Object
162         * @throws OGCWebServiceException
163         */
164        private Object createHandler( OGCWebServiceRequest request, Class<?> requestClass, String className )
165                                throws OGCWebServiceException {
166    
167            // describes the signature of the required constructor
168            Class<?>[] cl = new Class[2];
169            cl[0] = WMPSConfiguration.class;
170            cl[1] = requestClass;
171    
172            // set parameter to submitt to the constructor
173            Object[] o = new Object[2];
174            o[0] = this.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                // call constructor and instantiate a new DataStore
184                handler = con.newInstance( o );
185            } catch ( Exception e ) {
186                LOG.logError( e.getMessage(), e );
187                throw new OGCWebServiceException( "Couldn't instantiate " + className + '!' );
188            }
189    
190            return handler;
191        }
192    
193        /**
194         * reads/creates the capabilities of the WMPS.
195         *
196         * @param request
197         *            capabilities request
198         * @return WMPSGetCapabilitiesResult
199         * @throws CurrentUpdateSequenceException
200         * @throws InvalidUpdateSequenceException
201         */
202        private WMPSGetCapabilitiesResult handleGetCapabilities( WMPSGetCapabilities request )
203                                throws CurrentUpdateSequenceException, InvalidUpdateSequenceException {
204    
205            String rUp = request.getUpdateSequence();
206            String cUp = this.configuration.getUpdateSequence();
207    
208            if ( ( rUp != null ) && ( cUp != null ) && ( rUp.compareTo( cUp ) == 0 ) ) {
209                throw new CurrentUpdateSequenceException( "request update sequence: " + rUp + "is equal to capabilities"
210                                                          + " update sequence " + cUp );
211            }
212    
213            if ( ( rUp != null ) && ( cUp != null ) && ( rUp.compareTo( cUp ) > 0 ) ) {
214                throw new InvalidUpdateSequenceException( "request update sequence: " + rUp + " is higher then the "
215                                                          + "capabilities update sequence " + cUp );
216            }
217            return WMPSProtocolFactory.createGetCapabilitiesResult( request, null, this.configuration );
218        }
219    
220    }