Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cf-dev] Proper way of adding resources dynamically to CoAP server

Hi All,

I created a method which can be used for adding dynamically URLs into CoAP server.This is the current approach I have implemented. But needs to know is this the proper way of adding resources into the CoAP server? Let's say coapendpoints/home/room1/sensor1 is the one which needs to be added.

// endpoint--> coapendpoints/home/room1/sensor1
    String[] resources = endpoint.split("/");
    Resource childResource = coapServer.getRoot().getChild(resources[0]);
    Resource parentResource = childResource;
    if (parentResource == null && (resources.length > 1)) {
        coapServer.add(new CoAPResourcePath(resources));
    }
    if (parentResource == null && (resources.length == 1)) {
        coapServer.add(new CoAPResourceServlet(resources[0]));
    } else if (parentResource != null && resources.length > 1) {
        int j = 1;
        for (; j < resources.length; j++) {
            if (childResource != null) {
                parentResource = childResource;
                childResource = childResource.getChild(resources[j]);
            } else {
                break;
            }
        }
        String[] coapEndpoint = Arrays.copyOfRange(resources, j - 1, resources.length);
        if (coapEndpoint.length > 1) {
            parentResource.add(new CoAPResourcePath(coapEndpoint));
        } else if (coapEndpoint.length == 1) {
            parentResource.add(new CoAPResourceServlet(coapEndpoint[0]));
        }
    } else if (parentResource != null && resources.length == 1) {
        parentResource.add(new CoAPResourceServlet(resources[0]));
    }

Here CoAPResourcePath and CoAPResourceServlet classes are extended from CoapResource.

This is the output after adding resources in this way.

 Inline image 1 

Note: I just asked this question in  stackoverflow and didn't get an answer. If you guys can explain little bit on this it would be really helpful for me.

Thanks,

Geesara

--
Geesara Prathap Kulathunga
Software Engineer
WSO2 Inc; http://wso2.com
Mobile : +940772684174


Back to the top