Search in sources :

Example 36 with BsonBinary

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()));
    }
}
Also used : Expression(org.springframework.expression.Expression) BsonBinary(org.bson.BsonBinary) Binary(org.bson.types.Binary) BsonBinary(org.bson.BsonBinary) Nullable(org.springframework.lang.Nullable)

Example 37 with BsonBinary

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));
    }
}
Also used : BsonBinary(org.bson.BsonBinary) BSONException(org.bson.BSONException)

Example 38 with BsonBinary

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)));
}
Also used : BsonBinarySubType(org.bson.BsonBinarySubType) BsonBinary(org.bson.BsonBinary)

Example 39 with BsonBinary

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);
}
Also used : BsonBinarySubType(org.bson.BsonBinarySubType) BsonBinary(org.bson.BsonBinary)

Example 40 with BsonBinary

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());
}
Also used : BsonBinary(org.bson.BsonBinary) Test(org.junit.Test)

Aggregations

BsonBinary (org.bson.BsonBinary)78 Test (org.junit.Test)51 BsonDocument (org.bson.BsonDocument)32 BsonString (org.bson.BsonString)16 Document (org.bson.Document)16 EncryptOptions (com.mongodb.client.model.vault.EncryptOptions)13 DataKeyOptions (com.mongodb.client.model.vault.DataKeyOptions)11 BSONException (org.bson.BSONException)9 Map (java.util.Map)8 BasicDBObject (com.mongodb.BasicDBObject)6 ClientEncryptionSettings (com.mongodb.ClientEncryptionSettings)6 ConnectionString (com.mongodb.ConnectionString)6 MongoClientSettings (com.mongodb.MongoClientSettings)6 SecureRandom (java.security.SecureRandom)6 HashMap (java.util.HashMap)6 ClientEncryption (com.mongodb.client.vault.ClientEncryption)5 UUID (java.util.UUID)5 Binary (org.bson.types.Binary)5 MongoNamespace (com.mongodb.MongoNamespace)4 IndexOptions (com.mongodb.client.model.IndexOptions)4