    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/ogcwebservices/wfs/GetFeatureHandler.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.ogcwebservices.wfs;
    037
    038    import static org.deegree.framework.util.TimeTools.getISOFormattedTime;
    039    import static org.deegree.ogcbase.ExceptionCode.INVALIDPARAMETERVALUE;
    040
    041    import java.util.ArrayList;
    042    import java.util.List;
    043    import java.util.concurrent.Callable;
    044    import java.util.concurrent.CancellationException;
    045
    046    import org.deegree.datatypes.QualifiedName;
    047    import org.deegree.framework.concurrent.ExecutionFinishedEvent;
    048    import org.deegree.framework.concurrent.Executor;
    049    import org.deegree.framework.log.ILogger;
    050    import org.deegree.framework.log.LoggerFactory;
    051    import org.deegree.i18n.Messages;
    052    import org.deegree.io.datastore.Datastore;
    053    import org.deegree.io.datastore.PropertyPathResolvingException;
    054    import org.deegree.io.datastore.schema.MappedFeatureType;
    055    import org.deegree.model.feature.FeatureCollection;
    056    import org.deegree.model.feature.GMLFeatureAdapter;
    057    import org.deegree.model.feature.GMLFeatureCollectionDocument;
    058    import org.deegree.ogcwebservices.OGCWebServiceException;
    059    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
    060    import org.deegree.ogcwebservices.wfs.capabilities.WFSFeatureType;
    061    import org.deegree.ogcwebservices.wfs.capabilities.WFSOperationsMetadata;
    062    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
    063    import org.deegree.ogcwebservices.wfs.operation.FeatureResult;
    064    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
    065    import org.deegree.ogcwebservices.wfs.operation.Query;
    066    import org.deegree.ogcwebservices.wfs.operation.GetFeature.RESULT_TYPE;
    067    import org.deegree.owscommon.OWSDomainType;
    068
    069    /**
    070     * Handles {@link GetFeature} requests to the {@link WFService}. Since a {@link GetFeature} request may contain more
    071     * than one {@link Query}, each {@link Query} is delegated to an own thread.
    072     * <p>
    073     * The results of all threads are collected and merged before they are returned to the calling {@link WFService} as a
    074     * single {@link FeatureCollection}.
    075     *
    076     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    077     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
    078     * @author last edited by: $Author: mschneider $
    079     *
    080     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    081     */
    082    class GetFeatureHandler {
    083
    084        private static final ILogger LOG = LoggerFactory.getLogger( GetFeatureHandler.class );
    085
    086        // upper limit for timeout (overrides WFS configuration)
    087        private static long MAX_TIMEOUT_MILLIS = 60 * 60 * 1000;
    088
    089        private WFService wfs;
    090
    091        private int maxFeatures = -1;
    092
    093        /**
    094         * Creates a new instance of <code>GetFeatureHandler</code>. Only called by the associated {@link WFService} (once).
    095         *
    096         * @param wfs
    097         *            associated WFService
    098         */
    099        GetFeatureHandler( WFService wfs ) {
    100            this.wfs = wfs;
    101            WFSCapabilities capa = wfs.getCapabilities();
    102            WFSOperationsMetadata md = (WFSOperationsMetadata) capa.getOperationsMetadata();
    103            OWSDomainType[] dt = md.getConstraints();
    104            for ( int i = 0; i < dt.length; i++ ) {
    105                if ( dt[i].getName().equals( "DefaultMaxFeatures" ) ) {
    106                    try {
    107                        String tmp = dt[i].getValues()[0];
    108                        this.maxFeatures = Integer.parseInt( tmp );
    109                    } catch ( Exception e ) {
    110                        // e.printStackTrace();
    111                    }
    112                    break;
    113                }
    114            }
    115            LOG.logDebug( "default maxFeatures " + this.maxFeatures );
    116        }
    117
    118        /**
    119         * Handles a {@link GetFeature} request by delegating the contained {@link Query} objects to different threads.
    120         * <p>
    121         * If at least one query fails an exception will be thrown and all running threads will be stopped.
    122         *
    123         * @param getFeature
    124         * @return result of the request
    125         * @throws OGCWebServiceException
    126         */
    127        FeatureResult handleRequest( GetFeature getFeature )
    128                                throws OGCWebServiceException {
    129
    130            WFSConfiguration conf = (WFSConfiguration) wfs.getCapabilities();
    131
    132            if ( getFeature.getMaxFeatures() > this.maxFeatures || getFeature.getMaxFeatures() <= 0 ) {
    133                getFeature.setMaxFeatures( this.maxFeatures );
    134            }
    135
    136            LOG.logDebug( "maxFeatures " + getFeature.getMaxFeatures() );
    137
    138            Query[] queries = getFeature.getQuery();
    139            List<Callable<FeatureCollection>> queryTasks = new ArrayList<Callable<FeatureCollection>>( queries.length );
    140
    141            for ( Query query : queries ) {
    142
    143                if ( conf.getDeegreeParams().checkUTMZones() ) {
    144                    query.performBBoxTest();
    145                }
    146
    147                query.deleteBBoxTest();
    148
    149                QualifiedName[] ftNames = query.getTypeNames();
    150                MappedFeatureType[] requestedFts = new MappedFeatureType[ftNames.length];
    151                Datastore ds = null;
    152
    153                for ( int i = 0; i < ftNames.length; i++ ) {
    154                    QualifiedName ftName = ftNames[i];
    155                    MappedFeatureType ft = this.wfs.getMappedFeatureType( ftName );
    156
    157                    if ( ft == null ) {
    158                        String msg = Messages.getMessage( "WFS_FEATURE_TYPE_UNKNOWN", ftName );
    159                        throw new OGCWebServiceException( this.getClass().getName(), msg );
    160                    }
    161                    if ( ft.isAbstract() ) {
    162                        String msg = Messages.getMessage( "WFS_FEATURE_TYPE_ABSTRACT", ftName );
    163                        throw new OGCWebServiceException( this.getClass().getName(), msg );
    164                    }
    165                    if ( !ft.isVisible() ) {
    166                        String msg = Messages.getMessage( "WFS_FEATURE_TYPE_INVISIBLE", ftName );
    167                        throw new OGCWebServiceException( this.getClass().getName(), msg );
    168                    }
    169                    Datastore dsForFt = ft.getGMLSchema().getDatastore();
    170                    if ( ds != null ) {
    171                        if ( ds != dsForFt ) {
    172                            String msg = Messages.getMessage( "WFS_QUERY_JOIN_OVER_DIFFERENT_DS" );
    173                            throw new OGCWebServiceException( this.getClass().getName(), msg );
    174                        }
    175                    } else {
    176                        ds = dsForFt;
    177                    }
    178                    requestedFts[i] = ft;
    179                }
    180
    181                // TODO what about joins here?
    182                String srsName = query.getSrsName();
    183                if ( srsName != null ) {
    184                    WFSFeatureType wfsFT = this.wfs.getCapabilities().getFeatureTypeList().getFeatureType( ftNames[0] );
    185
    186                    if ( !( wfsFT.supportsSrs( srsName ) ) ) {
    187                        String msg = Messages.getMessage( "WFS_FEATURE_TYPE_SRS_UNSUPPORTED", ftNames[0], srsName );
    188                        throw new OGCWebServiceException( this.getClass().getName(), msg );
    189                    }
    190                }
    191
    192                QueryTask task = new QueryTask( ds, query, requestedFts );
    193                queryTasks.add( task );
    194            }
    195
    196            long timeout = conf.getDeegreeParams().getRequestTimeLimit() * 1000;
    197            if ( timeout > MAX_TIMEOUT_MILLIS ) {
    198                // limit max timeout
    199                timeout = MAX_TIMEOUT_MILLIS;
    200            }
    201
    202            List<ExecutionFinishedEvent<FeatureCollection>> finishedEvents = null;
    203            try {
    204                finishedEvents = Executor.getInstance().performSynchronously( queryTasks, timeout );
    205            } catch ( InterruptedException e ) {
    206                String msg = "Exception occured while waiting for the GetFeature results: " + e.getMessage();
    207                throw new OGCWebServiceException( this.getClass().getName(), msg );
    208            }
    209
    210            // use id of the request as id of the result feature collection
    211            // to allow identification of the original request that produced
    212            // the feature collection
    213            FeatureCollection fc = null;
    214            if ( getFeature.getResultType() == RESULT_TYPE.RESULTS ) {
    215                fc = mergeResults( getFeature.getId(), finishedEvents );
    216            } else {
    217                fc = mergeHits( getFeature.getId(), finishedEvents );
    218            }
    219
    220            // TODO this is not a good solution
    221            // I think it can happen if more than one feature type is requested
    222            while ( getFeature.getMaxFeatures() > 0 && fc.size() > getFeature.getMaxFeatures() ) {
    223                fc.remove( fc.size() - 1 );
    224            }
    225
    226            if ( LOG.isDebug() ) {
    227                try {
    228                    GMLFeatureAdapter ada = new GMLFeatureAdapter( false );
    229                    GMLFeatureCollectionDocument doc = ada.export( fc );
    230                    LOG.logDebugXMLFile( "GetFeatureHandler_result", doc );
    231                } catch ( Exception e ) {
    232                    LOG.logError( e.getMessage(), e );
    233                }
    234            }
    235
    236            FeatureResult fr = new FeatureResult( getFeature, fc );
    237            return fr;
    238        }
    239
    240        /**
    241         * Merges the results of the request subparts into one feature collection.
    242         *
    243         * @param fcid
    244         *            id of the new (result) feature collection
    245         * @param finishedEvents
    246         * @return feature collection containing all features from all responses
    247         * @throws OGCWebServiceException
    248         */
    249        private FeatureCollection mergeResults( String fcid, List<ExecutionFinishedEvent<FeatureCollection>> finishedEvents )
    250                                throws OGCWebServiceException {
    251
    252            FeatureCollection result = null;
    253
    254            try {
    255                for ( ExecutionFinishedEvent<FeatureCollection> event : finishedEvents ) {
    256                    if ( result == null ) {
    257                        result = event.getResult();
    258                    } else {
    259                        result.addAllUncontained( event.getResult() );
    260                    }
    261                }
    262
    263                if ( result == null ) {
    264                    return result;
    265                }
    266            } catch ( CancellationException e ) {
    267                LOG.logError( e.getMessage(), e );
    268                String msg = Messages.getMessage( "WFS_GET_FEATURE_TIMEOUT", e.getMessage() );
    269                throw new OGCWebServiceException( this.getClass().getName(), msg );
    270            } catch ( PropertyPathResolvingException e ) {
    271                LOG.logDebug( "Stack trace", e );
    272                throw new OGCWebServiceException( e.getLocalizedMessage(), INVALIDPARAMETERVALUE );
    273            } catch ( Throwable t ) {
    274                LOG.logError( t.getMessage(), t );
    275                String msg = Messages.getMessage( "WFS_GET_FEATURE_BACKEND", t.getMessage() );
    276                throw new OGCWebServiceException( this.getClass().getName(), msg );
    277            }
    278
    279            result.setId( fcid );
    280            result.setAttribute( "numberOfFeatures", "" + result.size() );
    281            return result;
    282        }
    283
    284        /**
    285         * Merges the results of the request subparts into one feature collection.
    286         * <p>
    287         * This method is used if only the HITS have been requested, i.e. the number of features.
    288         *
    289         * TODO: Do this a better way (maybe change feature model).
    290         *
    291         * @param fcid
    292         *            id of the new (result) feature collection
    293         * @param finishedEvents
    294         * @return empty feature collection with "numberOfFeatures" attribute
    295         * @throws OGCWebServiceException
    296         */
    297        private FeatureCollection mergeHits( String fcid, List<ExecutionFinishedEvent<FeatureCollection>> finishedEvents )
    298                                throws OGCWebServiceException {
    299
    300            FeatureCollection result = null;
    301            int numberOfFeatures = 0;
    302
    303            try {
    304                for ( ExecutionFinishedEvent<FeatureCollection> event : finishedEvents ) {
    305                    FeatureCollection fc = event.getResult();
    306                    try {
    307                        numberOfFeatures += Integer.parseInt( ( fc.getAttribute( "numberOfFeatures" ) ) );
    308                    } catch ( NumberFormatException e ) {
    309                        String msg = "Internal error. Could not parse 'numberOfFeatures' attribute "
    310                                     + "of sub-result as an integer value.";
    311                        throw new OGCWebServiceException( this.getClass().getName(), msg );
    312                    }
    313                    if ( result == null ) {
    314                        result = fc;
    315                    } else {
    316                        result.addAllUncontained( fc );
    317                    }
    318                }
    319
    320                if ( result == null ) {
    321                    return result;
    322                }
    323            } catch ( CancellationException e ) {
    324                String msg = Messages.getMessage( "WFS_GET_FEATURE_TIMEOUT" );
    325                LOG.logError( msg, e );
    326                throw new OGCWebServiceException( this.getClass().getName(), msg );
    327            } catch ( Throwable t ) {
    328                String msg = Messages.getMessage( "WFS_GET_FEATURE_BACKEND", t.getMessage() );
    329                LOG.logError( msg, t );
    330                throw new OGCWebServiceException( this.getClass().getName(), msg );
    331            }
    332
    333            result.setId( fcid );
    334            result.setAttribute( "numberOfFeatures", "" + numberOfFeatures );
    335            result.setAttribute( "timeStamp", getISOFormattedTime() );
    336            return result;
    337        }
    338
    339        // ///////////////////////////////////////////////////////////////////////////
    340        // inner classes
    341        // ///////////////////////////////////////////////////////////////////////////
    342
    343        /**
    344         * Inner class for performing queries on a datastore.
    345         */
    346        private class QueryTask implements Callable<FeatureCollection> {
    347
    348            private Datastore ds;
    349
    350            private Query query;
    351
    352            private MappedFeatureType[] fts;
    353
    354            QueryTask( Datastore ds, Query query, MappedFeatureType[] fts ) {
    355                this.ds = ds;
    356                this.query = query;
    357                this.fts = fts;
    358            }
    359
    360            /**
    361             * Performs the associated {@link Query} and returns the result.
    362             *
    363             * @return resulting feature collection
    364             * @throws Exception
    365             */
    366            public FeatureCollection call()
    367                                    throws Exception {
    368                FeatureCollection result = this.ds.performQuery( query, fts );
    369                return result;
    370            }
    371        }
    372    }