Search in sources :

Example 1 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class SelectRecordsExample method createUserRecords.

/**
   * Create some example user records.
   */
private static void createUserRecords() {
    Random random = new Random();
    for (int i = 0; i < 30; i++) {
        try {
            client.execute(GNSCommand.createGUID(accountGuidEntry, "user-" + i));
            GuidEntry userGuid = GuidUtils.getGUIDKeys("user-" + i);
            System.out.print("+");
            // Randomly place the in the region
            double lon = LEFT_LON + (RIGHT_LON - LEFT_LON) * random.nextDouble();
            double lat = BOTTOM_LAT + (TOP_LAT - BOTTOM_LAT) * random.nextDouble();
            client.execute(GNSCommand.setLocation(userGuid, lon, lat));
            // Add some additional random attributes
            client.execute(GNSCommand.fieldUpdate(userGuid, "age", random.nextInt(40) + 20));
            client.execute(GNSCommand.fieldUpdate(userGuid, "preference", random.nextInt(2) == 0 ? "Long" : "Short"));
        } catch (ClientException e) {
            // Catch the normal case where the records already exist
            if (ResponseCode.CONFLICTING_GUID_EXCEPTION.equals(e.getCode())) {
                System.out.print(".");
            } else {
                System.out.println("Problem creating user: " + e.getMessage());
            }
        } catch (IOException e) {
            System.out.println("Problem creating user: " + e.getMessage());
        }
    }
    System.out.println();
}
Also used : Random(java.util.Random) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 2 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class Select method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        StringTokenizer st = new StringTokenizer(commandText.trim());
        GuidEntry guidEntry;
        switch(st.countTokens()) {
            case 2:
                guidEntry = null;
                break;
            case 3:
                String alias = st.nextToken();
                guidEntry = KeyPairUtils.getGuidEntry(module.getGnsInstance(), alias);
                if (guidEntry == null) {
                    console.printString("Unknown alias " + alias);
                    console.printNewline();
                    return;
                }
                break;
            default:
                wrongArguments();
                return;
        }
        String field = st.nextToken();
        String value = st.nextToken();
        JSONArray result;
        if (guidEntry != null) {
            result = gnsClient.select(guidEntry, field, value);
        } else {
            result = gnsClient.select(field, value);
        }
        console.printString(result.toString());
        console.printNewline();
    } catch (IOException | ClientException e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONArray(org.json.JSONArray) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 3 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class SetDefaultGuid method parse.

@Override
public void parse(String commandText) throws Exception {
    StringTokenizer st = new StringTokenizer(commandText.trim());
    if (st.countTokens() != 1) {
        wrongArguments();
        return;
    }
    String aliasName = st.nextToken();
    GNSClientCommands gnsClient = module.getGnsClient();
    try {
        try {
            gnsClient.lookupGuid(aliasName);
        } catch (ClientException e) {
            console.printString("Alias " + aliasName + " is not registered in the GNS\n");
            return;
        }
        if (!module.isSilent()) {
            console.printString("Looking up alias " + aliasName + " GUID and certificates...\n");
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid == null) {
            console.printString("You do not have the private key for alias " + aliasName);
            console.printNewline();
            return;
        }
        // Success, let's update the console prompt with the new alias name
        // module.setCurrentGuid(myGuid);
        module.setDefaultGuidAndCheckForVerified(myGuid);
        if (!module.isSilent()) {
            console.printString("Default GUID set to " + aliasName + "\n");
        }
    } catch (IOException e) {
        console.printString("Failed to access the GNS ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 4 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class PrivateKeyExport method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 2) {
            wrongArguments();
            return;
        }
        String aliasName = st.nextToken();
        String filename = st.nextToken();
        if (!module.isSilent()) {
            console.printString("Looking up alias " + aliasName + " GUID and certificates...\n");
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid == null) {
            console.printString("You do not have the private key for alias " + aliasName);
            console.printNewline();
            return;
        }
        if (KeyPairUtils.writePrivateKeyToPKCS8File(myGuid.getPrivateKey(), filename)) {
            console.printString("Private key for " + aliasName + " stored in " + filename + " in " + myGuid.getPrivateKey().getFormat() + " format.");
            console.printNewline();
        }
    } catch (Exception e) {
        console.printString("Failed to save keys ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 5 with GuidEntry

use of edu.umass.cs.gnsclient.client.util.GuidEntry in project GNS by MobilityFirst.

the class CreateSomeRecordsForDns method createAnARecord.

// Creates an A record in the GNS
private static void createAnARecord(String domain, String address) throws Exception {
    // domains need to end with a period
    if (!domain.endsWith(".")) {
        domain = domain + ".";
    }
    GuidEntry guid = GuidUtils.lookupOrCreateGuid(client, accountGuid, domain);
    client.fieldUpdate(guid, "A", address);
    System.out.println("Value of A in " + guid.getEntityName() + " is " + client.fieldRead(guid, "A"));
}
Also used : GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Aggregations

GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)134 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)87 IOException (java.io.IOException)85 Test (org.junit.Test)69 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)61 RandomString (edu.umass.cs.gnscommon.utils.RandomString)54 BasicGuidEntry (edu.umass.cs.gnsclient.client.util.BasicGuidEntry)46 JSONException (org.json.JSONException)45 JSONArray (org.json.JSONArray)36 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)18 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)15 DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)14 StringTokenizer (java.util.StringTokenizer)13 JSONObject (org.json.JSONObject)12 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)11 FileNotFoundException (java.io.FileNotFoundException)11 HashSet (java.util.HashSet)10 FileInputStream (java.io.FileInputStream)8 Repeat (edu.umass.cs.utils.Repeat)7