7 Replies Latest reply on Jul 14, 2016 5:19 PM by meghan RSS
    rkandasamy Apprentice

    Connecting with incorrect appKey does not throw any exception on Java SDK

    We are trying to use an incorrect appKey while connecting to a Remote Thing on the TW Server from a JAVA SDK. However, we do not receive any exception or error on SDK. Is there any way to get the error/exception on the SDK.

      • Re: Connecting with incorrect appKey does not throw any exception on Java SDK
        aanjan Heavyweight Champ

        Rdhakrishnan, does that generate any message at all or just simply doesn't connect?

          • Re: Connecting with incorrect appKey does not throw any exception on Java SDK
            rkandasamy Apprentice

            It simply does not connect. No message or exception is received at SDK end.

              • Re: Connecting with incorrect appKey does not throw any exception on Java SDK
                supandey Collaborator

                Hi Rdhakrishnan, a very specific error is thrown in the SDK when there is an issue with AppKey, something like this :

                 

                ERROR c.t.c.c.e.ClientCommunicationEndpoint - Exception refilling connections was:

                java.lang.Exception: Connection authentication/registration FAILED [code: STATUS_UNAUTHORIZED, reason: Error [context: authenticating connection][cause: invalid AppKey SecurityClaim provided]]

                 

                I tested this in Eclipse and I virtually can't run my code without catching the exception (with try & catch) this should definitely help you in catching the exception should there be any during the connection process.

                 

                Would it be possible for you to share the part of the code you are writing to connect to the platform?

                 

                Sushant

                  • Re: Connecting with incorrect appKey does not throw any exception on Java SDK
                    rkandasamy Apprentice

                    Hi Sushant,

                     

                    Thanks for your inputs.We are not getting any error exception in SDK code. It is getting cached in system level and we are not receiving any exception in our program code while starting the client.

                     

                    Please find the requested sample code below,

                     

                    private static void executeClient(String ThingName) throws Exception {

                    CPAssetThing connectionThing = new CPAssetThing(ThingName ,"Thing" ,client);

                    client.bindThing(connectionThing);

                    client.start();

                    log.info("\nConnecting client to server ************************* ");

                    while(!client.isShutdown()) {

                    if(client.isConnected()) {

                    try{

                    log.info("\nThe client is now connected.");

                    InfoTable result;

                    ValueCollection params = new ValueCollection();

                    params.put(key, value);

                    result = client.invokeService(ThingworxEntityTypes.Things, ThingName, "seviceName", params, 5000);

                    }catch (Exception eProcessing){

                    log.error("\nError in sevice Request for [" + ThingName + "] :  " , eProcessing);

                    }

                    break;

                    }

                    }

                    }

                     

                    Please let me know if you were able to catch the exception in SDK code, so that we can change the flow of program execution accordingly.

                    if you are able to catch explicitly, please share your code.

                      • Re: Connecting with incorrect appKey does not throw any exception on Java SDK
                        meghan Communicator

                        Rdhakrishnan,

                         

                        The error messages are definitely visible in the output logging, as Sushant is saying, but they do not bubble up through the SteamSensorClient class file for you to catch and do something with. As of right now, other than the client.isShutdown() and client.isConnected() methods, there is not really much else you can access to tell you the status of your connection with the SteamSensorClient class.

                         

                        Meghan

                • Re: Connecting with incorrect appKey does not throw any exception on Java SDK
                  supandey Collaborator

                  Thanks Meghan for pointing that out.

                   

                  Rdhakrishnan, here's a sample code for connecting and binding to the platform within the try & catch

                   

                  try {

                    // Setting the server URL and the appkey to enable this client to

                    // connect to the ThingWorx platform

                    String serverURL = "ws://localhost/Thingworx/WS";

                    String appKey = "<AppKey>";

                   

                   

                    // configure client

                    ClientConfigurator config = new ClientConfigurator();

                    config.setUri(serverURL);

                    config.setAppKey(appKey);

                    config.getSecurityClaims().addClaim(appKey, appKey);

                   

                   

                    // connected to client

                    ConnectedThingClient client = new ConnectedThingClient(config, null);

                   

                   

                    // start the client

                    client.start();

                   

                   

                    //create and bind the virtual thing to the platform

                    TractorVirtualThing tractorThing = new TractorVirtualThing("GreenCo.Tractor1", "A virtual thing", client);

                   

                    LOG.debug("Binding to " + serverURL + "to register thing" + tractorThing.getName());

                   

                    // binding happens just once with 1 connection

                    client.bindThing(tractorThing);

                   

                    LOG.debug("Connecting to " + serverURL + "using key" + appKey);

                   

                    //

                    while (true) {

                    if(client.getEndpoint().isConnected()) {

                    tractorThing.processScanRequest();

                    }

                    Thread.sleep(6000); // cycle every miliseconds

                    }

                   

                   

                    } catch (Exception e) {

                    // Catching the exception in case the connection or the binding fails

                    LOG.error("An error occured", e);

                    e.printStackTrace();

                    }

                   

                  Sushant