@Test public void generate_4_04_on_unknown_resources() { ResourceRegistry reg = new ResourceRegistry(); CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null, new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "test".getBytes()) }, new byte[] {}); CoapMessage resp = reg.respond(msg, null); assertEquals(1, resp.getVersion()); assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode()); assertArrayEquals(new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, resp.getOptions()); assertArrayEquals("not found !".getBytes(), resp.getPayload()); }
public CoapMessage respond(CoapMessage request, IoSession session) { StringBuilder urlBuilder = new StringBuilder(""); for (CoapOption opt : request.getOptions()) { if (opt.getType() == CoapOptionType.URI_PATH) { if (urlBuilder.length() > 0) { urlBuilder.append("/"); } urlBuilder.append(new String(opt.getData())); } } String url = urlBuilder.toString(); LOG.debug("requested URL : {}", url); if (url.length() < 1) { return new CoapMessage(1, MessageType.ACK, CoapCode.BAD_REQUEST.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "no URL !".getBytes()); } if (".well-known/core".equalsIgnoreCase(url)) { return new CoapMessage(1, MessageType.ACK, CoapCode.CONTENT.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 40 }) }, getDiscovery()); } else { ResourceHandler handler = handlers.get(url); if (handler == null) { return new CoapMessage(1, MessageType.ACK, CoapCode.NOT_FOUND.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "not found !".getBytes()); } else { CoapResponse response = handler.handle(request, session); return new CoapMessage(1, request.getType() == MessageType.CONFIRMABLE ? MessageType.ACK : MessageType.NON_CONFIRMABLE, response.getCode(), request.getId(), request.getToken(), response.getOptions(), response.getContent()); } } }
ResourceRegistry { public CoapMessage respond(CoapMessage request, IoSession session) { StringBuilder urlBuilder = new StringBuilder(""); for (CoapOption opt : request.getOptions()) { if (opt.getType() == CoapOptionType.URI_PATH) { if (urlBuilder.length() > 0) { urlBuilder.append("/"); } urlBuilder.append(new String(opt.getData())); } } String url = urlBuilder.toString(); LOG.debug("requested URL : {}", url); if (url.length() < 1) { return new CoapMessage(1, MessageType.ACK, CoapCode.BAD_REQUEST.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "no URL !".getBytes()); } if (".well-known/core".equalsIgnoreCase(url)) { return new CoapMessage(1, MessageType.ACK, CoapCode.CONTENT.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 40 }) }, getDiscovery()); } else { ResourceHandler handler = handlers.get(url); if (handler == null) { return new CoapMessage(1, MessageType.ACK, CoapCode.NOT_FOUND.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "not found !".getBytes()); } else { CoapResponse response = handler.handle(request, session); return new CoapMessage(1, request.getType() == MessageType.CONFIRMABLE ? MessageType.ACK : MessageType.NON_CONFIRMABLE, response.getCode(), request.getId(), request.getToken(), response.getOptions(), response.getContent()); } } } }
ResourceRegistry { public CoapMessage respond(CoapMessage request, IoSession session) { StringBuilder urlBuilder = new StringBuilder(""); for (CoapOption opt : request.getOptions()) { if (opt.getType() == CoapOptionType.URI_PATH) { if (urlBuilder.length() > 0) { urlBuilder.append("/"); } urlBuilder.append(new String(opt.getData())); } } String url = urlBuilder.toString(); LOG.debug("requested URL : {}", url); if (url.length() < 1) { return new CoapMessage(1, MessageType.ACK, CoapCode.BAD_REQUEST.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "no URL !".getBytes()); } if (".well-known/core".equalsIgnoreCase(url)) { return new CoapMessage(1, MessageType.ACK, CoapCode.CONTENT.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 40 }) }, getDiscovery()); } else { ResourceHandler handler = handlers.get(url); if (handler == null) { return new CoapMessage(1, MessageType.ACK, CoapCode.NOT_FOUND.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "not found !".getBytes()); } else { CoapResponse response = handler.handle(request, session); return new CoapMessage(1, request.getType() == MessageType.CONFIRMABLE ? MessageType.ACK : MessageType.NON_CONFIRMABLE, response.getCode(), request.getId(), request.getToken(), response.getOptions(), response.getContent()); } } }  }
ResourceRegistry { public CoapMessage respond(CoapMessage request, IoSession session) { StringBuilder urlBuilder = new StringBuilder(""); for (CoapOption opt : request.getOptions()) { if (opt.getType() == CoapOptionType.URI_PATH) { if (urlBuilder.length() > 0) { urlBuilder.append("/"); } urlBuilder.append(new String(opt.getData())); } } String url = urlBuilder.toString(); LOG.debug("requested URL : {}", url); if (url.length() < 1) { return new CoapMessage(1, MessageType.ACK, CoapCode.BAD_REQUEST.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "no URL !".getBytes()); } if (".well-known/core".equalsIgnoreCase(url)) { return new CoapMessage(1, MessageType.ACK, CoapCode.CONTENT.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 40 }) }, getDiscovery()); } else { ResourceHandler handler = handlers.get(url); if (handler == null) { return new CoapMessage(1, MessageType.ACK, CoapCode.NOT_FOUND.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "not found !".getBytes()); } else { CoapResponse response = handler.handle(request, session); return new CoapMessage(1, request.getType() == MessageType.CONFIRMABLE ? MessageType.ACK : MessageType.NON_CONFIRMABLE, response.getCode(), request.getId(), request.getToken(), response.getOptions(), response.getContent()); } } }  void register(ResourceHandler handler); CoapMessage respond(CoapMessage request, IoSession session); }
ResourceRegistry { public CoapMessage respond(CoapMessage request, IoSession session) { StringBuilder urlBuilder = new StringBuilder(""); for (CoapOption opt : request.getOptions()) { if (opt.getType() == CoapOptionType.URI_PATH) { if (urlBuilder.length() > 0) { urlBuilder.append("/"); } urlBuilder.append(new String(opt.getData())); } } String url = urlBuilder.toString(); LOG.debug("requested URL : {}", url); if (url.length() < 1) { return new CoapMessage(1, MessageType.ACK, CoapCode.BAD_REQUEST.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "no URL !".getBytes()); } if (".well-known/core".equalsIgnoreCase(url)) { return new CoapMessage(1, MessageType.ACK, CoapCode.CONTENT.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 40 }) }, getDiscovery()); } else { ResourceHandler handler = handlers.get(url); if (handler == null) { return new CoapMessage(1, MessageType.ACK, CoapCode.NOT_FOUND.getCode(), request.getId(), request.getToken(), new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) }, "not found !".getBytes()); } else { CoapResponse response = handler.handle(request, session); return new CoapMessage(1, request.getType() == MessageType.CONFIRMABLE ? MessageType.ACK : MessageType.NON_CONFIRMABLE, response.getCode(), request.getId(), request.getToken(), response.getOptions(), response.getContent()); } } }  void register(ResourceHandler handler); CoapMessage respond(CoapMessage request, IoSession session);  }