Skip to content

Commit 7e228b1

Browse files
author
Harshit Vasu
committed
working proxy setter
1 parent ed90043 commit 7e228b1

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/main/java/com/browserstack/client/BrowserStackClient.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,31 @@
1515
import com.fasterxml.jackson.core.JsonProcessingException;
1616
import com.fasterxml.jackson.databind.ObjectMapper;
1717
import 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.*;
2519
import com.google.api.client.http.javanet.NetHttpTransport;
20+
import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
2621
import com.google.api.client.util.ObjectParser;
2722

2823
import javax.annotation.Nonnull;
2924
import java.io.IOException;
3025
import java.io.InputStream;
3126
import java.io.Reader;
3227
import java.lang.reflect.Type;
28+
import java.net.InetSocketAddress;
29+
import java.net.Proxy;
3330
import java.nio.charset.Charset;
31+
import java.nio.charset.StandardCharsets;
3432
import java.util.ArrayList;
3533
import java.util.Arrays;
3634
import java.util.List;
3735
import java.util.Map;
36+
import java.nio.charset.Charset;
3837

3938
public 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
});

src/main/java/com/browserstack/client/BrowserStackClientInterface.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ List<Session> getSessions(String buildId, BuildStatus status, int limit)
3737

3838
List<Session> getSessions(String buildId, BuildStatus status)
3939
throws BuildNotFound, BrowserStackException;
40+
41+
void setProxy(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword);
42+
4043
}

0 commit comments

Comments
 (0)