    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/gazetteer/AbstractGazetteerCommand.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.portal.standard.gazetteer;
    037
    038    import java.io.IOException;
    039    import java.io.InputStream;
    040    import java.net.URL;
    041    import java.util.ArrayList;
    042    import java.util.HashMap;
    043    import java.util.Iterator;
    044    import java.util.List;
    045    import java.util.Map;
    046
    047    import org.apache.commons.httpclient.HttpException;
    048    import org.deegree.datatypes.QualifiedName;
    049    import org.deegree.framework.log.ILogger;
    050    import org.deegree.framework.log.LoggerFactory;
    051    import org.deegree.framework.util.HttpUtils;
    052    import org.deegree.framework.xml.XMLException;
    053    import org.deegree.io.datastore.PropertyPathResolvingException;
    054    import org.deegree.model.feature.Feature;
    055    import org.deegree.model.feature.FeatureCollection;
    056    import org.deegree.model.feature.FeatureProperty;
    057    import org.deegree.model.feature.GMLFeatureCollectionDocument;
    058    import org.deegree.ogcbase.ElementStep;
    059    import org.deegree.ogcbase.PropertyPath;
    060    import org.deegree.ogcbase.PropertyPathStep;
    061    import org.deegree.ogcwebservices.getcapabilities.DCPType;
    062    import org.deegree.ogcwebservices.getcapabilities.HTTP;
    063    import org.deegree.ogcwebservices.wfs.XMLFactory;
    064    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
    065    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilitiesDocument;
    066    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
    067    import org.xml.sax.SAXException;
    068
    069    /**
    070     * TODO add class documentation here
    071     *
    072     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    073     * @author last edited by: $Author: apoth $
    074     *
    075     * @version $Revision: 26614 $, $Date: 2010-09-07 15:55:38 +0200 (Di, 07. Sep 2010) $
    076     */
    077    abstract class AbstractGazetteerCommand {
    078
    079        private static final ILogger LOG = LoggerFactory.getLogger( AbstractGazetteerCommand.class );
    080
    081        protected List<GazetteerItem> items;
    082
    083        protected String gazetteerAddress;
    084
    085        protected QualifiedName featureType;
    086
    087        protected static Map<String, WFSCapabilities> capabilitiesMap;
    088
    089        protected Map<String, String> properties;
    090
    091        static {
    092            if ( capabilitiesMap == null ) {
    093                capabilitiesMap = new HashMap<String, WFSCapabilities>();
    094            }
    095        }
    096
    097        /**
    098         * @throws IOException
    099         * @throws HttpException
    100         * @throws SAXException
    101         * @throws XMLException
    102         *
    103         */
    104        protected void loadCapabilities()
    105                                throws Exception {
    106            InputStream is = HttpUtils.performHttpGet( gazetteerAddress, "request=GetCapabilities&service=WFS", 60000,
    107                                                       null, null, null ).getResponseBodyAsStream();
    108            WFSCapabilitiesDocument doc = new WFSCapabilitiesDocument();
    109            doc.load( is, gazetteerAddress );
    110            WFSCapabilities capa = (WFSCapabilities) doc.parseCapabilities();
    111            capabilitiesMap.put( gazetteerAddress, capa );
    112        }
    113
    114        protected FeatureCollection performGetFeature( WFSCapabilities capabilities, GetFeature getFeature )
    115                                throws Exception {
    116
    117            // find a valid URL for performing GetFeature requests
    118            URL wfs = null;
    119            org.deegree.ogcwebservices.getcapabilities.Operation[] op = capabilities.getOperationsMetadata().getOperations();
    120            for ( org.deegree.ogcwebservices.getcapabilities.Operation operation : op ) {
    121                if ( "GetFeature".equalsIgnoreCase( operation.getName() ) ) {
    122                    DCPType[] dcp = operation.getDCPs();
    123                    for ( DCPType dcpType : dcp ) {
    124                        if ( dcpType.getProtocol() instanceof HTTP ) {
    125                            wfs = ( (HTTP) dcpType.getProtocol() ).getPostOnlineResources()[0];
    126                        }
    127                    }
    128                }
    129            }
    130
    131            String gf = XMLFactory.export( getFeature ).getAsString();
    132
    133            LOG.logDebug( "GetFeature request: ", gf );
    134            LOG.logDebug( "Sending against: ", wfs );
    135            InputStream is = HttpUtils.performHttpPost( wfs.toURI().toASCIIString(), gf, 60000, null, null, "text/xml",
    136                                                        null, null ).getResponseBodyAsStream();
    137
    138            GMLFeatureCollectionDocument gml = new GMLFeatureCollectionDocument();
    139            gml.load( is, wfs.toURI().toASCIIString() );
    140            FeatureCollection fc = gml.parse();
    141
    142            return fc;
    143        }
    144
    145        protected void createItemsList( FeatureCollection fc )
    146                                throws PropertyPathResolvingException {
    147            items = new ArrayList<GazetteerItem>( fc.size() );
    148            Iterator<Feature> iterator = fc.iterator();
    149            PropertyPath gi = createPropertyPath( properties.get( "GeographicIdentifier" ) );
    150            PropertyPath gai = null;
    151            if ( properties.get( "AlternativeGeographicIdentifier" ) != null ) {
    152                gai = createPropertyPath( properties.get( "AlternativeGeographicIdentifier" ) );
    153            }
    154            PropertyPath disp = createPropertyPath( properties.get( "DisplayName" ) );
    155
    156            while ( iterator.hasNext() ) {
    157                Feature feature = (Feature) iterator.next();
    158                String gmlID = feature.getId();
    159                String geoId = feature.getDefaultProperty( gi ).getValue().toString();
    160                String displayName = (String) feature.getDefaultProperty( disp ).getValue();
    161                String altGeoId = null;
    162                if ( gai != null ) {
    163                    FeatureProperty fp = feature.getDefaultProperty( gai );
    164                    if ( fp != null ) {
    165                        altGeoId = (String) fp.getValue();
    166                    }
    167                }
    168                items.add( new GazetteerItem( gmlID, geoId, altGeoId, displayName ) );
    169            }
    170        }
    171
    172        /**
    173         * @param properties
    174         * @return
    175         */
    176        protected PropertyPath[] getResultProperties( Map<String, String> properties ) {
    177            List<PropertyPath> pathes = new ArrayList<PropertyPath>();
    178
    179            pathes.add( createPropertyPath( properties.get( "GeographicIdentifier" ) ) );
    180
    181            String tmp = properties.get( "DisplayName" );
    182            if ( tmp != null && !tmp.equals( properties.get( "GeographicIdentifier" ) ) ) {
    183                pathes.add( createPropertyPath( tmp ) );
    184            }
    185
    186            tmp = properties.get( "AlternativeGeographicIdentifier" );
    187            if ( tmp != null && !tmp.equals( properties.get( "GeographicIdentifier" ) ) ) {
    188                pathes.add( createPropertyPath( tmp ) );
    189            }
    190
    191            return pathes.toArray( new PropertyPath[pathes.size()] );
    192        }
    193
    194        protected PropertyPath createPropertyPath( String name ) {
    195            List<String> l1 = new ArrayList<String>();
    196            StringBuilder sb = new StringBuilder();
    197            boolean opened = false;
    198            for ( int i = 0; i < name.length(); i++ ) {
    199                char c = name.charAt( i );
    200                if ( c == '{' ) {
    201                    opened = true;
    202                }
    203                if ( c == '/' && !opened ) {
    204                    l1.add( sb.toString() );
    205                    sb.delete( 0, sb.length() );
    206                } else {
    207                    sb.append( c );
    208                }
    209                if ( c == '}' ) {
    210                    opened = false;
    211                }
    212            }
    213            l1.add( sb.toString() );
    214
    215            String[] tmp = l1.toArray( new String[l1.size()] );
    216            List<PropertyPathStep> steps = new ArrayList<PropertyPathStep>();
    217            for ( String string : tmp ) {
    218                QualifiedName qn = null;
    219                if ( name.startsWith( "{" ) ) {
    220                    qn = new QualifiedName( string );
    221                } else {
    222                    qn = new QualifiedName( string, featureType.getNamespace() );
    223                }
    224                steps.add( new ElementStep( qn ) );
    225            }
    226
    227            return new PropertyPath( steps );
    228        }
    229
    230    }