Skip to content

Commit 998a196

Browse files
author
Harshit Vasu
committed
checking and working apache without deprecation
1 parent 7769a2b commit 998a196

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@
111111
</build>
112112

113113
<dependencies>
114+
115+
<!-- https://mvnrepository.com/artifact/com.google.http-client/google-http-client-apache-v2 -->
114116
<dependency>
115117
<groupId>com.google.http-client</groupId>
116-
<artifactId>google-http-client</artifactId>
117-
<version>1.28.0</version>
118+
<artifactId>google-http-client-apache-v2</artifactId>
119+
<version>1.38.0</version>
118120
</dependency>
119121

122+
120123
<dependency>
121124
<groupId>org.json</groupId>
122125
<artifactId>json</artifactId>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package com.browserstack;public class Run {
2+
}

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,27 @@
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.HttpTransport;
20-
import com.google.api.client.http.apache.ApacheHttpTransport;
18+
19+
import com.google.api.client.http.*;
20+
import com.google.api.client.http.apache.v2.ApacheHttpTransport;
2121
import com.google.api.client.util.ObjectParser;
2222
import org.apache.http.HttpHost;
23-
import org.apache.http.HttpRequestFactory;
2423
import org.apache.http.auth.AuthScope;
2524
import org.apache.http.auth.UsernamePasswordCredentials;
26-
import org.apache.http.impl.client.DefaultHttpClient;
25+
import org.apache.http.client.HttpClient;
26+
import org.apache.http.impl.client.BasicCredentialsProvider;
27+
import org.apache.http.impl.client.HttpClientBuilder;
2728

2829
import javax.annotation.Nonnull;
2930
import java.io.IOException;
3031
import java.io.InputStream;
3132
import java.io.Reader;
3233
import java.lang.reflect.Type;
33-
import java.net.InetSocketAddress;
34-
import java.net.Proxy;
3534
import java.nio.charset.Charset;
36-
import java.nio.charset.StandardCharsets;
3735
import java.util.ArrayList;
3836
import java.util.Arrays;
3937
import java.util.List;
4038
import java.util.Map;
41-
import java.nio.charset.Charset;
4239

4340
public abstract class BrowserStackClient implements BrowserStackClientInterface {
4441
private static final String BASE_URL = "https://www.browserstack.com";
@@ -106,8 +103,7 @@ public BrowserStackClient(String baseUrl, String username, String accessKey) {
106103
}
107104

108105
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();
106+
111107
String protocol = "http";
112108
proxyHost = System.getProperty(protocol + ".proxyHost", proxyHost);
113109
proxyUsername = System.getProperty(protocol + ".proxyUser", proxyUsername);
@@ -117,12 +113,17 @@ public void setProxy(String proxyHost, int proxyPort, String proxyUsername, Stri
117113
return;
118114
}
119115
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));
116+
117+
BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();
118+
AuthScope proxyAuthScope = new AuthScope(proxyHost, proxyPort);
119+
UsernamePasswordCredentials proxyAuthentication =
120+
new UsernamePasswordCredentials(proxyUsername, proxyPassword);
121+
basicCredentialsProvider.setCredentials(proxyAuthScope, proxyAuthentication);
122+
123+
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
124+
HttpClient client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(basicCredentialsProvider).build();
125+
ApacheHttpTransport transport = new ApacheHttpTransport(client);
126+
this.HTTP_TRANSPORT = transport;
126127
this.requestFactory = newRequestFactory();
127128
}
128129

@@ -137,7 +138,8 @@ protected synchronized void setAccessKey(final String accessKey) {
137138

138139
static HttpRequestFactory newRequestFactory() {
139140
return HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
140-
public void initialize(HttpRequest httpRequest) throws IOException {
141+
@Override
142+
public void initialize(com.google.api.client.http.HttpRequest httpRequest) throws IOException {
141143
httpRequest.setParser(OBJECT_PARSER);
142144
}
143145
});

0 commit comments

Comments
 (0)