/** * 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 ); }
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 * @return some object from the url * @throws MalformedURLException * @throws IOException */ public static <T> T retrieve( Worker<T> worker, String url ) throws MalformedURLException, IOException { return retrieve( worker, new DURL( url ) ); }
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 );
InputStream in = post( STREAM, proxiedUrl, pin, headers ); pin.close(); OutputStream out = response.getOutputStream();
/** * 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 null; DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); if ( user != null && pass != null ) { authenticate( client, user, pass, u );
DURL u = new DURL( url ); LOG.debug( "Sending HTTP POST against {}", url ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); client.setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy() { @Override
/** * @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 ); }
/** * @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 ); }
handleProxies( null, client, host );
DURL u = new DURL( url ); LOG.debug( "Sending HTTP POST against {}", url ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); client.setKeepAliveStrategy( new DefaultConnectionKeepAliveStrategy() { @Override
/** * 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 ); } }
return worker.work( u.getURL().openStream() ); DefaultHttpClient client = enableProxyUsage( new DefaultHttpClient(), u ); if ( user != null && pass != null ) { authenticate( client, user, pass, u );
normalizedKVPParams.remove( "USER" ); normalizedKVPParams.remove( "PASSWORD" ); InputStream in = retrieve( STREAM, proxiedUrl, normalizedKVPParams ); OutputStream out = response.getOutputStream(); boolean successful = false;
/** * 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; } }
/** * 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 ); } }