Skip to main content
12-Amethyst
November 19, 2021
Solved

Access Thing property inside custom Authenticator

  • November 19, 2021
  • 2 replies
  • 1382 views

Hello,

 

I am attempting to access a thing property from inside a service on a custom authenticator extension.

 

Inside a thingshape I am able to access the thing property like below with no issue

@ThingworxServiceDefinition(name = "Start", aspects = {"isAsync:false"})
@ThingworxServiceResult(name = "Result", baseType = "STRING")
public String Start() throws Exception {
Thing apiConfig = ThingUtilities.findThing("MESLite.Thing.APIConfig");
return apiConfig.getPropertyValue("BasePath").getStringValue();
}

 When inside the Authenticator I get a null pointer exception on 

apiConfig.getPropertyValue("BasePath").getStringValue();

 

Is it possible to access thing property inside the custom authenticator?

Best answer by jvisser

I was able to achieve what I needed using a configuration table.

@ThingworxConfigurationTableDefinitions(tables = {
@ThingworxConfigurationTableDefinition(name = "AuthenticatorRegex", description = "",
isMultiRow = false, ordinal = 0, dataShape = @ThingworxDataShapeDefinition(fields = {
@ThingworxFieldDefinition(name = "Regex", description = "", baseType = "STRING", ordinal
= 0)}))})

Then I can set the value inside TW and access it inside my authenticator using the code below:
 

String CONFIGURATION_TABLE = "AuthenticatorRegex";
String COLUMN_NAME = "Regex";
@ThingworxServiceDefinition(name = "GetAuthenticatorRegex", description = "", category = "",
isAllowOverride = false, aspects = {
"isAsync:false"})
@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})
public String GetAuthenticatorRegex() throws Exception {
return this.getConfigurationTable(CONFIGURATION_TABLE).getFirstRow()
.getStringValue(COLUMN_NAME);
}

 

2 replies

Community Manager
December 2, 2021

Hi @jvisser.

 

Can you provide some additional information?  Why do you need to access that property value?  What does that property value hold?  There may actually be an easier way of doing what you need to do, but we need a better understanding of your use case.

 

Regards.

 

--Sharon

Community Manager
December 2, 2021

Hi @jvisser.

 

Glad to hear you found a solution!

 

Regards.

 

--Sharon

jvisser12-AmethystAuthorAnswer
12-Amethyst
December 2, 2021

I was able to achieve what I needed using a configuration table.

@ThingworxConfigurationTableDefinitions(tables = {
@ThingworxConfigurationTableDefinition(name = "AuthenticatorRegex", description = "",
isMultiRow = false, ordinal = 0, dataShape = @ThingworxDataShapeDefinition(fields = {
@ThingworxFieldDefinition(name = "Regex", description = "", baseType = "STRING", ordinal
= 0)}))})

Then I can set the value inside TW and access it inside my authenticator using the code below:
 

String CONFIGURATION_TABLE = "AuthenticatorRegex";
String COLUMN_NAME = "Regex";
@ThingworxServiceDefinition(name = "GetAuthenticatorRegex", description = "", category = "",
isAllowOverride = false, aspects = {
"isAsync:false"})
@ThingworxServiceResult(name = "Result", description = "", baseType = "STRING", aspects = {})
public String GetAuthenticatorRegex() throws Exception {
return this.getConfigurationTable(CONFIGURATION_TABLE).getFirstRow()
.getStringValue(COLUMN_NAME);
}