Skip to content

Commit

Permalink
Merge pull request #26 from IN-CORE/22-disable-passing-id-on-post-calls
Browse files Browse the repository at this point in the history
#22: Disable setting an id during eq creation
  • Loading branch information
navarroc committed Oct 22, 2021
2 parents 36472f9 + a2493fe commit 7866d45
Show file tree
Hide file tree
Showing 24 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ public Dataset ingestDataset(@ApiParam(value = "JSON representing an input datas
throw new BadRequestException("Invalid input dataset. Please verify that the dataset does not contain any wrong parameters.");
}

if (JsonUtils.extractValueFromJsonString("id", inDatasetJson) != ""){
throw new IncoreHTTPException(Response.Status.BAD_REQUEST, "Ids are auto-generated by the system. " +
"Setting an id is not allowed");
}

String title = "";
String dataType = "";
String sourceDataset = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Dataset {
*/
@Id
@Property("_id")
private ObjectId id = new ObjectId();
private ObjectId id;

/**
* Should the bean be assumed to be deleted and not be returned
Expand Down Expand Up @@ -406,18 +406,8 @@ public String toString() {
*
* @return id of the bean
*/
public final String getId() {
return id.toString();
}

/**
* Sets the id of the bean. This has to be a unique id since it is used as
* the key in the database.
*
* @param id the id of the object.
*/
public void setId(String id) {
this.id = new ObjectId(id);
public String getId() {
return (id == null) ? null : id.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public void testIngestDataset() throws IOException {
"{\"preferred_username\": \"test\"}").post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
Dataset output = response.readEntity(Dataset.class);

assertNotNull(output.getId());
assertNotNull(output.getTitle());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public List<FragilitySet> getFragilities(@ApiParam(value = "demand type filter",
@Produces({MediaType.APPLICATION_JSON})
@ApiOperation(value = "Create a fragility set", notes = "Post a fragility set to the dfr3 service")
public FragilitySet uploadFragilitySet(@ApiParam(value = "json representing the fragility set") FragilitySet fragilitySet) {

UserInfoUtils.throwExceptionIfIdPresent(fragilitySet.getId());
fragilitySet.setCreator(username);
String fragilityId = this.fragilityDAO.saveFragility(fragilitySet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public MappingSet getMappingSetById(@ApiParam(value = "mapping id", example = "5
"DFR3 object sets")
public MappingSet uploadMapping(@ApiParam(value = "json representing the fragility mapping") MappingSet mappingSet) {

UserInfoUtils.throwExceptionIfIdPresent(mappingSet.getId());
List<Mapping> mappings = mappingSet.getMappings();
int idx = 0;
String prevRuleClassName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public List<RepairSet> getRepairs(@ApiParam(value = "hazard type filter", examp
@Produces({MediaType.APPLICATION_JSON})
@ApiOperation(value = "Create a repair set", notes = "Post a repair set to the repair service")
public RepairSet uploadRepairSet(@ApiParam(value = "json representing the repair set") RepairSet repairSet) {

UserInfoUtils.throwExceptionIfIdPresent(repairSet.getId());
repairSet.setCreator(username);
String repairId = this.repairDAO.saveRepair(repairSet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ public List<RestorationSet> getRestorations(@ApiParam(value = "hazard type filt
@Produces({MediaType.APPLICATION_JSON})
@ApiOperation(value = "Create a restoration set", notes = "Post a restoration set to the restoration service")
public RestorationSet uploadRestorationSet(@ApiParam(value = "json representing the restoration set") RestorationSet restorationSet) {

UserInfoUtils.throwExceptionIfIdPresent(restorationSet.getId());
restorationSet.setCreator(username);
String restorationId = this.restorationDAO.saveRestoration(restorationSet);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import dev.morphia.annotations.Id;
import dev.morphia.annotations.Property;
import org.bson.types.ObjectId;

import javax.xml.bind.annotation.XmlRootElement;
Expand All @@ -12,6 +13,7 @@
@XmlRootElement
public abstract class DFR3Set {
@Id
@Property("_id")
protected ObjectId id;

@XmlTransient
Expand All @@ -38,13 +40,8 @@ public abstract class DFR3Set {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private List<String> spaces;

// region Getters
public String getId() {
if (id == null) {
return null;
} else {
return id.toHexString();
}
return (id == null) ? null : id.toString();
}

public String getLegacyId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ public class MappingSet {
private List<String> spaces;

public String getId() {
if (id == null) {
return null;
} else {
return id.toHexString();
}
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public Earthquake createEarthquake(
Earthquake earthquake = null;
try {
earthquake = mapper.readValue(eqJson, Earthquake.class);
UserInfoUtils.throwExceptionIfIdPresent(earthquake.getId());

// Create temporary working directory
File incoreWorkDirectory = File.createTempFile("incore", ".dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public Flood createFlood(
Flood flood = null;
try {
flood = mapper.readValue(floodJson, Flood.class);
UserInfoUtils.throwExceptionIfIdPresent(flood.getId());

// Create temporary working directory
File incoreWorkDirectory = File.createTempFile("incore", ".dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public Hurricane createHurricane(
Hurricane hurricane = null;
try {
hurricane = mapper.readValue(hurricaneJson, Hurricane.class);
UserInfoUtils.throwExceptionIfIdPresent(hurricane.getId());

// Create temporary working directory
File incoreWorkDirectory = File.createTempFile("incore", ".dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public List<HurricaneWindfields> getHurricaneWindfields(
public HurricaneWindfields createHurricaneWindfields(
HurricaneWindfields inputHurricane) {

UserInfoUtils.throwExceptionIfIdPresent(inputHurricane.getId());

HurricaneWindfields hurricaneWindfields = new HurricaneWindfields();
if (inputHurricane != null) {
String demandType = inputHurricane.getDemandType().trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public Tornado createTornado(
String datasetId = null;
try {
tornado = mapper.readValue(tornadoJson, Tornado.class);
UserInfoUtils.throwExceptionIfIdPresent(tornado.getId());

// TODO verify that parameters like title are not null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public Tsunami createTsunami(
Tsunami tsunami = null;
try {
tsunami = mapper.readValue(tsunamiJson, Tsunami.class);
UserInfoUtils.throwExceptionIfIdPresent(tsunami.getId());

// Create temporary working directory
File incoreWorkDirectory = File.createTempFile("incore", ".dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setDescription(String description) {
}

public String getId() {
return id.toString();
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setDescription(String description) {
}

public String getId() {
return id.toString();
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setDescription(String description) {
}

public String getId() {
return id.toString();
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void setDemandUnits(WindfieldsDemandUnits demandUnits) {
}

public String getId() {
return id.toString();
return (id == null) ? null : id.toString();
}

public List<String> getTimes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setDescription(String description) {
}

public String getId() {
return id.toString();
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void setDescription(String description) {
}

public String getId() {
return id.toString();
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class Space {
@Id
@Property("_id")
private ObjectId id = new ObjectId();
private ObjectId id;

@JsonProperty("metadata")
private SpaceMetadata metadata;
Expand All @@ -54,13 +54,7 @@ public Space(String name) {
}

public String getId() {

return id.toString();
}

public void setId(String id) {

this.id = new ObjectId(id);
return (id == null) ? null : id.toString();
}

public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ public static String getUsername(String userInfo) {
}
}

/***
* Checks if a field "id" exists in the object created from the Json. This will be used to validate the incoming json in POST API calls.
* If present, it should reject the API call because new ids are created by MongoDB, and user shouldn't update the document through a
* POST call.
* @param id
*/
public static void throwExceptionIfIdPresent(String id){
if(id != null){
throw new IncoreHTTPException(Response.Status.BAD_REQUEST, "Ids are auto-generated by the system. " +
"Setting an id is not allowed");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public Space ingestSpace(
ObjectMapper spaceObjectMapper = new ObjectMapper();
try {
Space newSpace = spaceObjectMapper.readValue(spaceJson, Space.class);
UserInfoUtils.throwExceptionIfIdPresent(newSpace.getId());

if (newSpace.getName().equals("")) {
throw new IncoreHTTPException(Response.Status.BAD_REQUEST, "Invalid name");
Expand All @@ -127,6 +128,8 @@ public Space ingestSpace(
} else {
throw new IncoreHTTPException(Response.Status.BAD_REQUEST, "Space already exists with name " + newSpace.getName());
}
} catch (IncoreHTTPException e){
throw e;
} catch (Exception e) {
throw new IncoreHTTPException(Response.Status.BAD_REQUEST, "Invalid space JSON. " + e);
}
Expand Down

0 comments on commit 7866d45

Please sign in to comment.