use of org.bson.BsonBinary in project drill by axbaretto.
the class TestBsonRecordReader method testBinaryTypes.
@Test
public void testBinaryTypes() throws IOException {
// test with different binary types
BsonDocument bsonDoc = new BsonDocument();
// Binary
// String
byte[] bytes = "binaryValue".getBytes();
bsonDoc.append("binaryKey", new BsonBinary(BsonBinarySubType.BINARY, bytes));
// String
byte[] bytesString = "binaryStringValue".getBytes();
bsonDoc.append("binaryStringKey", new BsonBinary((byte) 2, bytesString));
// Double
byte[] bytesDouble = new byte[8];
java.nio.ByteBuffer.wrap(bytesDouble).putDouble(23.0123);
BsonBinary bsonDouble = new BsonBinary((byte) 1, bytesDouble);
bsonDoc.append("binaryDouble", bsonDouble);
// Boolean
byte[] booleanBytes = new byte[8];
java.nio.ByteBuffer.wrap(booleanBytes).put((byte) 1);
BsonBinary bsonBoolean = new BsonBinary((byte) 8, booleanBytes);
bsonDoc.append("bsonBoolean", bsonBoolean);
writer.reset();
bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader();
assertTrue(Arrays.equals(bytes, mapReader.reader("binaryKey").readByteArray()));
assertEquals("binaryStringValue", mapReader.reader("binaryStringKey").readText().toString());
assertEquals(23.0123, mapReader.reader("binaryDouble").readDouble().doubleValue(), 0);
FieldReader reader = mapReader.reader("bsonBoolean");
assertEquals(true, reader.readBoolean().booleanValue());
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReader method visitHexDataConstructor.
private BsonBinary visitHexDataConstructor() {
verifyToken(JsonTokenType.LEFT_PAREN);
JsonToken subTypeToken = popToken();
if (subTypeToken.getType() != JsonTokenType.INT32) {
throw new JsonParseException("JSON reader expected a binary subtype but found '%s'.", subTypeToken.getValue());
}
verifyToken(JsonTokenType.COMMA);
String hex = readStringFromExtendedJson();
verifyToken(JsonTokenType.RIGHT_PAREN);
if ((hex.length() & 1) != 0) {
hex = "0" + hex;
}
for (final BsonBinarySubType subType : BsonBinarySubType.values()) {
if (subType.getValue() == subTypeToken.getValue(Integer.class)) {
return new BsonBinary(subType, decodeHex(hex));
}
}
return new BsonBinary(decodeHex(hex));
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReader method visitLegacyBinaryExtendedJson.
private BsonBinary visitLegacyBinaryExtendedJson(final String firstKey) {
Mark mark = new Mark();
try {
verifyToken(JsonTokenType.COLON);
byte[] data;
byte type;
if (firstKey.equals("$binary")) {
data = Base64.getDecoder().decode(readStringFromExtendedJson());
verifyToken(JsonTokenType.COMMA);
verifyString("$type");
verifyToken(JsonTokenType.COLON);
type = readBinarySubtypeFromExtendedJson();
} else {
type = readBinarySubtypeFromExtendedJson();
verifyToken(JsonTokenType.COMMA);
verifyString("$binary");
verifyToken(JsonTokenType.COLON);
data = Base64.getDecoder().decode(readStringFromExtendedJson());
}
verifyToken(JsonTokenType.END_OBJECT);
return new BsonBinary(type, data);
} catch (JsonParseException e) {
mark.reset();
return null;
} catch (NumberFormatException e) {
mark.reset();
return null;
} finally {
mark.discard();
}
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class ClientSideEncryptionAutoEncryptionSettingsTour method main.
/**
* Run this main method to see the output of this quick example.
*
* Requires the mongodb-crypt library in the class path and mongocryptd on the system path.
*
* @param args ignored args
*/
public static void main(final String[] args) {
// This would have to be the same master key as was used to create the encryption key
final byte[] localMasterKey = new byte[96];
new SecureRandom().nextBytes(localMasterKey);
Map<String, Map<String, Object>> kmsProviders = new HashMap<String, Map<String, Object>>() {
{
put("local", new HashMap<String, Object>() {
{
put("key", localMasterKey);
}
});
}
};
String keyVaultNamespace = "admin.datakeys";
ClientEncryptionSettings clientEncryptionSettings = ClientEncryptionSettings.builder().keyVaultMongoClientSettings(MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb://localhost")).build()).keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).build();
ClientEncryption clientEncryption = ClientEncryptions.create(clientEncryptionSettings);
BsonBinary dataKeyId = clientEncryption.createDataKey("local", new DataKeyOptions());
final String base64DataKeyId = Base64.getEncoder().encodeToString(dataKeyId.getData());
final String dbName = "test";
final String collName = "coll";
AutoEncryptionSettings autoEncryptionSettings = AutoEncryptionSettings.builder().keyVaultNamespace(keyVaultNamespace).kmsProviders(kmsProviders).schemaMap(new HashMap<String, BsonDocument>() {
{
put(dbName + "." + collName, // Need a schema that references the new data key
BsonDocument.parse("{" + " properties: {" + " encryptedField: {" + " encrypt: {" + " keyId: [{" + " \"$binary\": {" + " \"base64\": \"" + base64DataKeyId + "\"," + " \"subType\": \"04\"" + " }" + " }]," + " bsonType: \"string\"," + " algorithm: \"AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic\"" + " }" + " }" + " }," + " \"bsonType\": \"object\"" + "}"));
}
}).build();
MongoClientSettings clientSettings = MongoClientSettings.builder().autoEncryptionSettings(autoEncryptionSettings).build();
MongoClient mongoClient = MongoClients.create(clientSettings);
MongoCollection<Document> collection = mongoClient.getDatabase("test").getCollection("coll");
// Clear old data
collection.drop();
collection.insertOne(new Document("encryptedField", "123456789"));
System.out.println(collection.find().first().toJson());
// release resources
mongoClient.close();
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class AbstractExplicitUuidCodecUuidRepresentationTest method shouldEncodeDocumentWithUuidRepresentation.
@Test
public void shouldEncodeDocumentWithUuidRepresentation() {
documentCollection.insertOne(new Document("_id", uuid));
BsonDocument document = bsonDocumentCollection.find().first();
assertNotNull(document);
BsonBinary uuidAsBinary = document.getBinary("_id");
assertEquals(subType.getValue(), uuidAsBinary.getType());
assertArrayEquals(encodedValue, uuidAsBinary.getData());
}
Aggregations