Commit e07aa6a9 authored by Matija Obreza's avatar Matija Obreza
Browse files

RestController returns HTTP 400 status code on Exception, REST API

/upsert throws exception for invalid data type in JSON
parent f406d4f9
......@@ -153,6 +153,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = accnJson.get("mlsStat");
if (value != null) {
if (!value.isNull() && !value.isBoolean())
throw new RESTApiDataTypeException("If provided, 'mlsStat' must be a boolean");
Boolean inMls = value.isNull() ? null : value.asBoolean();
if (!areEqual(inMls, accession.getMlsStatus())) {
......@@ -162,6 +164,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = accnJson.get("inTrust");
if (value != null) {
if (!value.isNull() && !value.isBoolean())
throw new RESTApiDataTypeException("If provided, 'inTrust' must be a boolean");
Boolean inTrust = value.isNull() ? null : value.asBoolean();
if (!areEqual(inTrust, accession.getInTrust())) {
......@@ -171,6 +175,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = accnJson.get("available");
if (value != null) {
if (!value.isNull() && !value.isBoolean())
throw new RESTApiDataTypeException("If provided, 'available' must be a boolean");
Boolean availability = value.isNull() ? null : value.asBoolean();
if (!areEqual(availability, accession.getAvailability())) {
......@@ -202,6 +208,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
value = accnJson.get("sampStat");
if (value != null) {
if (!value.isNull() && !value.isNumber())
throw new RESTApiDataTypeException("If provided, 'sampStat' must be a number");
Integer sampStat = value.isNull() || !value.isNumber() ? null : value.asInt();
if (!areEqual(sampStat, accession.getSampleStatus())) {
accession.setSampleStatus(sampStat);
......@@ -236,6 +244,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = collecting.get("collSrc");
if (value != null) {
if (!value.isNull() && !value.isNumber())
throw new RESTApiDataTypeException("If provided, 'collSrc' must be a number");
accnColl.setCollSrc(value.isNumber() ? value.intValue() : null);
}
value = collecting.get("collCode");
......@@ -271,27 +281,35 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = geo.get("latitude");
if (value != null) {
if (!value.isNull() && !value.isNumber())
throw new RESTApiDataTypeException("If provided, 'latitude' must be a number");
accnGeo.setLatitude(value.isNumber() ? value.asDouble() : null);
}
value = geo.get("longitude");
if (value != null) {
if (!value.isNull() && !value.isNumber())
throw new RESTApiDataTypeException("If provided, 'longitude' must be a number");
accnGeo.setLongitude(value.isNumber() ? value.asDouble() : null);
}
value = geo.get("elevation");
if (value != null) {
if (!value.isNull() && !value.isNumber())
throw new RESTApiDataTypeException("If provided, 'elevation' must be a number");
accnGeo.setElevation(value.isNumber() ? value.asDouble() : null);
}
value = geo.get("coordUncert");
if (value != null) {
if (!value.isNull() && !value.isNumber())
throw new RESTApiDataTypeException("If provided, 'coordUncert' must be a number");
accnGeo.setUncertainty(value.isNumber() ? value.asDouble() : null);
}
value = geo.get("coordDatum");
if (value != null) {
accnGeo.setDatum(value.isTextual() ? value.asText() : null);
accnGeo.setDatum(StringUtils.defaultIfBlank(value.textValue(), null));
}
value = geo.get("georefMeth");
if (value != null) {
accnGeo.setMethod(value.isTextual() ? value.asText() : null);
accnGeo.setMethod(StringUtils.defaultIfBlank(value.textValue(), null));
}
toSaveGeo.add(accnGeo);
}
......@@ -308,7 +326,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = accnJson.get("ancest");
if (value != null) {
accnBred.setPedigree(value.isTextual() ? value.asText() : null);
accnBred.setPedigree(StringUtils.defaultIfBlank(value.textValue(), null));
}
toSaveBreed.add(accnBred);
}
......@@ -321,7 +339,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = accnJson.get("donorCode");
if (value != null) {
accnExch.setDonorInstitute(value.isTextual() ? value.asText() : null);
accnExch.setDonorInstitute(StringUtils.defaultIfBlank(value.textValue(), null));
}
value = accnJson.get("donorNumb");
if (value != null) {
......@@ -329,7 +347,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
value = accnJson.get("donorName");
if (value != null) {
accnExch.setDonorName(value.isTextual() ? value.asText() : null);
accnExch.setDonorName(StringUtils.defaultIfBlank(value.textValue(), null));
}
ArrayNode donorNumb = accnJson.arrayNode();
......@@ -463,9 +481,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
current.setSubtaxa(stringIfProvided(accnJson.get("subtaxa"), current.getSubtaxa()));
current.setSubtAuthor(stringIfProvided(accnJson.get("subtauthor"), current.getSubtAuthor()));
Taxonomy2 ensuredTaxonomy = taxonomyService.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(),
current.getSubtaxa(), current.getSubtAuthor());
Taxonomy2 ensuredTaxonomy = taxonomyService.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
if (!(ensuredTaxonomy.sameAs(taxonomy))) {
accession.setTaxonomy(ensuredTaxonomy);
......@@ -492,7 +510,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
// We expect a String node
throw new RESTApiDataTypeException("Not a String");
}
if (jsonNode.isNull()) return null;
if (jsonNode.isNull())
return null;
return StringUtils.defaultIfBlank(jsonNode.textValue(), currentValue);
}
return currentValue;
......@@ -521,8 +540,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
* @param accnJson
* @param key
* @return
* @throws RESTApiDataTypeException
*/
private ArrayNode toMcpdArray(ObjectNode accnJson, String key) {
private ArrayNode toMcpdArray(ObjectNode accnJson, String key) throws RESTApiDataTypeException {
JsonNode value = accnJson.get(key);
if (value == null || value.isNull()) {
......@@ -537,9 +557,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
arr.add(s.trim());
}
return arr;
} else {
throw new RESTApiDataTypeException("If provided, '" + key + "' must be an array");
}
return null;
}
/**
......@@ -561,7 +581,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
String val = item.textValue();
if (val.contains(":")) {
String[] s = val.split(":", 2);
alias.instCode = s[0].trim();
alias.usedBy = s[0].trim();
alias.name = s[1].trim();
} else {
alias.name = item.textValue().trim();
......@@ -665,6 +685,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
accessionAlias.setName(aa.name);
accessionAlias.setInstCode(aa.instCode);
accessionAlias.setUsedBy(aa.usedBy);
accessionAlias.setAliasType(aliasType != null ? aliasType : AliasType.getType(aa.type));
toSave.add(accessionAlias);
......
......@@ -55,6 +55,7 @@ public abstract class RestController {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LOG.warn(request.getRequestURI() + " " + ex.getMessage() + " for "
+ (authentication != null ? ((AuthUserDetails) authentication.getPrincipal()).getUsername() : "null"));
response.setStatus(404); // ?
return new ExceptionJson(ex);
}
......@@ -65,6 +66,7 @@ public abstract class RestController {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LOG.warn(request.getRequestURI() + " " + ex.getMessage() + " for "
+ (authentication != null ? ((AuthUserDetails) authentication.getPrincipal()).getUsername() : "null"));
response.setStatus(400); // ?
return new ExceptionJson(ex);
}
......@@ -74,6 +76,7 @@ public abstract class RestController {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LOG.warn(request.getRequestURI() + " " + ex.getMessage() + " for "
+ (authentication != null ? ((AuthUserDetails) authentication.getPrincipal()).getUsername() : "null"));
response.setStatus(400); // ?
return new ExceptionJson(ex);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment