Skip to content

Commit

Permalink
Clean code by avoiding duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed May 3, 2024
1 parent bcfd49a commit 778eecd
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<LwM2mPath, LwM2mNode> decodeCompositeCoapResponse(List<LwM2mPath> 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<LwM2mPath, LwM2mNode> decodeCompositeCoapResponse(List<LwM2mPath> paths, Response coapResponse,
private TimestampedLwM2mNode decodeCoapTimestampedResponse(LwM2mPath path, Response coapResponse,
LwM2mRequest<?> request, String endpoint) {
List<TimestampedLwM2mNode> 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")
Expand All @@ -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<TimestampedLwM2mNode> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<LwM2mPath, LwM2mNode> decodeCompositeCoapResponse(List<LwM2mPath> 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<LwM2mPath, LwM2mNode> decodeCompositeCoapResponse(List<LwM2mPath> paths, CoapResponse coapResponse,
private TimestampedLwM2mNode decodeCoapTimestampedResponse(LwM2mPath path, CoapResponse coapResponse,
LwM2mRequest<?> request, String endpoint) {
// Get content format
List<TimestampedLwM2mNode> 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")
Expand All @@ -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<TimestampedLwM2mNode> 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);
}
}
}

0 comments on commit 778eecd

Please sign in to comment.