org.bson.BsonInvalidOperationException Java Examples

The following examples show how to use org.bson.BsonInvalidOperationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RdbmsHandler.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
protected static BsonDocument generateFilterDoc(BsonDocument keyDoc, BsonDocument valueDoc, OperationType opType) {
    if (keyDoc.keySet().isEmpty()) {
        if (opType.equals(OperationType.CREATE) || opType.equals(OperationType.READ)) {
            //create: no PK info in keyDoc -> generate ObjectId
            return new BsonDocument(DBCollection.ID_FIELD_NAME,new BsonObjectId());
        }
        //update or delete: no PK info in keyDoc -> take everything in 'before' field
        try {
            BsonDocument filter = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD);
            if (filter.isEmpty())
                throw new BsonInvalidOperationException("value doc before field is empty");
            return filter;
        } catch(BsonInvalidOperationException exc) {
            throw new DataException("error: value doc 'before' field is empty or has invalid type" +
                    " for update/delete operation which seems severely wrong -> defensive actions taken!",exc);
        }
    }
    //build filter document composed of all PK columns
    BsonDocument pk = new BsonDocument();
    for (String f : keyDoc.keySet()) {
        pk.put(f,keyDoc.get(f));
    }
    return new BsonDocument(DBCollection.ID_FIELD_NAME,pk);
}
 
Example #2
Source File: SerialisationTest.java    From octarine with Apache License 2.0 6 votes vote down vote up
@Test public void
write_map_with_null_values_to_bson() {
    Map<String, Integer> mapToSerialise = new HashMap<>();
    mapToSerialise.put("foo", 42);
    mapToSerialise.put("bar", null);

    BsonDocument doc = (BsonDocument) BsonMapSerialiser
            .writingValuesWith(BsonSerialisers.toInteger)
            .apply(mapToSerialise);

    assertEquals("invalid foo", 42, doc.getInt32("foo").getValue());
    try {
        Integer value = doc.getInt32("bar").getValue();
        fail("bar should not have been written to the document");
    } catch (BsonInvalidOperationException e) {
        // expected
    }
}
 
Example #3
Source File: BsonRecordDeserialiser.java    From octarine with Apache License 2.0 5 votes vote down vote up
Stream<Value> valuesFrom(BsonValue bsonValue) throws BsonInvalidOperationException {
    BsonDocument doc = bsonValue.asDocument();
    return deserialiserMap.values().stream()
            .map(d -> d.apply(doc))
            .filter(Optional::isPresent)
            .map(Optional::get);
}
 
Example #4
Source File: ReaderState.java    From morphia with Apache License 2.0 5 votes vote down vote up
void startArray() {
    if (!(value instanceof List)) {
        throw new BsonInvalidOperationException(Sofia.invalidBsonOperation(List.class, getCurrentBsonType()));
    }
    if (!(nextReaderState instanceof ListValueReaderState)) {
        processNextStages(new ListEndReaderState(reader), new ArrayIterator(reader, ((List) value).iterator()));
    }
}
 
Example #5
Source File: ReaderState.java    From morphia with Apache License 2.0 5 votes vote down vote up
void startDocument() {
    if (!(value instanceof Document)) {
        throw new BsonInvalidOperationException(Sofia.invalidBsonOperation(Document.class, getCurrentBsonType()));
    }
    if (!(nextReaderState instanceof DocumentStartReaderState)) {
        processNextStages(new DocumentEndReaderState(reader), new DocumentIterator(reader, ((Document) value).entrySet().iterator()));
        next(new DocumentStartReaderState(reader, ArrayIterator.empty())).advance();
    } else {
        advance();
    }
}
 
Example #6
Source File: BsonMapDeserialiser.java    From octarine with Apache License 2.0 5 votes vote down vote up
@Override
public PMap<String, T> applyUnsafe(BsonValue p) throws BsonInvalidOperationException {
    BsonDocument doc = p.asDocument();
    Map<String, T> values = new HashMap<>();
    for (Map.Entry<String,BsonValue> e : doc.entrySet()) {
        values.put(e.getKey(), valueDeserialiser.apply(e.getValue()));
    }

    return HashTreePMap.from(values);
}
 
Example #7
Source File: RdbmsHandler.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
static BsonDocument generateFilterDoc(
    final BsonDocument keyDoc, final BsonDocument valueDoc, final OperationType opType) {
  if (keyDoc.keySet().isEmpty()) {
    if (opType.equals(OperationType.CREATE) || opType.equals(OperationType.READ)) {
      // create: no PK info in keyDoc -> generate ObjectId
      return new BsonDocument(ID_FIELD, new BsonObjectId());
    }
    // update or delete: no PK info in keyDoc -> take everything in 'before' field
    try {
      BsonDocument filter = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD);
      if (filter.isEmpty()) {
        throw new BsonInvalidOperationException("value doc before field is empty");
      }
      return filter;
    } catch (BsonInvalidOperationException exc) {
      throw new DataException(
          "Error: value doc 'before' field is empty or has invalid type"
              + " for update/delete operation which seems severely wrong -> defensive actions taken!",
          exc);
    }
  }
  // build filter document composed of all PK columns
  BsonDocument pk = new BsonDocument();
  for (String f : keyDoc.keySet()) {
    pk.put(f, keyDoc.get(f));
  }
  return new BsonDocument(ID_FIELD, pk);
}
 
Example #8
Source File: SafeBsonDeserialiser.java    From octarine with Apache License 2.0 5 votes vote down vote up
default S apply(BsonValue p) {
    try {
        return applyUnsafe(p);
    } catch (BsonInvalidOperationException e) {
        throw new BsonDeserialisationException(e);
    }
}
 
Example #9
Source File: BsonValidRecordDeserialiser.java    From octarine with Apache License 2.0 5 votes vote down vote up
@Override
public Validation<T> applyUnsafe(BsonValue p) throws BsonInvalidOperationException {
    try {
        return validated(reader.apply(p));
    } catch (RecordValidationException e) {
        return e.toValidation();
    }
}
 
Example #10
Source File: PrimitiveArrayCodec.java    From Prism with MIT License 5 votes vote down vote up
@Override
public PrimitiveArray decode(BsonReader reader, DecoderContext decoderContext) {
    reader.readStartDocument();
    String key = reader.readString("key");

    List<Number> value = Lists.newArrayList();
    if (StringUtils.equals(key, PrimitiveArray.BYTE_ARRAY_ID)) {
        reader.readName("value");
        reader.readStartArray();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            value.add(byteCodec.decode(reader, decoderContext));
        }

        reader.readEndArray();
    } else if (StringUtils.equals(key, PrimitiveArray.INT_ARRAY_ID)) {
        reader.readName("value");
        reader.readStartArray();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            value.add(integerCodec.decode(reader, decoderContext));
        }

        reader.readEndArray();
    } else if (StringUtils.equals(key, PrimitiveArray.LONG_ARRAY_ID)) {
        reader.readName("value");
        reader.readStartArray();
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            value.add(longCodec.decode(reader, decoderContext));
        }

        reader.readEndArray();
    } else {
        reader.readEndDocument();
        throw new BsonInvalidOperationException("Unsupported primitive type");
    }

    reader.readEndDocument();
    return new PrimitiveArray(key, value);
}
 
Example #11
Source File: PersistenceModule.java    From EDDI with Apache License 2.0 5 votes vote down vote up
@Override
public URI decode(BsonReader reader, DecoderContext decoderContext) {
    String uriString = reader.readString();
    try {
        return new URI(uriString);
    } catch (URISyntaxException e) {
        throw new BsonInvalidOperationException(
                String.format("Cannot create URI from string '%s'", uriString));

    }
}
 
Example #12
Source File: BsonDeserialisationException.java    From octarine with Apache License 2.0 4 votes vote down vote up
public BsonDeserialisationException(BsonInvalidOperationException e) {
    super(e);
}
 
Example #13
Source File: BsonRecordDeserialiser.java    From octarine with Apache License 2.0 4 votes vote down vote up
@Override
public Record applyUnsafe(BsonValue bsonValue) throws BsonInvalidOperationException {
    return Record.of(valuesFrom(bsonValue));
}
 
Example #14
Source File: BsonListDeserialiser.java    From octarine with Apache License 2.0 4 votes vote down vote up
@Override
public PVector<V> applyUnsafe(BsonValue p) throws BsonInvalidOperationException {
    BsonArray bsonArray = p.asArray();
    List<V> values = bsonArray.getValues().stream().map(v -> itemDeserialiser.apply(v)).collect(Collectors.toList());
    return TreePVector.from(values);
}
 
Example #15
Source File: ReaderState.java    From morphia with Apache License 2.0 4 votes vote down vote up
void endArray() {
    throw new BsonInvalidOperationException(Sofia.invalidBsonOperation(List.class, getCurrentBsonType()));
}
 
Example #16
Source File: ReaderState.java    From morphia with Apache License 2.0 4 votes vote down vote up
void endDocument() {
    throw new BsonInvalidOperationException(Sofia.invalidBsonOperation(Document.class, getCurrentBsonType()));
}
 
Example #17
Source File: ReaderState.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
String name() {
    throw new BsonInvalidOperationException(Sofia.cannotReadName());
}
 
Example #18
Source File: ReaderState.java    From morphia with Apache License 2.0 4 votes vote down vote up
void end(final String message) {
    if (getIterator().hasNext()) {
        throw new BsonInvalidOperationException(message);
    }
    advance();
}
 
Example #19
Source File: SafeBsonDeserialiser.java    From octarine with Apache License 2.0 votes vote down vote up
S applyUnsafe(BsonValue p) throws BsonInvalidOperationException;