{"id":1122,"date":"2010-06-23T13:07:51","date_gmt":"2010-06-23T07:37:51","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1122"},"modified":"2016-12-19T15:06:30","modified_gmt":"2016-12-19T09:36:30","slug":"opening-pdf-in-google-docs-using-time-based-cache","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/opening-pdf-in-google-docs-using-time-based-cache\/","title":{"rendered":"Open Pdf in Google Docs using Time Based Cache in Grails"},"content":{"rendered":"<p>Recently, we had a requirement where secure URLs were to be made accessible to Google.<\/p>\n<p>Our implementation was to send a url with token valid for 10 seconds. The token expired automatically after 10 seconds. We used time based cache for this as described here:<span style=\"font-size: 13.3333px;\"> <\/span><\/p>\n<p>T set max time to 10 seconds , I set default MaxAge to 10 millis in above file only which could have also been done easily using its constructor.<\/p>\n<p>Here&#8217;s how we did it ,<\/p>\n<pre lang=\"groovy\">class AbcController {\r\n    static TimedMap timedMap = new TimedMap()\r\n\r\n   \/\/  while calling \"addDocumentUrl\" path of document was passed as params attachmentPath\r\n    def addDocumentUrl = {\r\n \t        String attachmentPath = params.attachmentPath\r\n \t        String token = UUID.randomUUID().toString()\r\n \t        timedMap.put(uniqueIdentification,attachmentPath)\r\n \t        redirect(url: getGoogleUrl(token))          \/\/ this will  make google hit specified url\r\n \t    }\r\n\r\n     String getGoogleUrl(String token) {\r\n \t        String absoluteUrl = ConfigurationHolder.config.builder.absoluteURL\r\n                String GOOGLE_URL=\"http:\/\/docs.google.com\/viewer?embedded=true&amp;url=\"\r\n \t        String googlePath = GOOGLE_URL + absoluteUrl + \"\/abc\/someAction\/\" + token\r\n \t        return googlePath\r\n \t    }\r\n}<\/pre>\n<p>Action someAction was made public so that Google was able to access it. And when Google hits this action it gives token as id.<\/p>\n<pre lang=\"groovy\">   def someAction = {\r\n        String key = params.id.toString()\r\n        try {\r\n            String attachmentPath = timedMap.get(key)\r\n            File file = new File(attachmentPath)\r\n            response.setHeader(\"Content-disposition\", \"attachment; filename=pdf\")\r\n            response.setContentType(\"application\/pdf\")\r\n            response.setContentLength(file.size().toInteger());\r\n            OutputStream out = response.getOutputStream();\r\n            out.write(file.readBytes());\r\n            out.flush()\r\n            out.close();\r\n        } catch (Exception e) {\r\n            e.printStackTrace()\r\n        }\r\n    }<\/pre>\n<p>_______________________________<br \/>\nHitesh Bhatia<br \/>\nhitesh@intelligrape.com<\/p>\n<p>_______________________________<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, we had a requirement where secure URLs were to be made accessible to Google. Our implementation was to send a url with token valid for 10 seconds. The token expired automatically after 10 seconds. We used time based cache for this as described here: T set max time to 10 seconds , I set [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[291,4840,9,292,290,293,294],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1122"}],"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\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1122"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1122\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}