Search in sources :

Example 1 with GMLStreamingFeatureCollection

use of gov.usgs.cida.gml.GMLStreamingFeatureCollection in project coastal-hazards by USGS-CIDA.

the class HttpComponentsWFSClient method getFeatureCollection.

@Override
public SimpleFeatureCollection getFeatureCollection(String typeName, Filter filter) throws IOException {
    SimpleFeatureCollection collection = null;
    String filterXml = null;
    InputStream is = null;
    OutputStream os = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    if (filter != null) {
        try {
            FilterTransformer transformer = new FilterTransformer();
            transformer.setOmitXMLDeclaration(true);
            transformer.setNamespaceDeclarationEnabled(false);
            filterXml = "<ogc:Filter>" + transformer.transform(filter) + "</ogc:Filter>";
        } catch (TransformerException ex) {
            throw new RuntimeException("Specified filter cannot be transformed", ex);
        }
    } else {
        filterXml = "";
    }
    try {
        HttpPost post = new HttpPost(wfsEndpoint);
        StringEntity stringEntity = new StringEntity(fillInTemplate(typeName, filterXml), ContentType.APPLICATION_XML);
        post.setEntity(stringEntity);
        HttpContext localContext = new BasicHttpContext();
        httpClient.setReuseStrategy(new NoConnectionReuseStrategy());
        HttpResponse methodResponse = httpClient.execute(post, localContext);
        if (methodResponse.getStatusLine().getStatusCode() != 200) {
            throw new IOException(methodResponse.getStatusLine().getReasonPhrase());
        }
        is = methodResponse.getEntity().getContent();
        os = new FileOutputStream(tmpWfsFile);
        IOUtils.copy(is, os);
    } finally {
        if (httpClient.getConnectionManager() != null) {
            httpClient.getConnectionManager().closeExpiredConnections();
        }
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
    collection = new GMLStreamingFeatureCollection(tmpWfsFile);
    return collection;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStream(java.io.InputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) StringEntity(org.apache.http.entity.StringEntity) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) GMLStreamingFeatureCollection(gov.usgs.cida.gml.GMLStreamingFeatureCollection) FileOutputStream(java.io.FileOutputStream) FilterTransformer(org.geotools.filter.FilterTransformer) TransformerException(javax.xml.transform.TransformerException)

Aggregations

GMLStreamingFeatureCollection (gov.usgs.cida.gml.GMLStreamingFeatureCollection)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 TransformerException (javax.xml.transform.TransformerException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1 NoConnectionReuseStrategy (org.apache.http.impl.NoConnectionReuseStrategy)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1 HttpContext (org.apache.http.protocol.HttpContext)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 FilterTransformer (org.geotools.filter.FilterTransformer)1