Using LDP-CoAP

LDP-CoAP consists of the following sub-projects:

  • ldp-coap-core : basic framework implementation including the proposed LDP-CoAP mapping;
  • californium-core-ldp : a modified version of the Californium CoAP framework. The californium-core library was extended to support LDP features;
  • ldp-coap-proxy : a modified version of the californium-proxy used to translate LDP-HTTP request methods and headers into the corresponding LDP-CoAP ones and then map back LDP-CoAP responses to LDP-HTTP;
  • ldp-coap-raspberry : a project exploiting ldp-coap-core on Raspberry Pi board;
  • ldp-coap-android : a simple project using ldp-coap-core on Android platform;

You can try LDP-CoAP as described in the following examples (also available on github).

Create an LDP-CoAP server with ldp-coap-core

								
final String BASE_URI = "coap://example.org";

CoAPLDPServer server = new CoAPLDPServer(BASE_URI);

/*** Init server settings and LDP-CoAP resources ***/
init();

server.start();
// Server running
server.shutdown();
								
							

Init server settings

								
/*** Handle additional namespaces ***/
server.addHandledNamespace(SSN_XG.PREFIX, SSN_XG.NAMESPACE + "#");

/*** Handle read-only properties ***/
server.setReadOnlyProperty("http://purl.org/dc/terms/created");

/*** Handle not persisted properties ***/
server.setNotPersistedProperty("http://example.org/comment");

/*** Set the URI identifying all server constraints ***/
server.setConstrainedByURI("http://example.org/ldp-report.html");
								
							

Create LDP RDF Source and Non-RDF Source

								
/*** Create LDP RDF Source ***/
CoAPLDPRDFSource src = server.createRDFSource("alice");
src.setRDFTitle("My first CoAP RDFSource");

/*** Create LDP RDF Source with Data Handler ***/
CoAPLDPRDFSource cpu = server.createRDFSource("cpu");
cpu.setDataHandler(new CPUHandler());

/*** Create LDP Non-RDF Source ***/
CoAPLDPNonRDFSource nr = server.createNonRDFSource("hello", MediaTypeRegistry.TEXT_PLAIN);
nr.setData(("Hello World!").getBytes());
								
							

Create LDP Basic Container

								
/*** Create LDP Basic Container ***/
CoAPLDPBasicContainer bc = server.createBasicContainer("sensors");
bc.setRDFTitle("LDP Basic sensors container");
bc.setRDFDescription("This container will contain data of sensors.");

/*** Add LDP-RS to the LDP-BC ***/
CoAPLDPRDFSource cpu = bc.createRDFSource("cpu");
cpu.setDataHandler(new CPUHandler());

/*** Add LDP-RS with an additional rdf:type (SSN_XG.SensorOutput) to the LDP-BC ***/
CoAPLDPRDFSource memory = bc.createRDFSource("memory", SSN_XG.SensorOutput.toString());
memory.setDataHandler(new MemoryHandler());
								
							

Create LDP Direct Container

								
/************************************
* Create LDP Direct Container
*
* Usage of createDirectContainer
* server.createDirectContainer(String ldp_dc_name, String member_res_name, String member_res_type, String membershipRelation, String isMemberOfRelation)
*
************************************/
CoAPLDPDirectContainer dc = server.createDirectContainer("obs-dc", "tempSensor", SSN_XG.SensingDevice.toString(), SSN_XG.observes.toString(), null);
dc.setRDFTitle("Product description of LDP Demo product which is also an LDP-DC");
dc.getMemberResource().setRDFTitle("LDP-DC Member Resource");

/*** LDP-DC also accept POST with Non-RDF Resource (TEXT_PLAIN) ***/
dc.setAcceptPostType(MediaTypeRegistry.TEXT_PLAIN);

/*** Add LDP-RS to the LDP-DC ***/
CoAPLDPRDFSource resDC = dc.createRDFSource("obs1", SSN_XG.Observation.toString());
resDC.setRDFTitle("Single observation.");
								
							

Create LDP Indirect Container

								
/************************************
* Create LDP Indirect Container
*
* server.createIndirectContainer(String ldp_ic_name, String member_res_name, String member_res_type, String membershipRelation,
* 		String insertedContentRelation)
*
************************************/
CoAPLDPIndirectContainer ic = server.createIndirectContainer("obs-ic", "system", SSN_XG.System.toString(), SSN_XG.hasSubSystem.toString(),
		SSN_XG.attachedSystem.toString());
ic.setRDFTitle("LDP-CoAP Indirect Container");
ic.getMemberResource().setRDFTitle("LDP-IC Member Resource");

/*** Add LDP-RS with Derived URI to the LDP-IC ***/
CoAPLDPRDFSource subSystem = ic.createRDFSourceWithDerivedURI("sub-system", SSN_XG.SensorOutput.toString(), BASE_URI + "/sensors/extra/output/out1");
subSystem.setRDFTitle("LDP-IC Resource");
								
							

Implement a specific Data Handler

								
/*** Create a new class extending basic LDPDataHandler  ***/
public class ExtendedDataHandler extends LDPDataHandler {

	/*** Ovveride the method handleData() and insert here all operations useful to update data within the RDF dataset  ***/

	@Override
	protected void handleData() {
		double value = example.readData(); //numeric value to store (e.g., retrieved from a sensor class)
    	mng.updateRDFLiteralStatement(mng.getBaseURI() + resource, SSN_XG.hasValue.toString(), value);
		mng.updateRDFLiteralStatement(mng.getBaseURI() + resource, DCTERMS.CREATED.toString(), new Date());
	}

}