Search in sources :

Example 76 with ClientException

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

the class AclExists method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        GNSClientCommands gnsClient = module.getGnsClient();
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if (st.countTokens() != 2) {
            wrongArguments();
            return;
        }
        String field = st.nextToken();
        boolean isWrite = "write".equalsIgnoreCase(st.nextToken());
        // Set ACL
        boolean exists = gnsClient.fieldAclExists(isWrite ? AclAccessType.WRITE_WHITELIST : AclAccessType.READ_WHITELIST, module.getCurrentGuid(), field);
        if (exists) {
            // Then read ACLs
            JSONArray acl = gnsClient.aclGet(isWrite ? AclAccessType.WRITE_WHITELIST : AclAccessType.READ_WHITELIST, module.getCurrentGuid(), field, module.getCurrentGuid().getGuid());
            console.printString((isWrite ? "Write" : "Read") + " ACL is: " + acl.toString());
            console.printNewline();
        } else {
            console.printString((isWrite ? "Write" : "Read") + " ACL for field " + field + " does not exist");
            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)

Example 77 with ClientException

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

the class FieldCreateList method parse.

@Override
public void parse(String commandText) throws Exception {
    try {
        StringTokenizer st = new StringTokenizer(commandText.trim());
        if ((st.countTokens() != 1) && (st.countTokens() != 2)) {
            wrongArguments();
            return;
        }
        String field = st.nextToken();
        String value = "";
        if (st.hasMoreTokens()) {
            value = st.nextToken();
        }
        GNSClientCommands gnsClient = module.getGnsClient();
        gnsClient.fieldCreateList(module.getCurrentGuid().getGuid(), field, new JSONArray().put(value), module.getCurrentGuid());
        console.printString("New field " + field + " created with value '" + value + "'");
        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)

Example 78 with ClientException

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

the class RegisterAccountSecured method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, UnsupportedEncodingException, InternalRequestException {
    JSONObject json = commandPacket.getCommand();
    String name = json.getString(GNSProtocol.NAME.toString());
    String publicKey = json.getString(GNSProtocol.PUBLIC_KEY.toString());
    String password = json.getString(GNSProtocol.PASSWORD.toString());
    String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
    String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
    String guid = SharedGuidUtils.createGuidStringFromBase64PublicKey(publicKey);
    if (!NSAccessSupport.verifySignature(publicKey, signature, message)) {
        return new CommandResponse(ResponseCode.SIGNATURE_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.BAD_SIGNATURE.toString());
    }
    try {
        // Add the account but don't enable email verification
        CommandResponse result = AccountAccess.addAccount(header, commandPacket, handler.getHttpServerHostPortString(), name, guid, publicKey, password, false, handler);
        if (result.getExceptionOrErrorCode().isOKResult()) {
            // Everything is hunkey dorey so return the new guid
            return new CommandResponse(ResponseCode.NO_ERROR, guid);
        } else {
            assert (result.getExceptionOrErrorCode() != null);
            // Otherwise return the error response.
            return result;
        }
    } catch (ClientException | IOException e) {
        return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException)

Example 79 with ClientException

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

the class AddToGroup method execute.

@Override
public CommandResponse execute(InternalRequestHeader header, CommandPacket commandPacket, ClientRequestHandlerInterface handler) throws InvalidKeyException, InvalidKeySpecException, JSONException, NoSuchAlgorithmException, SignatureException, ParseException {
    JSONObject json = commandPacket.getCommand();
    String guid = json.getString(GNSProtocol.GUID.toString());
    String member = json.getString(GNSProtocol.MEMBER.toString());
    // writer might be same as guid
    String writer = json.optString(GNSProtocol.WRITER.toString(), guid);
    // signature and message can be empty for unsigned cases
    String signature = json.optString(GNSProtocol.SIGNATURE.toString(), null);
    String message = json.optString(GNSProtocol.SIGNATUREFULLMESSAGE.toString(), null);
    Date timestamp = json.has(GNSProtocol.TIMESTAMP.toString()) ? Format.parseDateISO8601UTC(json.getString(GNSProtocol.TIMESTAMP.toString())) : // can be null on older client
    null;
    ResponseCode responseCode;
    try {
        if (!(responseCode = GroupAccess.addToGroup(header, guid, member, writer, signature, message, timestamp, handler)).isExceptionOrError()) {
            return new CommandResponse(ResponseCode.NO_ERROR, GNSProtocol.OK_RESPONSE.toString());
        } else {
            return new CommandResponse(responseCode, GNSProtocol.BAD_RESPONSE.toString() + " " + responseCode.getProtocolCode());
        }
    } catch (ClientException | IOException | InternalRequestException e) {
        return new CommandResponse(ResponseCode.UNSPECIFIED_ERROR, GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.UNSPECIFIED_ERROR.toString() + " " + e.getMessage());
    }
}
Also used : ResponseCode(edu.umass.cs.gnscommon.ResponseCode) JSONObject(org.json.JSONObject) InternalRequestException(edu.umass.cs.gnscommon.exceptions.server.InternalRequestException) CommandResponse(edu.umass.cs.gnsserver.gnsapp.clientCommandProcessor.commandSupport.CommandResponse) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) IOException(java.io.IOException) Date(java.util.Date)

Example 80 with ClientException

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

the class ServerIntegrationTest method test_190_Substitute.

/**
   * Tests different DB substitute methods.
   */
@Test
public void test_190_Substitute() {
    //CHECKED FOR VALIDITY
    String testSubstituteGuid = "testSubstituteGUID" + RandomString.randomString(12);
    String field = "people";
    GuidEntry testEntry = null;
    try {
        // Utils.clearTestGuids(client);
        // System.out.println("cleared old GUIDs");
        testEntry = clientCommands.guidCreate(masterGuid, testSubstituteGuid);
    } catch (Exception e) {
        failWithStackTrace("Exception during init: ", e);
    }
    try {
        clientCommands.fieldAppendOrCreateList(testEntry.getGuid(), field, new JSONArray(Arrays.asList("Frank", "Joe", "Sally", "Rita")), testEntry);
    } catch (IOException | ClientException e) {
        failWithStackTrace("Exception during create: ", e);
    }
    try {
        HashSet<String> expected = new HashSet<>(Arrays.asList("Frank", "Joe", "Sally", "Rita"));
        HashSet<String> actual = JSONUtils.JSONArrayToHashSet(clientCommands.fieldReadArray(testEntry.getGuid(), field, testEntry));
        Assert.assertEquals(expected, actual);
    } catch (Exception e) {
        failWithStackTrace("Exception when we were not expecting it: ", e);
    }
    try {
        clientCommands.fieldSubstitute(testEntry.getGuid(), field, "Christy", "Sally", testEntry);
    } catch (Exception e) {
        failWithStackTrace("Exception during substitute: ", e);
    }
    try {
        HashSet<String> expected = new HashSet<>(Arrays.asList("Frank", "Joe", "Christy", "Rita"));
        HashSet<String> actual = JSONUtils.JSONArrayToHashSet(clientCommands.fieldReadArray(testEntry.getGuid(), field, testEntry));
        Assert.assertEquals(expected, actual);
    } catch (Exception e) {
        failWithStackTrace("Exception when we were not expecting it: ", e);
    }
}
Also used : JSONArray(org.json.JSONArray) RandomString(edu.umass.cs.gnscommon.utils.RandomString) IOException(java.io.IOException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) BasicGuidEntry(edu.umass.cs.gnsclient.client.util.BasicGuidEntry) GuidEntry(edu.umass.cs.gnsclient.client.util.GuidEntry) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FieldNotFoundException(edu.umass.cs.gnscommon.exceptions.client.FieldNotFoundException) JSONException(org.json.JSONException) ClientException(edu.umass.cs.gnscommon.exceptions.client.ClientException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) HashSet(java.util.HashSet) DefaultGNSTest(edu.umass.cs.gnsserver.utils.DefaultGNSTest) Test(org.junit.Test)

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