{"id":161,"date":"2019-09-19T12:19:24","date_gmt":"2019-09-19T12:19:24","guid":{"rendered":"http:\/\/lhotsetechnologies.com\/blog\/?p=161"},"modified":"2019-11-22T07:49:59","modified_gmt":"2019-11-22T07:49:59","slug":"html-of-a-page-in-osgi-service-in-aem","status":"publish","type":"post","link":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/","title":{"rendered":"HTML of a Page in OSGi Service in AEM"},"content":{"rendered":"<p>In this blog, I will explain one interesting use-case of one of my project. Here is the user-case-<\/p>\n<p>&nbsp;<\/p>\n<p>\u201cwhenever we activates a page on author instance we need to get the HTML for the activated page and send this page HTML to another search engine. \u201c<\/p>\n<p>&nbsp;<\/p>\n<p>In this blog, I will explain how I got the HTML of the activated page in my CustomTransportHander. Let\u2019s start with questions and answers.<\/p>\n<p>&nbsp;<\/p>\n<p><b>Have you used the HTTPClient API?<\/b><\/p>\n<p>No, we have the instructions not to use HTTPClient API to get the HTML. We need to use some internal AEM library for it.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Which APIs you have used?<\/b><\/p>\n<p>We completed this task using threeOOTB services.&nbsp;<\/p>\n<ol>\n<li style=\"font-weight: 400;\">RequestResponseFactory<\/li>\n<li style=\"font-weight: 400;\">SlingRequestProcessor<\/li>\n<li style=\"font-weight: 400;\">ResourceResolverFactory<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><b>What is the use of <\/b><b>RequestResponseFactory<\/b><b>?<\/b><\/p>\n<p>We have used this service to create HttpServletRequest and HttpServletResponse objects.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>What is the use of <\/b><b>SlingRequestProcessor<\/b><b>?<\/b><\/p>\n<p>This service is used to execute the newly created HttpServletRequest.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>What is the use of <\/b><b>ResourceResolverFactory<\/b><b>?<\/b><\/p>\n<p>This service is used to create the Resource Resolver object.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>What code you have added for getting the HTML of the activated page?<\/b><\/p>\n<p>Here are the code snippet-&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000080;\"><b>private <\/b><\/span>String getHTMLForRequestedPage(ReplicationLog replicationLog) {<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;&nbsp;&nbsp;HttpServletRequest req = <span style=\"color: #800080;\"><b>requestResponseFactory<\/b><\/span>.createRequest(<span style=\"color: #008000;\"><b>&#8220;GET&#8221;<\/b><\/span>, <span style=\"color: #800080;\"><b>pageURL<\/b><\/span>);<\/p>\n<p>&nbsp;&nbsp;&nbsp;WCMMode.<span style=\"color: #800080;\"><b><i>DISABLED<\/i><\/b><\/span>.toRequest(req);<\/p>\n<p>&nbsp;&nbsp;&nbsp;ByteArrayOutputStream out = <span style=\"color: #000080;\"><b>new <\/b><\/span>ByteArrayOutputStream();<\/p>\n<p>&nbsp;&nbsp;&nbsp;HttpServletResponse resp = <span style=\"color: #800080;\"><b>requestResponseFactory<\/b><\/span>.createResponse(out);<\/p>\n<p>&nbsp;&nbsp;&nbsp;<span style=\"color: #000080;\"><b>try <\/b><\/span>(ResourceResolver serviceResourceResolver = <span style=\"color: #800080;\"><b>resolverFactory<\/b><\/span>.getServiceResourceResolver(Collections.<i>singletonMap<\/i>(ResourceResolverFactory.<span style=\"color: #800080;\"><b><i>SUBSERVICE<\/i><\/b>, <b><i>SUB_SERVICE<\/i><\/b><\/span>))) {<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color: #800080;\"><b>slingRequestProcessor<\/b><\/span>.processRequest(req, resp, serviceResourceResolver);<\/p>\n<p>&nbsp;&nbsp;&nbsp;} <span style=\"color: #000080;\"><b>catch <\/b><\/span>(IOException | LoginException | ServletException e) {<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replicationLog.error(<span style=\"color: #008000;\"><b>&#8220;Exception occured&nbsp; : {}&#8221;<\/b><\/span>, e.getMessage());<\/p>\n<p>&nbsp;&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;&nbsp;<span style=\"color: #000080;\"><b>return <\/b><\/span>out.toString();<\/p>\n<p>}<\/p>\n<p>Don\u2019t forget to refer to all of the above mentioned services.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>What are the reference statements for these services?<\/p>\n<p>Here are these-<\/p>\n<p>@Reference<\/p>\n<p><span style=\"color: #000080;\"><b>private <\/b><\/span>SlingSettingsService <span style=\"color: #800080;\"><b>slingSettingsService<\/b><\/span>;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #808000;\">@Reference<\/span><\/p>\n<p><span style=\"color: #000080;\"><b>private <\/b><\/span>RequestResponseFactory <span style=\"color: #800080;\"><b>requestResponseFactory<\/b><\/span>;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #808000;\">@Reference<\/span><\/p>\n<p><span style=\"color: #000080;\"><b>private <\/b><\/span>SlingRequestProcessor <span style=\"color: #800080;\"><b>slingRequestProcessor<\/b><\/span>;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #808000;\">@Reference<\/span><\/p>\n<p><span style=\"color: #000080;\"><b>private <\/b><\/span>ResourceResolverFactory <span style=\"color: #800080;\"><b>resolverFactory<\/b><\/span>;<\/p>\n<p>&nbsp;<\/p>\n<p><b>In the above defined method, what is the value of pageURL?<\/b><\/p>\n<p>pageURL is the page URL with .html extension of the newly activated page. For example-<\/p>\n<p>If author activates a page &#8211; \/content\/demo\/en then pageURL value should be \/content\/demo\/en.html.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>How are you managing other assets or configurations which got activated with the page?<\/b><\/p>\n<p>We have written down this code in such a way that it will handle only page activation request. It will not work for other assets.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><b>Is this solution worked for you?<\/b><\/p>\n<p>Yes I got the expected results.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Happy Coding<\/strong><br \/>\n<strong>Namah Shivay<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, I will explain one interesting use-case of one of my project. Here is the user-case- &nbsp; \u201cwhenever [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-161","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>HTML of a Page in OSGi Service in AEM - AEM Blog | Lhotse Technologies<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HTML of a Page in OSGi Service in AEM - AEM Blog | Lhotse Technologies\" \/>\n<meta property=\"og:description\" content=\"In this blog, I will explain one interesting use-case of one of my project. Here is the user-case- &nbsp; \u201cwhenever [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\" \/>\n<meta property=\"og:site_name\" content=\"AEM Blog | Lhotse Technologies\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-19T12:19:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-22T07:49:59+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/person\/d1daa7081420d3cf2a02c309ee3f5800\"},\"headline\":\"HTML of a Page in OSGi Service in AEM\",\"datePublished\":\"2019-09-19T12:19:24+00:00\",\"dateModified\":\"2019-11-22T07:49:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\"},\"wordCount\":449,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\",\"url\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\",\"name\":\"HTML of a Page in OSGi Service in AEM - AEM Blog | Lhotse Technologies\",\"isPartOf\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#website\"},\"datePublished\":\"2019-09-19T12:19:24+00:00\",\"dateModified\":\"2019-11-22T07:49:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/lhotsetechnologies.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HTML of a Page in OSGi Service in AEM\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#website\",\"url\":\"https:\/\/lhotsetechnologies.com\/blog\/\",\"name\":\"AEM Blog | Lhotse Technologies\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/lhotsetechnologies.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#organization\",\"name\":\"AEM Blog | Lhotse Technologies\",\"url\":\"https:\/\/lhotsetechnologies.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/lhotsetechnologies.com\/blog\/wp-content\/uploads\/2019\/07\/lhotse-logo.png\",\"contentUrl\":\"https:\/\/lhotsetechnologies.com\/blog\/wp-content\/uploads\/2019\/07\/lhotse-logo.png\",\"width\":539,\"height\":172,\"caption\":\"AEM Blog | Lhotse Technologies\"},\"image\":{\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/person\/d1daa7081420d3cf2a02c309ee3f5800\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/829547de0cecf5477afeaf7ff2a49fc87a026be43cb5403fa54d0fd419192479?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/829547de0cecf5477afeaf7ff2a49fc87a026be43cb5403fa54d0fd419192479?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/lhotsetechnologies.com\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"HTML of a Page in OSGi Service in AEM - AEM Blog | Lhotse Technologies","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/","og_locale":"en_US","og_type":"article","og_title":"HTML of a Page in OSGi Service in AEM - AEM Blog | Lhotse Technologies","og_description":"In this blog, I will explain one interesting use-case of one of my project. Here is the user-case- &nbsp; \u201cwhenever [&hellip;]","og_url":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/","og_site_name":"AEM Blog | Lhotse Technologies","article_published_time":"2019-09-19T12:19:24+00:00","article_modified_time":"2019-11-22T07:49:59+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#article","isPartOf":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/"},"author":{"name":"admin","@id":"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/person\/d1daa7081420d3cf2a02c309ee3f5800"},"headline":"HTML of a Page in OSGi Service in AEM","datePublished":"2019-09-19T12:19:24+00:00","dateModified":"2019-11-22T07:49:59+00:00","mainEntityOfPage":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/"},"wordCount":449,"commentCount":0,"publisher":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/","url":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/","name":"HTML of a Page in OSGi Service in AEM - AEM Blog | Lhotse Technologies","isPartOf":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/#website"},"datePublished":"2019-09-19T12:19:24+00:00","dateModified":"2019-11-22T07:49:59+00:00","breadcrumb":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/lhotsetechnologies.com\/blog\/html-of-a-page-in-osgi-service-in-aem\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/lhotsetechnologies.com\/blog\/"},{"@type":"ListItem","position":2,"name":"HTML of a Page in OSGi Service in AEM"}]},{"@type":"WebSite","@id":"https:\/\/lhotsetechnologies.com\/blog\/#website","url":"https:\/\/lhotsetechnologies.com\/blog\/","name":"AEM Blog | Lhotse Technologies","description":"","publisher":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/lhotsetechnologies.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/lhotsetechnologies.com\/blog\/#organization","name":"AEM Blog | Lhotse Technologies","url":"https:\/\/lhotsetechnologies.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/lhotsetechnologies.com\/blog\/wp-content\/uploads\/2019\/07\/lhotse-logo.png","contentUrl":"https:\/\/lhotsetechnologies.com\/blog\/wp-content\/uploads\/2019\/07\/lhotse-logo.png","width":539,"height":172,"caption":"AEM Blog | Lhotse Technologies"},"image":{"@id":"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/person\/d1daa7081420d3cf2a02c309ee3f5800","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/lhotsetechnologies.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/829547de0cecf5477afeaf7ff2a49fc87a026be43cb5403fa54d0fd419192479?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/829547de0cecf5477afeaf7ff2a49fc87a026be43cb5403fa54d0fd419192479?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/lhotsetechnologies.com\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":5,"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":553,"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/161\/revisions\/553"}],"wp:attachment":[{"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lhotsetechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}