Skip to content

Commit

Permalink
- IoT mockup Application (used in the NGSI emulator) has been equippe…
Browse files Browse the repository at this point in the history
…d also for NGSI-9 notifications
  • Loading branch information
flaviocirillo committed Jun 28, 2017
1 parent 7861874 commit c1d4f01
Showing 1 changed file with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@

import eu.neclab.iotplatform.iotbroker.commons.ContentType;
import eu.neclab.iotplatform.ngsi.api.datamodel.NgsiStructure;
import eu.neclab.iotplatform.ngsi.api.datamodel.NotifyContextAvailabilityRequest;
import eu.neclab.iotplatform.ngsi.api.datamodel.NotifyContextRequest;
import eu.neclab.iotplatform.ngsi.api.datamodel.NotifyContextResponse;
import eu.neclab.iotplatform.ngsi.api.datamodel.StatusCode;
import eu.neclab.iotplatform.ngsiemulator.utils.HeaderExtractor;

@Path("ngsi10")
@Path("")
public class IoTApplication {

// The logger
Expand All @@ -89,7 +90,7 @@ public class IoTApplication {
ContentType.XML);

@GET
@Path("/test")
@Path("/ngsi10/test")
@Produces("application/xml")
public String test() {

Expand All @@ -98,7 +99,7 @@ public String test() {
}

@POST
@Path("/testPost")
@Path("/ngsi10/testPost")
@Produces("application/json")
public String testPost(@Context HttpHeaders headers,
@Context ResourceConfig config, String body) {
Expand All @@ -108,7 +109,7 @@ public String testPost(@Context HttpHeaders headers,
}

@POST
@Path("/notify")
@Path("/ngsi10/notify")
@Consumes("application/json,application/xml")
@Produces("application/json,application/xml")
public String notifyResp(@Context HttpHeaders headers,
Expand All @@ -117,15 +118,28 @@ public String notifyResp(@Context HttpHeaders headers,
// Get the accepted content type
final ContentType outgoingContentType = HeaderExtractor.getAccept(
headers, defaultOutgoingContentType);
final ContentType incomingContentType = HeaderExtractor.getContentType(
headers, defaultOutgoingContentType);

NotifyContextResponse response = new NotifyContextResponse();
logger.info("Received a NGSI-10 Notification");
if (logger.isDebugEnabled()) {
logger.debug("Received a NGSI-10 Notification:" + body);
}

NotifyContextRequest notification = (NotifyContextRequest) NgsiStructure
.parseStringToJson(body, NotifyContextRequest.class);
if (logger.isDebugEnabled()) {
NotifyContextRequest notification;
if (incomingContentType == ContentType.JSON) {
notification = (NotifyContextRequest) NgsiStructure
.parseStringToJson(body, NotifyContextRequest.class);
} else {
notification = (NotifyContextRequest) NgsiStructure
.convertStringToXml(body, NotifyContextRequest.class);
}
logger.debug("Parsed NGSI-10 Notification:"
+ notification.toJsonString());

}

response.setResponseCode(new StatusCode(200, "OK", null));

Expand All @@ -134,7 +148,51 @@ public String notifyResp(@Context HttpHeaders headers,
} else {
return response.toJsonString();
}
}

@POST
@Path("/ngsi9/notify")
@Consumes("application/json,application/xml")
@Produces("application/json,application/xml")
public String notifyAvailablityResp(@Context HttpHeaders headers,
@Context ResourceConfig config, String body) {

// Get the accepted content type
final ContentType outgoingContentType = HeaderExtractor.getAccept(
headers, defaultOutgoingContentType);
final ContentType incomingContentType = HeaderExtractor.getContentType(
headers, defaultOutgoingContentType);

NotifyContextResponse response = new NotifyContextResponse();
logger.info("Received a NGSI-9 Notification");
if (logger.isDebugEnabled()) {
logger.debug("Received a NGSI-9 Notification:" + body);
}

if (logger.isDebugEnabled()) {
NotifyContextAvailabilityRequest notification;

if (incomingContentType == ContentType.JSON) {
notification = (NotifyContextAvailabilityRequest) NgsiStructure
.parseStringToJson(body,
NotifyContextAvailabilityRequest.class);
} else {
notification = (NotifyContextAvailabilityRequest) NgsiStructure
.convertStringToXml(body,
NotifyContextAvailabilityRequest.class);
}
logger.debug("Parsed NGSI-9 Notification:"
+ notification.toJsonString());

}

response.setResponseCode(new StatusCode(200, "OK", null));

if (outgoingContentType == ContentType.XML) {
return response.toString();
} else {
return response.toJsonString();
}
}

}

0 comments on commit c1d4f01

Please sign in to comment.