001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/sos/WFSRequester.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.sos;
037    
038    import java.io.ByteArrayInputStream;
039    import java.io.ByteArrayOutputStream;
040    import java.io.IOException;
041    
042    import javax.xml.parsers.ParserConfigurationException;
043    import javax.xml.transform.TransformerException;
044    
045    import org.deegree.framework.log.ILogger;
046    import org.deegree.framework.log.LoggerFactory;
047    import org.deegree.framework.xml.XMLParsingException;
048    import org.deegree.framework.xml.XMLTools;
049    import org.deegree.model.feature.FeatureCollection;
050    import org.deegree.model.feature.GMLFeatureAdapter;
051    import org.deegree.ogcwebservices.OGCWebService;
052    import org.deegree.ogcwebservices.wfs.operation.FeatureResult;
053    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
054    import org.w3c.dom.Document;
055    import org.xml.sax.SAXException;
056    
057    /**
058     * sends a request to a WFS and checks the result for errors
059     *
060     * @author <a href="mailto:mkulbe@lat-lon.de">Matthias Kulbe </a>
061     * @author last edited by: $Author:wanhoff$
062     *
063     * @version $Revision: 18195 $, $Date:20.03.2007$
064     */
065    public class WFSRequester {
066    
067        private static ILogger LOG = LoggerFactory.getLogger( WFSRequester.class );
068    
069        /**
070         *
071         * @param request
072         * @param ows
073         * @return the resulting document
074         * @throws Exception
075         * @throws IOException
076         * @throws SAXException
077         * @throws ParserConfigurationException
078         * @throws XMLParsingException
079         * @throws TransformerException
080         */
081        public static Document sendWFSrequest( Document request, OGCWebService ows )
082                                throws Exception {
083    
084            GetFeature getFeature = GetFeature.create( "ID-" + System.currentTimeMillis(), request.getDocumentElement() );
085            FeatureResult result = (FeatureResult) ows.doService( getFeature );
086            ByteArrayOutputStream bos = new ByteArrayOutputStream( 100000 );
087    
088            new GMLFeatureAdapter().export( (FeatureCollection) result.getResponse(), bos );
089    
090            Document resultDoc = XMLTools.parse( new ByteArrayInputStream( bos.toByteArray() ) );
091    
092            String nn = resultDoc.getDocumentElement().getLocalName();
093    
094            if ( "FeatureCollection".equals( nn ) ) {
095                return resultDoc;
096            }
097            LOG.logDebug( "error: invalid wfs result" );
098            throw new XMLParsingException( "returned document doesn't contain a valid " + "WFS GetFeature response" );
099    
100        }
101    
102    }