Saving Data

Version 5.1 by Vincent Massol on 2012/08/20

There are times when you wish to save data. The XWiki platform offers various places that you can use depending on your needs. Here's a rundown of all the options you have.

Transient Saves

Execution Context

If the data to be saved should only be saved for the duration of a Request (i.e. a call to a XWiki URL), then you should use the Execution Context.

For example, from Java in a Component:

import org.xwiki.execution.*;

@Inject Execution execution;
...
// Saving
ExecutionContext ec = execution.getContext();
ec.setProperty("mykey", myvalue);
...
// Retrieving
Object myvalue = e.getProperty("mykey");

From a wiki page (Note that we currently cannot access easily the Execution Context from a wiki page but this should be improved and currently it's not available only from Velocity but it is from Groovy for example):

{{groovy}}
import org.xwiki.context.*

def ec = services.component.getInstance(Execution.class).getContext()
ec.setProperty("mykey", myvalue)
{{/groovy}}

Note that from a wiki page it's much easier to use the older XWiki Context (which is supposed to be fully replaced by the Execution Context at some point in the future). For example:

{{velocity}}
$xcontext.put("mykey", value)
{{/velocity}}

Note that it's also possible to save data in the HTTP Request even though it's recommended to use the Execution Context instead (since the Execution Context is independent of the execution environment and will work everywhere: Servlets, Portlets, JavaSE, etc). For example from Velocity in a wiki page:

{{velocity}}
$request.setAttribute("mykey", value)
{{/velocity}}

Servlet Session

If the data should last a little longer (i.e. for example span more than 1 request), you could save it in the Servlet Session (note that in this case your code will only work in a Servlet Environment).

For example, from Java in a Component:

import org.xwiki.container.*;

@Inject Container container;
...
Request request = container.getRequest();
// Note that this is a bit of a hack and the notion of Session exists in the Container class (getSession()) but the Session interface is empty at the moment, making it useless)
if (request instanceof ServletRequest) {
  HttpServletRequest servletRequest = (ServletRequest) request;
  HttpSession session = servletRequest.getSession();
  session.setAttribute("mykey", myvalue);
}

Example from a wiki page:

{{velocity}}
$request.getSession().setAttribute("mykey", myvalue)
{{/velocity}}

Temporary Directory

If the data should last even a little longer than a session, you could save it in a File in the Environment's Temporary Directory.

For example, from Java in a Component:

import org.xwiki.environment.*;
import org.apache.commons.io.*;

@Inject Environment environment;
...
File tmpDir = environment.getTemporaryDirectory();
FileUtils.write(tmpDir, "something");

Servlet Context

If the Data should last as long as the web application is up, you could use the Servlet Context. It's not very easy to access it but it's possible (note that in this case your code will only work in a Servlet Environment).

For example, from Java in a Component:

import org.xwiki.container.*;
import com.xpn.xwiki.*;
import com.xpn.xwiki.web.*;

@Inject Execution execution;
...
// Get the older XWiki Context
XWikiContext xc = (XWikiContext) execution.getContext().getProperty("xwikicontext");
XWikiEngineContext ec = xc.getEngineContext();
ec.setAttribute("mykey", myvalue);

Permanent Saves

Wiki Model

The best place to save Data is usually in the wiki itself (See the Data Model and the API Guide for some examples). There are several places where you can store data in the model:

  • In the Document content itself,
  • In an XObject attached to a Document,
  • In an Attachment

Permanent Directory

Another possibility is to use the Environment's Permanent Directory.

For example, from Java in a Component:

import org.xwiki.environment.*;
import org.apache.commons.io.*;

@Inject Environment environment;
...
File permDir = environment.getPermanentDirectory();
FileUtils.write(permDir, "something");

Get Connected