String url = urlBuilder.toString();
LOG.debug("requested URL : {}", url);
if (url.length() < 1) {
// 4.00 !
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)) {
// discovery !
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) {
// 4.04 !
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());
}
}
}