1515import com .fasterxml .jackson .core .JsonProcessingException ;
1616import com .fasterxml .jackson .databind .ObjectMapper ;
1717import com .fasterxml .jackson .databind .node .ObjectNode ;
18- import com .google .api .client .http .* ;
19- import com .google .api .client .http .javanet . NetHttpTransport ;
20- import com .google .api .client .repackaged . org . apache .commons . codec . binary . Base64 ;
18+ import com .google .api .client .http .BasicAuthentication ;
19+ import com .google .api .client .http .HttpTransport ;
20+ import com .google .api .client .http . apache .ApacheHttpTransport ;
2121import com .google .api .client .util .ObjectParser ;
22+ import org .apache .http .HttpHost ;
23+ import org .apache .http .HttpRequestFactory ;
24+ import org .apache .http .auth .AuthScope ;
25+ import org .apache .http .auth .UsernamePasswordCredentials ;
26+ import org .apache .http .impl .client .DefaultHttpClient ;
2227
2328import javax .annotation .Nonnull ;
2429import java .io .IOException ;
@@ -39,7 +44,7 @@ public abstract class BrowserStackClient implements BrowserStackClientInterface
3944 private static final String BASE_URL = "https://www.browserstack.com" ;
4045 private static final String CACHE_KEY_PREFIX_BROWSERS = "browsers" ;
4146
42- private static HttpTransport HTTP_TRANSPORT = new NetHttpTransport ();
47+ private static HttpTransport HTTP_TRANSPORT = new ApacheHttpTransport ();
4348 private static final ObjectMapper JSON_MAPPER = new ObjectMapper ();
4449
4550 private static final ObjectParser OBJECT_PARSER = new ObjectParser () {
@@ -76,7 +81,7 @@ public Object parseAndClose(Reader reader, Type type) throws IOException {
7681
7782 protected BrowserStackClient () {
7883 this .cacheMap = new BrowserStackCache <String , Object >();
79- this .requestFactory = newRequestFactory (null );
84+ this .requestFactory = newRequestFactory ();
8085 }
8186
8287 public BrowserStackClient (String baseUrl , String username , String accessKey ) {
@@ -100,14 +105,25 @@ public BrowserStackClient(String baseUrl, String username, String accessKey) {
100105 this .authentication = new BasicAuthentication (this .username , this .accessKey );
101106 }
102107
103- public void setProxy (String proxyHost , int proxyPort , String proxyUsername , String proxyPassword ){
104- String usernameAndPassword = proxyUsername + ":" + proxyPassword ;
105- String encoded = new String (Base64 .encodeBase64 (usernameAndPassword .getBytes ()));
106- String credential = "Basic " + encoded ;
107- HttpHeaders header = new HttpHeaders ();
108- header .set ("Proxy-Authorization" , credential );
109- this .HTTP_TRANSPORT = new NetHttpTransport .Builder ().setProxy (new Proxy (Proxy .Type .HTTP , new InetSocketAddress (proxyHost , proxyPort ))).build ();
110- this .requestFactory = newRequestFactory (header );
108+ public void setProxy (String proxyHost , int proxyPort , String proxyUsername , String proxyPassword ) {
109+ HttpHost proxy = new HttpHost (proxyHost , proxyPort );
110+ ApacheHttpTransport transport = new ApacheHttpTransport .Builder ().setProxy (proxy ).build ();
111+ String protocol = "http" ;
112+ proxyHost = System .getProperty (protocol + ".proxyHost" , proxyHost );
113+ proxyUsername = System .getProperty (protocol + ".proxyUser" , proxyUsername );
114+ proxyPassword = System .getProperty (protocol + ".proxyPassword" , proxyPassword );
115+
116+ if (proxyHost == null || proxyUsername == null || proxyPassword == null ) {
117+ return ;
118+ }
119+ proxyPort = Integer .parseInt (System .getProperty (protocol + ".proxyPort" , Integer .toString (proxyPort )));
120+ DefaultHttpClient httpClient = (DefaultHttpClient ) transport .getHttpClient ();
121+ httpClient
122+ .getCredentialsProvider ()
123+ .setCredentials (
124+ new AuthScope (proxyHost , proxyPort ),
125+ new UsernamePasswordCredentials (proxyUsername , proxyPassword ));
126+ this .requestFactory = newRequestFactory ();
111127 }
112128
113129 protected String getAccessKey () {
@@ -119,12 +135,9 @@ protected synchronized void setAccessKey(final String accessKey) {
119135 this .authentication = new BasicAuthentication (this .username , this .accessKey );
120136 }
121137
122- static HttpRequestFactory newRequestFactory (HttpHeaders headers ) {
138+ static HttpRequestFactory newRequestFactory () {
123139 return HTTP_TRANSPORT .createRequestFactory (new HttpRequestInitializer () {
124140 public void initialize (HttpRequest httpRequest ) throws IOException {
125- if (headers !=null ){
126- httpRequest .setHeaders (headers );
127- }
128141 httpRequest .setParser (OBJECT_PARSER );
129142 }
130143 });
0 commit comments