/** * Performs an HTTP-Get request and provides typed access to the response. * * @param <T> * @param worker * @param url * @param postBody * @param headers * may be null * @return some object from the url * @throws IOException */ public static <T> T post( Worker<T> worker, String url, InputStream postBody, Map<String, String> headers ) throws IOException { DURL u = new DURL( url ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); HttpPost post = new HttpPost( url ); post.setEntity( new InputStreamEntity( postBody, -1 ) ); if ( headers != null ) { for ( String key : headers.keySet() ) { post.addHeader( key, headers.get( key ) ); } } return worker.work( client.execute( post ).getEntity().getContent() ); }
return worker.work( entity.getContent() );
HttpEntity entity = resp.getEntity(); LOG.debug( "Received response with content type {}", entity.getContentType() ); return new Pair<T, HttpResponse>( worker.work( entity.getContent() ), resp );
res.first = IMAGE.work( conn.getInputStream() ); } else if ( conn.getContentType() != null && conn.getContentType().startsWith( "application/vnd.ogc.se_xml" ) ) { res.second = XmlHttpUtils.XML.work( conn.getInputStream() ).toString(); } else { // try and find out the hard way res.first = IMAGE.work( conn.getInputStream() ); if ( res.first == null ) { conn = theUrl.openConnection(); res.second = XmlHttpUtils.XML.work( conn.getInputStream() ).toString();
return new Pair<T, HttpResponse>( worker.work( response.getEntity().getContent() ), response );
return worker.work( client.execute( post ).getEntity().getContent() );
public static <T> Pair<T, HttpResponse> getFullResponse( Worker<T> worker, String url, Map<String, String> headers, String user, String pass, final int readTimeout ) throws IOException { DURL u = new DURL( url ); if ( !u.valid() ) { return null; } DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); client.setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration( HttpResponse response, HttpContext context ) { long keepAlive = super.getKeepAliveDuration( response, context ); if ( keepAlive == -1 ) { keepAlive = readTimeout * 1000; } return keepAlive; } } ); if ( user != null && pass != null ) { authenticate( client, user, pass, u ); } HttpGet get = new HttpGet( url ); if ( headers != null ) { for ( String key : headers.keySet() ) { get.addHeader( key, headers.get( key ) ); } } HttpResponse response = client.execute( get ); return new Pair<T, HttpResponse>( worker.work( response.getEntity().getContent() ), response ); }
/** * Returns the tile as an image. * * @return image, never <code>null</code> * @throws IOException * @throws XMLStreamException * @throws OWSExceptionReport */ public BufferedImage getAsImage() throws IOException, OWSExceptionReport, XMLStreamException { rawResponse.assertNoXmlContentTypeAndExceptionReport(); BufferedImage image = null; try { InputStream is = rawResponse.getAsBinaryStream(); image = HttpUtils.IMAGE.work( is ); } finally { rawResponse.close(); } return image; }
/** * * @param url * @return a {@link Pair} of {@link BufferedImage} and {@link String}. In case the WPVS returns an image the former * will be used, otherwise an XML response ( as a String ) will be returned in the second value. * @throws IOException */ public synchronized Pair<BufferedImage, String> getView( URL url ) throws IOException { Pair<BufferedImage, String> res = new Pair<BufferedImage, String>(); InputStream inStream = url.openStream(); res.first = IMAGE.work( inStream ); // rb: the following will never happen (I guess) if ( res.first == null ) { res.second = XmlHttpUtils.XML.work( url.openStream() ).toString(); } inStream.close(); return res; } }
/** * @param <T> * @param worker * @param url * must be valid * @return some object from the url * @throws IOException */ public static <T> T retrieve( Worker<T> worker, DURL url ) throws IOException { return worker.work( url.openStream() ); }