Skip to content

Commit

Permalink
GH-678: Enhance Base64 support & GH-1325: Fix Base64 from SenML-JSON.
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Serodzinski <adamserodzinski@gmail.com>
Co-authored-by: Bartosz Stolarczyk <bartosz.stolarczyk@orange.com>
Co-authored-by: Simon Bernard <sbernard@sierrawireless.com>
  • Loading branch information
3 people committed Dec 12, 2022
1 parent e7f1ac4 commit ce98924
Show file tree
Hide file tree
Showing 16 changed files with 776 additions and 1,258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@
import org.eclipse.leshan.core.node.TimestampedLwM2mNode;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.TimestampedNodeDecoder;
import org.eclipse.leshan.core.util.Base64;
import org.eclipse.leshan.core.util.TimestampUtil;
import org.eclipse.leshan.core.util.base64.Base64Decoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Decoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Decoder.DecoderAlphabet;
import org.eclipse.leshan.core.util.base64.DefaultBase64Decoder.DecoderPadding;
import org.eclipse.leshan.core.util.datatype.NumberUtil;
import org.eclipse.leshan.core.util.datatype.ULong;
import org.slf4j.Logger;
Expand All @@ -65,14 +68,17 @@ public class LwM2mNodeJsonDecoder implements TimestampedNodeDecoder {

private final LwM2mJsonDecoder decoder;
private final LinkParser linkParser;
private final Base64Decoder base64decoder;

public LwM2mNodeJsonDecoder() {
this(new LwM2mJsonJacksonEncoderDecoder(), new DefaultLwM2mLinkParser());
this(new LwM2mJsonJacksonEncoderDecoder(), new DefaultLwM2mLinkParser(),
new DefaultBase64Decoder(DecoderAlphabet.BASE64, DecoderPadding.REQUIRED));
}

public LwM2mNodeJsonDecoder(LwM2mJsonDecoder jsonDecoder, LinkParser linkParser) {
public LwM2mNodeJsonDecoder(LwM2mJsonDecoder jsonDecoder, LinkParser linkParser, Base64Decoder base64decoder) {
this.decoder = jsonDecoder;
this.linkParser = linkParser;
this.base64decoder = base64decoder;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -462,7 +468,7 @@ private Object parseJsonValue(Object value, Type expectedType, LwM2mPath path) t
case OPAQUE:
// If the Resource data type is opaque the string value
// holds the Base64 encoded representation of the Resource
return Base64.decodeBase64((String) value);
return base64decoder.decode((String) value);
case STRING:
return value;
case OBJLNK:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.LwM2mValueConverter;
import org.eclipse.leshan.core.node.codec.TimestampedNodeEncoder;
import org.eclipse.leshan.core.util.Base64;
import org.eclipse.leshan.core.util.TimestampUtil;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.core.util.base64.Base64Encoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Encoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Encoder.EncoderAlphabet;
import org.eclipse.leshan.core.util.base64.DefaultBase64Encoder.EncoderPadding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -56,14 +59,18 @@ public class LwM2mNodeJsonEncoder implements TimestampedNodeEncoder {

private final LwM2mJsonEncoder encoder;
private final LinkSerializer linkSerializer;
private final Base64Encoder base64Encoder;

public LwM2mNodeJsonEncoder() {
this(new LwM2mJsonJacksonEncoderDecoder(), new DefaultLinkSerializer());
this(new LwM2mJsonJacksonEncoderDecoder(), new DefaultLinkSerializer(),
new DefaultBase64Encoder(EncoderAlphabet.BASE64, EncoderPadding.WITH));
}

public LwM2mNodeJsonEncoder(LwM2mJsonEncoder jsonEncoder, LinkSerializer linkSerializer) {
public LwM2mNodeJsonEncoder(LwM2mJsonEncoder jsonEncoder, LinkSerializer linkSerializer,
Base64Encoder base64Encoder) {
this.encoder = jsonEncoder;
this.linkSerializer = linkSerializer;
this.base64Encoder = base64Encoder;
}

@Override
Expand Down Expand Up @@ -289,7 +296,7 @@ private void setResourceValue(Object value, Type type, JsonArrayEntry jsonResour
jsonResource.setFloatValue((((Date) value).getTime() / 1000L));
break;
case OPAQUE:
jsonResource.setStringValue(Base64.encodeBase64String((byte[]) value));
jsonResource.setStringValue(base64Encoder.encode((byte[]) value));
break;
case OBJLNK:
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
import org.eclipse.leshan.core.node.ObjectLink;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.NodeDecoder;
import org.eclipse.leshan.core.util.Base64;
import org.eclipse.leshan.core.util.base64.Base64Decoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Decoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Decoder.DecoderAlphabet;
import org.eclipse.leshan.core.util.base64.DefaultBase64Decoder.DecoderPadding;
import org.eclipse.leshan.core.util.base64.InvalidBase64Exception;
import org.eclipse.leshan.core.util.datatype.ULong;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -41,18 +45,20 @@ public class LwM2mNodeTextDecoder implements NodeDecoder {

// parser used for core link data type
private final LinkParser linkParser;
private final Base64Decoder base64Decoder;

public LwM2mNodeTextDecoder() {
this(new DefaultLwM2mLinkParser());
this(new DefaultLwM2mLinkParser(), new DefaultBase64Decoder(DecoderAlphabet.BASE64, DecoderPadding.REQUIRED));
}

/**
* Create a new LwM2mNodeTextDecoder with a custom {@link LinkParser}
*
* @param linkParser the link parser for core link format resources.
*/
public LwM2mNodeTextDecoder(LinkParser linkParser) {
public LwM2mNodeTextDecoder(LinkParser linkParser, Base64Decoder base64Decoder) {
this.linkParser = linkParser;
this.base64Decoder = base64Decoder;
}

@Override
Expand Down Expand Up @@ -137,10 +143,12 @@ private Object parseTextValue(String value, Type type, LwM2mPath path) throws Co
throw new CodecException(e, "Invalid value [%s] for CoreLink resource [%s]", value, path);
}
case OPAQUE:
if (!Base64.isBase64(value)) {
throw new CodecException("Invalid value for opaque resource [%s], base64 expected", path);
try {
return base64Decoder.decode(value);
} catch (InvalidBase64Exception e) {
throw new CodecException(e, "Invalid value for opaque resource [%s], base64 expected", path);
}
return Base64.decodeBase64(value);

default:
throw new CodecException("Could not handle %s value with TEXT encoder for resource %s", type, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.LwM2mValueConverter;
import org.eclipse.leshan.core.node.codec.NodeEncoder;
import org.eclipse.leshan.core.util.Base64;
import org.eclipse.leshan.core.util.Validate;
import org.eclipse.leshan.core.util.base64.Base64Encoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Encoder;
import org.eclipse.leshan.core.util.base64.DefaultBase64Encoder.EncoderAlphabet;
import org.eclipse.leshan.core.util.base64.DefaultBase64Encoder.EncoderPadding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,13 +48,15 @@ public class LwM2mNodeTextEncoder implements NodeEncoder {
private static final Logger LOG = LoggerFactory.getLogger(LwM2mNodeTextEncoder.class);

private final LinkSerializer linkSerializer;
private final Base64Encoder base64Encoder;

public LwM2mNodeTextEncoder() {
this(new DefaultLinkSerializer());
this(new DefaultLinkSerializer(), new DefaultBase64Encoder(EncoderAlphabet.BASE64, EncoderPadding.WITH));
}

public LwM2mNodeTextEncoder(LinkSerializer linkSerializer) {
public LwM2mNodeTextEncoder(LinkSerializer linkSerializer, Base64Encoder base64Encoder) {
this.linkSerializer = linkSerializer;
this.base64Encoder = base64Encoder;
}

@Override
Expand Down Expand Up @@ -154,7 +159,7 @@ private String getStringValue(Type expectedType, Object val) {
break;
case OPAQUE:
byte[] binaryValue = (byte[]) val;
strValue = Base64.encodeBase64String(binaryValue);
strValue = base64Encoder.encode(binaryValue);
break;
default:
throw new CodecException("Cannot encode %s in text format for %s", val, path);
Expand Down
Loading

0 comments on commit ce98924

Please sign in to comment.