{"id":8958,"date":"2012-09-27T17:11:35","date_gmt":"2012-09-27T11:41:35","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8958"},"modified":"2012-09-27T17:11:35","modified_gmt":"2012-09-27T11:41:35","slug":"cool-way-to-getset-processed-html-markup-in-filters","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/cool-way-to-getset-processed-html-markup-in-filters\/","title":{"rendered":"Cool way to get\/set processed HTML Markup in filters"},"content":{"rendered":"<p>Hi, <\/p>\n<p>&nbsp;<\/p>\n<p>There was a case where i had to encrypt the HTML output of the GSP before displaying it to user. There are a lot of ways to do it, but i wanted to make is as simple as possible. Hence here is the pattern i used:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong> This is just an example, you can use this concept to do thousands of cool things, like: <strong>pdf export etc.. <\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>ON THE GSP<br \/>\n[html]<br \/>\n&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;meta name=&quot;encrypt&quot; value=&quot;true&quot; \/&gt;<br \/>\n    &#8230;<br \/>\n&lt;\/head&gt;<br \/>\n &#8230;. misc code<br \/>\n&lt;\/html&gt;<br \/>\n[\/html]<br \/>\nHere what is did is, i created a GSP and just added a meta property to it saying &#8220;encrypt true&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>Now here is the Code for the Grails Filters (See inline comments)<br \/>\n[java]<br \/>\ndef filters = {<br \/>\n        all(controller: &#8216;*&#8217;, action: &#8216;*&#8217;) {<br \/>\n            before = {<br \/>\n            }<br \/>\n            after = { Map model -&gt;<br \/>\n                try {<br \/>\n                    \/\/Get the Site Mesh Page Instance<br \/>\n                    GSPSitemeshPage sitemeshPage = response?.getContent() as GSPSitemeshPage<\/p>\n<p>                    \/\/Check if it is actual sitemest page, not just plain rendered content<br \/>\n                    if (sitemeshPage &amp;&amp; sitemeshPage.getPage()) {<br \/>\n                        \/\/Get the meta property value.<br \/>\n                        String metaPropValue = sitemeshPage.getProperty(&quot;meta.encrypt&quot;)<br \/>\n                        if (metaPropValue &amp;&amp; metaPropValue == &quot;true&quot;) {<br \/>\n                            \/\/This is how you can get html contents of head\/body after all tags resolved.<br \/>\n                            String head = new String(sitemeshPage.getHead())<br \/>\n                            String body = new String(sitemeshPage.getBody())<\/p>\n<p>                            \/\/Buffer for new body<br \/>\n                            StreamCharBuffer writer = new StreamCharBuffer()<br \/>\n                            \/\/Write to buffer new content<br \/>\n                            writer.getWriter().write(encryptionService.encrypt(body))<br \/>\n                            sitemeshPage.setBodyBuffer(writer)<br \/>\n                        }<br \/>\n                    }<br \/>\n                } catch (Throwable ex) {<br \/>\n                    ex.printStackTrace()<br \/>\n                }<br \/>\n            }<br \/>\n            afterView = { Exception e -&gt;<\/p>\n<p>            }<br \/>\n        }<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>And hence we have this processed content in response \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Regards<br \/>\nKushal Likhi<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, &nbsp; There was a case where i had to encrypt the HTML output of the GSP before displaying it to user. There are a lot of ways to do it, but i wanted to make is as simple as possible. Hence here is the pattern i used: &nbsp; Note: This is just an example, [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[1093,4840,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8958"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8958"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8958\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}