private void parseOptionsFromURI(MongoClientURI uri, Map<String, String> options) { if (uri.getUsername() != null && uri.getUsername().isEmpty() == false) { options.put(MongoDumpOptions.USERNAME, uri.getUsername()); } if (uri.getPassword() != null && uri.getPassword().length > 0) { options.put(MongoDumpOptions.PASSWORD, new String(uri.getPassword())); } if (uri.getHosts() != null && uri.getHosts().isEmpty() == false) { final String hostWithPort = uri.getHosts().get(0); final Pattern p = Pattern.compile("(.*)(:(\\d+))?"); final Matcher m = p.matcher(hostWithPort); if (m.matches()) { final String host = m.group(1); final String port = m.group(3); if (host.isEmpty() == false) { options.put(MongoDumpOptions.HOST, host); if (port != null) { options.put(MongoDumpOptions.PORT, port); } } } } if (uri.getDatabase() != null && uri.getDatabase().isEmpty() == false) { options.put(MongoDumpOptions.DB, uri.getDatabase()); } if (uri.getCollection() != null && uri.getCollection().isEmpty() == false) { options.put(MongoDumpOptions.COLLECTION, uri.getCollection()); } }
public void setMongoURI(MongoClientURI uri) { hostsListModel.clear(); optionsListModel.clear(); for (String string : uri.getHosts()) { final String[] rawHost = string.split(":"); final String hostname = urlDecode(rawHost[0]); final Integer port = rawHost.length > 1 ? Integer.parseInt(rawHost[1]) : null; hostsListModel.addElement(new Host(hostname, port)); } usernameField.setText(uri.getUsername()); if (uri.getPassword() != null) { passwordField.setText(new String(uri.getPassword())); } databaseField.setText(uri.getDatabase()); final List<Option> options = decodeOptions(uri); optionsListModel.clear(); for (Option option : options) { optionsListModel.addElement(option); } updateURIField(); }
/** * Get an authenticated DBCollection from a MongodB URI. * * @param authURI the URI with which to authenticate * @param uri the MongoDB URI * @return the authenticated DBCollection */ public static DBCollection getCollectionWithAuth( final MongoClientURI uri, final MongoClientURI authURI) { // Make sure auth uri is valid and actually has a username/pw to use if (authURI == null || authURI.getUsername() == null || authURI.getPassword() == null) { throw new IllegalArgumentException( "auth URI is empty or does not contain a valid username/password combination."); } DBCollection coll; try { Mongo mongo = getMongoClient(authURI); coll = mongo.getDB(uri.getDatabase()).getCollection(uri.getCollection()); return coll; } catch (Exception e) { throw new IllegalArgumentException("Couldn't connect and authenticate to get collection", e); } }