Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
Hi,
When I tried to run another thread in the Thingworx extension I'm making and tried to access an entity in the thread,
I got an error related 'permission', 'not authorized'.. something.
(for example, when writing property, 'com.thingworx.common.exceptions.InvalidRequestException: Not authorized.....'
or
ThingManager.getInstance().getEntity('mythingname') returns null )
-> Is it not permitted for the extensions to run another thread in it?
Or is there any way to get the access permission in the other thread?
Security permissions are tracked using ThreadLocal storage in the ThreadLocalContext class. If you create a new thread that needs to access data from Thingworx, you will need to use ThreadLocalContext to assign it the proper security context.
Hi Jesse,
I want to ask the a question about using ThreadLocalContext.I am trying run a thread in a function.
public void startThread() { Thread t = new Thread(() -> { String line = ""; try { SecurityContext systemContext = SecurityContext.createSystemUserContext(); ThreadLocalContext.setSecurityContext(systemContext); Thing thing = ThingUtilities.findThing("AEOSEvents"); ThreadLocalContext.clearSecurityContext(); while ((line = reader.readLine()) != null) { thing.setPropertyValue("rawEvents", new StringPrimitive(line)); } } } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }); t.start(); }
This function gets the data coming from the socket and assing this data into thinking property.But I get the same error.Not authorized for Property Write in thingworx.I looked at the thingworx extension API Doc. But I did not find any examples about this ThreadLocalContext.Where do I make mistakes.
I have the same problem, the solution is to set securitycontext this code to your thread it will work.
public void messageArrived(String topic, MqttMessage message) throws Exception {
SecurityContext securityContext = SecurityContext.createSuperUserContext(); ThreadLocalContext.setSecurityContext(securityContext);
this.setPropertyValue("topic",new StringPrimitive(topic));
this.setPropertyValue("message",new StringPrimitive(message));
}