Search in sources :

Example 41 with ClientException

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

the class GNSClientCommandsExample 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 that connects to a default reconfigurator as
		 * specified in gigapaxos properties file. */
    client = new GNSClientCommands();
    System.out.println("[Client connected to GNS]\n");
    try {
        /**
       * Create an account GUID if one doesn't already exists. The true
       * flag makes it verbosely print out what it is doing. The password
       * is for future use and is needed mainly if the keypair is
       * generated on the server in order to retrieve the private key.
       * lookupOrCreateAccountGuid "cheats" by bypassing email-based or
       * other verification mechanisms using a shared secret between the
       * server and the client.
       *
       */
        System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)");
        guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // 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(guid, json);
    System.out.println("\n// record update\n" + "client.update(GUID, record) // record=" + json);
    // and read the entire object back in
    JSONObject result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Change a field
    client.update(guid, new JSONObject("{\"occupation\":\"rocket scientist\"}"));
    System.out.println("\n// field update\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue={\"occupation\":\"rocket scientist\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add a field
    client.update(guid, new JSONObject("{\"ip address\":\"127.0.0.1\"}"));
    System.out.println("\n// field add\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue= {\"ip address\":\"127.0.0.1\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Remove a field
    client.fieldRemove(guid.getGuid(), "gibberish", guid);
    System.out.println("\n// field remove\n" + "client.fieldRemove(GUID, \"gibberish\")");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add some more stuff to read back
    JSONObject newJson = new JSONObject();
    JSONObject subJson = new JSONObject();
    subJson.put("sally", "red");
    subJson.put("sammy", "green");
    JSONObject subsubJson = new JSONObject();
    subsubJson.put("right", "seven");
    subsubJson.put("left", "eight");
    subJson.put("sally", subsubJson);
    newJson.put("flapjack", subJson);
    client.update(guid, newJson);
    System.out.println("\n// field add with JSON value\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue=" + newJson);
    // Read a single field at the top level
    String resultString = client.fieldRead(guid, "flapjack");
    System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
    // Read a single field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sally.right");
    System.out.println("\n// dotted field read\n" + "client.fieldRead(GUID, \"flapjack.sally.right\") -> " + resultString);
    // Update a field using dot notation
    JSONArray newValue = new JSONArray(Arrays.asList("One", "Ready", "Frap"));
    client.fieldUpdate(guid, "flapjack.sammy", newValue);
    System.out.println("\n// dotted field update\n" + "client.fieldUpdate(GUID, \"flapjack.sammy\", " + newValue);
    // Read the same field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sammy");
    System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + resultString);
    // Read two fields at a time
    resultString = client.fieldRead(guid, new ArrayList<String>(Arrays.asList("name", "occupation")));
    System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"] -> " + resultString);
    // Read the entire object back in
    result = client.read(guid);
    System.out.println("\nclient.read(GUID) -> " + result.toString());
    // Delete created GUID
    client.accountGuidRemove(guid);
    System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + guid);
    // Try read the entire record
    try {
        result = client.read(guid);
    } catch (Exception e) {
        System.out.println("\n// non-existent GUID error (expected)\n" + "client.read(GUID) // GUID= " + guid + "\n  " + e.getMessage());
    }
    client.close();
    System.out.println("\nclient.close() // test successful");
}
Also used : GNSClientCommands(edu.umass.cs.gnsclient.client.GNSClientCommands) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 42 with ClientException

use of edu.umass.cs.gnscommon.exceptions.client.ClientException 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 43 with ClientException

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

the class HTTPClientExample 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 will connect to GNS HTTP server running locally.	 
    client = new HttpClient("127.0.0.1", 8080);
    try {
        /**
       * Create an account GUID if one doesn't already exists. The true
       * flag makes it verbosely print out what it is doing. The password
       * is for future use.
       * lookupOrCreateAccountGuid "cheats" by bypassing email-based or
       * other verification mechanisms using a shared secret between the
       * server and the client.
       *
       */
        System.out.println("// account GUID creation\n" + "GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS," + " \"password\", true)");
        guid = GuidUtils.lookupOrCreateAccountGuid(client, ACCOUNT_ALIAS, "password", true);
    } catch (Exception | Error e) {
        System.out.println("Exception during accountGuid creation: " + e);
        e.printStackTrace();
        System.exit(1);
    }
    // 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(guid, json);
    System.out.println("\n// record update\n" + "client.update(GUID, record) // record=" + json);
    // and read the entire object back in
    JSONObject result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Change a field
    client.update(guid, new JSONObject("{\"occupation\":\"rocket scientist\"}"));
    System.out.println("\n// field update\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue={\"occupation\":\"rocket scientist\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add a field
    client.update(guid, new JSONObject("{\"ip address\":\"127.0.0.1\"}"));
    System.out.println("\n// field add\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue= {\"ip address\":\"127.0.0.1\"}");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Remove a field
    client.fieldRemove(guid.getGuid(), "gibberish", guid);
    System.out.println("\n// field remove\n" + "client.fieldRemove(GUID, \"gibberish\")");
    // and read the entire object back in
    result = client.read(guid);
    System.out.println("client.read(GUID) -> " + result.toString());
    // Add some more stuff to read back
    JSONObject newJson = new JSONObject();
    JSONObject subJson = new JSONObject();
    subJson.put("sally", "red");
    subJson.put("sammy", "green");
    JSONObject subsubJson = new JSONObject();
    subsubJson.put("right", "seven");
    subsubJson.put("left", "eight");
    subJson.put("sally", subsubJson);
    newJson.put("flapjack", subJson);
    client.update(guid, newJson);
    System.out.println("\n// field add with JSON value\n" + "client.update(GUID, fieldKeyValue) // fieldKeyValue=" + newJson);
    // Read a single field at the top level
    String resultString = client.fieldRead(guid, "flapjack");
    System.out.println("client.fieldRead(\"flapjack\") -> " + resultString);
    // Read a single field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sally.right");
    System.out.println("\n// dotted field read\n" + "client.fieldRead(GUID, \"flapjack.sally.right\") -> " + resultString);
    // Update a field using dot notation
    JSONArray newValue = new JSONArray(Arrays.asList("One", "Ready", "Frap"));
    client.fieldUpdate(guid, "flapjack.sammy", newValue);
    System.out.println("\n// dotted field update\n" + "client.fieldUpdate(GUID, \"flapjack.sammy\", " + newValue);
    // Read the same field using dot notation
    resultString = client.fieldRead(guid, "flapjack.sammy");
    System.out.println("client.fieldRead(GUID, \"flapjack.sammy\") -> " + resultString);
    // Read two fields at a time
    resultString = client.fieldRead(guid, new ArrayList<String>(Arrays.asList("name", "occupation")));
    System.out.println("\n// multi-field read\n" + "client.fieldRead(GUID, [\"name\",\"occupation\"] -> " + resultString);
    // Read the entire object back in
    result = client.read(guid);
    System.out.println("\nclient.read(GUID) -> " + result.toString());
    // Delete created GUID
    client.accountGuidRemove(guid);
    System.out.println("\n// GUID delete\n" + "client.accountGuidRemove(GUID) // GUID=" + guid);
    // Try read the entire record
    try {
        result = client.read(guid);
    } catch (Exception e) {
        System.out.println("\n// non-existent GUID error (expected)\n" + "client.read(GUID) // GUID= " + guid + "\n  " + e.getMessage());
    }
    client.close();
    System.out.println("\nclient.close() // test successful");
}
Also used : JSONObject(org.json.JSONObject) HttpClient(edu.umass.cs.gnsclient.client.http.HttpClient) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 44 with ClientException

use of edu.umass.cs.gnscommon.exceptions.client.ClientException 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 45 with ClientException

use of edu.umass.cs.gnscommon.exceptions.client.ClientException 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)

Aggregations

ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)239 IOException (java.io.IOException)215 Test (org.junit.Test)134 JSONException (org.json.JSONException)126 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)125 RandomString (edu.umass.cs.gnscommon.utils.RandomString)113 JSONArray (org.json.JSONArray)74 GuidEntry (edu.umass.cs.gnsclient.client.util.GuidEntry)71 JSONObject (org.json.JSONObject)59 GNSClientCommands (edu.umass.cs.gnsclient.client.GNSClientCommands)40 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 StringTokenizer (java.util.StringTokenizer)30 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)23 BasicGuidEntry (edu.umass.cs.gnsclient.client.util.BasicGuidEntry)20 FieldNotFoundException (edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException)19 FileNotFoundException (java.io.FileNotFoundException)18 InternalRequestException (edu.umass.cs.gnscommon.exceptions.server.InternalRequestException)15 ResponseCode (edu.umass.cs.gnscommon.ResponseCode)13 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)13 InvalidKeyException (java.security.InvalidKeyException)12