Plugin Tutorial: Using API in plugins
All the Vertical Site API is available in any plugins. The client API is accessed using “ClientFactory” and the plugin environment is accessed using “PluginEnvironment”. Below I will illustrate some common usages.
Using the reqest
Sometimes it makes sense to use the client request in a user-defined function. This example shows a simple usage:
... import com.enonic.cms.api.plugin.PluginEnvironment; ... PluginEnvironment env = PluginEnvironment.getInstance(); String pathInfo = env.getCurrentRequest().getPathInfo(); Object sessionValue = env.getCurrentSession().getValue(“mystate”);
Share some values
In some cases you need to share values between invocations. The session could be used, but if you need to share values between sessions? We have provided API functions to do this.
...
import com.enonic.cms.api.plugin.PluginEnvironment;
...
PluginEnvironment env = PluginEnvironment.getInstance();
String myState = (String)env.getSharedObject(“mystate”);
if (myState == null) {
env.setSharedObject(“mystate”, “hello”);
}
Right now it is not cluster safe. In later versions of Vertical Site we will replicate this state between cluster nodes.
Accessing the user
To access data inside Vertical Site you must use the client API. You can do alot of things with the functions available there, but I will illustrate how to access the current user.
... import com.enonic.cms.api.client.Client; import com.enonic.cms.api.client.ClientFactory; ... Client client = ClientFactory.getLocalClient(); String runAsUser = client.getRunAsUser();
One usage of this is to replace current user in a request by using the request filter.




Comments
If you want to comment on this article you need to be logged in.