Skip to content

Commit 7769a2b

Browse files
author
Harshit Vasu
committed
first commit for apache client
1 parent 7e228b1 commit 7769a2b

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@
114114
<dependency>
115115
<groupId>com.google.http-client</groupId>
116116
<artifactId>google-http-client</artifactId>
117-
<version>1.21.0</version>
118-
<exclusions>
119-
<exclusion>
120-
<groupId>org.apache.httpcomponents</groupId>
121-
<artifactId>httpclient</artifactId>
122-
</exclusion>
123-
</exclusions>
117+
<version>1.28.0</version>
124118
</dependency>
125119

126120
<dependency>

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

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
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.*;
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;
2121
import 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

2328
import javax.annotation.Nonnull;
2429
import 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
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ List<Session> getSessions(String buildId, BuildStatus status, int limit)
3838
List<Session> getSessions(String buildId, BuildStatus status)
3939
throws BuildNotFound, BrowserStackException;
4040

41-
void setProxy(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword);
41+
void setProxy(String proxyHost, int proxyPort, String proxyUsername, String proxyPassword) throws BrowserStackException;
4242

4343
}

0 commit comments

Comments
 (0)