Skip to content

Commit

Permalink
Use encoder/decoder to know if a content format is supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 authored and Manuel Sangoi committed Jul 21, 2016
1 parent ad24783 commit 1552b4e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void handleGET(CoapExchange exchange) {
ContentFormat format = ContentFormat.TLV; // use TLV as default format
if (exchange.getRequestOptions().hasAccept()) {
format = ContentFormat.fromCode(exchange.getRequestOptions().getAccept());
if (format == null) {
if (!encoder.isSupported(format)) {
exchange.respond(ResponseCode.NOT_ACCEPTABLE);
return;
}
Expand Down Expand Up @@ -182,7 +182,7 @@ public void handlePUT(final CoapExchange coapExchange) {
else {
LwM2mPath path = new LwM2mPath(URI);
ContentFormat contentFormat = ContentFormat.fromCode(coapExchange.getRequestOptions().getContentFormat());
if (contentFormat == null) {
if (!decoder.isSupported(contentFormat)) {
coapExchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
return;
}
Expand Down Expand Up @@ -227,7 +227,7 @@ public void handlePOST(final CoapExchange exchange) {

// handle content format for Write (Update) and Create request
ContentFormat contentFormat = ContentFormat.fromCode(exchange.getRequestOptions().getContentFormat());
if (contentFormat == null) {
if (!decoder.isSupported(contentFormat)) {
exchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,17 @@ private static Class<? extends LwM2mNode> nodeClassFromPath(LwM2mPath path) {
}
throw new IllegalArgumentException("invalid path level: " + path);
}

@Override
public boolean isSupported(ContentFormat format) {
switch (format.getCode()) {
case ContentFormat.TEXT_CODE:
case ContentFormat.TLV_CODE:
case ContentFormat.OPAQUE_CODE:
case ContentFormat.JSON_CODE:
return true;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ public byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes,
LOG.trace("Encoded node timestampedNode: {}", timestampedNodes, Arrays.toString(encoded));
return encoded;
}

@Override
public boolean isSupported(ContentFormat format) {
switch (format.getCode()) {
case ContentFormat.TEXT_CODE:
case ContentFormat.TLV_CODE:
case ContentFormat.OPAQUE_CODE:
case ContentFormat.JSON_CODE:
return true;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ <T extends LwM2mNode> T decode(byte[] content, ContentFormat format, LwM2mPath p
List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, ContentFormat format, LwM2mPath path,
LwM2mModel model) throws InvalidValueException;

/**
* return true is the given ContentFomart is supported
*/
boolean isSupported(ContentFormat format);

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public interface LwM2mNodeEncoder {
*/
byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes, ContentFormat format, LwM2mPath path,
LwM2mModel model);

/**
* return true is the given ContentFomart is supported
*/
boolean isSupported(ContentFormat format);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static ContentFormat fromCode(int code) {
return t;
}
}
return null;
return new ContentFormat(code);
}

/**
Expand Down

0 comments on commit 1552b4e

Please sign in to comment.