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