Search in sources :

Example 11 with GuidEntry

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

the class SelectTest method test_058_QuerySelectwithProjectionAll.

/**
   * Check a query select with a projection of all fields
   */
@Test
public void test_058_QuerySelectwithProjectionAll() {
    String fieldName = "testQueryProjectionAll";
    try {
        for (int cnt = 0; cnt < 5; cnt++) {
            String queryTestName = "queryTest-" + RandomString.randomString(12);
            client.execute(GNSCommand.createGUID(masterGuid, queryTestName));
            GuidEntry testEntry = GuidUtils.getGUIDKeys(queryTestName);
            // Remove default all fields / all guids ACL;
            client.execute(GNSCommand.aclRemove(AclAccessType.READ_WHITELIST, testEntry, GNSProtocol.ENTIRE_RECORD.toString(), GNSProtocol.ALL_GUIDS.toString()));
            // save them so we can delete them later
            CREATED_GUIDS.add(testEntry);
            JSONObject json = new JSONObject();
            json.put(fieldName, Arrays.asList(25));
            json.put("field1", "value1");
            JSONObject subJson = new JSONObject();
            subJson.put("subfield", "subvalue1");
            json.put("nested", subJson);
            client.execute(GNSCommand.update(testEntry.getGuid(), json, testEntry));
        }
        waitSettle(WAIT_SETTLE);
    } catch (ClientException | IOException | JSONException e) {
        Utils.failWithStackTrace("Exception while trying to create the guids: " + e);
    }
    try {
        String query = "~" + fieldName + " : ($gt: 0)";
        JSONArray result = waitForQueryResults(query);
        //JSONArray result = client.execute(GNSCommand.selectRecords(masterGuid, query, null)).getResultJSONArray();
        for (int i = 0; i < result.length(); i++) {
            System.out.println(result.get(i).toString());
        }
        // best we can do should be at least 5, but possibly more objects in results
        Assert.assertThat(result.length(), Matchers.greaterThanOrEqualTo(5));
    } catch (JSONException e) {
        //} catch (ClientException | IOException | JSONException e) {
        Utils.failWithStackTrace("Exception executing selectRecords: " + e);
    }
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) RandomString(edu.umass.cs.gnscommon.utils.RandomString) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) Test(org.junit.Test) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest)

Example 12 with GuidEntry

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

the class ConsoleModule method useGnsDefaults.

/**
   * Connect and try to find default GNSProtocol.GUID.toString() if defined.
   */
public void useGnsDefaults() {
    GuidEntry guid = KeyPairUtils.getDefaultGuidEntry(getGnsInstance());
    if (guid == null) {
        return;
    }
    try {
        printString("Looking for default GUID: " + guid + "\n");
        new GuidUse(this).parse(guid.getEntityName());
    } catch (Exception e) {
        printString("Couldn't connect default GUID " + guid);
    }
}
Also used : GuidUse(edu.umass.cs.gnsclient.console.commands.GuidUse) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with GuidEntry

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

the class AccountDelete 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();
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        try {
            gnsClient.lookupGuid(aliasName);
        } catch (IOException | ClientException expected) {
            printString("Alias " + aliasName + " doesn't exist.\n");
            return;
        }
        GuidEntry myGuid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), aliasName);
        if (myGuid == null) {
            printString("Unable to retrieve GUID for alias " + aliasName + "\n");
            return;
        }
        gnsClient.accountGuidRemove(myGuid);
        KeyPairUtils.removeKeyPair(module.getGnsInstance(), aliasName);
        module.setCurrentGuidAndCheckForVerified(null);
        module.setPromptString(ConsoleModule.CONSOLE_PROMPT + ">");
        if (!module.isSilent()) {
            printString("Removed account GUID " + myGuid.getGuid() + "\n");
        }
    } catch (ClientException | IOException e) {
        printString("Failed to delete guid ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry)

Example 14 with GuidEntry

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

the class GuidImport method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 1) {
            wrongArguments();
            return;
        }
        String filename = st.nextToken();
        File f = new File(filename);
        GuidEntry guidEntry;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {
            guidEntry = new GuidEntry(ois);
            ois.close();
        }
        KeyPairUtils.saveKeyPair(module.getGnsInstance(), guidEntry.getEntityName(), guidEntry.getGuid(), new KeyPair(guidEntry.getPublicKey(), guidEntry.getPrivateKey()));
        console.printString("Keys for " + guidEntry.getEntityName() + " read from " + filename + " and saved in local preferences.");
        console.printNewline();
    } catch (IOException | EncryptionException e) {
        console.printString("Failed to load keys ( " + e + ")\n");
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) KeyPair(java.security.KeyPair) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) IOException(java.io.IOException) File(java.io.File) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 15 with GuidEntry

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

the class AccountVerify method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        GNSClientCommands client = module.getGnsClient();
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 2) {
            wrongArguments();
            return;
        }
        String alias = st.nextToken();
        GuidEntry guid = KeyPairUtils.getGuidEntry(module.getGnsInstance(), alias);
        if (guid == null) {
            printString("Unable to retrieve GUID for alias " + alias + "\n");
            return;
        }
        String code = st.nextToken();
        try {
            if (client.accountGuidVerify(guid, code).startsWith(GNSProtocol.OK_RESPONSE.toString())) {
                printString("Account verified.\n");
                module.setAccountVerified(true);
                return;
            }
        // this happens if it was already verified, but we didn't notice
        } catch (VerificationException e) {
            module.setAccountVerified(true);
            printString("Account already verified" + "\n");
            return;
        } catch (Exception e) {
            printString("Account not verified: " + e + "\n");
        }
    } catch (Exception e) {
        console.printString("Failed to access GNS ( " + e + ")\n");
    }
    module.setAccountVerified(false);
}
Also used : StringTokenizer(java.util.StringTokenizer) GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) VerificationException(edu.umass.cs.gnscommon.exceptions.client.VerificationException)

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