Example usage for com.mongodb BasicDBObject BasicDBObject

List of usage examples for com.mongodb BasicDBObject BasicDBObject

Introduction

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

Prototype

public BasicDBObject(final String key, final Object value) 

Source Link

Document

Creates an object with the given key/value

Usage

From source file:DeleteUser.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("utf-8");
    response.setHeader("Access-Control-Allow-Origin", "*");
    try {/*from w w w  .  j a  v  a  2s .c om*/
        out = response.getWriter();
        String id = request.getParameter("id");
        System.out.println("id received : " + id);
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        // Now connect to your databases
        DB db = mongoClient.getDB("XHiring");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("users");
        System.out.println("Collection users selected successfully");
        //read example
        try {
            BasicDBObject query = new BasicDBObject("_id", new ObjectId(id));
            coll.findAndRemove(query);
            success = new JSONObject();
            success.put("message", "document deleted");
            out.println(success);
        } catch (NullPointerException ex) {
            error = new JSONObject();
            error.put("message", "No result found");
            out.println(error);
        }
        System.out.println("Document deleted successfully");
    } catch (Exception e) {
        exception = new JSONObject();
        try {
            exception.put("message", "exception");
            out.println(exception);
        } catch (JSONException ex) {
            Logger.getLogger(DeleteUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        e.printStackTrace();
    }

}

From source file:blowfish.java

public String encrypt(String str) {
    try {/*from  www.ja va 2  s  . c o m*/
        SecretKey key = KeyGenerator.getInstance("blowfish").generateKey();
        Cipher ecipher = Cipher.getInstance("blowfish");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] utf8 = str.getBytes("UTF8");
        byte[] enc = ecipher.doFinal(utf8);
        String s = new sun.misc.BASE64Encoder().encode(enc);

        crypter.count++;
        DBObject en = new BasicDBObject("id", crypter.count).append("string", s);
        crypter.collection.insert(en);
        crypter.vecb.add(key);
        crypter.indexb.add(crypter.count);
        return s;
    } catch (BadPaddingException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (UnsupportedEncodingException e) {
    } catch (NoSuchAlgorithmException e) {
    } catch (InvalidKeyException e) {
    } catch (NoSuchPaddingException e) {
    }
    return null;
}

From source file:blowfish.java

public String decrypt(int i) throws InvalidKeyException, IOException {
    String str = null;//w  w  w  .  j  a  v a 2 s.c  om
    SecretKey key = null;
    int j;
    int c = 0;
    for (j = 0; j < crypter.indexb.size(); j++) {
        if (crypter.indexb.get(j) == i) {
            c++;
            break;
        }
    }
    if (c == 0) {
        return new String("Wrong Algorithm chosen");
    }
    try {
        Cipher dcipher = Cipher.getInstance("blowfish");
        DBObject query = new BasicDBObject("id", i);
        DBCursor cursor = crypter.collection.find(query);
        str = (String) cursor.one().get("string");
        dcipher.init(Cipher.DECRYPT_MODE, crypter.vecb.get(j));
        byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
        byte[] utf8 = dcipher.doFinal(dec);
        return new String(utf8, "UTF8");
    } catch (BadPaddingException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (UnsupportedEncodingException e) {
    } catch (NoSuchAlgorithmException e) {
    } catch (NoSuchPaddingException e) {
    }
    return null;
}

From source file:MenuDefCats.java

public static void getCats() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {
        List<String> conv = (List<String>) doc.get("Cats");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            CatsTextArea.append(s + "\n");

    }/* ww  w.ja  v  a 2 s.  com*/
}

From source file:MenuDefObj.java

public static void getObjects() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {
        List<String> conv = (List<String>) doc.get("Objects");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            ObjTextArea.append(s + "\n");

    }/* w  w w .  j  av a 2s .com*/
}

From source file:FollowingCnt.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w w w.ja  v  a 2  s . c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        BasicDBObject query = new BasicDBObject("username", username);
        DBCursor curssc = User.find(query);
        ArrayList<String> e = new ArrayList();
        e = (ArrayList<String>) curssc.next().get("following_username");
        out.println(e.size());
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:gMIRC_handler.java

/**
 * Add a user to a channel (can check if the channel is a new channel)
 *
 * @param Username//from w  w w.  j  a v a 2 s .c  o m
 * @param Channel
 * @return code
 */
private int AddChannel(String Username, String Channel) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                ret = 1;
                System.err.println(Username + " has joined Channel : " + Channel + "!");
            } else {
                query = new BasicDBObject("channel", Channel);
                DBCursor cursor2 = coll.find(query);
                try {
                    if (!cursor2.hasNext()) {
                        ret = 2;
                    }
                } finally {
                    cursor2.close();
                }
                BasicDBObject doc = new BasicDBObject("username", Username).append("channel", Channel);
                coll.insert(doc);
                System.out.println(Username + " joined Channel : " + Channel);
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Delete a user from a channel (used in leave method)
 *
 * @param Channel/*from  w  w  w  .  j  av  a 2s  .com*/
 * @param Username
 * @return code
 */
private int DeleteChannelUser(String Channel, String Username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel);

        DBCursor cursor = coll.find(query);

        try {
            ret = 1;
            while (cursor.hasNext()) {
                ret = 1;
                coll.remove(cursor.next());
                ret = 0;
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has leaved Channel : " + Channel);
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Delete a user in all channel (used in cleaning)
 *
 * @param Username//from w w w  . ja  v  a 2  s  .c o m
 * @return code
 */
public int DeleteUserInChannel(String Username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username);

        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                coll.remove(cursor.next());
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has been deleted in Channel Collection!");
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Register a user to the server (used in RegUser)
 *
 * @param username/*from   ww w.j a  va 2  s  .c  o m*/
 * @return code
 */
private int AddUser(String username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                Date now = new Date();
                long timestamp_now = now.getTime();
                long treshold = timestamp_now - (1000 * 10); //10
                BasicDBObject temp = (BasicDBObject) cursor.next();
                Date time_temp = (Date) temp.get("timestamp");
                long timestamp_temp = time_temp.getTime();
                if (timestamp_temp < treshold) {
                    ret = 2;
                    System.out.println(username + " has joined back!");
                } else {
                    ret = 1;
                    System.err.println(username + " has been used !");
                }
            } else {
                java.util.Date date = new java.util.Date();
                BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date);
                coll.insert(doc);
                System.out.println(username + " online !");
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}