Search in sources :

Example 1 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class HTTPClientCASAAppExample method main.

/**
   * @param args
   * @throws IOException
   * @throws InvalidKeySpecException
   * @throws NoSuchAlgorithmException
   * @throws ClientException
   * @throws InvalidKeyException
   * @throws SignatureException
   * @throws Exception
   */
public static void main(String[] args) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException, ClientException, InvalidKeyException, SignatureException, Exception {
    // Create the client
    client = new HttpClient("127.0.0.1", 8080);
    // First thing we do is create an account to simulate the master account guid
    // that the ACS is using - don't worry about this for now.
    createSimulatedMasterAccount();
    //
    // Now on to the app commands.
    //
    // Check to see if our guid already exists
    String lookupResult = null;
    try {
        lookupResult = client.lookupGuid(ourAppAlias);
    } catch (ClientException e) {
    // The normal result if the alias doesn't exist
    } catch (IOException e) {
        System.out.println("\nProblem during lookupGuid: " + e);
        System.exit(1);
    }
    // If it exists we exit
    if (lookupResult != null) {
        System.out.println("\nGuid for " + ourAppAlias + " already exists. This can happen if you " + "run this class twice. Exiting.");
        System.exit(1);
    }
    System.out.println("\n// guid lookup\n" + "lookupGuid didn't find " + ourAppAlias + ". This is good.");
    // Note that accountGuidCreate looks up or creates a private public key pair for us.
    try {
        guidEntry = client.accountGuidCreate(ourAppAlias, "samplePassword");
    } catch (DuplicateNameException e) {
        // ignore as it is most likely because of a seemingly failed creation operation that actually succeeded.
        System.out.println("Account GUID " + guidEntry + " aready exists on the server; continuing.");
    }
    System.out.println("\n// guid create\n" + "Created guid for " + ourAppAlias + ": " + guidEntry.getGuid());
    //
    // Update the ACLs.
    // Next we remove the default read access that the GNS gives for all fields.
    client.aclRemove(AclAccessType.READ_WHITELIST, guidEntry, GNSProtocol.ENTIRE_RECORD.toString(), GNSProtocol.EVERYONE.toString());
    System.out.println("\n// acl update\n" + "Removed default read whitelist for " + ourAppAlias);
    // Look up the guid of the simulate master account we created above.
    // In the running architecture this is created by the ACS.
    String masterGuid;
    // Give the master ACS guid (simulated) access to all fields of our guid for reading and writing.
    masterGuid = client.lookupGuid(ourAppAlias);
    client.aclAdd(AclAccessType.READ_WHITELIST, guidEntry, GNSProtocol.ENTIRE_RECORD.toString(), masterGuid);
    client.aclAdd(AclAccessType.WRITE_WHITELIST, guidEntry, GNSProtocol.ENTIRE_RECORD.toString(), masterGuid);
    System.out.println("\n// acl update part two\n" + "Added read and write access for " + masterGuid + " to " + ourAppAlias);
    // Now do some writing and reading writing.
    // Create a JSON Object to initialize our guid record
    JSONObject json = new JSONObject("{\"occupation\":\"busboy\"," + "\"friends\":[\"Joe\",\"Sam\",\"Billy\"]," + "\"gibberish\":{\"meiny\":\"bloop\",\"einy\":\"floop\"}," + "\"location\":\"work\",\"name\":\"frank\"}");
    // Write out the JSON Object.
    client.update(guidEntry, json);
    System.out.println("\n// record update\n" + "client.update(guidEntry, record) // record=" + json);
    // And read the entire object back in.
    JSONObject result = client.read(guidEntry);
    System.out.println("client.read(guidEntry) -> " + result.toString());
    client.close();
    System.out.println("\nclient.close() // test successful");
}
Also used : JSONObject(org.json.JSONObject) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) HttpClient(edu.umass.cs.gnsclient.client.http.HttpClient) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException)

Example 2 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class HighConcurrencyReadsTest method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
    String gnsInstance = GNSClientCommands.getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        LOGGER.log(Level.FINE, "Creating account GUID {0}", new Object[] { ACCOUNT_GUID_PREFIX + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], ACCOUNT_GUID_PREFIX + i, PASSWORD);
            LOGGER.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
            LOGGER.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exceptions should be thrown up
    }
    System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    if (accountGuidsOnly) {
        for (int i = 0; i < accountGuidEntries.length; i++) {
            guidEntries[i] = accountGuidEntries[i];
        }
        return;
    }
    guidEntries = new GuidEntry[numGuids];
    Set<String> subGuids = new HashSet<String>();
    for (int i = 0, j = 0; i < numGuids; i++) {
        subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
        if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
            // because batch creation seems buggy
            if (subGuids.size() == 1) {
                String subGuid = subGuids.iterator().next();
                try {
                    GuidEntry created = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exceptions should be thrown up
            } else {
                try {
                    // batch create
                    clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
                } catch (ClientException | IOException e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                        } catch (DuplicateNameException de) {
                        // ignore, will retrieve it locally below
                        }
                    // any other exception should be throw up
                    }
                }
            }
            for (String subGuid : subGuids) {
                guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
            }
            LOGGER.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
            subGuids.clear();
        }
    }
    for (GuidEntry guidEntry : accountGuidEntries) {
        assert (guidEntry != null);
    }
    for (GuidEntry guidEntry : guidEntries) {
        assert (guidEntry != null);
    }
    System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) 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) HashSet(java.util.HashSet)

Example 3 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class GNSClientCapacityTest method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClient[numClients];
    executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(50 * numClients);
    for (int i = 0; i < numClients; i++) clients[i] = new GNSClientCommands();
    @SuppressWarnings("deprecation") String gnsInstance = GNSClient.getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        log.log(Level.FINE, "Creating account GUID {0}", new Object[] { accountGUIDPrefix + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + i, PASSWORD);
            log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i);
            log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exceptions should be thrown up
    }
    accessor = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + "_accessor", PASSWORD);
    System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    if (accountGuidsOnly) {
        for (int i = 0; i < accountGuidEntries.length; i++) guidEntries[i] = accountGuidEntries[i];
        return;
    }
    guidEntries = new GuidEntry[numGuids];
    Set<String> subGuids = new HashSet<String>();
    for (int i = 0, j = 0; i < numGuids; i++) {
        subGuids.add(accountGUIDPrefix + Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
        if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
            // because batch creation seems buggy
            if (subGuids.size() == 1) {
                String subGuid = subGuids.iterator().next();
                try {
                    GuidEntry created = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exceptions should be thrown up
            } else
                try {
                    // batch create
                    clients[0].execute(GNSCommand.batchCreateGUIDs(accountGuidEntries[i / numGuidsPerAccount], subGuids));
                } catch (Exception e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].execute(GNSCommand.guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid));
                        } catch (DuplicateNameException de) {
                        // ignore, will retrieve it locally below
                        }
                    // any other exception should be throw up
                    }
                }
            for (String subGuid : subGuids) {
                guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
            }
            log.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
            subGuids.clear();
        }
    }
    for (GuidEntry guidEntry : accountGuidEntries) assert (guidEntry != null);
    for (GuidEntry guidEntry : guidEntries) assert (guidEntry != null);
    System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) HashSet(java.util.HashSet)

Example 4 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class SomeReadsEncryptionFails method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
    for (int i = 0; i < numClients; i++) {
        clients[i] = new GNSClientCommands();
    }
    String gnsInstance = clients[0].getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        log.log(Level.FINE, "Creating account GUID {0}", new Object[] { ACCOUNT_GUID_PREFIX + i });
        try {
            accountGuidEntries[i] = clients[0].accountGuidCreate(ACCOUNT_GUID_PREFIX + i, PASSWORD);
            log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, ACCOUNT_GUID_PREFIX + i);
            log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exception should be throw up
    }
    System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    if (accountGuidsOnly) {
        for (int i = 0; i < accountGuidEntries.length; i++) {
            guidEntries[i] = accountGuidEntries[i];
        }
        return;
    }
    guidEntries = new GuidEntry[numGuids];
    Set<String> subGuids = new HashSet<String>();
    for (int i = 0, j = 0; i < numGuids; i++) {
        subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
        if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
            // because batch creation seems buggy
            if (subGuids.size() == 1) {
                String subGuid = subGuids.iterator().next();
                try {
                    GuidEntry created = clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exception should be throw up
            } else {
                try {
                    // batch create
                    clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
                } catch (Exception e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                        } catch (DuplicateNameException de) {
                        // ignore, will retrieve it locally below
                        }
                    // any other exception should be throw up
                    }
                }
            }
            for (String subGuid : subGuids) {
                guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
            }
            log.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
            subGuids.clear();
        }
    }
    for (GuidEntry guidEntry : accountGuidEntries) {
        assert (guidEntry != null);
    }
    for (GuidEntry guidEntry : guidEntries) {
        assert (guidEntry != null);
    }
    System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) HashSet(java.util.HashSet)

Example 5 with DuplicateNameException

use of edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException in project GNS by MobilityFirst.

the class GNSClientCapacityTest method setupClientsAndGuids.

private static void setupClientsAndGuids() throws Exception {
    clients = new GNSClientCommands[numClients];
    executor = (ScheduledThreadPoolExecutor) Executors.newScheduledThreadPool(numClients);
    for (int i = 0; i < numClients; i++) clients[i] = new GNSClientCommands();
    String gnsInstance = clients[0].getGNSProvider();
    accountGuidEntries = new GuidEntry[numAccountGuids];
    int numPreExisting = 0;
    for (int i = 0; i < numAccountGuids; i++) {
        log.log(Level.FINE, "Creating account GUID {0}", new Object[] { accountGUIDPrefix + i });
        try {
            accountGuidEntries[i] = GuidUtils.lookupOrCreateAccountGuid(clients[0], accountGUIDPrefix + i, PASSWORD);
            log.log(Level.FINE, "Created account {0}", new Object[] { accountGuidEntries[i] });
            assert (accountGuidEntries[i].getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i).getGuid()));
        } catch (DuplicateNameException e) {
            numPreExisting++;
            accountGuidEntries[i] = KeyPairUtils.getGuidEntry(gnsInstance, accountGUIDPrefix + i);
            log.log(Level.INFO, "Found that account {0} already exists", new Object[] { accountGuidEntries[i] });
        }
    // any other exceptions should be thrown up
    }
    System.out.println("Created (" + (numAccountGuids - numPreExisting) + ") or found pre-existing (" + numPreExisting + ") a total of " + numAccountGuids + " account GUIDs: " + Arrays.asList(accountGuidEntries));
    if (accountGuidsOnly) {
        for (int i = 0; i < accountGuidEntries.length; i++) guidEntries[i] = accountGuidEntries[i];
        return;
    }
    guidEntries = new GuidEntry[numGuids];
    Set<String> subGuids = new HashSet<String>();
    for (int i = 0, j = 0; i < numGuids; i++) {
        subGuids.add(Config.getGlobalString(TC.TEST_GUID_PREFIX) + i);
        if (subGuids.size() == numGuidsPerAccount || i == numGuids - 1) {
            // because batch creation seems buggy
            if (subGuids.size() == 1) {
                String subGuid = subGuids.iterator().next();
                try {
                    GuidEntry created = GuidUtils.lookupOrCreateGuid(clients[0], accountGuidEntries[i / numGuidsPerAccount], subGuid);
                    assert (created.getGuid().equals(KeyPairUtils.getGuidEntry(gnsInstance, subGuid).getGuid()));
                } catch (DuplicateNameException de) {
                // ignore, will retrieve it locally below
                }
            // any other exceptions should be thrown up
            } else
                try {
                    // batch create
                    clients[0].guidBatchCreate(accountGuidEntries[i / numGuidsPerAccount], subGuids);
                } catch (Exception e) {
                    for (String subGuid : subGuids) {
                        try {
                            clients[0].guidCreate(accountGuidEntries[i / numGuidsPerAccount], subGuid);
                        } catch (DuplicateNameException de) {
                        // ignore, will retrieve it locally below
                        }
                    // any other exception should be throw up
                    }
                }
            for (String subGuid : subGuids) {
                guidEntries[j++] = KeyPairUtils.getGuidEntry(gnsInstance, subGuid);
            }
            log.log(Level.FINE, "Created sub-guid(s) {0}", new Object[] { subGuids });
            subGuids.clear();
        }
    }
    for (GuidEntry guidEntry : accountGuidEntries) assert (guidEntry != null);
    for (GuidEntry guidEntry : guidEntries) assert (guidEntry != null);
    System.out.println("Created or found " + guidEntries.length + " pre-existing sub-guids " + Arrays.asList(guidEntries));
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) DuplicateNameException(edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException) HashSet(java.util.HashSet)

Aggregations

DuplicateNameException (edu.umass.cs.gnscommon.exceptions.client.DuplicateNameException)8 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)7 IOException (java.io.IOException)7 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)6 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)6 HashSet (java.util.HashSet)6 JSONException (org.json.JSONException)3 HttpClient (edu.umass.cs.gnsclient.client.http.HttpClient)1 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)1 AclException (edu.umass.cs.gnscommon.exceptions.client.AclException)1 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)1 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)1 InvalidFieldException (edu.umass.cs.gnscommon.exceptions.client.InvalidFieldException)1 InvalidGuidException (edu.umass.cs.gnscommon.exceptions.client.InvalidGuidException)1 VerificationException (edu.umass.cs.gnscommon.exceptions.client.VerificationException)1 RandomString (edu.umass.cs.gnscommon.utils.RandomString)1 JSONObject (org.json.JSONObject)1