Commits

György Kohut committed 35597f3

shadowserver_geoip: allow empty fields

Comments (0)

Files changed (1)

src/main/java/org/honeynet/hbbackend/shadowserver_geoip/ShadowserverGeoip.java

 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Types;
 import java.util.regex.Pattern;
 
 import javax.annotation.Resource;
 			int i = -1;
 			try {
 				result.cc = handleStringField(responseFields[i=1]);
-				result.city = handleStringField(responseFields[i=4]);
-				result.latitude = handleFloatField(responseFields[i=5]);
-				result.longitude = handleFloatField(responseFields[i=6]);
+				result.city = handleStringField(responseFields[i=4], true);
+				result.latitude = handleFloatField(responseFields[i=5], true);
+				result.longitude = handleFloatField(responseFields[i=6], true);
 			}
 			catch (ValidationException e) {
 				emsg = "invalid response: field " + i + ": " + e.getMessage();
 			pStmt = db.prepareStatement("insert into geoip_shadowserver (ip, cc, city, latitude, longitude) values (inet(?),?,?,?,?) returning id");
 			pStmt.setString(1, ip);
 			pStmt.setString(2, result.cc);
-			pStmt.setString(3, result.city);
-			pStmt.setFloat(4, result.latitude);
-			pStmt.setFloat(5, result.longitude);
+			
+			if (result.city == null)
+				pStmt.setNull(3, Types.VARCHAR);
+			else pStmt.setString(3, result.city);
+			
+			if (result.latitude == null)
+				pStmt.setNull(4, Types.FLOAT);
+			else pStmt.setFloat(4, result.latitude);
+			
+			if (result.longitude == null)
+				pStmt.setNull(5, Types.FLOAT);
+			else pStmt.setFloat(5, result.longitude);
+			
 			queryRes = pStmt.executeQuery();
 			queryRes.next();
 			recordId = queryRes.getLong(1);
 	}
 	
 	
-	private static String handleStringField(String value) throws ValidationException {
+	private static String handleStringField(String value, boolean allowEmpty) throws ValidationException {
 		value = value.trim();
-    	if (value.equals("")) throw new ValidationException("empty field");
+		if (value.equals("")) {
+			if (allowEmpty) {
+				return null;
+			}
+			else {
+				throw new ValidationException("empty field");
+			}
+		}
         return value;
 	}
 	
-	private static float handleFloatField(String value) throws ValidationException {
+	private static String handleStringField(String value) throws ValidationException {
+		return handleStringField(value, false);
+	}
+	
+	private static Float handleFloatField(String value, boolean allowEmpty) throws ValidationException {
 		value = value.trim();
-    	if (value.equals("")) throw new ValidationException("empty field");
+		if (value.equals("")) {
+			if (allowEmpty) {
+				return null;
+			}
+			else {
+				throw new ValidationException("number not parsable");
+			}
+		}
     	try {
     		return Float.parseFloat(value);
     	}
 		}
 	}
 	
+	private static Float handleFloatField(String value) throws ValidationException {
+		return handleFloatField(value, false);
+	}
+	
 	
 	private static class Result {	
 		String cc;
 		String city;
-		float latitude;
-		float longitude;
+		Float latitude;
+		Float longitude;
 		
 		@Override
 		public String toString() {