Skip to content

Commit b621c93

Browse files
committed
add proxy support with SmartUIAppSnapshot
1 parent d75fd24 commit b621c93

File tree

5 files changed

+167
-53
lines changed

5 files changed

+167
-53
lines changed

Diff for: build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = 'io.github.lambdatest'
9-
version = '1.0.12-beta.2'
9+
version = '1.0.12-beta.3'
1010
description = 'lambdatest-java-sdk'
1111

1212
repositories {
@@ -83,7 +83,7 @@ afterEvaluate {
8383
mavenJava(MavenPublication) {
8484
groupId = 'io.github.lambdatest'
8585
artifactId = 'lambdatest-java-sdk'
86-
version = '1.0.12-beta.2'
86+
version = '1.0.12-beta.3'
8787

8888
pom {
8989
name.set('LambdaTest Java SDK')

Diff for: pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>io.github.lambdatest</groupId>
88
<artifactId>lambdatest-java-sdk</artifactId>
9-
<version>1.0.12-beta.2</version>
9+
<version>1.0.12-beta.3</version>
1010
<name>lambdatest-java-sdk</name>
1111
<description>LambdaTest SDK in Java</description>
1212
<url>https://www.lambdatest.com</url>
@@ -41,12 +41,12 @@
4141
<dependency>
4242
<groupId>org.apache.httpcomponents</groupId>
4343
<artifactId>httpclient</artifactId>
44-
<version>4.5.13</version>
44+
<version>4.5.14</version>
4545
</dependency>
4646
<dependency>
4747
<groupId>org.apache.httpcomponents</groupId>
4848
<artifactId>httpmime</artifactId>
49-
<version>4.5.13</version>
49+
<version>4.5.14</version>
5050
</dependency>
5151
<dependency>
5252
<groupId>org.json</groupId>
@@ -138,7 +138,7 @@
138138
<plugin>
139139
<groupId>org.apache.maven.plugins</groupId>
140140
<artifactId>maven-compiler-plugin</artifactId>
141-
<version>2.3.2</version>
141+
<version>3.8.1</version>
142142
<configuration>
143143
<source>1.8</source>
144144
<target>1.8</target>
+52-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.github.lambdatest;
22

3-
43
import com.google.gson.Gson;
54
import io.github.lambdatest.constants.Constants;
65
import io.github.lambdatest.models.*;
@@ -19,42 +18,53 @@
1918
import io.github.lambdatest.utils.LoggerUtil;
2019

2120
public class SmartUIAppSnapshot {
22-
private final Logger log;
21+
private final Logger log = LoggerUtil.createLogger("lambdatest-java-app-sdk");
2322
private final SmartUIUtil util;
24-
private final Gson gson;
23+
private final Gson gson = new Gson();
2524
private String projectToken;
2625

2726
private BuildData buildData;
2827

2928
public SmartUIAppSnapshot() {
30-
this.log = LoggerUtil.createLogger("lambdatest-java-app-sdk");
3129
this.util = new SmartUIUtil();
32-
this.gson = new Gson();
3330
}
3431

35-
public void start(Map<String, String> options) throws Exception{
36-
try{
32+
public SmartUIAppSnapshot(String proxyHost, int proxyPort) throws Exception {
33+
this.util = new SmartUIUtil(proxyHost, proxyPort);
34+
}
35+
36+
public SmartUIAppSnapshot(String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
37+
this.util = new SmartUIUtil(proxyHost, proxyPort, allowInsecure);
38+
}
39+
40+
public SmartUIAppSnapshot(String proxyProtocol, String proxyHost, int proxyPort, boolean allowInsecure)
41+
throws Exception {
42+
this.util = new SmartUIUtil(proxyProtocol, proxyHost, proxyPort, allowInsecure);
43+
}
44+
45+
public void start(Map<String, String> options) throws Exception {
46+
try {
3747
this.projectToken = getProjectToken(options);
3848
log.info("Project token set as: " + this.projectToken);
39-
} catch (Exception e){
49+
} catch (Exception e) {
4050
log.severe(Constants.Errors.PROJECT_TOKEN_UNSET);
4151
throw new Exception("Project token is a mandatory field", e);
4252
}
43-
53+
4454
try {
4555
Map<String, String> envVars = new HashMap<>(System.getenv());
4656
GitInfo git = GitUtils.getGitInfo(envVars);
4757
BuildResponse buildRes = util.build(git, this.projectToken, options);
4858
this.buildData = buildRes.getData();
49-
log.info("Build ID set : " + this.buildData.getBuildId() + "for Build name : "+ this.buildData.getName());
59+
log.info("Build ID set : " + this.buildData.getBuildId() + "for Build name : " + this.buildData.getName());
5060
options.put("buildName", this.buildData.getName());
51-
} catch(Exception e) {
61+
} catch (Exception e) {
5262
log.severe("Couldn't create smartui build: " + e.getMessage());
5363
throw new Exception("Couldn't create smartui build: " + e.getMessage());
5464
}
5565
}
5666

57-
public void start() throws Exception{
67+
public void start() throws Exception {
5868
this.start(new HashMap<>());
5969
}
6070

@@ -72,11 +82,11 @@ private String getProjectToken(Map<String, String> options) {
7282
throw new IllegalArgumentException(Constants.Errors.PROJECT_TOKEN_UNSET);
7383
}
7484

75-
76-
public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Map<String, String> options) throws Exception {
85+
public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Map<String, String> options)
86+
throws Exception {
7787
try {
7888
if (appiumDriver == null) {
79-
log.severe(Constants.Errors.SELENIUM_DRIVER_NULL +" during take snapshot");
89+
log.severe(Constants.Errors.SELENIUM_DRIVER_NULL + " during take snapshot");
8090
throw new IllegalArgumentException(Constants.Errors.SELENIUM_DRIVER_NULL);
8191
}
8292
if (screenshotName == null || screenshotName.isEmpty()) {
@@ -93,32 +103,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
93103
uploadSnapshotRequest.setProjectToken(projectToken);
94104
Dimension d = appiumDriver.manage().window().getSize();
95105
int w = d.getWidth(), h = d.getHeight();
96-
uploadSnapshotRequest.setViewport(w+"x"+h);
97-
log.info("Device viewport set to: "+ uploadSnapshotRequest.getViewport());
98-
String platform = "", deviceName="", browserName ="";
99-
if(options != null && options.containsKey("platform")){
106+
uploadSnapshotRequest.setViewport(w + "x" + h);
107+
log.info("Device viewport set to: " + uploadSnapshotRequest.getViewport());
108+
String platform = "", deviceName = "", browserName = "";
109+
if (options != null && options.containsKey("platform")) {
100110
platform = options.get("platform").trim();
101111
}
102-
if(options != null && options.containsKey("deviceName")){
112+
if (options != null && options.containsKey("deviceName")) {
103113
deviceName = options.get("deviceName").trim();
104114
}
105-
if(deviceName == null || deviceName.isEmpty()){
115+
if (deviceName == null || deviceName.isEmpty()) {
106116
throw new IllegalArgumentException(Constants.Errors.DEVICE_NAME_NULL);
107117
}
108-
if(platform == null || platform.isEmpty()){
109-
if(deviceName.toLowerCase().startsWith("i")){
110-
browserName = "iOS";
111-
}
112-
else {
118+
if (platform == null || platform.isEmpty()) {
119+
if (deviceName.toLowerCase().startsWith("i")) {
120+
browserName = "iOS";
121+
} else {
113122
browserName = "Android";
114123
}
115124
}
116125
uploadSnapshotRequest.setOs(platform != null && !platform.isEmpty() ? platform : browserName);
117-
if(platform != null && !platform.isEmpty()){
118-
uploadSnapshotRequest.setDeviceName(deviceName+" "+platform);
119-
}
120-
else {
121-
uploadSnapshotRequest.setDeviceName(deviceName + " "+browserName);
126+
if (platform != null && !platform.isEmpty()) {
127+
uploadSnapshotRequest.setDeviceName(deviceName + " " + platform);
128+
} else {
129+
uploadSnapshotRequest.setDeviceName(deviceName + " " + browserName);
122130
}
123131

124132
if (platform.toLowerCase().contains("ios")) {
@@ -130,28 +138,30 @@ public void smartuiAppSnapshot(WebDriver appiumDriver, String screenshotName, Ma
130138
uploadSnapshotRequest.setBuildId(buildData.getBuildId());
131139
uploadSnapshotRequest.setBuildName(buildData.getName());
132140
}
133-
UploadSnapshotResponse uploadSnapshotResponse = util.uploadScreenshot(screenshot,uploadSnapshotRequest, this.buildData);
134-
log.info("For uploading: " + uploadSnapshotRequest.toString() + " received response: "+ uploadSnapshotResponse.getData());
141+
UploadSnapshotResponse uploadSnapshotResponse = util.uploadScreenshot(screenshot, uploadSnapshotRequest,
142+
this.buildData);
143+
log.info("For uploading: " + uploadSnapshotRequest.toString() + " received response: "
144+
+ uploadSnapshotResponse.getData());
135145
} catch (Exception e) {
136-
log.severe(Constants.Errors.UPLOAD_SNAPSHOT_FAILED + " due to: " +e.getMessage());
137-
throw new Exception("Couldnt upload image to Smart UI due to: " + e.getMessage());
146+
log.severe(Constants.Errors.UPLOAD_SNAPSHOT_FAILED + " due to: " + e.getMessage());
147+
throw new Exception("Couldnt upload image to Smart UI due to: " + e.getMessage());
138148
}
139149
}
140150

141-
public void stop() throws Exception{
151+
public void stop() throws Exception {
142152
try {
143153
if (this.buildData != null) {
144154
log.info("Stopping session for buildId: " + this.buildData.getBuildId());
145-
if(Objects.nonNull(this.buildData.getBuildId())){
146-
util.stopBuild(this.buildData.getBuildId(), projectToken);
147-
log.info("Session ended for token: " + projectToken);}
148-
else {
149-
log.info("Build ID not found to stop build for "+ projectToken);
155+
if (Objects.nonNull(this.buildData.getBuildId())) {
156+
util.stopBuild(this.buildData.getBuildId(), projectToken);
157+
log.info("Session ended for token: " + projectToken);
158+
} else {
159+
log.info("Build ID not found to stop build for " + projectToken);
150160
}
151161
}
152162
} catch (Exception e) {
153163
log.severe("Couldn't stop the build due to an exception: " + e.getMessage());
154-
throw new Exception(Constants.Errors.STOP_BUILD_FAILED +" due to : "+ e.getMessage());
164+
throw new Exception(Constants.Errors.STOP_BUILD_FAILED + " due to : " + e.getMessage());
155165
}
156166
}
157167
}

Diff for: src/main/java/io/github/lambdatest/utils/HttpClientUtil.java

+95-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,108 @@
2727
import java.util.Objects;
2828
import java.util.logging.Level;
2929
import java.util.logging.Logger;
30+
import io.github.lambdatest.utils.LoggerUtil;
3031
import com.fasterxml.jackson.databind.ObjectMapper;
3132

33+
import org.apache.http.HttpHost;
34+
import org.apache.http.client.config.RequestConfig;
35+
import org.apache.http.impl.client.CloseableHttpClient;
36+
import org.apache.http.impl.client.HttpClients;
37+
import org.apache.http.conn.ssl.SSLContextBuilder;
38+
import org.apache.http.conn.ssl.TrustAllStrategy;
39+
import org.apache.http.conn.ssl.NoopHostnameVerifier;
40+
import javax.net.ssl.SSLContext;
41+
3242
public class HttpClientUtil {
3343
private final CloseableHttpClient httpClient;
34-
private Logger log;
44+
private Logger log = LoggerUtil.createLogger("lambdatest-java-sdk");
3545

3646
public HttpClientUtil() {
3747
this.httpClient = HttpClients.createDefault();
38-
this.log = LoggerUtil.createLogger("lambdatest-java-sdk");
48+
}
49+
50+
public HttpClientUtil(String proxyHost, int proxyPort) throws Exception {
51+
this(proxyHost, proxyPort, false);
52+
}
53+
54+
public HttpClientUtil(String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
55+
try {
56+
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
57+
RequestConfig requestConfig = RequestConfig.custom()
58+
.setProxy(proxy)
59+
.build();
60+
// Build the HttpClient with conditional SSL settings
61+
CloseableHttpClient clientBuilder;
62+
63+
// If allowInsecure is true, disable SSL verification
64+
if (allowInsecure) {
65+
SSLContext sslContext = new SSLContextBuilder()
66+
.loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
67+
.build();
68+
69+
clientBuilder = HttpClients.custom()
70+
.setDefaultRequestConfig(requestConfig)
71+
.setSSLContext(sslContext)
72+
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
73+
.build();
74+
} else {
75+
// Build standard HttpClient with proxy
76+
clientBuilder = HttpClients.custom()
77+
.setDefaultRequestConfig(requestConfig)
78+
.build();
79+
}
80+
81+
this.httpClient = clientBuilder;
82+
String proxyConfig = String.format("%s:%d (Insecure: %b)", proxyHost, proxyPort, allowInsecure);
83+
log.info(proxyConfig);
84+
85+
} catch (Exception e) {
86+
log.severe("Error configuring HttpClient" + e.getMessage());
87+
throw new Exception("Failed to create HttpClient" + e.getMessage());
88+
}
89+
}
90+
91+
public HttpClientUtil(String proxyProtocol, String proxyHost, int proxyPort, boolean allowInsecure)
92+
throws Exception {
93+
try {
94+
HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxyProtocol);
95+
96+
RequestConfig requestConfig = RequestConfig.custom()
97+
.setProxy(proxy)
98+
.build();
99+
100+
// Build the HttpClient with conditional SSL settings
101+
CloseableHttpClient clientBuilder;
102+
103+
// If allowInsecure is true, disable SSL verification
104+
if (allowInsecure) {
105+
SSLContext sslContext = new SSLContextBuilder()
106+
.loadTrustMaterial(null, TrustAllStrategy.INSTANCE)
107+
.build();
108+
109+
clientBuilder = HttpClients.custom()
110+
.setDefaultRequestConfig(requestConfig)
111+
.setSSLContext(sslContext)
112+
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
113+
.build();
114+
115+
} else {
116+
// Build standard HttpClient with proxy
117+
clientBuilder = HttpClients.custom()
118+
.setDefaultRequestConfig(requestConfig)
119+
.build();
120+
}
121+
122+
// Assign the built HttpClient
123+
this.httpClient = clientBuilder;
124+
125+
String proxyConfig = String.format("%s://%s:%d (Insecure: %b)",
126+
proxyProtocol, proxyHost, proxyPort, allowInsecure);
127+
log.info(proxyConfig);
128+
} catch (Exception e) {
129+
log.severe("Error configuring HttpClient" + e.getMessage());
130+
throw new Exception("Failed to create HttpClient" + e.getMessage());
131+
}
39132
}
40133

41134
public String request(String url, String method, String data) throws IOException {

Diff for: src/main/java/io/github/lambdatest/utils/SmartUIUtil.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,23 @@
1111

1212
public class SmartUIUtil {
1313
private final HttpClientUtil httpClient;
14-
private final Logger log;
14+
private final Logger log = LoggerUtil.createLogger("lambdatest-java-sdk");
1515
private Gson gson = new Gson();
1616

1717
public SmartUIUtil() {
1818
this.httpClient = new HttpClientUtil();
19-
this.log = LoggerUtil.createLogger("lambdatest-java-sdk");
19+
}
20+
21+
public SmartUIUtil(String proxyHost, int proxyPort) throws Exception {
22+
this.httpClient = new HttpClientUtil(proxyHost, proxyPort);
23+
}
24+
25+
public SmartUIUtil(String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
26+
this.httpClient = new HttpClientUtil(proxyHost, proxyPort, allowInsecure);
27+
}
28+
29+
public SmartUIUtil(String proxyProtocol, String proxyHost, int proxyPort, boolean allowInsecure) throws Exception {
30+
this.httpClient = new HttpClientUtil(proxyProtocol, proxyHost, proxyPort, allowInsecure);
2031
}
2132

2233
public boolean isSmartUIRunning() {
@@ -111,7 +122,7 @@ public BuildResponse build(GitInfo git, String projectToken, Map<String, String>
111122
log.info("Build name set from system: " + buildNameStr);
112123
}
113124
} else {
114-
createBuildRequest.setBuildName("smartui-" + UUID.randomUUID().toString().substring(0, 10));
125+
createBuildRequest.setBuildName("smartui-" + UUID.randomUUID().toString().substring(0, 10));
115126
}
116127
if (Objects.nonNull(git)) {
117128
createBuildRequest.setGit(git);

0 commit comments

Comments
 (0)