/** * @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() ); }
/** * Performs an HTTP-Get request and provides typed access to the response. * * @param <T> * @param worker * @param url * @param headers * may be null * @return some object from the url, null, if url is not valid * @throws IOException */ public static <T> T get( Worker<T> worker, String url, Map<String, String> headers ) throws IOException { return get( worker, url, headers, null, null ); }
DURL u = new DURL( url ); if ( !u.valid() ) { return null; if ( !u.getURL().getProtocol().equalsIgnoreCase( "http" ) ) { return worker.work( u.getURL().openStream() ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); if ( user != null && pass != null ) { authenticate( client, user, pass, u ); return worker.work( client.execute( get ).getEntity().getContent() );
/** * 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() ); }
/** * * @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; } }
/** * reads proxyHost and proxyPort from system parameters and sets them to the passed HttpClient instance * * @see HttpClient * @param client * @param url * must be valid * @return HttpClient with proxy configuration */ public static DefaultHttpClient enableProxyUsage( DefaultHttpClient client, DURL url ) { HttpConnectionParams.setConnectionTimeout( client.getParams(), DEFAULT_CONN_TIMEOUT ); client.setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration( HttpResponse response, HttpContext context ) { long keepAlive = super.getKeepAliveDuration( response, context ); if ( keepAlive == -1 ) { keepAlive = DEFAULT_SOCKET_TIMEOUT; } return keepAlive; } } ); String host = url.getURL().getHost(); String protocol = url.getURL().getProtocol().toLowerCase(); handleProxies( protocol, client, host ); return client; } }
private void setProxies( URL url, DefaultHttpClient client ) { String host = url.getHost(); String protocol = url.getProtocol().toLowerCase(); handleProxies( protocol, client, host ); }
/** * @param <T> * @param worker * @param url * @param map * @return some object from the url, null, if url is not valid * @throws IOException * @throws MalformedURLException */ public static <T> T retrieve( Worker<T> worker, String url, Map<String, String> map ) throws MalformedURLException, IOException { if ( !url.endsWith( "?" ) && !url.endsWith( "&" ) ) { url += url.indexOf( "?" ) == -1 ? "?" : "&"; } LinkedList<String> list = new LinkedList<String>(); for ( Entry<String, String> e : map.entrySet() ) { list.add( encode( e.getKey(), "UTF-8" ) + "=" + encode( e.getValue(), "UTF-8" ) ); } url += join( "&", list ); return retrieve( worker, url ); }
private static void authenticate( DefaultHttpClient client, String user, String pass, DURL u ) { client.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials( user, pass ) ); // preemptive authentication used to be easier in pre-4.x httpclient AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost host = new HttpHost( u.getURL().getHost(), u.getURL().getPort() ); authCache.put( host, basicAuth ); BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute( ClientContext.AUTH_CACHE, authCache ); }
String user, String pass ) throws IOException { DURL u = new DURL( url ); if ( !u.valid() ) { return null; DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); if ( user != null && pass != null ) { authenticate( client, user, pass, u ); return new Pair<T, HttpResponse>( worker.work( response.getEntity().getContent() ), response );
final int readTimeout ) throws IOException { DURL u = new DURL( url ); LOG.debug( "Sending HTTP POST against {}", url ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); client.setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy() { @Override return worker.work( entity.getContent() );
/** * 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; }
/** * @return an input stream from the URL * @throws IOException */ public InputStream openStream() throws IOException { // custom handlers should handle proxies themselves if ( handler != null ) { return url.openStream(); } return get( STREAM, url.toExternalForm(), null ); }
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 ); }
Map<String, String> headers, final int readTimeout ) throws IOException { DURL u = new DURL( url ); LOG.debug( "Sending HTTP POST against {}", url ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); client.setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy() { @Override HttpEntity entity = resp.getEntity(); LOG.debug( "Received response with content type {}", entity.getContentType() ); return new Pair<T, HttpResponse>( worker.work( entity.getContent() ), resp );
/** * Initializes this <code>XMLAdapter</code> with the content from the given <code>URL</code>. Sets the SystemId, * too. * * @param url * source of the xml content * @throws XMLProcessingException */ public void load( URL url ) throws XMLProcessingException { if ( url == null ) { throw new IllegalArgumentException( "The given url may not be null" ); } try { load( get( STREAM, url.toExternalForm(), null ), url.toExternalForm() ); } catch ( IOException e ) { throw new XMLProcessingException( e.getMessage(), e ); } }
String pass ) throws IOException { DURL u = new DURL( url ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); HttpPost post = new HttpPost( url ); if ( user != null && pass != null ) { authenticate( client, user, pass, u ); return worker.work( client.execute( post ).getEntity().getContent() );
/** * Same as #load(URL), but with http basic authentication * * @param url * @param httpBasicUser * @param httpBasicPass * @throws XMLProcessingException */ public void load( URL url, String httpBasicUser, String httpBasicPass ) throws XMLProcessingException { if ( url == null ) { throw new IllegalArgumentException( "The given url may not be null" ); } try { load( get( STREAM, url.toExternalForm(), null, httpBasicUser, httpBasicPass ), url.toExternalForm() ); } catch ( IOException e ) { throw new XMLProcessingException( e.getMessage(), e ); } }