Temporary Resource API

Last modified by Admin on 2024/02/26 12:39

cogAdds support for temporary resources
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Compatibility

XWiki 8.3+, 8.2.2+, 7.4.6+

Installable with the Extension Manager

Description

Adds support for creating and accessing temporary resources by implementing and extending the Resource API.

Usage

From Java

Here's how you can create temporary resources from Java. First you need to inject the required components.

/**
 * Used to create and access the temporary files.
 */

@Inject
private TemporaryResourceStore temporaryResourceStore;

/**
 * Used to obtain the URL that corresponds to a temporary resource.
 */

@Inject
private ResourceReferenceSerializer<ResourceReference, ExtendedURL> resourceReferenceSerializer;

Then you need to use the injected components like this:

// (1) Create the temporary resource reference.
String moduleName = "chart";
List<String> resourcePath = Arrays.asList("pie", "issuesByReporter.png");
EntityReference owningEntityReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
TemporaryResourceReference temporaryResourceReference =
   new TemporaryResourceReference(moduleName, resourcePath, owningEntityReference);

// (2) Create the temporary file ...
InputStream content = new ByteArrayInputStream("content".getBytes());
File tempFile = this.temporaryResourceStore.createTemporaryFile(temporaryResourceReference, content);

// ... or retrieve the temporary file by its reference (e.g. if you want to update it).
tempFile = this.temporaryResourceStore.getTemporaryFile(temporaryResourceReference);

// (3) Obtain the URL that can be used to access the above temporary file.
String temporaryResourceURL =
   this.resourceReferenceSerializer.serialize(temporaryResourceReference).serialize();

Note that the URL format is an implementation detail and thus is subject to change. You should always use the ResourceReferenceSerializer to obtain the temporary resource URL.

From Scripts

14.4.2+, 14.5+ 

There is a script service available to check if a temporary resource exists and to get its URL:

{{velocity}}
#set ($tempResourceReference = ...)
#if ($services.resource.temporary.exists($tempResourceReference))
  [[Download>>path:$services.resource.temporary.getURL($tempResourceReference)]]
#end
{{/velocity}}

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager).

You can also use the manual method which involves dropping the JAR file and all its dependencies into the WEB-INF/lib folder and restarting XWiki.

Dependencies

Dependencies for this extension (org.xwiki.platform:xwiki-platform-resource-temporary 16.1.0):

Tags:
    

Get Connected