Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

In this page you can find the example usage for com.mongodb DBCollection find.

Prototype

public DBCursor find(final DBObject query) 

Source Link

Document

Select documents in collection and get a cursor to the selected documents.

Usage

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//www.j  ava  2 s  .  com
public AccessToken findAccessTokenByRefreshToken(String refreshToken, String clientId) {
    BasicDBObject dbObject = new BasicDBObject();
    // TODO: add indexes
    dbObject.put(REFRESH_TOKEN_ID_NAME, refreshToken);
    dbObject.put(CLIENTS_ID_NAME, clientId);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list != null && list.size() == 1) {
        Map<String, Object> mapLoaded = list.get(0).toMap();
        // convert details list to String
        if (mapLoaded.get("details") instanceof BasicDBObject) {
            BasicDBObject details = (BasicDBObject) mapLoaded.get("details");
            mapLoaded.put("details", details.toString());
        }
        AccessToken loadedAccessToken = AccessToken.loadFromMap(mapLoaded);
        log.debug(loadedAccessToken.getToken());
        return loadedAccessToken;
    } else {
        return null;
    }
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void updateAccessTokenValidStatus(String accessToken, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("token", accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }/*from   w  w w  .  j  a v a 2  s.c  o m*/
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void updateAuthCodeValidStatus(String authCode, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("code", authCode);
    DBCollection coll = db.getCollection(AUTH_CODE_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }//from   ww w  .  ja va2 s. co m
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

protected Object getObject(DBCollection coll, BasicDBObject query) {
    DBCursor cursor = coll.find(query);
    Object result = null;//from   ww w .  j av  a2  s . c o  m
    try {
        // TODO: if more than once throw exception
        while (cursor.hasNext()) {
            result = cursor.next();
            log.debug("found: " + result);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return result;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public boolean updateClientApp(String clientId, String scope, String description, Integer status,
        Map<String, String> applicationDetails) {
    boolean updated = false;
    DBCollection coll = db.getCollection(CLIENTS_COLLECTION_NAME);
    BasicDBObject query = new BasicDBObject(ID_NAME, clientId);
    List<DBObject> list = coll.find(query).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        if (scope != null && scope.length() > 0) {
            newObject.put("scope", scope);
        }//  www  .j  a  v a2 s .  co m
        if (description != null && description.length() > 0) {
            newObject.put("descr", description);
        }
        if (status != null) {
            newObject.put("status", status);
        }
        if (applicationDetails != null && applicationDetails.size() > 0) {
            newObject.put("applicationDetails", applicationDetails);
        }
        coll.findAndModify(query, newObject);
        updated = true;
    }
    return updated;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public List<AccessToken> getAccessTokenByUserIdAndClientApp(String userId, String clientId) {
    List<AccessToken> accessTokens = new ArrayList<AccessToken>();
    BasicDBObject dbObject = new BasicDBObject();
    // TODO: add indexes
    dbObject.put(USER_ID, userId);//from w ww . j  a  va2s.  c  om
    dbObject.put(CLIENTS_ID_NAME, clientId);
    dbObject.put(VALID_NAME, true);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    for (DBObject object : list) {
        Map<String, Object> mapLoaded = object.toMap();
        // convert details list to String
        if (mapLoaded.get("details") instanceof BasicDBObject) {
            BasicDBObject details = (BasicDBObject) mapLoaded.get("details");
            mapLoaded.put("details", details.toString());
        }
        AccessToken loadedAccessToken = AccessToken.loadFromMap(mapLoaded);
        accessTokens.add(loadedAccessToken);
    }
    return accessTokens;
}

From source file:com.apifest.oauth20.MongoDBManager.java

License:Apache License

@Override
public void removeAccessToken(String accessToken) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(ACCESS_TOKEN_ID_NAME, accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 0) {
        DBObject newObject = list.get(0);
        coll.findAndModify(dbObject, newObject);
        coll.remove(dbObject);/*from ww w  .  j a va 2s  .co m*/
    }
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from   w  ww .  ja v  a 2  s . co m
public AccessToken findAccessToken(String accessToken) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(ACCESS_TOKEN_ID, accessToken);
    dbObject.put(ACCESS_TOKEN_VALID, true);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list.size() > 1) {
        // throw exception
        log.warn("Several access tokens found");
        return null;
    }
    if (list.size() > 0) {
        Map<String, Object> mapLoaded = list.get(0).toMap();
        // convert details map to String
        if (mapLoaded.get("details") instanceof BasicDBObject) {
            BasicDBObject details = (BasicDBObject) mapLoaded.get("details");
            mapLoaded.put("details", details.toString());
        }
        return AccessToken.loadFromMap(mapLoaded);
    } else {
        log.debug("No access token found");
        return null;
    }
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*  ww w .j a v a 2s  .c  o m*/
public AccessToken findAccessTokenByRefreshToken(String refreshToken, String clientId) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put(ACCESS_TOKEN_REFRESH_TOKEN_ID, refreshToken);
    dbObject.put(CLIENTS_CLIENTID, clientId);
    // Searching for unknown validity token -> no VALID query param
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    List<DBObject> list = coll.find(dbObject).toArray();
    if (list != null && list.size() == 1) {
        Map<String, Object> mapLoaded = list.get(0).toMap();
        // convert details list to String
        if (mapLoaded.get("details") instanceof BasicDBObject) {
            BasicDBObject details = (BasicDBObject) mapLoaded.get("details");
            mapLoaded.put("details", details.toString());
        }
        AccessToken loadedAccessToken = AccessToken.loadFromMap(mapLoaded);
        log.debug(loadedAccessToken.getToken());
        return loadedAccessToken;
    }

    return null;
}

From source file:com.apifest.oauth20.persistence.mongodb.MongoDBManager.java

License:Apache License

@Override
public void updateAccessTokenValidStatus(String accessToken, boolean valid) {
    BasicDBObject dbObject = new BasicDBObject();
    dbObject.put("token", accessToken);
    DBCollection coll = db.getCollection(ACCESS_TOKEN_COLLECTION_NAME);
    DBObject newObject = coll.find(dbObject).next();
    if (newObject != null) {
        newObject.put("valid", valid);
        coll.findAndModify(dbObject, newObject);
    }/*from www. j a v  a  2s .c o  m*/
}