Skip to content

Commit e014ed9

Browse files
committed
made object converters more strict, extended request builder
1 parent d1290c7 commit e014ed9

File tree

5 files changed

+61
-21
lines changed

5 files changed

+61
-21
lines changed

src/main/java/net/b07z/sepia/server/core/microservices/DbStationResults.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public DbStation(JSONObject data){
121121

122122
this.stationName = (String) data.get("name");
123123
this.stationId = (String) data.get("id");
124-
this.longitude = Converters.obj2Double(data.get("lon"));
125-
this.latitude = Converters.obj2Double(data.get("lat"));
124+
this.longitude = Converters.obj2DoubleOrDefault(data.get("lon"), Double.NEGATIVE_INFINITY); //TODO: is this ok?
125+
this.latitude = Converters.obj2DoubleOrDefault(data.get("lat"), Double.NEGATIVE_INFINITY);
126126
}
127127

128128
@Override
@@ -143,6 +143,9 @@ public double getLongitude(){
143143
return longitude;
144144
}
145145
public double getApproxDistance(double lat, double lng){
146+
if (this.latitude == Double.NEGATIVE_INFINITY || this.longitude == Double.NEGATIVE_INFINITY){
147+
return Double.MAX_VALUE; //TODO: is this ok?
148+
}
146149
try{
147150
return Math.sqrt((Math.pow(lat - latitude, 2) + Math.pow(lng - longitude, 2)));
148151

src/main/java/net/b07z/sepia/server/core/server/FakeRequest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
import org.json.simple.JSONObject;
7+
68
import net.b07z.sepia.server.core.data.Defaults;
9+
import net.b07z.sepia.server.core.tools.Converters;
710
import net.b07z.sepia.server.core.users.Account;
811
import net.b07z.sepia.server.core.users.LocalTestAccount;
912

@@ -16,21 +19,39 @@
1619
*
1720
*/
1821
public class FakeRequest extends Request {
19-
private final Map<String, String> params = new HashMap<>();
22+
private final Map<String, String> params;
2023
private Account fakeAccount;
2124

2225
public FakeRequest(String... params) {
26+
this.params = new HashMap<>();
2327
for (String param : params) {
2428
String[] parts = param.split("=",2);
2529
this.params.put(parts[0], parts[1]);
2630
}
2731
fakeAccount = new LocalTestAccount();
2832
}
33+
public FakeRequest(Map<String, String> params) {
34+
this.params = params;
35+
fakeAccount = new LocalTestAccount();
36+
}
37+
public FakeRequest(JSONObject params) {
38+
this.params = Converters.json2HashMapStrStr(params);
39+
fakeAccount = new LocalTestAccount();
40+
}
2941
public FakeRequest(Account fakeAccount, String... params) {
3042
this(params);
3143
this.fakeAccount = fakeAccount;
3244
}
3345

46+
/**
47+
* Overwrite a certain parameter
48+
* @param key
49+
* @param value
50+
*/
51+
public void overwriteParameter(String key, String value){
52+
this.params.put(key, value);
53+
}
54+
3455
/**
3556
* Set a new account (e.g. LocalTestAccount) as this request's account.<br>
3657
* Note: use attribute(Defaults.ACCOUNT_ATTR) to get account back.

src/main/java/net/b07z/sepia/server/core/tools/Converters.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static String url_utf8(String in){
9898
* @return rounded double as string or empty
9999
*/
100100
public static String smartRound(Object val, boolean round_to_int){
101-
double v = obj2Double(val);
101+
double v = obj2DoubleOrDefault(val, Double.NEGATIVE_INFINITY);
102102
if (v == Double.NEGATIVE_INFINITY){
103103
System.err.println(DateTime.getLogDate() + " ERROR - Converters.java smartRound(..) FAILED! with: " + val.toString());
104104
return "";
@@ -124,52 +124,56 @@ public static String smartRound(Object val, boolean round_to_int){
124124
}
125125

126126
/**
127-
* Convert a string to a long. If it fails the long will be -1. Best used for System.time checks.
128-
* @param in - string in long-format to convert
129-
* @return long value or -1. If -1 is important to you DON'T use this method
127+
* Convert an unknown object to String or return null.
128+
* @param in - object to convert
129+
* @param def - default
130+
* @return String or default
130131
*/
131-
public static long str2Long(String in){
132+
public static String obj2StringOrDefault(Object in, String def){
132133
try {
133-
return Long.parseLong(in);
134+
return in.toString();
134135
} catch (Exception e){
135-
return -1;
136+
return def;
136137
}
137138
}
138139
/**
139140
* Convert an unknown object that holds a double (real double or string double) to double.
140141
* @param in - string to convert, must be in double format
141-
* @return double value or NEGATIVE_INFINITY. If NEGATIVE_INFINITY is important to you DON'T use this method
142+
* @param def - default
143+
* @return double value or default
142144
*/
143-
public static double obj2Double(Object in){
145+
public static double obj2DoubleOrDefault(Object in, Double def){
144146
try {
145147
return Double.parseDouble((String.valueOf(in)));
146148
} catch (Exception e){
147-
return Double.NEGATIVE_INFINITY;
149+
return def;
148150
}
149151
}
150152
/**
151153
* Convert an unknown object that holds a double (real double or string double) to long.
152154
* @param in - string to convert, must be in double format
153-
* @return long value or -1. If -1 is important to you DON'T use this method
155+
* @param def - default
156+
* @return long value or default
154157
*/
155-
public static long obj2Long(Object in){
158+
public static long obj2LongOrDefault(Object in, Long def){
156159
try {
157160
return (long) Double.parseDouble((String.valueOf(in)));
158161
} catch (Exception e){
159-
return -1;
162+
return def;
160163
}
161164
}
162165
/**
163166
* Convert an unknown object that holds an integer/double (real type or as string) to an integer.
164167
* Removes all non-numbers (except .,+-) in the string!
165168
* @param in - string to convert, must be in double format
166-
* @return int value or -1. If -1 is important to you DON'T use this method or check if it was there before
169+
* @param def - default
170+
* @return int value or def
167171
*/
168-
public static int obj2Int(Object in){
172+
public static int obj2IntOrDefault(Object in, Integer def){
169173
try {
170174
return (int) Double.parseDouble((String.valueOf(in).replaceAll("[^\\d\\.,\\-\\+]", "")));
171175
} catch (Exception e){
172-
return -1;
176+
return def;
173177
}
174178
}
175179

@@ -372,6 +376,18 @@ public static Map<String, String> json2HashMapStrStr(JSONObject jsonObject) {
372376
public static Map<String, Object> json2HashMap(JSONObject jsonObject) {
373377
return object2HashMapStrObj(jsonObject);
374378
}
379+
/**
380+
* Add content of JSONObject to a Map converting all values to String.
381+
* @param jsonSource - source JSONObject
382+
* @param targetMap - target Map (non null!)
383+
*/
384+
@SuppressWarnings("unchecked")
385+
public static void addJsonToMapAsStrings(JSONObject jsonSource, Map<String, String> targetMap){
386+
for (Object entry : jsonSource.entrySet()) {
387+
Map.Entry<String, Object> entryObj = (Map.Entry<String, Object>) entry;
388+
targetMap.put(entryObj.getKey(), entryObj.getValue().toString());
389+
}
390+
}
375391

376392
/**
377393
* Makes an unchecked (cause you can't check it, can you?) cast from Object to HashMap&#60String, Object&#62.

src/main/java/net/b07z/sepia/server/core/users/Account.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public void importJSON(JSONObject account){
256256
userId = (String) account.get("userId");
257257
email = (String) account.get("email");
258258
phone = (String) account.get("phone");
259-
accessLevel = Converters.obj2Int(account.get("accessLevel"));
259+
accessLevel = Converters.obj2IntOrDefault(account.get("accessLevel"), -1);
260260
JSONObject uname = (JSONObject) account.get("userName");
261261
if (uname != null){
262262
userName = uname;

src/main/java/net/b07z/sepia/server/core/users/AuthenticationAssistAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public boolean authenticate(JSONObject info) {
8080

8181
//ACCESS LEVEL
8282
//TODO: not yet fully implemented, but should be 0 for access via token, 1 for real password and -1 for no access.
83-
accessLevel = Converters.obj2Int(response.get("access_level"));
83+
accessLevel = Converters.obj2IntOrDefault(response.get("access_level"), -1);
8484

8585
//NAME
8686
JSONObject user_name = (JSONObject) response.get("user_name");

0 commit comments

Comments
 (0)