{"id":1964,"date":"2015-01-26T21:50:44","date_gmt":"2015-01-26T19:50:44","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=1964"},"modified":"2015-01-26T22:00:53","modified_gmt":"2015-01-26T20:00:53","slug":"howto-vaadin-on-domino-4","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=1964","title":{"rendered":"HowTo: Vaadin on Domino (4)"},"content":{"rendered":"<p>Now, let&#8217;s access some Domino resources.<\/p>\n<p>I have created a database named &#8222;<em>VaadinResources.nsf<\/em>&#8222;, containing a normal image resource, and an image added via package explorer to the &#8222;<em>WEB-INF&#8220;<\/em> folder:<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-ImageResource.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1966\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-ImageResource-288x300.png\" alt=\"Screenshot - ImageResource\" width=\"288\" height=\"300\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-ImageResource-288x300.png 288w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-ImageResource.png 332w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-WEB-INF-Resource.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1967\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-WEB-INF-Resource-300x289.png\" alt=\"Screenshot - WEB-INF Resource\" width=\"300\" height=\"289\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-WEB-INF-Resource-300x289.png 300w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-WEB-INF-Resource.png 462w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Vaadin provides stream resources, which allows creating of dynamic resources. These resources handle &#8222;<em>InputStream<\/em>&#8220; objects, which we can grab from Domino via Java NAPI.<\/p>\n<p>To do this, it is first required to add some plug-ins to the dependencies of the &#8222;<a title=\"HowTo: Vaadin on Domino\" href=\"https:\/\/hasselba.ch\/blog\/?p=1891\" target=\"_blank\"><em>HelloVaadin<\/em><\/a>&#8220; plug-in:<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Dependencies.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1968\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Dependencies-204x300.png\" alt=\"Screenshot - Dependencies\" width=\"204\" height=\"300\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Dependencies-204x300.png 204w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Dependencies.png 454w\" sizes=\"auto, (max-width: 204px) 100vw, 204px\" \/><\/a><\/p>\n<ul>\n<li>com.ibm.domino.napi<\/li>\n<li>com.ibm.domino.commons<\/li>\n<li>com.ibm.commons<\/li>\n<\/ul>\n<p>Then we can import the &#8222;<a title=\"XPages: WebContent Files (2) \u2013 Manipulate exitsting files using the Java NAPI\" href=\"https:\/\/hasselba.ch\/blog\/?p=1763\" target=\"_blank\"><em>NAPIUtils<\/em><\/a>&#8220; class to the project and add a new method &#8222;<em>loadBinaryFile<\/em>&#8220; to it:<\/p>\n<pre><code>\/**\r\n * loads given file from a database and returns the Inputsstream\r\n * \r\n * @param serverName\r\n *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the server to use\r\n * @param dbPath\r\n *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the database path\r\n * @param fileName\r\n *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 the file to load\r\n * @return the file data as InputStream\r\n * \r\n *\/\r\nstatic public InputStream loadBinaryFile(final String serverName, final String dbPath,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0final String fileName) {\r\n\r\n\u00a0\u00a0 \u00a0NotesSession nSession = null;\r\n\u00a0\u00a0 \u00a0NotesDatabase nDatabase = null;\r\n\u00a0\u00a0 \u00a0NotesNote nNote = null;\r\n\r\n\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0nSession = new NotesSession();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ open database\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0nDatabase = nSession.getDatabaseByPath(serverName + \"!!\" + dbPath);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (NotesAPIException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0nDatabase.open();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ load existing data\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0nNote = FileAccess.getFileByPath(nDatabase, fileName);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ get Filedate and return String\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0InputStream is = FileAccess.readFileContentAsInputStream(nNote);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return is;\r\n\u00a0\u00a0 \u00a0} catch (Exception e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0} finally {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ recycle NAPI objects\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0recycleNAPIObject(nNote, nDatabase, nSession);\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0return null;\r\n}\r\n<\/code><\/pre>\n<p>A new class &#8222;<em>DominoImageSource<\/em>&#8220; which implements the &#8222;<em>StreamResource.StreamSource<\/em>&#8220; interface uses this method to pipe the &#8222;<em>InputStream<\/em>&#8220; object returned from the Domino Java NAPI:<\/p>\n<pre><code>package ch.hasselba.vaadin;\r\n\r\nimport java.io.InputStream;\r\nimport ch.hasselba.napi.NAPIUtils;\r\nimport com.vaadin.server.StreamResource.StreamSource;\r\n\r\n@SuppressWarnings(\"serial\")\r\npublic class DominoImageSource implements StreamSource {\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0private String fileName;\r\n\u00a0\u00a0 \u00a0private String serverName;\r\n\u00a0\u00a0 \u00a0private String dbPath;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0public DominoImageSource(final String serverName, final String dbPath, \r\n            final String fileName){\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0super();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.serverName = serverName;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.dbPath = dbPath;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0this.fileName = fileName;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0public InputStream getStream () {\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return NAPIUtils.loadBinaryFile( this.serverName, this.dbPath, this.fileName );\r\n    }\r\n}<\/code><\/pre>\n<p>Now, we can create a new Application named &#8222;<em>ResourceUI<\/em>&#8220; which displays these two resources:<\/p>\n<pre><code>package ch.hasselba.vaadin;\r\n\r\nimport com.vaadin.server.StreamResource;\r\nimport com.vaadin.server.StreamResource.StreamSource;\r\nimport com.vaadin.server.VaadinRequest;\r\nimport com.vaadin.ui.Image;\r\nimport com.vaadin.ui.UI;\r\nimport com.vaadin.ui.VerticalLayout;\r\n\r\n@SuppressWarnings(\"serial\")\r\npublic class ResourcesUI extends UI\u00a0 {\r\n\r\n\u00a0\u00a0 \u00a0@Override\r\n\u00a0\u00a0 \u00a0protected void init(VaadinRequest request) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 VerticalLayout layout = new VerticalLayout();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 setContent(layout);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 layout.setSizeFull();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Create an instance of our stream source.\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 StreamSource webinfResource = new DominoImageSource ( \r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \"Dev01\", \"VaadinResources.nsf\" , \"\/WEB-INF\/WEB-INFResource.png\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 StreamSource imageResource = new DominoImageSource ( \r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \"Dev01\", \"VaadinResources.nsf\" , \"ImageResource.gif\");\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Create an image component that gets its contents\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ from the resource.\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 layout.addComponent(new Image(\"WEB-INF Resource\", \r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 new StreamResource(webinfResource, \"image01.png\")));\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 layout.addComponent(new Image(\"Image Resource\", \r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 new StreamResource(imageResource, \"image02.gif\")));\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0}\r\n}<\/code><\/pre>\n<p>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:<\/p>\n<p><a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Resource-Application.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-1970\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Resource-Application-300x174.png\" alt=\"Screenshot - Resource Application\" width=\"300\" height=\"174\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Resource-Application-300x174.png 300w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2015\/01\/Screenshot-Resource-Application.png 643w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>As soon we are changing the images in the database (and the cache is expired), the new image will appear in our Vaadin application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now, let&#8217;s access some Domino resources. I have created a database named &#8222;VaadinResources.nsf&#8222;, containing a normal image resource, and an image added via package explorer to the &#8222;WEB-INF&#8220; folder: Vaadin provides stream resources, which allows creating of dynamic resources. These &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=1964\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[64,63],"tags":[7,65,66],"class_list":["post-1964","post","type-post","status-publish","format-standard","hentry","category-osgi","category-vaadin","tag-domino","tag-osgi","tag-vaadin"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1964"}],"version-history":[{"count":3,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1964\/revisions"}],"predecessor-version":[{"id":1972,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1964\/revisions\/1972"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}