Skip to content

Commit 2d4b877

Browse files
authored
fix: Remove org.json dependency (#197)
* fix: updated version to 2.0.0-SNAPSHOT
1 parent 02039ed commit 2d4b877

19 files changed

Lines changed: 98 additions & 69 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
## 1.16.0 [unreleased]
1+
## 2.0 [unreleased]
2+
3+
### API
4+
1. [#197](https://github.com/influxdata/influxdb-client-java/pull/197): InfluxException bodyError type changed from JSONObject to Map<String, Object>
5+
6+
### Bug Fixes
7+
1. [#197](https://github.com/influxdata/influxdb-client-java/issues/196): Removed badly licenced JSON-Java library
28

39
## 1.15.0 [2021-01-29]
410

client-core/pom.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<parent>
2929
<artifactId>influxdb-client</artifactId>
3030
<groupId>com.influxdb</groupId>
31-
<version>1.16.0-SNAPSHOT</version>
31+
<version>2.0.0-SNAPSHOT</version>
3232
</parent>
3333

3434
<artifactId>influxdb-client-core</artifactId>
@@ -116,11 +116,6 @@
116116
<artifactId>commons-csv</artifactId>
117117
</dependency>
118118

119-
<dependency>
120-
<groupId>org.json</groupId>
121-
<artifactId>json</artifactId>
122-
</dependency>
123-
124119
<dependency>
125120
<groupId>com.squareup.okhttp3</groupId>
126121
<artifactId>mockwebserver</artifactId>
@@ -145,6 +140,12 @@
145140
<scope>test</scope>
146141
</dependency>
147142

143+
<dependency>
144+
<groupId>com.google.code.gson</groupId>
145+
<artifactId>gson</artifactId>
146+
</dependency>
147+
148+
148149
</dependencies>
149150

150151
</project>

client-core/src/main/java/com/influxdb/exceptions/InfluxException.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
*/
2222
package com.influxdb.exceptions;
2323

24+
import java.util.HashMap;
25+
import java.util.Map;
2426
import java.util.logging.Level;
2527
import java.util.logging.Logger;
2628
import java.util.stream.Stream;
2729
import javax.annotation.Nonnull;
2830
import javax.annotation.Nullable;
2931

32+
import com.google.gson.Gson;
33+
import com.google.gson.reflect.TypeToken;
3034
import okhttp3.ResponseBody;
31-
import org.json.JSONObject;
3235
import retrofit2.HttpException;
3336
import retrofit2.Response;
3437

@@ -45,7 +48,7 @@ public class InfluxException extends RuntimeException {
4548
private final Response<?> response;
4649
private final String message;
4750

48-
private JSONObject errorBody = new JSONObject();
51+
private Map<String, Object> errorBody = new HashMap<>();
4952

5053
public InfluxException(@Nullable final String message) {
5154

@@ -118,22 +121,23 @@ public int status() {
118121
* @return a response body
119122
*/
120123
@Nonnull
121-
public JSONObject errorBody() {
122-
124+
public Map<String, Object> errorBody() {
123125
return errorBody;
124126
}
125127

126128
@Nullable
127129
private String messageFromResponse() {
128-
129130
if (response != null) {
130-
131131
try {
132132
ResponseBody body = response.errorBody();
133133
if (body != null) {
134-
errorBody = new JSONObject(body.source().readUtf8());
135-
if (errorBody.has("message")) {
136-
return errorBody.getString("message");
134+
String json = body.source().readUtf8();
135+
if (!json.isEmpty()) {
136+
errorBody = new Gson().fromJson(json, new TypeToken<Map<String, Object>>() {
137+
}.getType());
138+
if (errorBody.containsKey("message")) {
139+
return errorBody.get("message").toString();
140+
}
137141
}
138142
}
139143
} catch (Exception e) {

client-core/src/main/java/com/influxdb/internal/AbstractQueryApi.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
package com.influxdb.internal;
2323

2424
import java.io.IOException;
25+
import java.util.HashMap;
26+
import java.util.Map;
2527
import java.util.function.BiConsumer;
2628
import java.util.function.Consumer;
2729
import java.util.logging.Level;
@@ -35,11 +37,13 @@
3537
import com.influxdb.query.internal.FluxCsvParser;
3638
import com.influxdb.query.internal.FluxResultMapper;
3739

40+
import com.google.gson.Gson;
41+
import com.google.gson.GsonBuilder;
42+
import com.google.gson.JsonElement;
43+
import com.google.gson.JsonObject;
3844
import okhttp3.RequestBody;
3945
import okhttp3.ResponseBody;
4046
import okio.BufferedSource;
41-
import org.json.JSONArray;
42-
import org.json.JSONObject;
4347
import retrofit2.Call;
4448
import retrofit2.Callback;
4549
import retrofit2.Response;
@@ -57,15 +61,18 @@ public abstract class AbstractQueryApi extends AbstractRestClient {
5761
protected static final Runnable EMPTY_ACTION = () -> {
5862

5963
};
64+
protected static final String DEFAULT_DIALECT;
65+
static {
66+
Map<String, Object> dialect = new HashMap<>();
67+
dialect.put("header", true);
68+
dialect.put("delimiter", ",");
69+
dialect.put("quoteChar", "\"");
70+
dialect.put("commentPrefix", "#");
71+
dialect.put("annotations", new String[]{"datatype", "group", "default"});
72+
DEFAULT_DIALECT = new GsonBuilder().create().toJson(dialect);
73+
}
6074

61-
protected static final JSONObject DEFAULT_DIALECT = new JSONObject()
62-
.put("header", true)
63-
.put("delimiter", ",")
64-
.put("quoteChar", "\"")
65-
.put("commentPrefix", "#")
66-
.put("annotations", new JSONArray().put("datatype").put("group").put("default"));
67-
68-
protected static final Consumer<Throwable> ERROR_CONSUMER = throwable -> {
75+
protected static final Consumer<Throwable> ERROR_CONSUMER = throwable -> {
6976
if (throwable instanceof InfluxException) {
7077
throw (InfluxException) throwable;
7178
} else {
@@ -77,11 +84,13 @@ public abstract class AbstractQueryApi extends AbstractRestClient {
7784
protected RequestBody createBody(@Nullable final String dialect, @Nonnull final String query) {
7885

7986
Arguments.checkNonEmpty(query, "Flux query");
80-
JSONObject json = new JSONObject()
81-
.put("query", query);
87+
88+
JsonObject json = new JsonObject();
89+
json.addProperty("query", query);
8290

8391
if (dialect != null && !dialect.isEmpty()) {
84-
json.put("dialect", new JSONObject(dialect));
92+
JsonElement dialectJson = new Gson().fromJson(dialect, JsonElement.class);
93+
json.add("dialect", dialectJson);
8594
}
8695

8796
return createBody(json.toString());

client-core/src/test/java/com/influxdb/exceptions/InfluxExceptionTest.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
*/
2222
package com.influxdb.exceptions;
2323

24+
import java.util.Map;
2425
import java.util.function.Predicate;
2526
import javax.annotation.Nonnull;
2627
import javax.annotation.Nullable;
2728

29+
import com.google.gson.JsonObject;
2830
import okhttp3.MediaType;
2931
import okhttp3.Protocol;
3032
import okhttp3.Request;
3133
import okhttp3.ResponseBody;
3234
import org.assertj.core.api.Assertions;
33-
import org.json.JSONObject;
3435
import org.junit.jupiter.api.Test;
3536
import org.junit.platform.runner.JUnitPlatform;
3637
import org.junit.runner.RunWith;
@@ -160,7 +161,7 @@ void errorBody() {
160161
Response<Object> response = errorResponse("Wrong query", 501, 15, "{\"error\": \"error-body\"}");
161162
throw new InfluxException(new HttpException(response));
162163
})
163-
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().toString().equals("{\"error\":\"error-body\"}"));
164+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().get("error").equals("error-body"));
164165
}
165166

166167
@Test
@@ -201,9 +202,10 @@ void errorBodyReadAgain() {
201202
throw new InfluxException(new HttpException(response));
202203
})
203204
.matches((Predicate<Throwable>) throwable -> {
204-
JSONObject errorBody1 = ((InfluxException) throwable).errorBody();
205-
JSONObject errorBody2 = ((InfluxException) throwable).errorBody();
206-
return errorBody1.toString().equals("{\"error\":\"error-body\"}") && errorBody1.equals(errorBody2);
205+
Map errorBody1 = ((InfluxException) throwable).errorBody();
206+
Map errorBody2 = ((InfluxException) throwable).errorBody();
207+
return errorBody1.get("error").equals("error-body")
208+
&& errorBody1.equals(errorBody2);
207209
});
208210
}
209211

@@ -214,7 +216,7 @@ void errorBodyResponseNull() {
214216
.assertThatThrownBy(() -> {
215217
throw new InfluxException(new IllegalStateException("unExpectedError"));
216218
})
217-
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().toString().equals("{}"));
219+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().isEmpty());
218220
}
219221

220222
@Test
@@ -224,7 +226,7 @@ void errorBodyResponseWithoutBody() {
224226
.assertThatThrownBy(() -> {
225227
throw new InfluxException(new HttpException(errorResponse("Wrong query")));
226228
})
227-
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().toString().equals("{}"));
229+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().isEmpty());
228230
}
229231

230232
@Test
@@ -237,7 +239,7 @@ void message() {
237239
.hasMessage("Wrong query")
238240
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).status() == 0)
239241
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).reference() == 0)
240-
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().toString().equals("{}"));
242+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().isEmpty());
241243

242244
}
243245

@@ -251,7 +253,7 @@ void messageNull() {
251253
.hasMessage(null)
252254
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).status() == 0)
253255
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).reference() == 0)
254-
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().toString().equals("{}"));
256+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().isEmpty());
255257

256258
}
257259

@@ -264,7 +266,7 @@ void nullResponse() {
264266
.hasMessage(null)
265267
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).status() == 0)
266268
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).reference() == 0)
267-
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().toString().equals("{}"));
269+
.matches((Predicate<Throwable>) throwable -> ((InfluxException) throwable).errorBody().isEmpty());
268270
}
269271

270272
@Nonnull

client-core/src/test/java/com/influxdb/internal/QueryAbstractApiTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.function.BiConsumer;
2828
import javax.annotation.Nonnull;
2929

30+
import com.google.gson.JsonParser;
3031
import com.influxdb.Cancellable;
3132
import com.influxdb.exceptions.InfluxException;
3233
import com.influxdb.query.internal.FluxCsvParser;
@@ -90,7 +91,9 @@ void createBodyWithDialect() throws IOException {
9091
Buffer buffer = new Buffer();
9192
body.writeTo(buffer);
9293

93-
Assertions.assertThat(buffer.readUtf8()).isEqualTo("{\"dialect\":{\"header\":false},\"query\":\"from(bucket:\\\"telegraf\\\")\"}");
94+
JsonParser parser = new JsonParser();
95+
Assertions.assertThat(parser.parse(buffer.readUtf8()))
96+
.isEqualTo(parser.parse("{\"dialect\":{\"header\":false},\"query\":\"from(bucket:\\\"telegraf\\\")\"}"));
9497
}
9598

9699
@Test
@@ -107,13 +110,14 @@ void createBodyEmptyDialect() throws IOException {
107110
@Test
108111
void createBodyDefaultDialect() throws IOException {
109112

110-
RequestBody body = queryClient.createBody(AbstractQueryApi.DEFAULT_DIALECT.toString(), "from(bucket:\"telegraf\")");
113+
RequestBody body = queryClient.createBody(AbstractQueryApi.DEFAULT_DIALECT, "from(bucket:\"telegraf\")");
111114

112115
Buffer buffer = new Buffer();
113116
body.writeTo(buffer);
114117

118+
JsonParser parser = new JsonParser();
115119
String expected = "{\"dialect\":{\"quoteChar\":\"\\\"\",\"commentPrefix\":\"#\",\"delimiter\":\",\",\"header\":true,\"annotations\":[\"datatype\",\"group\",\"default\"]},\"query\":\"from(bucket:\\\"telegraf\\\")\"}";
116-
Assertions.assertThat(buffer.readUtf8()).isEqualTo(expected);
120+
Assertions.assertThat(parser.parse(buffer.readUtf8())).isEqualTo(parser.parse(expected));
117121
}
118122

119123
@Test

client-kotlin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<parent>
2727
<artifactId>influxdb-client</artifactId>
2828
<groupId>com.influxdb</groupId>
29-
<version>1.16.0-SNAPSHOT</version>
29+
<version>2.0.0-SNAPSHOT</version>
3030
</parent>
3131
<modelVersion>4.0.0</modelVersion>
3232

client-legacy/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<parent>
2929
<groupId>com.influxdb</groupId>
3030
<artifactId>influxdb-client</artifactId>
31-
<version>1.16.0-SNAPSHOT</version>
31+
<version>2.0.0-SNAPSHOT</version>
3232
</parent>
3333

3434
<artifactId>influxdb-client-flux</artifactId>

client-legacy/src/main/java/com/influxdb/client/flux/internal/FluxApiImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public List<FluxTable> query(@Nonnull final String query) {
9898

9999
FluxResponseConsumerTable consumer = fluxCsvParser.new FluxResponseConsumerTable();
100100

101-
query(query, DEFAULT_DIALECT.toString(), consumer, ERROR_CONSUMER, EMPTY_ACTION, false);
101+
query(query, DEFAULT_DIALECT, consumer, ERROR_CONSUMER, EMPTY_ACTION, false);
102102

103103
return consumer.getTables();
104104
}
@@ -130,7 +130,7 @@ public void accept(final int index,
130130
}
131131
};
132132

133-
query(query, DEFAULT_DIALECT.toString(), consumer, ERROR_CONSUMER, EMPTY_ACTION, false);
133+
query(query, DEFAULT_DIALECT, consumer, ERROR_CONSUMER, EMPTY_ACTION, false);
134134

135135
return measurements;
136136
}
@@ -209,7 +209,7 @@ public void accept(final int index,
209209
}
210210
};
211211

212-
query(query, DEFAULT_DIALECT.toString(), consumer, onError, onComplete, true);
212+
query(query, DEFAULT_DIALECT, consumer, onError, onComplete, true);
213213
}
214214

215215

@@ -246,7 +246,7 @@ public void accept(final int index,
246246
}
247247
};
248248

249-
query(query, DEFAULT_DIALECT.toString(), consumer, onError, onComplete, true);
249+
query(query, DEFAULT_DIALECT, consumer, onError, onComplete, true);
250250

251251
}
252252

client-reactive/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<parent>
2727
<artifactId>influxdb-client</artifactId>
2828
<groupId>com.influxdb</groupId>
29-
<version>1.16.0-SNAPSHOT</version>
29+
<version>2.0.0-SNAPSHOT</version>
3030
</parent>
3131
<modelVersion>4.0.0</modelVersion>
3232

0 commit comments

Comments
 (0)