{"id":2478,"date":"2010-12-27T15:38:07","date_gmt":"2010-12-27T10:08:07","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2478"},"modified":"2016-12-19T15:04:09","modified_gmt":"2016-12-19T09:34:09","slug":"request-mocking-to-use-groovypagestemplateengine-in-backend-threads","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/request-mocking-to-use-groovypagestemplateengine-in-backend-threads\/","title":{"rendered":"Request Mocking to use groovyPagesTemplateEngine in backend threads"},"content":{"rendered":"<p>We have a setup where a backend thread, fired by the Spring Events, does some processing, generates a PDF and emails the result to the user.<br \/>\nThe code we were using to generate the HTML from a GSP to be converted to a PDF using iText was as follows :<\/p>\n<p>[java]<br \/>\n        def webRequest = RequestContextHolder.getRequestAttributes()<br \/>\n        def originalOut = webRequest.out<br \/>\n        try {<br \/>\n            def sw = new StringWriter()<br \/>\n            def pw = new PrintWriter(sw)<br \/>\n            webRequest.out = pw<br \/>\n            groovyPagesTemplateEngine.createTemplate(&amp;quot;path_to_gsp&amp;quot;).make([model:model]).writeTo(pw)<br \/>\n            return sw.toString()<br \/>\n        } finally {<br \/>\n          webRequest.out = originalOut<br \/>\n        }<br \/>\n[\/java]<\/p>\n<p>There was the obvious fallibility of this code that there was no Current Request associated with the backend thread!<\/p>\n<p>After doing some googling around it, I came across a few threads and posts which talked about Request Mocking, which has been used in Grails Template Engine Plugin. \u00a0We overcame this by using the code snippet given below to mock the web request<\/p>\n<p>[java]<br \/>\ndef webRequest = RequestContextHolder.getRequestAttributes()<br \/>\nif(!webRequest) {<br \/>\n          def servletContext \u00a0= ServletContextHolder.getServletContext()<br \/>\n          def applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)<br \/>\n          webRequest = grails.util.GrailsWebUtil.bindMockWebRequest(applicationContext)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>However, things didn&#8217;t end there. This code broke while working on a WAR environment. The problem was that the MockWebRequest class was part of the &#8220;org.springframework:org.springframework.test:3.0.3.RELEASE&#8221; jar and had to be included in the BuildConfig.groovy as<\/p>\n<p>[java]<\/p>\n<p>dependencies{<\/p>\n<p>runtime &#8216;org.springframework:org.springframework.test:3.0.3.RELEASE&#8217;<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>Ensure that the line<\/p>\n<p>[java]<\/p>\n<p>mavenCentral()<\/p>\n<p>[\/java]<\/p>\n<p>is <span style=\"color: #ff0000;\">not commented<\/span> in BuildConfig.groovy<\/p>\n<p>We were working on Grails 1.3.4<\/p>\n<p>&nbsp;<\/p>\n<p>Hope this helps.<br \/>\nVivek<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have a setup where a backend thread, fired by the Spring Events, does some processing, generates a PDF and emails the result to the user. The code we were using to generate the HTML from a GSP to be converted to a PDF using iText was as follows : [java] def webRequest = RequestContextHolder.getRequestAttributes() [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[4840,9,32,292,4841,484],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2478"}],"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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2478"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2478\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}