Java Code Examples for org.bson.Document#getList()

The following examples show how to use org.bson.Document#getList() . 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: PrimitiveArray.java    From Prism with MIT License 8 votes vote down vote up
public static PrimitiveArray of(Document document) {
    if (document.size() != 2 || !(document.containsKey("key") && document.containsKey("value"))) {
        return null;
    }

    if (!(document.get("key") instanceof String)) {
        return null;
    }

    String key = document.getString("key");
    if (!StringUtils.equalsAny(key, BYTE_ARRAY_ID, INT_ARRAY_ID, LONG_ARRAY_ID)) {
        return null;
    }

    List<Number> value = document.getList("value", Number.class);
    if (value == null) {
        return null;
    }

    return new PrimitiveArray(key, value);
}
 
Example 2
Source File: MongoDetailsAdapter.java    From rya with Apache License 2.0 6 votes vote down vote up
private static PCJIndexDetails.Builder getPCJIndexDetails(final Document document) {
    final Document pcjIndexDoc = document.get(PCJ_DETAILS_KEY, Document.class);

    final PCJIndexDetails.Builder pcjBuilder = PCJIndexDetails.builder();
    if (!pcjIndexDoc.getBoolean(PCJ_ENABLED_KEY)) {
        pcjBuilder.setEnabled(false);
    } else {
        pcjBuilder.setEnabled(true);//no fluo details to set since mongo has no fluo support
        final List<Document> pcjs = pcjIndexDoc.getList(PCJ_PCJS_KEY, Document.class);
        if (pcjs != null) {
            for (final Document pcj : pcjs) {
                pcjBuilder.addPCJDetails(toPCJDetails(pcj));
            }
        }
    }
    return pcjBuilder;
}
 
Example 3
Source File: SpringBooksIntegrationTests.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Before
public void before() throws Exception {

	if (operations.count(new Query(), "books") == 0) {

		File file = new ClassPathResource("books.json").getFile();
		String content = Files.contentOf(file, StandardCharsets.UTF_8);
		Document wrapper = Document.parse("{wrapper: " + content + "}");
		List<Object> books = wrapper.getList("wrapper", Object.class);

		operations.insert(books, "books");
	}
}
 
Example 4
Source File: ConnectionValidator.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
/**
 * Checks the roles info document for matching actions and removes them from the provided list
 *
 * <p>See: https://docs.mongodb.com/manual/reference/command/rolesInfo See:
 * https://docs.mongodb.com/manual/reference/resource-document/
 */
private static List<String> removeUserActions(
    final Document rolesInfo,
    final String authDatabase,
    final String databaseName,
    final String collectionName,
    final List<String> userActions) {
  List<Document> privileges =
      rolesInfo.getList("inheritedPrivileges", Document.class, emptyList());
  if (privileges.isEmpty() || userActions.isEmpty()) {
    return userActions;
  }

  List<String> unsupportedUserActions = new ArrayList<>(userActions);
  for (final Document privilege : privileges) {
    Document resource = privilege.get("resource", new Document());
    if (resource.containsKey("cluster") && resource.getBoolean("cluster")) {
      unsupportedUserActions.removeAll(privilege.getList("actions", String.class, emptyList()));
    } else if (resource.containsKey("db") && resource.containsKey("collection")) {
      String database = resource.getString("db");
      String collection = resource.getString("collection");

      boolean resourceMatches = false;
      boolean collectionMatches = collection.isEmpty() || collection.equals(collectionName);
      if (database.isEmpty() && collectionMatches) {
        resourceMatches = true;
      } else if (database.equals(authDatabase) && collection.isEmpty()) {
        resourceMatches = true;
      } else if (database.equals(databaseName) && collectionMatches) {
        resourceMatches = true;
      }

      if (resourceMatches) {
        unsupportedUserActions.removeAll(privilege.getList("actions", String.class, emptyList()));
      }
    }

    if (unsupportedUserActions.isEmpty()) {
      break;
    }
  }

  return unsupportedUserActions;
}
 
Example 5
Source File: EntityDocumentConverter.java    From rya with Apache License 2.0 4 votes vote down vote up
@Override
public Entity fromDocument(final Document document) throws DocumentConverterException {
    requireNonNull(document);

    // Preconditions.
    if(!document.containsKey(SUBJECT)) {
        throw new DocumentConverterException("Could not convert document '" + document +
                "' because its '" + SUBJECT + "' field is missing.");
    }

    if(!document.containsKey(EXPLICIT_TYPE_IDS)) {
        throw new DocumentConverterException("Could not convert document '" + document +
                "' because its '" + EXPLICIT_TYPE_IDS + "' field is missing.");
    }

    if(!document.containsKey(PROPERTIES)) {
        throw new DocumentConverterException("Could not convert document '" + document +
                "' because its '" + PROPERTIES + "' field is missing.");
    }

    if(!document.containsKey(VERSION)) {
        throw new DocumentConverterException("Could not convert document '" + document +
                "' because its '" + VERSION + "' field is missing.");
    }

    if(!document.containsKey(SMART_URI)) {
        throw new DocumentConverterException("Could not convert document '" + document +
                "' because its '" + SMART_URI + "' field is missing.");
    }

    // Perform the conversion.
    final Entity.Builder builder = Entity.builder()
            .setSubject( new RyaIRI(document.getString(SUBJECT)) );

    final List<String> explicitTypeIds = document.getList(EXPLICIT_TYPE_IDS, String.class);
    explicitTypeIds.stream()
        .forEach(explicitTypeId -> builder.setExplicitType(new RyaIRI(explicitTypeId)));

    final Document propertiesDoc = (Document) document.get(PROPERTIES);
    for(final String typeId : propertiesDoc.keySet()) {
        final Document typePropertiesDoc = (Document) propertiesDoc.get(typeId);
        for(final String propertyName : typePropertiesDoc.keySet()) {
            final String decodedPropertyName = MongoDbSafeKey.decodeKey(propertyName);
            final Document value = (Document) typePropertiesDoc.get(propertyName);
            final RyaType propertyValue = ryaTypeConverter.fromDocument( value );
            builder.setProperty(new RyaIRI(typeId), new Property(new RyaIRI(decodedPropertyName), propertyValue));
        }
    }

    builder.setVersion( document.getInteger(VERSION) );

    builder.setSmartUri( SimpleValueFactory.getInstance().createIRI(document.getString(SMART_URI)) );

    return builder.build();
}