diff --git a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/request/LwM2mResponseBuilder.java b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/request/LwM2mResponseBuilder.java index 4e675e2d90..8be8a38b13 100644 --- a/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/request/LwM2mResponseBuilder.java +++ b/leshan-server-cf/src/main/java/org/eclipse/leshan/server/californium/request/LwM2mResponseBuilder.java @@ -472,50 +472,59 @@ private boolean isResponseCodeChanged() { private LwM2mNode decodeCoapResponse(LwM2mPath path, Response coapResponse, LwM2mRequest request, String endpoint) { - - // Get content format - ContentFormat contentFormat = null; - if (coapResponse.getOptions().hasContentFormat()) { - contentFormat = ContentFormat.fromCode(coapResponse.getOptions().getContentFormat()); + try { + return decoder.decode(coapResponse.getPayload(), getContentFormat(coapResponse), path, model); + } catch (CodecException e) { + handleCodecException(e, request, coapResponse, endpoint); + return null; // should not happen as handleCodecException raise exception } + } - // Decode payload + private Map decodeCompositeCoapResponse(List paths, Response coapResponse, + LwM2mRequest request, String endpoint) { try { - return decoder.decode(coapResponse.getPayload(), contentFormat, path, model); + return decoder.decodeNodes(coapResponse.getPayload(), getContentFormat(coapResponse), paths, model); } catch (CodecException e) { - if (LOG.isDebugEnabled()) { - byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload(); - LOG.debug( - String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", - request, endpoint, Hex.encodeHexString(payload))); - } - throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", - request, endpoint); + handleCodecException(e, request, coapResponse, endpoint); + return null; // should not happen as handleCodecException raise exception } } - private Map decodeCompositeCoapResponse(List paths, Response coapResponse, + private TimestampedLwM2mNode decodeCoapTimestampedResponse(LwM2mPath path, Response coapResponse, LwM2mRequest request, String endpoint) { + List timestampedNodes = null; + try { + timestampedNodes = decoder.decodeTimestampedData(coapResponse.getPayload(), getContentFormat(coapResponse), + path, model); + if (timestampedNodes.size() != 1) { + throw new InvalidResponseException( + "Unable to decode response payload of request [%s] from client [%s] : should receive only 1 timestamped node but received %s", + request, endpoint, timestampedNodes.size()); + } + return timestampedNodes.get(0); + } catch (CodecException e) { + handleCodecException(e, request, coapResponse, endpoint); + return null; // should not happen as handleCodecException raise exception + } + } - // Get content format + private ContentFormat getContentFormat(Response coapResponse) { ContentFormat contentFormat = null; if (coapResponse.getOptions().hasContentFormat()) { contentFormat = ContentFormat.fromCode(coapResponse.getOptions().getContentFormat()); } + return contentFormat; + } - // Decode payload - try { - return decoder.decodeNodes(coapResponse.getPayload(), contentFormat, paths, model); - } catch (CodecException e) { - if (LOG.isDebugEnabled()) { - byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload(); - LOG.debug( - String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", - request, endpoint, Hex.encodeHexString(payload))); - } - throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", - request, endpoint); + private void handleCodecException(CodecException e, LwM2mRequest request, Response coapResponse, + String endpoint) { + if (LOG.isDebugEnabled()) { + byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload(); + LOG.debug(String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", + request, endpoint, Hex.encodeHexString(payload))); } + throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", + request, endpoint); } @SuppressWarnings("unchecked") @@ -528,33 +537,4 @@ private void handleUnexpectedResponseCode(String clientEndpoint, LwM2mRequest clientEndpoint, coapResponse.getCode(), request); } - private TimestampedLwM2mNode decodeCoapTimestampedResponse(LwM2mPath path, Response coapResponse, - LwM2mRequest request, String endpoint) { - // Get content format - ContentFormat contentFormat = null; - if (coapResponse.getOptions().hasContentFormat()) { - contentFormat = ContentFormat.fromCode(coapResponse.getOptions().getContentFormat()); - } - - // Decode payload - List timestampedNodes = null; - try { - timestampedNodes = decoder.decodeTimestampedData(coapResponse.getPayload(), contentFormat, path, model); - if (timestampedNodes.size() != 1) { - throw new InvalidResponseException( - "Unable to decode response payload of request [%s] from client [%s] : should receive only 1 timestamped not but received %s", - request, endpoint, timestampedNodes.size()); - } - return timestampedNodes.get(0); - } catch (CodecException e) { - if (LOG.isDebugEnabled()) { - byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload(); - LOG.debug( - String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", - request, endpoint, Hex.encodeHexString(payload))); - } - throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", - request, endpoint); - } - } } diff --git a/leshan-tl-javacoap-server/src/main/java/org/eclipse/leshan/transport/javacoap/server/request/LwM2mResponseBuilder.java b/leshan-tl-javacoap-server/src/main/java/org/eclipse/leshan/transport/javacoap/server/request/LwM2mResponseBuilder.java index ae5a1f6160..c207ceafb6 100644 --- a/leshan-tl-javacoap-server/src/main/java/org/eclipse/leshan/transport/javacoap/server/request/LwM2mResponseBuilder.java +++ b/leshan-tl-javacoap-server/src/main/java/org/eclipse/leshan/transport/javacoap/server/request/LwM2mResponseBuilder.java @@ -486,49 +486,62 @@ public static ResponseCode toLwM2mResponseCode(Code coapResponseCode) { private LwM2mNode decodeCoapResponse(LwM2mPath path, CoapResponse coapResponse, LwM2mRequest request, String endpoint) { - - // Get content format - ContentFormat contentFormat = null; - if (coapResponse.options().getContentFormat() != null) { - contentFormat = ContentFormat.fromCode(coapResponse.options().getContentFormat()); + try { + return decoder.decode(coapResponse.getPayload().getBytes(), getContentFormat(coapResponse), path, model); + } catch (CodecException e) { + handleCodecException(e, request, coapResponse, endpoint); + return null; // should not happen as handleCodecException raise exception } + } - // Decode payload + private Map decodeCompositeCoapResponse(List paths, CoapResponse coapResponse, + LwM2mRequest request, String endpoint) { try { - return decoder.decode(coapResponse.getPayload().getBytes(), contentFormat, path, model); + return decoder.decodeNodes(coapResponse.getPayload().getBytes(), getContentFormat(coapResponse), paths, + model); } catch (CodecException e) { - if (LOG.isDebugEnabled()) { - byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload().getBytes(); - LOG.debug( - String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", - request, endpoint, Hex.encodeHexString(payload))); - } - throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", - request, endpoint); + handleCodecException(e, request, coapResponse, endpoint); + return null; // should not happen as handleCodecException raise exception } } - private Map decodeCompositeCoapResponse(List paths, CoapResponse coapResponse, + private TimestampedLwM2mNode decodeCoapTimestampedResponse(LwM2mPath path, CoapResponse coapResponse, LwM2mRequest request, String endpoint) { - // Get content format + List timestampedNodes = null; + try { + + timestampedNodes = decoder.decodeTimestampedData(coapResponse.getPayload().getBytes(), + getContentFormat(coapResponse), path, model); + if (timestampedNodes.size() != 1) { + throw new InvalidResponseException( + "Unable to decode response payload of request [%s] from client [%s] : should receive only 1 timestamped node but received %s", + request, endpoint, timestampedNodes.size()); + } + return timestampedNodes.get(0); + } catch (CodecException e) { + handleCodecException(e, request, coapResponse, endpoint); + return null; // should not happen as handleCodecException raise exception + } + + } + + private ContentFormat getContentFormat(CoapResponse coapResponse) { ContentFormat contentFormat = null; if (coapResponse.options().getContentFormat() != null) { contentFormat = ContentFormat.fromCode(coapResponse.options().getContentFormat()); } + return contentFormat; + } - // Decode payload - try { - return decoder.decodeNodes(coapResponse.getPayload().getBytes(), contentFormat, paths, model); - } catch (CodecException e) { - if (LOG.isDebugEnabled()) { - byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload().getBytes(); - LOG.debug( - String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", - request, endpoint, Hex.encodeHexString(payload))); - } - throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", - request, endpoint); + private void handleCodecException(CodecException e, LwM2mRequest request, CoapResponse coapResponse, + String endpoint) { + if (LOG.isDebugEnabled()) { + byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload().getBytes(); + LOG.debug(String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", + request, endpoint, Hex.encodeHexString(payload))); } + throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", + request, endpoint); } @SuppressWarnings("unchecked") @@ -541,36 +554,4 @@ private void handleUnexpectedResponseCode(String clientEndpoint, LwM2mRequest throw new InvalidResponseException("Client [%s] returned unexpected response code [%s] for [%s]", clientEndpoint, coapResponse.getCode(), request); } - - private TimestampedLwM2mNode decodeCoapTimestampedResponse(LwM2mPath path, CoapResponse coapResponse, - LwM2mRequest request, String endpoint) { - // Get content format - ContentFormat contentFormat = null; - if (coapResponse.options().getContentFormat() != null) { - contentFormat = ContentFormat.fromCode(coapResponse.options().getContentFormat()); - } - - // Decode payload - List timestampedNodes = null; - try { - - timestampedNodes = decoder.decodeTimestampedData(coapResponse.getPayload().getBytes(), contentFormat, path, - model); - if (timestampedNodes.size() != 1) { - throw new InvalidResponseException( - "Unable to decode response payload of request [%s] from client [%s] : should receive only 1 timestamped not but received %s", - request, endpoint, timestampedNodes.size()); - } - return timestampedNodes.get(0); - } catch (CodecException e) { - if (LOG.isDebugEnabled()) { - byte[] payload = coapResponse.getPayload() == null ? new byte[0] : coapResponse.getPayload().getBytes(); - LOG.debug( - String.format("Unable to decode response payload of request [%s] from client [%s] [payload:%s]", - request, endpoint, Hex.encodeHexString(payload))); - } - throw new InvalidResponseException(e, "Unable to decode response payload of request [%s] from client [%s]", - request, endpoint); - } - } }