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 .BasicAuthentication ;
19- import com .google .api .client .http .GenericUrl ;
20- import com .google .api .client .http .HttpRequest ;
21- import com .google .api .client .http .HttpRequestFactory ;
22- import com .google .api .client .http .HttpRequestInitializer ;
23- import com .google .api .client .http .HttpResponse ;
24- import com .google .api .client .http .HttpTransport ;
18+ import com .google .api .client .http .*;
2519import com .google .api .client .http .javanet .NetHttpTransport ;
20+ import com .google .api .client .repackaged .org .apache .commons .codec .binary .Base64 ;
2621import com .google .api .client .util .ObjectParser ;
2722
2823import javax .annotation .Nonnull ;
2924import java .io .IOException ;
3025import java .io .InputStream ;
3126import java .io .Reader ;
3227import java .lang .reflect .Type ;
28+ import java .net .InetSocketAddress ;
29+ import java .net .Proxy ;
3330import java .nio .charset .Charset ;
31+ import java .nio .charset .StandardCharsets ;
3432import java .util .ArrayList ;
3533import java .util .Arrays ;
3634import java .util .List ;
3735import java .util .Map ;
36+ import java .nio .charset .Charset ;
3837
3938public abstract class BrowserStackClient implements BrowserStackClientInterface {
4039 private static final String BASE_URL = "https://www.browserstack.com" ;
4140 private static final String CACHE_KEY_PREFIX_BROWSERS = "browsers" ;
4241
43- private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport ();
42+ private static HttpTransport HTTP_TRANSPORT = new NetHttpTransport ();
4443 private static final ObjectMapper JSON_MAPPER = new ObjectMapper ();
4544
4645 private static final ObjectParser OBJECT_PARSER = new ObjectParser () {
@@ -65,7 +64,7 @@ public Object parseAndClose(Reader reader, Type type) throws IOException {
6564
6665 protected final BrowserStackCache <String , Object > cacheMap ;
6766
68- private final HttpRequestFactory requestFactory ;
67+ private HttpRequestFactory requestFactory ;
6968
7069 private String baseUrl ;
7170
@@ -77,7 +76,7 @@ public Object parseAndClose(Reader reader, Type type) throws IOException {
7776
7877 protected BrowserStackClient () {
7978 this .cacheMap = new BrowserStackCache <String , Object >();
80- this .requestFactory = newRequestFactory ();
79+ this .requestFactory = newRequestFactory (null );
8180 }
8281
8382 public BrowserStackClient (String baseUrl , String username , String accessKey ) {
@@ -101,6 +100,16 @@ public BrowserStackClient(String baseUrl, String username, String accessKey) {
101100 this .authentication = new BasicAuthentication (this .username , this .accessKey );
102101 }
103102
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 );
111+ }
112+
104113 protected String getAccessKey () {
105114 return accessKey ;
106115 }
@@ -110,9 +119,12 @@ protected synchronized void setAccessKey(final String accessKey) {
110119 this .authentication = new BasicAuthentication (this .username , this .accessKey );
111120 }
112121
113- static HttpRequestFactory newRequestFactory () {
122+ static HttpRequestFactory newRequestFactory (HttpHeaders headers ) {
114123 return HTTP_TRANSPORT .createRequestFactory (new HttpRequestInitializer () {
115124 public void initialize (HttpRequest httpRequest ) throws IOException {
125+ if (headers !=null ){
126+ httpRequest .setHeaders (headers );
127+ }
116128 httpRequest .setParser (OBJECT_PARSER );
117129 }
118130 });
0 commit comments