title: Proper way of adding resources dynamically to CoAP server
body: <p>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 <strong>coapendpoints/home/room1/sensor1</strong> is the one which needs to be added. </p>
  
  <pre><code>    // endpoint--&gt; coapendpoints/home/room1/sensor1
      String[] resources = endpoint.split("/");
      Resource childResource = coapServer.getRoot().getChild(resources[0]);
      Resource parentResource = childResource;
      if (parentResource == null &amp;&amp; (resources.length &gt; 1)) {
          coapServer.add(new CoAPResourcePath(resources));
      }
      if (parentResource == null &amp;&amp; (resources.length == 1)) {
          coapServer.add(new CoAPResourceServlet(resources[0]));
      } else if (parentResource != null &amp;&amp; resources.length &gt; 1) {
          int j = 1;
          for (; j &lt; 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 &gt; 1) {
              parentResource.add(new CoAPResourcePath(coapEndpoint));
          } else if (coapEndpoint.length == 1) {
              parentResource.add(new CoAPResourceServlet(coapEndpoint[0]));
          }
      } else if (parentResource != null &amp;&amp; resources.length == 1) {
          parentResource.add(new CoAPResourceServlet(resources[0]));
      }
  </code></pre>
  
  <p>Here CoAPResourcePath and CoAPResourceServlet classes are extended from CoapResource.</p>
  
  <p>This is the output after adding resources in this way.<br>
  <a href="https://i.stack.imgur.com/YtsDA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YtsDA.png" alt="enter image description here"></a></p>