Skip to content

Commit b291310

Browse files
update json dependency
1 parent 4589019 commit b291310

File tree

9 files changed

+59
-40
lines changed

9 files changed

+59
-40
lines changed

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@
149149
<include>**</include>
150150
</includes>
151151
</filter>
152+
<filter>
153+
<artifact>org.slf4j:slf4j-api</artifact>
154+
<includes>
155+
<include>**</include>
156+
</includes>
157+
</filter>
152158
<filter>
153159
<artifact>ch.qos.logback:logback-classic</artifact>
154160
<includes>
@@ -271,10 +277,9 @@
271277
<version>1.9.0</version>
272278
</dependency>
273279
<dependency>
274-
<groupId>net.sf.json-lib</groupId>
275-
<artifactId>json-lib</artifactId>
276-
<version>2.4</version>
277-
<classifier>jdk15</classifier>
280+
<groupId>com.fasterxml.jackson.core</groupId>
281+
<artifactId>jackson-databind</artifactId>
282+
<version>2.18.2</version>
278283
</dependency>
279284
<dependency>
280285
<groupId>org.slf4j</groupId>

src/main/java/com/testingbot/tunnel/Api.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
import org.apache.http.impl.client.BasicCredentialsProvider;
2020
import org.apache.http.impl.client.CloseableHttpClient;
2121

22-
import net.sf.json.*;
22+
import com.fasterxml.jackson.databind.JsonNode;
23+
import com.fasterxml.jackson.databind.ObjectMapper;
2324
import org.apache.http.NameValuePair;
2425
import org.apache.http.client.entity.UrlEncodedFormEntity;
2526
import org.apache.http.client.methods.HttpPost;
@@ -38,14 +39,15 @@ public class Api {
3839
private final String apiHost = "api.testingbot.com";
3940
private final App app;
4041
private int tunnelID;
42+
private final ObjectMapper objectMapper = new ObjectMapper();
4143

4244
public Api(App app) {
4345
this.app = app;
4446
this.clientKey = app.getClientKey();
4547
this.clientSecret = app.getClientSecret();
4648
}
4749

48-
public JSONObject createTunnel() throws Exception {
50+
public JsonNode createTunnel() throws Exception {
4951
try {
5052
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
5153
nameValuePairs.add(new BasicNameValuePair("tunnel_version", App.VERSION.toString()));
@@ -69,7 +71,7 @@ public void setTunnelID(int tunnelID) {
6971
this.tunnelID = tunnelID;
7072
}
7173

72-
public JSONObject pollTunnel(String tunnelID) throws Exception {
74+
public JsonNode pollTunnel(String tunnelID) throws Exception {
7375
try {
7476
return this._get("https://" + apiHost + "/v1/tunnel/" + tunnelID);
7577
}
@@ -115,7 +117,7 @@ public void destroyTunnel() throws Exception {
115117
}
116118
}
117119

118-
private JSONObject _post(String url, List<NameValuePair> postData) throws Exception {
120+
private JsonNode _post(String url, List<NameValuePair> postData) throws Exception {
119121
try {
120122
HttpClientBuilder builder = HttpClientBuilder.create();
121123

@@ -160,9 +162,9 @@ private JSONObject _post(String url, List<NameValuePair> postData) throws Excep
160162
jsonData = jsonData.substring(1, (jsonData.length() - 1));
161163
}
162164

163-
return (JSONObject) JSONSerializer.toJSON(jsonData);
165+
return objectMapper.readTree(jsonData);
164166
}
165-
catch (JSONException e) {
167+
catch (Exception e) {
166168
throw new Exception("Json parse error: " + e.getMessage() + " for " + sb.toString());
167169
}
168170

@@ -171,7 +173,7 @@ private JSONObject _post(String url, List<NameValuePair> postData) throws Excep
171173
}
172174
}
173175

174-
private JSONObject _get(String url) throws Exception {
176+
private JsonNode _get(String url) throws Exception {
175177
try {
176178
HttpClientBuilder builder = HttpClientBuilder.create();
177179
if (app.getProxy() != null) {
@@ -217,9 +219,9 @@ private JSONObject _get(String url) throws Exception {
217219
jsonData = jsonData.substring(1, (jsonData.length() - 1));
218220
}
219221

220-
return (JSONObject) JSONSerializer.toJSON(jsonData);
222+
return objectMapper.readTree(jsonData);
221223
}
222-
catch (JSONException e) {
224+
catch (Exception e) {
223225
throw new Exception("Json parse error: " + e.getMessage() + " for " + sb);
224226
}
225227

src/main/java/com/testingbot/tunnel/App.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
import java.util.logging.Handler;
2222
import java.util.logging.Level;
2323
import java.util.logging.Logger;
24-
import net.sf.json.JSONObject;
24+
import com.fasterxml.jackson.databind.JsonNode;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
2526
import org.apache.commons.cli.*;
2627
import ssh.SSHTunnel;
2728
import ssh.TunnelPoller;
@@ -245,12 +246,13 @@ public static void main(String... args) throws Exception {
245246

246247
if (commandLine.hasOption("extra-headers")) {
247248
String extraHeadersValue = commandLine.getOptionValue("extra-headers");
248-
JSONObject obj = JSONObject.fromObject(extraHeadersValue);
249+
ObjectMapper mapper = new ObjectMapper();
250+
JsonNode obj = mapper.readTree(extraHeadersValue);
249251

250-
Iterator<String> keyIterator = obj.keys();
252+
Iterator<String> keyIterator = obj.fieldNames();
251253
while (keyIterator.hasNext()) {
252254
String key = keyIterator.next();
253-
String value = obj.getString(key);
255+
String value = obj.get(key).asText();
254256
app.addCustomHeader(key, value);
255257
}
256258
}
@@ -375,7 +377,7 @@ public void run() {
375377

376378
public void boot() throws Exception {
377379
api = new Api(this);
378-
JSONObject tunnelData = new JSONObject();
380+
JsonNode tunnelData = null;
379381

380382
try {
381383
tunnelData = api.createTunnel();
@@ -386,8 +388,8 @@ public void boot() throws Exception {
386388
}
387389

388390
if (tunnelData.has("error")) {
389-
System.err.println("An error ocurred: " + tunnelData.getString("error"));
390-
if (tunnelData.getString("error").contains("401")) {
391+
System.err.println("An error ocurred: " + tunnelData.get("error").asText());
392+
if (tunnelData.get("error").asText().contains("401")) {
391393
System.err.println("Missing required arguments API_KEY API_SECRET\nYou can get these two values from https://testingbot.com/members/user/edit");
392394
}
393395
System.exit(1);
@@ -398,20 +400,20 @@ public void boot() throws Exception {
398400
startInsightServer();
399401

400402
if (tunnelData.has("id")) {
401-
this.tunnelID = Integer.parseInt(tunnelData.getString("id"));
403+
this.tunnelID = Integer.parseInt(tunnelData.get("id").asText());
402404
api.setTunnelID(tunnelID);
403405
}
404406

405-
if (Float.parseFloat(tunnelData.getString("version")) > App.VERSION) {
406-
System.err.println("A new version (" + tunnelData.getString("version") + ") is available for download at https://testingbot.com\nYou have version " + App.VERSION);
407+
if (Float.parseFloat(tunnelData.get("version").asText()) > App.VERSION) {
408+
System.err.println("A new version (" + tunnelData.get("version").asText() + ") is available for download at https://testingbot.com\nYou have version " + App.VERSION);
407409
}
408410

409411
Logger.getLogger(App.class.getName()).log(Level.INFO, "Please wait while your personal Tunnel Server is being setup. Shouldn't take more than a minute.\nWhen the tunnel is ready you will see a message \"You may start your tests.\"");
410412

411-
if (tunnelData.getString("state").equals("READY")) {
413+
if (tunnelData.get("state").asText().equals("READY")) {
412414
this.tunnelReady(tunnelData);
413415
} else {
414-
poller = new TunnelPoller(this, tunnelData.getString("id"));
416+
poller = new TunnelPoller(this, tunnelData.get("id").asText());
415417
}
416418
}
417419

@@ -444,10 +446,10 @@ public void stop() {
444446
}
445447
}
446448

447-
public void tunnelReady(JSONObject apiResponse) {
449+
public void tunnelReady(JsonNode apiResponse) {
448450
// server is booted, make the connection
449451
try {
450-
String _serverIP = apiResponse.getString("ip");
452+
String _serverIP = apiResponse.get("ip").asText();
451453
tunnel = new SSHTunnel(this, _serverIP);
452454
if (tunnel.isAuthenticated()) {
453455
this.serverIP = _serverIP;

src/main/java/com/testingbot/tunnel/HttpForwarder.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,16 @@ public void stop() {
6969
}
7070

7171
public boolean testForwarding() {
72+
// Give the SSH tunnel a moment to fully establish
73+
try {
74+
Thread.sleep(500);
75+
} catch (InterruptedException e) {
76+
Thread.currentThread().interrupt();
77+
}
78+
7279
RequestConfig cfg = RequestConfig.custom()
73-
.setConnectTimeout(2000)
74-
.setSocketTimeout(2000)
80+
.setConnectTimeout(5000)
81+
.setSocketTimeout(10000)
7582
.setRedirectsEnabled(false)
7683
.build();
7784

src/main/java/com/testingbot/tunnel/HttpProxy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public boolean testProxy() {
149149

150150
// HttpClient with sane timeouts
151151
RequestConfig cfg = RequestConfig.custom()
152-
.setConnectTimeout(3000)
153-
.setSocketTimeout(5000)
152+
.setConnectTimeout(5000)
153+
.setSocketTimeout(10000)
154154
.setRedirectsEnabled(false)
155155
.build();
156156

src/main/java/com/testingbot/tunnel/proxy/TunnelProxyServlet.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import com.testingbot.tunnel.Statistics;
44
import jakarta.servlet.http.HttpServletRequest;
55
import jakarta.servlet.http.HttpServletResponse;
6-
import org.apache.commons.lang.exception.ExceptionUtils;
6+
import java.io.PrintWriter;
7+
import java.io.StringWriter;
78
import org.eclipse.jetty.client.HttpClient;
89
import org.eclipse.jetty.client.HttpProxy;
910
import org.eclipse.jetty.client.ProxyConfiguration;
@@ -81,7 +82,9 @@ protected void onResponseContent(HttpServletRequest request, HttpServletResponse
8182
@Override
8283
protected void onClientRequestFailure(HttpServletRequest clientRequest, Request proxyRequest, HttpServletResponse proxyResponse, Throwable failure) {
8384
if (!clientRequest.getRequestURL().toString().contains("squid-internal")) {
84-
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.WARNING, "{0} for request {1}\n{2}", new Object[]{failure.getMessage(), clientRequest.getMethod() + " - " + clientRequest.getRequestURL().toString(), ExceptionUtils.getStackTrace(failure)});
85+
StringWriter sw = new StringWriter();
86+
failure.printStackTrace(new PrintWriter(sw));
87+
Logger.getLogger(TunnelProxyServlet.class.getName()).log(Level.WARNING, "{0} for request {1}\n{2}", new Object[]{failure.getMessage(), clientRequest.getMethod() + " - " + clientRequest.getRequestURL().toString(), sw.toString()});
8588
}
8689

8790
super.onClientRequestFailure(clientRequest, proxyRequest, proxyResponse, failure);

src/main/java/ssh/TunnelPoller.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.TimerTask;
77
import java.util.logging.Level;
88
import java.util.logging.Logger;
9-
import net.sf.json.JSONObject;
9+
import com.fasterxml.jackson.databind.JsonNode;
1010

1111
/**
1212
*
@@ -34,7 +34,7 @@ class PollTask extends TimerTask {
3434
@Override
3535
public void run() {
3636
Api api = app.getApi();
37-
JSONObject response;
37+
JsonNode response;
3838
try {
3939
response = api.pollTunnel(tunnelID);
4040

@@ -44,12 +44,12 @@ public void run() {
4444
return;
4545
}
4646

47-
if (response.getString("state").equals("READY")) {
47+
if (response.get("state").asText().equals("READY")) {
4848
timer.cancel();
4949
app.tunnelReady(response);
5050
} else {
5151
this.counter += 1;
52-
Logger.getLogger(TunnelPoller.class.getName()).log(Level.INFO, "Current tunnel status: {0}", response.getString("state"));
52+
Logger.getLogger(TunnelPoller.class.getName()).log(Level.INFO, "Current tunnel status: {0}", response.get("state").asText());
5353
}
5454
} catch (Exception ex) {
5555
timer.cancel();

src/test/java/com/testingbot/tunnel/ApiTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.testingbot.tunnel;
22

33
import com.github.tomakehurst.wiremock.WireMockServer;
4-
import net.sf.json.JSONObject;
4+
import com.fasterxml.jackson.databind.JsonNode;
55
import org.junit.jupiter.api.AfterEach;
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;

src/test/java/com/testingbot/tunnel/integration/TunnelIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.testingbot.tunnel.Doctor;
77
import com.testingbot.tunnel.HttpForwarder;
88
import com.testingbot.tunnel.HttpProxy;
9-
import net.sf.json.JSONObject;
9+
import com.fasterxml.jackson.databind.JsonNode;
1010
import org.junit.jupiter.api.AfterEach;
1111
import org.junit.jupiter.api.BeforeEach;
1212
import org.junit.jupiter.api.Test;
@@ -118,7 +118,7 @@ void givenApiConfiguration_whenCreatingTunnel_thenShouldHandleApiResponse() {
118118
// Then: Should handle the API call structure correctly
119119
assertThatCode(() -> {
120120
try {
121-
JSONObject result = api.createTunnel();
121+
JsonNode result = api.createTunnel();
122122
assertThat((Object) result).isNotNull();
123123
} catch (Exception e) {
124124
// Expected in test environment

0 commit comments

Comments
 (0)