Now, let’s access some Domino resources.
I have created a database named „VaadinResources.nsf„, containing a normal image resource, and an image added via package explorer to the „WEB-INF“ folder:
Vaadin provides stream resources, which allows creating of dynamic resources. These resources handle „InputStream“ objects, which we can grab from Domino via Java NAPI.
To do this, it is first required to add some plug-ins to the dependencies of the „HelloVaadin“ plug-in:
- com.ibm.domino.napi
- com.ibm.domino.commons
- com.ibm.commons
Then we can import the „NAPIUtils“ class to the project and add a new method „loadBinaryFile“ to it:
/**
* loads given file from a database and returns the Inputsstream
*
* @param serverName
* the server to use
* @param dbPath
* the database path
* @param fileName
* the file to load
* @return the file data as InputStream
*
*/
static public InputStream loadBinaryFile(final String serverName, final String dbPath,
final String fileName) {
NotesSession nSession = null;
NotesDatabase nDatabase = null;
NotesNote nNote = null;
try {
nSession = new NotesSession();
// open database
try {
nDatabase = nSession.getDatabaseByPath(serverName + "!!" + dbPath);
} catch (NotesAPIException e) {
e.printStackTrace();
}
nDatabase.open();
// load existing data
nNote = FileAccess.getFileByPath(nDatabase, fileName);
// get Filedate and return String
InputStream is = FileAccess.readFileContentAsInputStream(nNote);
return is;
} catch (Exception e) {
e.printStackTrace();
} finally {
// recycle NAPI objects
recycleNAPIObject(nNote, nDatabase, nSession);
}
return null;
}
A new class „DominoImageSource“ which implements the „StreamResource.StreamSource“ interface uses this method to pipe the „InputStream“ object returned from the Domino Java NAPI:
package ch.hasselba.vaadin;
import java.io.InputStream;
import ch.hasselba.napi.NAPIUtils;
import com.vaadin.server.StreamResource.StreamSource;
@SuppressWarnings("serial")
public class DominoImageSource implements StreamSource {
private String fileName;
private String serverName;
private String dbPath;
public DominoImageSource(final String serverName, final String dbPath,
final String fileName){
super();
this.serverName = serverName;
this.dbPath = dbPath;
this.fileName = fileName;
}
public InputStream getStream () {
return NAPIUtils.loadBinaryFile( this.serverName, this.dbPath, this.fileName );
}
}
Now, we can create a new Application named „ResourceUI“ which displays these two resources:
package ch.hasselba.vaadin;
import com.vaadin.server.StreamResource;
import com.vaadin.server.StreamResource.StreamSource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Image;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@SuppressWarnings("serial")
public class ResourcesUI extends UI {
@Override
protected void init(VaadinRequest request) {
VerticalLayout layout = new VerticalLayout();
setContent(layout);
layout.setSizeFull();
// Create an instance of our stream source.
StreamSource webinfResource = new DominoImageSource (
"Dev01", "VaadinResources.nsf" , "/WEB-INF/WEB-INFResource.png");
StreamSource imageResource = new DominoImageSource (
"Dev01", "VaadinResources.nsf" , "ImageResource.gif");
// Create an image component that gets its contents
// from the resource.
layout.addComponent(new Image("WEB-INF Resource",
new StreamResource(webinfResource, "image01.png")));
layout.addComponent(new Image("Image Resource",
new StreamResource(imageResource, "image02.gif")));
}
}
Again, no rocket science and self-explaining code. When we open the application, the resources are loaded from the database and displayed in the browser:
As soon we are changing the images in the database (and the cache is expired), the new image will appear in our Vaadin application.
will be interesting to see if this will work in XPages for Bluemix 😉
That’s for interesting blog posts as always
i am more curious what boilerplates for xsp will be provided by third parties on bluemix? OpenNTF boilerplate?