Best way to stock tokens with WSO2 Enterprise Integrator - wso2

I am working on some systems that needs to use Access and Refresh tokens.
In my process now, I save tokens in registry, and I update them when they need to be updated. But sometimes, I get an error because one of my process tries to retrieve a token, but this token has been changed just before (like a lock in the registry).
What is the best way to store, update and retrieve that kind of informations ?
I tried to use a class mediator which stores properties in a Hashmap, but WSO2 always create a new instance of my custom class, so a new instance of my hashmap.
Any idea ? Thank you !

The usual practice to store the token is in the registry. This is used in the connector implementation [1] in the ESB server. Could you elaborate more on the issue? What is the ESB server version? What is the error stack trace? Have you used a similar implementation as in [1]?
Further to answer your question regarding the class mediator, you can use a static variable in the class mediator to share the token among the objects created. But then again, static variables are not thread-safe.
[1]-https://github.com/wso2-extensions/esb-connector-gmail/blob/master/src/main/resources/config/getAccessTokenFromRefreshToken.xml

I'm using the registry for reading / storing data, and here is some issue..
When you read using property mediator, for example token - he is also cached for default 15sec. So after read, revoke new one, and store back - you will still get from property mediator the "old" one. This can be bypassed, using script mediator and store registry entry using code, and setting the CachableDurationto 0.
I described this reading storing from registry in my blog, check out especially the Known downside paragraph.

Related

Camunda Rest API: Cannot fetch and lock an External Task for a Tenant

I have a Process Instance that was started by the Tenant 949.
I tried to fetch and lock that Task, like described here: https://docs.camunda.org/manual/7.10/reference/rest/external-task/fetch/
Here is the Body of the Request:
{"workerId":"testUser","maxTasks":1,"usePriority":false,
"topics":[
{"topicName":"archive-document","tenantIdIn":["949"],"lockDuration":10000,"localVariables":true,"deserializeValues":false}
]}
I don't get any Task with it.
The same request works if the Process Instance is started without a Tenant and fetched accordingly.
Do I miss something, or is this a Bug of Camunda?
Have you attempted to simply do a query to first retrieve the task? (Rather than attempting to fetch it and lock it?) You could use this endpoint: https://docs.camunda.org/manual/7.10/reference/rest/external-task/get-query/.
You may also want to query the runtime database directly using SQL. Your External Task would be in the ACT_RU_EXT_TASK table and would have a TOPIC_NAME_ defined within it (as well as a TENANT_ID_).
The problem was the Authentication.
I had a different User to start the process and to fetch the Task.
And this User had no rights to fetch the Task for this Tenant.

Designing RESTful API for Invoking process methods

I would like to know how do design the RESTful web service for process methods. For example I want to make a REST Api for ProcessPayroll for given employee id. Since ProcessPayroll is time consuming job, I don't need any response from the method call but just want to invoke the ProcessPayroll method asynchronously and return. I can't use ProcessPayroll in the URL since it is not a resource and it is not a verb. So I thought that, I can go with the below approach
Request 1
http://www.example.com/payroll/v1.0/payroll_processor POST
body
{
"employee" : "123"
}
Request 2
http://www.example.com/payroll/v1.0/payroll_processor?employee=123 GET
Which one of the above approach is correct one? Is there any Restful API Design guidelines to make a Restful service for process methods and functions?
Which one of the above approach is correct one?
Of the two, POST is closest.
The problem with using GET /mumble is that the specification of the GET method restricts its use to operations that are "safe"; which is to say that they don't change the resource in any way. In other words, GET promises that a resource can be pre-fetched, just in case it is needed, by the user agent and the caches along the way.
Is there any Restful API Design guidelines to make a Restful service for process methods and functions?
Jim Webber has a bunch of articles and talks that discuss this sort of thing. Start with How to GET a cup of coffee.
But the rough plot is that your REST api acts as an integration component between the process and the consumer. The protocol is implemented as the manipulation of one or more resources.
So you have some known bookmark that tells you how to submit a payroll request (think web form), and when you submit that request (typically POST, sometimes PUT, details not immediately important) the resource that handles it as a side effect (1) starts an instance of ProcessPayroll from the data in your message, (2) maps that instance to a new resource in its namespace and (3) redirects you to the resource that tracks your payroll instance.
In a simple web api, you just keep refreshing your copy of this new resource to get updates. In a REST api, that resource will be returning a hypermedia representation of the resource that describes what actions are available.
As Webber says, HTTP is a document transport application. Your web api handles document requests, and as a side effect of that handling interacts with your domain application protocol. In other words, a lot of the resources are just messages....
We've come up with the similar solution in my project, so don't blame if my opinion is wrong - I just want to share our experience.
What concerns the resource itself - I'd suggest something like
http://www.example.com/payroll/v1.0/payrollRequest POST
As the job is supposed to be run at the background, the api call should return Accepted (202) http code. That tells the user that the operation will take a lot time. However you should return a payrollRequestId unique identifier (Guid for example) to allow users to get the posted resource later on by calling:
http://www.example.com/payroll/v1.0/payrollRequest/{payrollRequestId} GET
Hope this helps
You decide the post and get on the basis of the API work-
If your Rest API create any new in row DB(means new resource in DB) , then you have to go for POST. In your case if your payroll process method create any resource then you have to choose to POST
If your Rest API do both, create and update the resources. Means ,if your payroll method process the data and update it and create a new data , then go for PUT
If your Rest API just read the data, go for GET. But as I think from your question your payroll method not send any data.So GET is not best for your case.
As I think your payroll method is doing both thing.
Process the data , means updating the data and
Create new Data , means creating the new row in DB
NOTE - One more thing , the PUT is idempotent and POST is not.Follow the link PUT vs POST in REST
So, you have to go for PUT method.

How can I write data about process assignees to database

I use camunda 7.2.0 and i'm not very experienced with it. I'm trying to write data about users, who had done something with process instance to database (i'm using rest services) to get some kind of reports later. The problem is that i don't know how to trigger my rest(that sends information to datebase about current user and assignee) when user assignes task to somebody else or claims task to himself. I see that camunda engine sends request like
link: engine/engine/default/task/5f965ab7-e74b-11e4-a710-0050568b5c8a/assignee
post: {"userId":"Tom"}
As partial solution I can think about creating a global variable "currentUser" and on form load check if user is different from current, and if he is - run the rest and change variable. But this solution don't looks correct to me. So is there any better way to do it? Thanks in advance
You could use a task listener which updates your data when the assignee of a task is changed. If you want this behavior for every task you could define a global task listener.

Updating a hit counter when an image is accessed in Django

I am working on doing some simple analytics on a Django webstite (v1.4.1). Seeing as this data will be gathered on pretty much every server request, I figured the right way to do this would be with a piece of custom middleware.
One important metric for the site is how often given images are accessed. Since each image is its own object, I thought about using django-hitcount, but figured that was unnecessary for what I was trying to do. If it proves easier, I may use it though.
The current conundrum I face is that I don't want to query the database and look for a given object for every HttpRequest that occurs. Instead, I would like to wait until a successful response (indicated by an HttpResponse.status of 200 or whatever), and then query the server and update a hit field for the corresponding image. The reason the only way to access the path of the image is in process_request, while the only way to access the status code is in process_response.
So, what do I do? Is it as simple as creating a class variable that can hold the path and then lookup the file once the response code of 200 is returned, or should I just use django-hitcount?
Thanks for your help
Set up a cron task to parse your Apache/Nginx/whatever access logs on a regular basis, perhaps with something like pylogsparser.
You could use memcache to store the counters and then periodically persist them to the database. There are risks that memcache will evict the value before it's been persisted but this could be acceptable to you.
This article provides more information and highlights a risk arising when using hosted memcache with keys distributed over multiple servers. http://bjk5.com/post/36567537399/dangers-of-using-memcache-counters-for-a-b-tests

Does DynamoDB support jsonp?

I took a look through the documentation, but couldn't find it. Can anybody who has used the service answer this?
DynamoDB objects' attributes can be int, string, int set or string set.
JSONP is string representing a javascript code that can be parsed.
What is the use you intend for it? What object are you going to store?
Are you invoking an AJAX call to the dynamodb api to retrieve an element and you want that element to follow JSONP pattern? or you wish to call DynamoDB in your server (perhaps using a servlet) and prepare a JSONP object to be sent to the client