use of org.bson.BsonBinary in project spring-data-mongodb by spring-projects.
the class EncryptionUtils method resolveKeyId.
/**
* Resolve a given plain {@link String} value into the store native {@literal keyId} format, considering potential
* {@link Expression expressions}. <br />
* The potential keyId is probed against an {@link UUID#fromString(String) UUID value} or decoded from the
* {@literal base64} representation prior to conversion into its {@link Binary} format.
*
* @param value the source value to resolve the keyId for. Must not be {@literal null}.
* @param evaluationContext a {@link Supplier} used to provide the {@link EvaluationContext} in case an
* {@link Expression} is {@link ExpressionUtils#detectExpression(String) detected}.
* @return can be {@literal null}.
* @throws IllegalArgumentException if one of the required arguments is {@literal null}.
*/
@Nullable
public static Object resolveKeyId(String value, Supplier<EvaluationContext> evaluationContext) {
Assert.notNull(value, "Value must not be null!");
Object potentialKeyId = value;
Expression expression = ExpressionUtils.detectExpression(value);
if (expression != null) {
potentialKeyId = expression.getValue(evaluationContext.get());
if (!(potentialKeyId instanceof String)) {
return potentialKeyId;
}
}
try {
return new Binary(BsonBinarySubType.UUID_STANDARD, new BsonBinary(UUID.fromString(potentialKeyId.toString())).getData());
} catch (IllegalArgumentException e) {
return new Binary(BsonBinarySubType.UUID_STANDARD, Base64Utils.decodeFromString(potentialKeyId.toString()));
}
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class UuidCodec method encode.
@Override
public void encode(final BsonWriter writer, final UUID value, final EncoderContext encoderContext) {
byte[] binaryData = new byte[16];
writeLongToArrayBigEndian(binaryData, 0, value.getMostSignificantBits());
writeLongToArrayBigEndian(binaryData, 8, value.getLeastSignificantBits());
switch(encoderUuidRepresentation) {
case C_SHARP_LEGACY:
UuidCodecHelper.reverseByteArray(binaryData, 0, 4);
UuidCodecHelper.reverseByteArray(binaryData, 4, 2);
UuidCodecHelper.reverseByteArray(binaryData, 6, 2);
break;
case JAVA_LEGACY:
UuidCodecHelper.reverseByteArray(binaryData, 0, 8);
UuidCodecHelper.reverseByteArray(binaryData, 8, 8);
break;
case PYTHON_LEGACY:
case STANDARD:
break;
default:
throw new BSONException("Unexpected UUID representation");
}
// changed the default subtype to STANDARD since 3.0
if (encoderUuidRepresentation == UuidRepresentation.STANDARD) {
writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_STANDARD, binaryData));
} else {
writer.writeBinaryData(new BsonBinary(BsonBinarySubType.UUID_LEGACY, binaryData));
}
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReader method visitBinDataExtendedJson.
private BsonBinary visitBinDataExtendedJson() {
verifyToken(JsonTokenType.COLON);
JsonToken bytesToken = popToken();
if (bytesToken.getType() != JsonTokenType.STRING) {
throw new JsonParseException("JSON reader expected a string but found '%s'.", bytesToken.getValue());
}
verifyToken(JsonTokenType.COMMA);
verifyString("$type");
verifyToken(JsonTokenType.COLON);
JsonToken subTypeToken = popToken();
if (subTypeToken.getType() != JsonTokenType.STRING && subTypeToken.getType() != JsonTokenType.INT32) {
throw new JsonParseException("JSON reader expected a string or number but found '%s'.", subTypeToken.getValue());
}
verifyToken(JsonTokenType.END_OBJECT);
byte subType;
if (subTypeToken.getType() == JsonTokenType.STRING) {
subType = (byte) Integer.parseInt(subTypeToken.getValue(String.class), 16);
} else {
subType = subTypeToken.getValue(Integer.class).byteValue();
}
for (final BsonBinarySubType st : BsonBinarySubType.values()) {
if (st.getValue() == subType) {
return new BsonBinary(st, DatatypeConverter.parseBase64Binary(bytesToken.getValue(String.class)));
}
}
return new BsonBinary(DatatypeConverter.parseBase64Binary(bytesToken.getValue(String.class)));
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReader method visitUUIDConstructor.
private BsonBinary visitUUIDConstructor(final String uuidConstructorName) {
//TODO verify information related to https://jira.mongodb.org/browse/SERVER-3168
verifyToken(JsonTokenType.LEFT_PAREN);
JsonToken bytesToken = popToken();
if (bytesToken.getType() != JsonTokenType.STRING) {
throw new JsonParseException("JSON reader expected a string but found '%s'.", bytesToken.getValue());
}
verifyToken(JsonTokenType.RIGHT_PAREN);
String hexString = bytesToken.getValue(String.class).replaceAll("\\{", "").replaceAll("\\}", "").replaceAll("-", "");
byte[] bytes = DatatypeConverter.parseHexBinary(hexString);
BsonBinarySubType subType = BsonBinarySubType.UUID_STANDARD;
if (!"UUID".equals(uuidConstructorName) || !"GUID".equals(uuidConstructorName)) {
subType = BsonBinarySubType.UUID_LEGACY;
}
return new BsonBinary(subType, bytes);
}
use of org.bson.BsonBinary in project mongo-java-driver by mongodb.
the class JsonReaderTest method testBinary.
@Test
public void testBinary() {
String json = "{ \"$binary\" : \"AQID\", \"$type\" : \"0\" }";
bsonReader = new JsonReader(json);
assertEquals(BsonType.BINARY, bsonReader.readBsonType());
BsonBinary binary = bsonReader.readBinaryData();
assertEquals(BsonBinarySubType.BINARY.getValue(), binary.getType());
assertArrayEquals(new byte[] { 1, 2, 3 }, binary.getData());
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
}
Aggregations