{"id":871,"date":"2012-09-12T10:49:14","date_gmt":"2012-09-12T08:49:14","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=871"},"modified":"2012-11-16T10:53:32","modified_gmt":"2012-11-16T08:53:32","slug":"xpages-the-outputstream-and-binary-data","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=871","title":{"rendered":"XPages: The Outputstream and binary data"},"content":{"rendered":"<p><strong>As wiritten in the comments, Verne is correct. I just missed the &#8222;<em>facesContext.responseComplete()<\/em>&#8220; in the <em>beforeRenderResponse<\/em> event.<\/strong><\/p>\n<p>If you want to get control over the outputstream of a XPage, you can use the response object from the <a title=\"oracle.com: javax.faces.context.ExternalContext\" href=\"http:\/\/docs.oracle.com\/javaee\/5\/api\/javax\/faces\/context\/ExternalContext.html\" target=\"_blank\">ExternalContext<\/a>:<\/p>\n<pre>var response = facesContext.getExternalContext().getResponse()<\/pre>\n<p>This will give you access to an object of type <em>com.ibm.xsp.webapp.XspHttpServletResponse<\/em> which allows some basic operations, but the response will always be encoded to UTF-8. <del>You can not return any binary data directly.<\/del><\/p>\n<p><del>But if you access the underlying <em>LCDAdapterHttpServletResponse<\/em> directly, it is possible to get the full control for the outputstream.<\/p>\n<p><\/del><\/p>\n<pre>var exCon = facesContext.getExternalContext();\r\nvar response = exCon.getResponse().getDelegate();<\/pre>\n<p>This allows you to send any data you want to the browser. Here is an example for a JPEG rendered directly in the XPage:<\/p>\n<p>1. The XPage<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;xp:view xmlns:xp=\"http:\/\/www.ibm.com\/xsp\/core\" rendered=\"true\"&gt;\r\n\r\n   &lt;xp:this.afterRenderResponse&gt;\r\n\u00a0\u00a0    &lt;![CDATA[#{javascript:\r\n\u00a0\u00a0 \u00a0\u00a0    importPackage( ch.hasselba.xpages.demo );\r\n         var exCon = facesContext.getExternalContext();\r\n\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0var response = exCon.getResponse().getDelegate();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 ch.hasselba.xpages.demo.JPEGGenerator.generate( response );\r\n      }]]&gt;\r\n   &lt;\/xp:this.afterRenderResponse&gt;\r\n\r\n&lt;\/xp:view&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>2. The Java object to generate a JPEG<\/p>\n<pre>package ch.hasselba.xpages.demo;\r\n\r\nimport javax.servlet.ServletOutputStream;\r\nimport javax.servlet.http.HttpServletResponse;\r\nimport java.awt.Graphics;\r\nimport java.awt.image.BufferedImage;\r\nimport java.io.IOException;\r\nimport com.sun.image.codec.jpeg.ImageFormatException;\r\nimport com.sun.image.codec.jpeg.JPEGCodec;\r\nimport java.awt.Color;\r\nimport java.awt.Font;\r\n\r\n\/**\r\n\u00a0* JPEG Generator for a simple demonstration of controlling the\r\n\u00a0* HttpServletResponse output\r\n\u00a0* \r\n\u00a0* @author Sven Hasselbach\r\n\u00a0* @category Demo\r\n\u00a0* @category Servlet\r\n\u00a0* @category Image\r\n\u00a0* @version 1.0\r\n\u00a0*\/\r\n\r\npublic class JPEGGenerator {\r\n\r\n\u00a0\u00a0 \u00a0private static final long serialVersionUID = 1L;\r\n\u00a0\u00a0 \u00a0private static final int WIDTH = 800;\r\n\u00a0\u00a0 \u00a0private static final int HEIGHT = 200;\r\n\u00a0\u00a0 \u00a0private static final Color BACKGROUND_COLOR = new Color(224, 224, 224);\r\n\u00a0\u00a0 \u00a0private static final Color COLOR = new Color(0, 0, 0);\r\n\u00a0\u00a0 \u00a0private static final Font FONT = new Font(\"Times New Roman\", Font.BOLD, 46);\r\n\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * generates a JPEG image and sends the result to the outputstream of the\r\n\u00a0\u00a0 \u00a0 * HttpServlet\r\n\u00a0\u00a0 \u00a0 * \r\n\u00a0\u00a0 \u00a0 * @param response\r\n\u00a0\u00a0 \u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 HttpServletResponse\r\n\u00a0\u00a0 \u00a0 * @author Sven Hasselbach\r\n\u00a0\u00a0 \u00a0 * @version 1.0\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public static void generate(final HttpServletResponse response) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ServletOutputStream out = null;\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0try {\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ set the content type for JPEG\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0response.setContentType(\"image\/jpg\");\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ get the ouput stream\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0out = response.getOutputStream();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ generate a image to convert\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0BufferedImage img = createImage();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ convert image to jpeg and send to output\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0JPEGCodec.createJPEGEncoder(out).encode(img);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (ImageFormatException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} catch (IOException e) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0e.printStackTrace();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0\/**\r\n\u00a0\u00a0 \u00a0 * creates a simple \"Hello World\" image\r\n\u00a0\u00a0 \u00a0 * \r\n\u00a0\u00a0 \u00a0 * @return BufferedImage\r\n\u00a0\u00a0 \u00a0 * @author Sven Hasselbach\r\n\u00a0\u00a0 \u00a0 * @version 1.0\r\n\u00a0\u00a0 \u00a0 *\/\r\n\u00a0\u00a0 \u00a0public static BufferedImage createImage() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0BufferedImage image = new BufferedImage(WIDTH, HEIGHT,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0BufferedImage.TYPE_BYTE_INDEXED);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Graphics graphics = image.getGraphics();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0graphics.setColor(BACKGROUND_COLOR);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0graphics.fillRect(0, 0, image.getWidth(), image.getHeight());\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0graphics.setColor(COLOR);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0graphics.setFont(FONT);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0graphics.drawString(\"Hello World!\", 10, HEIGHT \/ 2);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return image;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p>There is no need for splitting this example in a separated Java class. You can directly access the output in SSJS and send your binary data directly via\u00a0<a title=\"oracle.com: java.io.OutputStream\" href=\"http:\/\/docs.oracle.com\/javase\/1.5.0\/docs\/api\/java\/io\/OutputStream.html#write%28byte[]%29\" target=\"_blank\">write<\/a> method of the <a title=\"oracle.com: javax.servlet.ServletOutputStream\" href=\"http:\/\/docs.oracle.com\/javaee\/5\/api\/javax\/servlet\/ServletOutputStream.html\" target=\"_blank\">ServletOutputStream<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As wiritten in the comments, Verne is correct. I just missed the &#8222;facesContext.responseComplete()&#8220; in the beforeRenderResponse event. If you want to get control over the outputstream of a XPage, you can use the response object from the ExternalContext: var response &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=871\">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":[89,82,76,81,74],"tags":[33,7,11,31,16,5,12,3],"class_list":["post-871","post","type-post","status-publish","format-standard","hentry","category-java","category-server","category-ssjs","category-web","category-xpages","tag-8-5-3","tag-domino","tag-http","tag-java","tag-server","tag-ssjs","tag-web","tag-xpages"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/871","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=871"}],"version-history":[{"count":14,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/871\/revisions"}],"predecessor-version":[{"id":1059,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/871\/revisions\/1059"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}