202 Lotus blogs updated hourly. Who will post next? Home | Blogs | Search | About 
 
Latest 7 Posts
XPages JDBC Configuration via NotesDocuments
Tue, Dec 5th 2017 154
My New Approach for Sharing XPages Controls and Extensions
Fri, Dec 1st 2017 38
Embedded Experiences not rendering after IBM Notes FP9
Wed, Sep 27th 2017 8
BuildXPages Deployment Automation Tools – v1.0.0
Thu, Sep 21st 2017 7
Generating and Downloading Files Using XPages’ Persistence Service
Wed, Sep 13th 2017 7
Bundle Inspector – Diagnosing XPages Plugin Resolution problems
Wed, Aug 2nd 2017 8
Swiper Official Version 2 Release
Fri, Jul 21st 2017 6
Top 10
XPages JDBC Configuration via NotesDocuments
Tue, Dec 5th 2017 154
My New Approach for Sharing XPages Controls and Extensions
Fri, Dec 1st 2017 38
Preventing pasting of remotely hosted images in CKEditor
Mon, Nov 14th 2016 12
Webmail UI – You must learn about MIME
Wed, Apr 20th 2016 11
Preventing Pasting of Images in CKEditor
Mon, Nov 14th 2016 11
Pasting Images into XPages CKEditor
Sun, Feb 12th 2017 11
XPages PhoneNumber Control - Part 2 Upgrades to the Converter
Mon, Feb 23rd 2015 10
XPages webmail – Using Mime Inspector to debug Mime
Tue, Feb 14th 2017 9
Tips for Creating a Webmail UI with XPages
Tue, Apr 19th 2016 8
Bundle Inspector – Diagnosing XPages Plugin Resolution problems
Wed, Aug 2nd 2017 8


Generating and Downloading Files Using XPages’ Persistence Service
Twitter Google+ Facebook LinkedIn Addthis Email Gmail Flipboard Reddit Tumblr WhatsApp StumbleUpon Yammer Evernote Delicious
camerongregor    

When developing an XPages application you may run into the need to generate files to be downloaded by the user.

For example:

  • Generating a PDF Report
  • Exporting Data to Excel Spreadsheet
  • Creating a Zip File of several attachments

Achieving these tasks usually raises 2 main problems to solve;

  1. What temporary place can I use to generate the files?
  2. How do I allow the user to download the generated files?

For the temporary place problem, some common solution is to either generate the files to some ‘in memory’ representation or alternatively use the Java Temporary Directory. For the downloading problem, a common solution is to provide a button which calls a SSJS or Java bean method that hijacks the HttpResponse and writes out the appropriate headers and file contents.

These solutions work fine, however built in to the XPages framework is the ‘Persistence Service’ which is a mechanism for temporarily Persisting files that need to be available for download . Although this persistence service was built primarily for the standard operations of XPages, there is nothing stopping us from also enjoying the way it solves these 2 problems.

Why does the Persistence Service exist?

An advantage of Domino Databases is how easy it is to store Files and Images inside ‘Rich Text’ Fields. When XPages needs to display these Embedded Images or allow Downloads of files from within the Document, then the user’s web browser will need to make request for these embedded objects using some sort of URL.

Domino provides URLs which provide direct access to these embedded files and images, however these only provide access to the ‘saved’ version of a document as it exists inside the Notes Database. These URLs are of no help for the ‘No mans land’ period of time in which someone is creating a new document, or when editing an existing document and uploads a new File or Image but has not yet saved the document.

When you open up Domino Document via XPages, the persistence service extracts the Document’s embedded images to a temporary directory in the filesystem, and also uses this directory as a place for newly uploaded files/images to live until they are finally decided to be saved into the document inside the Notes Database. The files in this temporary directory are made available to the user via a special URL.

How does the Persistence Service Work?

The XPages server has a server-wide property which determines the Base Persistence Directory.

The default value for this is <DominoDataDirectory>/xsppers  but you can configure it to be anywhere you like using the xsp property xsp.state.persistence.directory

When a new XPages Application starts up, it is assigned an ‘Application Id’ which is really just a sequential number, so the first application that starts up gets number 1, next number 2 etc.

When a user starts using an Application they are given a ‘session id’ which is a bit more like a Universal ID.

Side Note: I’m not sure how this technique goes with Anonymous access as I have only been programming for authenticated users.

The persistence service uses these 3 pieces of information to determine a user’s persistence directory for that application.

So lets assume, my Base persistence directory is C:DominoDataxsppers, and the application id is 1 and my session id is 9847239OENTHUDO then we can find my persistence directory for that application in the file system at <xsppers>/<applicationid>/<sessionid> or in this example:

C:DominoDataxsppers19847239OENTHUDO

Now here is the handy part, XPages has a resource provider which maps a special URL /xsp/.ibmmodres/persistence to the user’s persistence directory for that application.

So lets assume we create a folder ‘myfolder’ under my user persistence directory and put a file ‘myfile.pdf’ in that folder::

C:DominoDataxsppers19847239OENTHUDOmyfoldermyfile.pdf

This file would then be available via the url:

https://yourserver/yourapp.nsf/xsp/.ibmmodres/persistence/myfolder/myfile.pdf

If another user tried to use the same url it would not work because they would have a different session id, and would be mapping to a different directory

Cleaning up files

Another handy feature of the persistence service is that it cleans up after itself! So you can stress less about generating a massive zip file knowing that it won’t be left around for too long and end up filling up your server’s hard drive.

After a user session for an application times-out then the user’s persistence directory and all its contents will be deleted.

After the entire application times-out, then the application’s persistence directory  e.g. C:DominoDataxsppers1 and all it’s contents will be deleted.

So how do I actually use this?

So your main 2 weapons here are :

  • You have a User’s Persistence Directory in which you can generate files (which will also clean up after itself)
  • You have a handy URL available for the user to access the User’s Persistence Directory

To generate files into the User’s Persistence directory we can either just figure out the path of the user’s application directory and write files straight to it, or alternatively we can also ask the applications PersistenceService to do it for us.

Doing it ourselves

To do it ourselves we need to be able to figure out the User’s persistence directory.  I have included a Utility class at the bottom of this post with some handy methods. Let’s have a look at 3 of the methods:

public static String getSessionId() {
		return SessionUtil.getSessionId(FacesContext.getCurrentInstance());
	}

	public static String getBasePersistenceFolder() {

		ApplicationEx ex = ApplicationEx.getInstance();

		String propDir = ex.getProperty("xsp.state.persistence.directory", null);
		if (StringUtil.isEmpty(propDir)) {
			File defaultDir = SystemUtil.DEFAULT_PERSISTENCEDIR;
			return defaultDir.getAbsolutePath();
		} else {
			return propDir;
		}

	}

	public static String getPersistenceSessionFolder() {

		ApplicationEx ex = ApplicationEx.getInstance();

		return getBasePersistenceFolder() + File.separator + ex.getApplicationId() + File.separator + getSessionId();

	}

So the main one here is getPersistenceSessionFolder and you can see it is just building up that folder location using the 3 parts mentioned earlier. We get the BasePersistenceFolder (xsppers) by querying the xsp property, and if it has not been set using an xsp property, we fall back to the default for the current platform.
We get the session id using the com.ibm.xsp.util.SessionUtil class

Once you have access to this folder, you can create folders and files directly in here. The convention of the PersistenceService is to place your generated files within a subfolder of the User Persistence Directory e.g. <userpersistencedir/<somefolder>/<somefile> instead of <userpersistencedir>/<somefile>. If you do not follow this convention then your download url will not work as it expects exactly this structure.

Doing it using the PersistenceService

An XPages Application has a PersistenceService which can be used for creating a ‘PersistedContent’ object. This persisted content object can be used to get an OutputStream to write to, and also the corresponding java ‘File’ object for that PersistedContent.

The utility class has a method which shows how to get the persistence service. Note the persistence service is marked as Deprecated but you can still use it.

@SuppressWarnings("deprecation")
	public static PersistenceService getPersistenceService() {

		ApplicationEx ex = ApplicationEx.getInstance();
		return ex.getPersistenceService();

	}

Also the utility class has a method to help create PersistedContent. You can see all the Persistence service methods follow the convention of using a folder and a file name.

public static PersistedContent createPersistedContent(String folderName, String fileName)
			throws IOException, NotesException {

		String sessionId = getSessionId();

		PersistenceService ps = getPersistenceService();

		PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);

		return pd;

	}

And you can see how this is used in the method persistInputStream

Note: I am using Apache Commons IOUtils for copying a stream but if you cannot use this then you can replace with another stream copying algorithm or perhaps using IBM Commons StreamUtil.copy(inputstream, outputstream). I am using Apache commons because it automatically buffers the streams so that if you are dealing with large files you don’t use too much memory

public static File persistInputStream(InputStream is, String folderName, String fileName)
			throws NotesException, IOException {
		String sessionId = getSessionId();
		return persistInputStream(is, sessionId, folderName, fileName);
	}

	public static File persistInputStream(InputStream is, String sessionId, String folderName, String fileName)
			throws IOException {

		PersistenceService ps = getPersistenceService();

		PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);

		OutputStream fos = pd.getOutputStream();

		org.apache.commons.io.IOUtils.copy(is, fos);

		fos.close();

		return pd.getContentAsFile();

	}

Providing a Link to download the file

This is the easy part. all you have to do is use the standard link control!

<xp:link escape="true" text="Download Me" id="link1" 
    value="/xsp/.ibmmodres/persistence/myfolder/myfile.pdf"></xp:link>

Summary

So I have found this way to be a very useful way to manage generating and downloading files, I hope it has been interesting / useful for you and if you have any questions or have found a mistake in this post please leave a comment and I promise to respond!

Utility Class

As promised here is the utility class which is also available as an XSnippet

package com.jord.xsp.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

import javax.faces.context.FacesContext;

import com.ibm.commons.util.StringUtil;
import com.ibm.xsp.application.ApplicationEx;
import com.ibm.xsp.component.UIFileuploadEx.UploadedFile;
import com.ibm.xsp.http.IUploadedFile;
import com.ibm.xsp.persistence.PersistedContent;
import com.ibm.xsp.persistence.PersistenceService;
import com.ibm.xsp.util.SessionUtil;
import com.ibm.xsp.util.SystemUtil;

import lotus.domino.NotesException;

public class PersistenceUtil {

	@SuppressWarnings("deprecation")
	public static PersistenceService getPersistenceService() {

		ApplicationEx ex = ApplicationEx.getInstance();
		return ex.getPersistenceService();

	}

	public static String getSessionId() {
		return SessionUtil.getSessionId(FacesContext.getCurrentInstance());
	}

	public static String getBasePersistenceFolder() {

		ApplicationEx ex = ApplicationEx.getInstance();

		String propDir = ex.getProperty("xsp.state.persistence.directory", null);
		if (StringUtil.isEmpty(propDir)) {
			File defaultDir = SystemUtil.DEFAULT_PERSISTENCEDIR;
			return defaultDir.getAbsolutePath();
		} else {
			return propDir;
		}

	}

	public static String getPersistenceSessionFolder() {

		ApplicationEx ex = ApplicationEx.getInstance();

		return getBasePersistenceFolder() + File.separator + ex.getApplicationId() + File.separator + getSessionId();

	}

	public static File createdPath(File file) {
		File dir = file;
		if (!file.isDirectory())
			dir = file.getParentFile();
		dir.mkdirs();
		return file;
	}

	public static File getAvailableFile(final File file) {

		if (file.exists()) {
			File testFile;
			int counter = 0;
			String extension = "";
			String fileName = file.getAbsolutePath();
			int dotIndex = fileName.lastIndexOf('.');
			if (dotIndex > 0) {
				extension = fileName.substring(dotIndex);
				fileName = fileName.substring(0, dotIndex);
			}
			while ((testFile = new File(fileName + "_" + counter + extension)).exists())
				counter++;

			return testFile;

		} else {
			return file;
		}

	}

	/**
	 * Returns a safe version of a given name Currently just prevents moving
	 * directories etc
	 *
	 * @return
	 */
	public static String safeFileName(String fileName) {
		return fileName.replace("/", "").replace("\", "");
	}

	public static File persistFile(File file, String folderName, String fileName) throws IOException, NotesException {

		String sessionId = getSessionId();
		return persistFile(file, sessionId, folderName, fileName);

	}

	public static File persistFile(File file, String sessionId, String folderName, String fileName) throws IOException {

		PersistenceService ps = getPersistenceService();

		PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);

		FileInputStream fis = new FileInputStream(file);
		OutputStream fos = pd.getOutputStream();

		org.apache.commons.io.IOUtils.copy(fis, fos);

		fos.close();
		fis.close();

		return pd.getContentAsFile();

	}

	public static File persistInputStream(InputStream is, String folderName, String fileName)
			throws NotesException, IOException {
		String sessionId = getSessionId();
		return persistInputStream(is, sessionId, folderName, fileName);
	}

	public static File persistInputStream(InputStream is, String sessionId, String folderName, String fileName)
			throws IOException {

		PersistenceService ps = getPersistenceService();

		PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);

		OutputStream fos = pd.getOutputStream();

		org.apache.commons.io.IOUtils.copy(is, fos);

		fos.close();

		return pd.getContentAsFile();

	}

	public static PersistedContent createPersistedContent(String folderName, String fileName)
			throws IOException, NotesException {

		String sessionId = getSessionId();

		PersistenceService ps = getPersistenceService();

		PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);

		return pd;

	}

	public static PersistedContent[] getPersistedContents(String folderName) throws IOException {

		String sessionId = getSessionId();

		PersistenceService ps = getPersistenceService();

		PersistedContent[] pcs = ps.getPersistedContents(sessionId, folderName);

		return pcs;

	}

	public static PersistedContent getPersistedContent(String folderName, String fileName)
			throws IOException, NotesException {

		String sessionId = getSessionId();

		PersistenceService ps = getPersistenceService();

		PersistedContent pd = ps.getPersistedContent(sessionId, folderName, fileName);

		return pd;

	}

	public static File persistUploadedFile(UploadedFile upload, String folderName) throws IOException, NotesException {

		String sessionId = getSessionId();
		return persistUploadedFile(upload, sessionId, folderName);

	}

	public static File persistUploadedFile(UploadedFile upload, String sessionId, String folderName)
			throws IOException {

		File file = upload.getUploadedFile().getServerFile();
		String fileName = upload.getUploadedFile().getClientFileName();

		return persistFile(file, sessionId, folderName, fileName);

	}

	public static File persistUploadedFile(IUploadedFile upload, String sessionId, String folderName)
			throws IOException {

		File file = upload.getServerFile();
		String fileName = upload.getClientFileName();

		return persistFile(file, sessionId, folderName, fileName);

	}

	public static void deletePersistenceFolder(String folderName) throws IOException {

		String sessionId = getSessionId();

		String[] folders = getPersistenceService().getFolders(sessionId);

		if (!Arrays.asList(folders).contains(folderName)) {
			return;
		}

		getPersistenceService().deleteFolder(sessionId, folderName);

	}

}

 

 



---------------------
http://camerongregor.com/2017/09/13/generating-and-downloading-files-using-xpages-persistence-service/
Sep 13, 2017
8 hits



Recent Blog Posts
154
XPages JDBC Configuration via NotesDocuments
Tue, Dec 5th 2017 11:34a   Cameron Gregor
If you have used the Extension Library’s Relational controls, then you are probably familiar with the process of configuring your JDBC Connection details. You have the option of setting up a ‘named’ connection so you can reference a database connection by name. This involves creating an xml file .jdbc in the WebContentWEB-INFjdbc directory. This xml file specifies the driver, url, username, password, and perhaps some information about connection pooling. Alternatively your can
38
My New Approach for Sharing XPages Controls and Extensions
Fri, Dec 1st 2017 12:33p   Cameron Gregor
Over the past few years I have shared a handful of controls and techniques on my blog, and most of these I have made available on github in one way or another. Originally I was creating a separate github project for each one such as EmailValidator, PhoneNumberControl and the Messages Controls. After these few I decided it would be more efficient to create a single project that I could put them all into, so I then began to contribute them via my GregorbyteXspLibrary. From time to time I had a fe
8
Embedded Experiences not rendering after IBM Notes FP9
Wed, Sep 27th 2017 5:36a   Cameron Gregor
Embedded Experiences are no longer rendering after upgrading to FP9. I suspect it is something to do with the changes to Embedded Browser, either removing it or upgrading it or something else. I haven’t had a chance to investigate fully. Perhaps there may be a workaround, or perhaps this is specific to the way we are using embedded experiences. We use embedded experiences quite a bit at our company so we have rolled back for now. If I figure anything out I will post an update but just be a
7
BuildXPages Deployment Automation Tools – v1.0.0
Thu, Sep 21st 2017 12:47p   Cameron Gregor
After quite a bit of work I have finally published the first release of BuildXPages! What is BuildXPages? BuildXPages is a project which is useful if you are interested in Automating tasks that are involved in building XPages. For example you may be interested in automating some of these tasks: Building NSFs from an On-Disk Project Building Plugins and Features Refreshing NSF Designs Setting Template Inheritance of NSFs Start, Stop and Restart Http server Uploading Plugins to an NSF Update
8
Generating and Downloading Files Using XPages’ Persistence Service
Wed, Sep 13th 2017 4:42p   Cameron Gregor
When developing an XPages application you may run into the need to generate files to be downloaded by the user. For example: Generating a PDF Report Exporting Data to Excel Spreadsheet Creating a Zip File of several attachments Achieving these tasks usually raises 2 main problems to solve; What temporary place can I use to generate the files? How do I allow the user to download the generated files? For the temporary place problem, some common solution is to either generate the files to some
8
Bundle Inspector – Diagnosing XPages Plugin Resolution problems
Wed, Aug 2nd 2017 12:26p   Cameron Gregor
A common task with XPages development is to installing some plugins that provide additional functionality. If you do any plugin development of your own, you end up doing this task a lot more as you create new plugins, install new versions etc. It is also common to encounter some problems after installing plugins to your Domino Server! Maybe your expected plugin doesn’t load up at all? maybe the wrong version is loading? The standard method of diagnosing these problems is to use the OSGi c
6
Swiper Official Version 2 Release
Fri, Jul 21st 2017 1:52p   Cameron Gregor
So I have finally posted Swiper version 2 to OpenNTF! (and Github of course) Swiper OpenNTF Project Page Swiper Github Releases The latest version is 2.0.1,  and is the same as 2.0.0beta but with a bug fix for the toolbar buttons. Swiper 2.0.0 beta has been available on the Github project site for a few months, and the core functionality of it works as planned. There was however a bug within the shortcut buttons that I added to the menu bar, and this is a bit of a nasty bug that can cause del
1
Auto-width Bootstrap Column XPages Controls
Tue, Mar 21st 2017 1:13p   Cameron Gregor
I’ve been stuck working with OneUI Version 3 for the past couple of years, due to a regretful decision made at the beginning of my major project. OneUI was better than nothing but very frustrating at times. Finally, I have moved on to my next project and I am now using bootstrap (version 3) A common task when laying out a page using bootstrap is to divide sections up into rows and columns, and use the appropriate css styles to do so. I’m going to assume you are familiar with bo
1
Swiper FP8 Version Beta Release
Thu, Mar 16th 2017 12:50p   Cameron Gregor
Last week I released the ‘alpha’ version of Swiper which was untested on FP8 but presumed to be ok. So far I have only had good reports from the pioneers who have gone ahead and installed FP8 + the alpha version. I have since managed to ugrade my home office setup to FP8 which unfortunately has broken my ability to launch designer from eclipse but I am seeking some advice on fixing this up. In the meantime I have to test the slow way of building plugins, import plugins, restart R
3
Swiper FP8 Integration Rollout
Wed, Mar 8th 2017 12:43p   Cameron Gregor
Notes Domino 9.0.1 FP8 is finally here and as far as I know (I have yet to download it) it includes the necessary changes which will allow Swiper to swipe whatever it wants, whenever it wants, which is good news for people who don’t like to have ‘Build Automatically’ turned on. Plan of Attack for release of Swiper version 2.0.0 So, I haven’t actually downloaded FP8 yet,  so I can’t say for sure that the updated version works perfectly. Here is a bit of backgrou




Created and Maintained by Yancy Lent - About - Planet Lotus Blog - Advertising - Mobile Edition