Example usage for com.mongodb MongoClient MongoClient

List of usage examples for com.mongodb MongoClient MongoClient

Introduction

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

Prototype

public MongoClient(final MongoClientURI uri) 

Source Link

Document

Creates a Mongo described by a URI.

Usage

From source file:com.gemini.discover.networkdiscover.GeminiNetworkDiscover.java

private static List<Host> discoverAllEnvNetworks(String envName) throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient("localhost");

    //get the environment object
    GeminiEnvironmentRepositoryMongoDBImpl netDB = new GeminiEnvironmentRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    GeminiEnvironment env = netDB.getEnvByName(envName);
    if (env == null) {
        Logger.error("Error: Discover Single Network - environment with name {} not found", envName);
        return null;
    }/*from   w  w w .j  av a2  s .  co  m*/

    //discover network
    discSvc.setDiscType(env.getType());
    List<GeminiNetwork> lNet = env.getNetworks();
    lNet.stream().forEach(l -> l.setDiscovered(false));
    return discSvc.discoverNetworks(lNet);
}

From source file:com.gemini.geminimain.GeminiMain.java

public static void main(String[] args) throws IOException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    //create table for the application, networks and servers
    ds = morphia.createDatastore(mongoClient, "Gemini");

    //create the mapper
    Injector injector = Guice.createInjector(new GeminiMapperModule());
    GeminiMapper mapper = injector.getInstance(GeminiMapper.class);

    //set the current logging level to debug
    Configurator.currentConfig().level(Level.INFO).activate();

    //create some data to transfer to the front end
    createSampleData();//from  www. jav  a  2s  .  c  o m

    //close the db client
    mongoClient.close();

    //check if authenticated, create the call context and user context here
    //for now it is empty!!!!
    before((request, response) -> {
        boolean authenticated = true;
        // ... check if authenticated
        if (!authenticated) {
            halt(401, "Nice try, you are not welcome here");
        }
    });

    after((request, response) -> {
        response.header("Access-Control-Allow-Origin", "*");
        //response.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        //response.header("Access-Control-Max-Age", "3600");
        //response.header("Access-Control-Allow-Headers", "x-requested-with");
    });

    //get all environments of the tenant
    get("/environments/:tenantid", "application/json", (request, response) -> {
        String tenantID = request.params("tenantid");
        try {
            List<GeminiEnvironment> lEnvs = getEnvironments(tenantID);
            if (lEnvs != null) {
                response.status(200);
                Logger.info("Found environments for tenant {}", tenantID);
                List<GeminiEnvironmentDTO> dtoEnvs = new ArrayList();
                lEnvs.stream().forEach(e -> dtoEnvs.add(mapper.getDTOFromEnv(e)));
                return lEnvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {}", DB_SERVER);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }

        return "not implemented yet";
    }, new JsonTransformer());

    //return all applications for a given tenant and environment
    get("/applications/:tenantid/:envname", "application/json", (request, response) -> {
        String tenantID, envName;
        try {
            tenantID = URLDecoder.decode(request.params(":tenantid"), "UTF-8");
            envName = URLDecoder.decode(request.params(":envname"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }

        try {
            List<GeminiApplication> apps = getEnvApplications(tenantID, envName);
            if (apps == null || apps.isEmpty()) {
                response.status(404);
                Logger.info("Could not find any applications.");
                return "No Applications found.";
            } else {
                response.status(200);
                Logger.debug("Found applications");
                List<GeminiApplicationDTO> dtoApps = new ArrayList();
                apps.stream().forEach((a) -> {
                    dtoApps.add(mapper.getDTOFromApp(a));
                });
                return dtoApps;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {}", DB_SERVER);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return application given a name
    get("/applications/:name", "application/json", (request, response) -> {
        String appName = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - application Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL - server name " + appName;
        }
        try {
            GeminiApplication a = getAppByName(appName);
            if (a != null) {
                Logger.debug("Found application {}", appName);
                return mapper.getDTOFromApp(a);
            } else {
                Logger.info("Could not find application {}", appName);
                return "Could not find application " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    post("/applications", (request, response) -> {
        String body = request.body();
        return "Hello World: " + request.body();
    });

    //return all networks related to application with ID = ':id'
    get("/applications/:name/networks", "application/json", (Request request, Response response) -> {
        String appName;
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiNetwork> lNet = getAppNetworks(appName);
            if (lNet != null) {
                Logger.debug("Found networks for application {}", appName);
                List<GeminiNetworkDTO> dtoNets = new ArrayList();
                lNet.stream().forEach(aNet -> dtoNets.add(mapper.getDTOFromNetwork(aNet)));
                return dtoNets;
            } else {
                response.status(404);
                Logger.info("Could not find any networks for application {}", appName);
                return "Could not find any networks for application: " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            response.status(500);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return all servers related to application with ID = ':id'
    get("/applications/:id/servers", "application/json", (Request request, Response response) -> {
        String appName = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            appName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiServer> lSrv = getAppServers(appName);
            if (lSrv != null) {
                Logger.debug("Found servers for application {}", appName);
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : lSrv) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                return dtoSrvs;
            } else {
                Logger.info("Could not find servers for application {}", appName);
                response.status(404);
                return "Could not find servers for application: " + appName;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    //return all servers related to application with ID = ':appID' AND network with ID = ':nID'
    get("/applications/:appname/networks/:netstart/:netend/servers", "application/json",
            (request, response) -> {
                String appName = "", netStart = "", netEnd = "";
                //decode the URL as it may contain escape characters, etc.
                try {
                    appName = URLDecoder.decode(request.params(":appname"), "UTF-8");
                    netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
                    netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
                } catch (UnsupportedEncodingException ex) {
                    Logger.error(
                            "Severe Error: Unsupported encoding in URL - application {} with network start: {} and end: {} Exception {}",
                            request.params(":appname"), request.params(":netstart"), request.params(":netend"),
                            ex);
                    return "Severe Error: Unsupported encoding in URL";
                }

                //get the servers for app network
                try {
                    List<GeminiServer> lSrv = getAppNetworkServers(appName, netStart, netEnd);
                    if (lSrv == null || lSrv.isEmpty()) {
                        Logger.info("No servers for application {} with network start: {} and end: {}", appName,
                                netStart, netEnd);
                        response.status(404);
                        return "No servers for application " + appName + " with network start: " + netStart
                                + " and end: " + netEnd;
                    } else {
                        Logger.debug("Found servers for application {} with network start: {} and end: ",
                                appName, netStart, netEnd);
                        List<GeminiServerDTO> dtoSrvs = new ArrayList();
                        for (GeminiServer s : lSrv) {
                            dtoSrvs.add(mapper.getDTOFromServer(s));
                        }
                        return dtoSrvs;
                    }
                } catch (UnknownHostException ex) {
                    Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
                    return "Severe Error: Unknown database host " + DB_SERVER;
                }
            }, new JsonTransformer());

    //get the networks for a tenant and environment,
    get("/networks/:tenantid/:envname", "application/json", (request, response) -> {
        String tenantID, envName;
        try {
            tenantID = URLDecoder.decode(request.params(":tenantid"), "UTF-8");
            envName = URLDecoder.decode(request.params(":envname"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - server Name: {} Exception: {}",
                    request.params(":name"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiNetwork> nets = getEnvNetworks(tenantID, envName);
            if (nets == null) {
                Logger.info("No networks discovered for tenant {} in environment {}", tenantID, envName);
                return "No networks discovered for tenant" + tenantID + "in environment" + envName;
            } else {
                response.status(200);
                List<GeminiNetworkDTO> dtoNets = new ArrayList();
                nets.stream().forEach(n -> dtoNets.add(mapper.getDTOFromNetwork(n)));
                Logger.debug("Found {} networks for tenant {} env {}", nets.size(), tenantID, envName);
                return dtoNets;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown datbase host - {}", DB_SERVER);
            return "Severe Error: Unknown data host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/networks/:netstart/:netend", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            GeminiNetwork n = getNetworkFromDB(netStart, netEnd);
            if (n == null) {
                Logger.info("No network with start {} and end {} found", netStart, netEnd);
                return "No network with start " + netStart + " and end " + netEnd + " found";
            } else {
                Logger.debug("Found network with start {} and end {} ", netStart, netEnd);
                return mapper.getDTOFromNetwork(n);
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/networks/:netstart/:netend/servers", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            List<GeminiServer> lSrv = getNetworkServersFromDB(netStart, netEnd);
            if (lSrv == null) {
                Logger.info("No servers in network with start {} and end {} found", netStart, netEnd);
                return "No servers in network with start " + netStart + " and end " + netEnd + " found";
            } else {
                Logger.debug("Found servers in network with start {} and end {} ", netStart, netEnd);
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : lSrv) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                return dtoSrvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    post("/networks/:netstart/:netend", "application/json", (request, response) -> {
        String netStart = "", netEnd = "";
        //decode the URL as it may contain escape characters, etc.
        try {
            netStart = URLDecoder.decode(request.params(":netstart"), "UTF-8");
            netEnd = URLDecoder.decode(request.params(":netend"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - netStart: {} netEnd: {} Exception: {}",
                    request.params(":netstart"), request.params(":netend"), ex);
            return "Severe Error: Unsupported encoding in URL";
        }

        //return the discovered networks
        //            DiscoverNetworkRange newNet = new DiscoverNetworkRange(netStart, netEnd);
        //            List<GeminiNetwork> lNet;
        //            if (autoDiscover) {
        //                try {
        //                    //start discovering...
        //                    lNet = discoverNetworks(netStart, netEnd);
        //                } catch (IOException ex) {
        //                    java.util.logging.Logger.getLogger(GeminiMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        //                }
        //            }

        //since all the services are running on the same computer
        response.header("Access-Control-Allow-Origin", "*");

        //return the networks...
        return "no networks";
    }, new JsonTransformer());

    //get all servers for tenant within an environment
    get("/servers", "application/json", (request, response) -> {
        try {
            List<GeminiServer> srvs = getServersFromDB();
            if (srvs == null) {
                Logger.info("Found no servers in database");
                return "No Networks";
            } else {
                Logger.debug("Found servers in database");
                List<GeminiServerDTO> dtoSrvs = new ArrayList();
                for (GeminiServer s : srvs) {
                    dtoSrvs.add(mapper.getDTOFromServer(s));
                }
                response.status(200);
                return dtoSrvs;
            }
        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

    get("/servers/:name", "application/json", (request, response) -> {
        String srvName;
        try {
            srvName = URLDecoder.decode(request.params(":name"), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            Logger.error("Severe Error: Unsupported encoding in URL - {} Exception {}", request.params(":name"),
                    ex);
            return "Severe Error: Unsupported encoding in URL";
        }
        try {
            GeminiServer s = getServerFromDB(srvName);
            if (s == null) {
                Logger.info("No server with name {} found", srvName);
                return "No server with name " + srvName;

            } else {
                Logger.debug("Found server with name {}", srvName);
                return mapper.getDTOFromServer(s);
            }

        } catch (UnknownHostException ex) {
            Logger.error("Severe Error: Unknown host - {} Exception: {}", DB_SERVER, ex);
            return "Severe Error: Unknown database host " + DB_SERVER;
        }
    }, new JsonTransformer());

}

From source file:com.gemini.geminimain.GeminiMain.java

private static List<GeminiApplication> getApplicationsFromDB() throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiApplicationRepositoryMongoDBImpl appDB = new GeminiApplicationRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    List<GeminiApplication> l = appDB.list();

    //close the db client
    mongoClient.close();/*  w ww  .j a va  2 s .c  o m*/

    return l;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static List<GeminiNetwork> getNetworksFromDB() throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiNetworkRepositoryMongoDBImpl appDB = new GeminiNetworkRepositoryMongoDBImpl(mongoClient, morphia,
            "Gemini");
    List<GeminiNetwork> l = appDB.list();

    //close the db client
    mongoClient.close();/* w w w .  ja  va  2 s. c om*/
    return l;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static List<GeminiServer> getServersFromDB() throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiServerRepositoryMongoDBImpl appDB = new GeminiServerRepositoryMongoDBImpl(mongoClient, morphia,
            "Gemini");
    List<GeminiServer> l = appDB.list();

    //close the db client
    mongoClient.close();//w w w  .j  av a 2s .c o  m
    return l;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static GeminiApplication getAppByName(String appName) throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiApplicationRepositoryMongoDBImpl appDB = new GeminiApplicationRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    GeminiApplication a = appDB.getAppByName(appName);

    //close the db client
    mongoClient.close();/*from  www . j  a v a 2 s .c  o m*/

    return a;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static List<GeminiNetwork> getAppNetworks(String appName) throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiApplicationRepositoryMongoDBImpl appDB = new GeminiApplicationRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    List<GeminiNetwork> lNet = appDB.getAppNetworks(appName);

    //close the db client
    mongoClient.close();/*www.java2s  . com*/

    return lNet;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static List<GeminiServer> getAppServers(String appName) throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiApplicationRepositoryMongoDBImpl appDB = new GeminiApplicationRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    List<GeminiServer> lSrv = appDB.getAppServers(appName);

    //close the db client
    mongoClient.close();//from   www  .  j  a v a  2 s .  co m

    return lSrv;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static List<GeminiServer> getAppNetworkServers(String appName, String netStart, String netEnd)
        throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiApplicationRepositoryMongoDBImpl appDB = new GeminiApplicationRepositoryMongoDBImpl(mongoClient,
            morphia, "Gemini");
    List<GeminiServer> lSrv = appDB.getNetworkServers(appName, netStart, netEnd);

    //close the db client
    mongoClient.close();/*from  w w  w.j a v  a 2  s .com*/

    return lSrv;
}

From source file:com.gemini.geminimain.GeminiMain.java

private static GeminiNetwork getNetworkFromDB(String netStart, String netEnd) throws UnknownHostException {
    //setup the mongodb access
    mongoClient = new MongoClient(DB_SERVER);

    GeminiNetworkRepositoryMongoDBImpl netDB = new GeminiNetworkRepositoryMongoDBImpl(mongoClient, morphia,
            "Gemini");
    GeminiNetwork aNet = netDB.getNetByStartAndEnd(netStart, netEnd);

    //close the db client
    mongoClient.close();//from  w  w  w  .j av a  2  s . co  m

    return aNet;
}