Skip to content

Commit

Permalink
issues solved and code refractoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Cimmino authored and Andrea Cimmino committed Dec 16, 2021
1 parent 969e794 commit 54eca8f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>es.upm.fi.oeg</groupId>
<artifactId>wothive</artifactId>
<version>0.1.7</version>
<version>0.1.8</version>
<name>Directory for the Web of Things</name>

<properties>
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/directory/things/ThingsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ private static final void prepareListingResponse(Response response, Integer limi

public static final Route registrationUpdate = (Request request, Response response) -> {
String graphId = buildGraphId(request);

String id = request.params("id");
String td = hasValidBody(request.body());
RDFFormat format = hasValidMime(request.headers(Utils.HEADER_CONTENT_TYPE), true);
Boolean exist = false;

if(format.equals(RDFFormat.JSONLD_FRAME_FLAT)) { // Create/Update
exist = ThingsService.registerJsonThing(graphId, td);
exist = ThingsService.registerJsonThing(graphId, id, td);
}else {
exist = ThingsService.registerRDFThing(graphId, format, td);
}
Expand All @@ -110,9 +110,9 @@ private static final void prepareListingResponse(Response response, Integer limi
public static final Route registrationAnonymous = (Request request, Response response) -> {
String td = hasValidBody(request.body());
RDFFormat format = hasValidMime(request.headers(Utils.HEADER_CONTENT_TYPE), true);

String graphID = buildGraphId(request);
if(format.equals(RDFFormat.JSONLD_FRAME_FLAT)) {
String newUUID = ThingsService.registerJsonThingAnonymous(td);
String newUUID = ThingsService.registerJsonThingAnonymous(td, graphID);
response.header(LOCATION_HEADER, newUUID);
}else {
throw new ThingRegistrationException("Things under a different form than application/td+json must be registered using PUT");
Expand All @@ -124,6 +124,8 @@ private static final void prepareListingResponse(Response response, Integer limi

public static final Route partialUpdate = (Request request, Response response) -> {
JsonObject tdJson = null;
String id = request.params("id");

String graphId = buildGraphId(request);
String td = hasValidBody(request.body());
if(!request.headers(Utils.HEADER_CONTENT_TYPE).equals("application/merge-patch+json"))
Expand All @@ -133,7 +135,7 @@ private static final void prepareListingResponse(Response response, Integer limi
}catch(Exception e) {
throw new ThingRegistrationException("Partial updates are only supported for Things under the form of application/td+json, provided update document has syntax errors");
}
ThingsService.updateThingPartially(graphId, tdJson);
ThingsService.updateThingPartially(graphId, id, tdJson);
response.status(204);

return "";
Expand Down Expand Up @@ -189,8 +191,14 @@ private static final RDFFormat hasValidMime(String mime, boolean strict) {

private static final String HTTP_CONSTANT = "http://";
private static final String buildGraphId(Request request) {
return Utils.buildMessage(HTTP_CONSTANT,request.host(),request.pathInfo(),request.params(":id"));
String id = request.params(":id");
if(id!=null) {
return Utils.buildMessage(HTTP_CONSTANT,request.host(),request.pathInfo());
}else {
return Utils.buildMessage(HTTP_CONSTANT,request.host(),request.pathInfo());
}
}



}
6 changes: 5 additions & 1 deletion src/main/java/directory/things/ThingsMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ protected static byte[] thingToStringBytes(Thing thing, RDFFormat format) {
}

// -- JSON factory
public static Thing createJsonThing(String td) {
public static Thing createJsonThing(String td, String id) {
Thing thing = null;
try {
JsonObject thingJson = JTD.parseJson(td);

if (!thingJson.has(ID1) && !thingJson.has(ID2))
throw new ThingRegistrationException(
"Things under the form application/td+json registered using PUT method must provide a valid 'id', otherwhise se the POST method for Things without 'id'");
// todo check if the td id and id are the same
thing = Thing.fromJson(thingJson);
//if(!thing.getId().equals(id))
//throw new ThingRegistrationException("Provided Thing has an ID different from the one specified in the request");

} catch (Exception e) {
throw new ThingParsingException(
Utils.buildMessage("Thing under the form application/td+json has errors, check:", e.toString()));
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/directory/things/ThingsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@ protected static final Thing retrieveThing(String graphId) {
return ThingsDAO.read(graphId);
}

public static final String registerJsonThingAnonymous(String td) {
public static final String registerJsonThingAnonymous(String td, String graphId) {
Thing thing = ThingsMapper.createJsonThingAnonymous(td);
graphId = Utils.buildMessage(graphId,"/",thing.getId());
enrichTD(thing);
ThingsDAO.create(thing, thing.getId(), false);
ThingsDAO.create(thing, graphId, false);
return thing.getId();
}

protected static final Boolean registerJsonThing(String graphId, String td) {
protected static final Boolean registerJsonThing(String graphId, String id, String td) {
Boolean exist = ThingsDAO.exist(graphId);
Thing thing = ThingsMapper.createJsonThing(td);
Thing thing = ThingsMapper.createJsonThing(td, id);
if(exist) {
ThingsDAO.delete(graphId);
markModification(thing);
Expand Down Expand Up @@ -78,12 +79,12 @@ protected static final Boolean registerRDFThing(String graphId, RDFFormat format
}


public static void updateThingPartially(String graphId, JsonObject partialUpdate) {
public static void updateThingPartially(String graphId, String id, JsonObject partialUpdate) {
Thing existingThing = ThingsService.retrieveThing(graphId);
try {
Thing updatedThing = JTD.updateJsonThingPartially(existingThing, partialUpdate);
markModification(updatedThing);
ThingsService.registerJsonThing(graphId, updatedThing.toJson().toString());
ThingsService.registerJsonThing(graphId, id, updatedThing.toJson().toString());
} catch (IOException e) {
throw new ThingParsingException(e.toString());
} catch (JsonPatchException e) {
Expand Down

0 comments on commit 54eca8f

Please sign in to comment.