NIFI-10764 Fixed handling BIT STRING in nifi-asn1-services

Representing as a string of 1s and 0s (e.g. "010011000")

This closes #6622

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Tamas Palfy 2022-11-04 19:36:01 +01:00 committed by exceptionfactory
parent 0643f336e8
commit 56d0061da4
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
7 changed files with 16 additions and 7 deletions

View File

@ -31,7 +31,7 @@ public class BerBitStringConverter implements JASN1TypeAndValueConverter {
@Override
public DataType convertType(Class<?> berType, JASN1Converter converter) {
return RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BOOLEAN.getDataType());
return RecordFieldType.STRING.getDataType();
}
@Override
@ -43,6 +43,6 @@ public class BerBitStringConverter implements JASN1TypeAndValueConverter {
public Object convertValue(BerType value, DataType dataType, JASN1Converter converter) {
final BerBitString berValue = (BerBitString) value;
return berValue.getValueAsBooleans();
return berValue.toString();
}
}

View File

@ -17,6 +17,7 @@
package org.apache.nifi.jasn1;
import com.beanit.asn1bean.ber.ReverseByteArrayOutputStream;
import com.beanit.asn1bean.ber.types.BerBitString;
import com.beanit.asn1bean.ber.types.BerBoolean;
import com.beanit.asn1bean.ber.types.BerInteger;
import com.beanit.asn1bean.ber.types.BerOctetString;
@ -62,6 +63,7 @@ public class ExampleDataGenerator {
basicTypes.setI(new BerInteger(789));
basicTypes.setOctStr(new BerOctetString(new byte[]{1, 2, 3, 4, 5}));
basicTypes.setUtf8Str(new BerUTF8String("Some UTF-8 String. こんにちは世界。"));
basicTypes.setBitStr(new BerBitString(new boolean[]{false, true, true, false, true, false, false, false}));
final int encoded = basicTypes.encode(rev);
out.write(rev.getArray(), 0, encoded);
LOG.info("Generated {} bytes to {}", encoded, file);

View File

@ -59,6 +59,7 @@ public class TestJASN1RecordReader implements JASN1ReadRecordTester {
assertEquals(789, record.getAsInt("i").intValue());
assertEquals("0102030405", record.getValue("octStr"));
assertEquals("Some UTF-8 String. こんにちは世界。", record.getValue("utf8Str"));
assertEquals("01101000", record.getValue("bitStr"));
record = reader.nextRecord(true, false);
assertNull(record);

View File

@ -16,6 +16,7 @@
*/
package org.apache.nifi.jasn1;
import com.beanit.asn1bean.ber.types.BerBitString;
import com.beanit.asn1bean.ber.types.BerBoolean;
import com.beanit.asn1bean.ber.types.BerInteger;
import com.beanit.asn1bean.ber.types.BerOctetString;
@ -79,19 +80,22 @@ public class TestJASN1RecordReaderWithComplexTypes implements JASN1ReadRecordTes
basicTypes.setI(new BerInteger(789));
basicTypes.setOctStr(new BerOctetString(new byte[]{1, 2, 3, 4, 5}));
basicTypes.setUtf8Str(new BerUTF8String("Some UTF-8 String. こんにちは世界。"));
basicTypes.setBitStr(new BerBitString(new boolean[] { true, false, true, true}));
Map<String, Object> expectedValues = new HashMap<String, Object>() {{
put("b", true);
put("i", BigInteger.valueOf(789));
put("octStr", "0102030405");
put("utf8Str", "Some UTF-8 String. こんにちは世界。");
put("bitStr", "1011");
}};
RecordSchema expectedSchema = new SimpleRecordSchema(Arrays.asList(
new RecordField("b", RecordFieldType.BOOLEAN.getDataType()),
new RecordField("i", RecordFieldType.BIGINT.getDataType()),
new RecordField("octStr", RecordFieldType.STRING.getDataType()),
new RecordField("utf8Str", RecordFieldType.STRING.getDataType())
new RecordField("utf8Str", RecordFieldType.STRING.getDataType()),
new RecordField("bitStr", RecordFieldType.STRING.getDataType())
));
testReadRecord(dataFile, basicTypes, expectedValues, expectedSchema);
@ -156,7 +160,8 @@ public class TestJASN1RecordReaderWithComplexTypes implements JASN1ReadRecordTes
new RecordField("b", RecordFieldType.BOOLEAN.getDataType()),
new RecordField("i", RecordFieldType.BIGINT.getDataType()),
new RecordField("octStr", RecordFieldType.STRING.getDataType()),
new RecordField("utf8Str", RecordFieldType.STRING.getDataType())
new RecordField("utf8Str", RecordFieldType.STRING.getDataType()),
new RecordField("bitStr", RecordFieldType.STRING.getDataType())
));
RecordSchema expectedSchema = new SimpleRecordSchema(Arrays.asList(

View File

@ -106,11 +106,11 @@ public class TestJASN1RecordReaderWithSimpleTypes implements JASN1ReadRecordTest
berValue.setValue(new BerBitString(new boolean[]{false, true, false, false, true, true, true, true, false, true, false, false}));
Map<String, Object> expectedValues = new HashMap<String, Object>() {{
put("value", new boolean[]{false, true, false, false, true, true, true, true, false, true, false, false});
put("value", "010011110100");
}};
RecordSchema expectedSchema = new SimpleRecordSchema(Arrays.asList(
new RecordField("value", RecordFieldType.ARRAY.getArrayDataType(RecordFieldType.BOOLEAN.getDataType())))
new RecordField("value", RecordFieldType.STRING.getDataType()))
);
testReadRecord(dataFile, berValue, expectedValues, expectedSchema);

View File

@ -13,7 +13,8 @@ BasicTypes ::= SEQUENCE
b [0] BOOLEAN,
i [1] INTEGER,
octStr [2] OCTET STRING (SIZE (1..10)),
utf8Str [3] UTF8String OPTIONAL
utf8Str [3] UTF8String OPTIONAL,
bitStr [4] BIT STRING OPTIONAL
}
Composite ::= SEQUENCE