Saving Data

Version 2.1 by Caleb James DeLisle 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}}

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.

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);
}

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.

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 in an XObject attached to a wiki page (See the Data Model).

In an attachment

You can store data inside of attachments, this velocity script will attach a small piece of data to the current document.

#set($content = "This is some small arbitrary content")
#set($discard = $doc.addAttachment("myfile.txt", $content.getBytes()))
$doc.save()

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