Skip to content

Commit 64db674

Browse files
committed
update
1 parent 6a78398 commit 64db674

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/main/java/io/ipgeolocation/api/IPGeolocationAPI.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.google.gson.Gson;
99

10+
import static io.ipgeolocation.api.Strings.isJsonString;
1011
import static io.ipgeolocation.api.Strings.isNullOrEmpty;
1112

1213
public class IPGeolocationAPI {
@@ -145,6 +146,9 @@ private Map<String, Object> callAPIEndpoint(String endpoint, String urlParams) {
145146
if(isNullOrEmpty(responseCode) || isNullOrEmpty(jsonString)) {
146147
responseCode = "422";
147148
jsonString = "{\"message\":\"Something went wrong while parsing IP Geolocation API response\"}";
149+
} else if (!isJsonString(jsonString)) {
150+
responseCode = "422";
151+
jsonString = "{\"message\":\"Connection problem or Invalid response by IP Geolocation API\"}";
148152
}
149153
} catch (IOException e) {
150154
responseCode = "422";
@@ -194,7 +198,7 @@ private Map<String, String> parseConnectionResponse(HttpURLConnection connection
194198
return responseMap;
195199
}
196200

197-
private Map<String, Object> convertJSONStringToMap(String responseCode, String response){
201+
private Map<String, Object> convertJSONStringToMap(String responseCode, String response) {
198202
Gson gson = new Gson();
199203
Map<String,Object> map = new LinkedHashMap<String, Object>();
200204

src/main/java/io/ipgeolocation/api/Strings.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.ipgeolocation.api;
22

3+
import com.google.gson.Gson;
4+
35
class Strings {
46

57
static String nullToEmpty(String s) {
@@ -12,4 +14,17 @@ static String nullToEmpty(String s) {
1214
static Boolean isNullOrEmpty(String string) {
1315
return string == null || string.trim().equals("");
1416
}
17+
18+
static Boolean isJsonString(String jsonString) {
19+
Gson gson = new Gson();
20+
try {
21+
Object jsonObjType = gson.fromJson(jsonString, Object.class).getClass();
22+
if(jsonObjType.equals(String.class)){
23+
return false;
24+
}
25+
return true;
26+
} catch (com.google.gson.JsonSyntaxException ex) {
27+
return false;
28+
}
29+
}
1530
}

0 commit comments

Comments
 (0)