<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sling Servlet Archives - Sourced Code</title>
	<atom:link href="https://sourcedcode.com/blog/category/aem/development/sling-servlet/feed" rel="self" type="application/rss+xml" />
	<link>https://sourcedcode.com</link>
	<description>AEM Blog Made Just for You.</description>
	<lastBuildDate>Sat, 24 Feb 2024 18:17:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.8</generator>

<image>
	<url>https://sourcedcode.com/wp-content/uploads/2019/09/cropped-favicon-32x32.png</url>
	<title>Sling Servlet Archives - Sourced Code</title>
	<link>https://sourcedcode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Handling Http Requests, HttpClient, in AEM</title>
		<link>https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem</link>
					<comments>https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 24 Feb 2024 18:13:27 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sling Models]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6538</guid>

					<description><![CDATA[<p>When integrating with external services or APIs from Adobe Experience Manager (AEM), utilizing the HttpClient for outbound HTTP requests is a common requirement. It&#8217;s crucial to handle these connections efficiently, securely, and in a way that is compatible with AEM&#8217;s architecture to ensure optimal performance and maintainability of your code. AEM provides a specialized factory, [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem">Handling Http Requests, HttpClient, in AEM</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>When integrating with external services or APIs from Adobe Experience Manager (AEM), utilizing the HttpClient for outbound HTTP requests is a common requirement. It&#8217;s crucial to handle these connections efficiently, securely, and in a way that is compatible with AEM&#8217;s architecture to ensure optimal performance and maintainability of your code. </p>
<p>AEM provides a specialized factory, HttpClientFactory, designed to streamline the creation and configuration of HttpClient instances. This factory not only simplifies the process of instantiating HttpClient objects but also ensures that the instances are optimized for use within the AEM environment. </p>
<p>By leveraging HttpClientFactory, developers can take advantage of built-in connection pooling and configuration optimizations that are specifically tailored for AEM applications. Furthermore, using AEM&#8217;s HttpClientFactory significantly facilitates the <a href="https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java" rel="noopener" target="_blank">unit testing</a> of code that makes HTTP requests, allowing for more maintainable and testable codebases.</p>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ul>
<li><a href="#UtilizeAEMsHttpClientFactory">1. Utilize AEM&#8217;s HttpClientFactory</a></li>
<li><a href="#OSGiComponentsSlingServlets">1.1 OSGi Components &#038; Sling Servlets</a></li>
<li><a href="#SlingModelsWorkflows">12 Sling Models &#038; Workflows</a></li>
<li><a href="#EnsureProperResourceManagement">2. Ensure Proper Resource Management</a></li>
<li><a href="#ConfigureHttpClientSecurely">3. Configure HttpClient Securely</a></li>
<li><a href="#ReuseHttpClientInstancesWisely">4. Reuse HttpClient Instances Wisely</a></li>
<li><a href="#MonitorAndLogHttpClientActivity">5. Monitor and Log HttpClient Activity</a></li>
<ul>
</div>
<hr class="spacer-large" />
<h2 id="UtilizeAEMsHttpClientFactory">1. Utilize AEM&#8217;s HttpClientFactory</h2>
<p>Leverage AEM’s HttpClientFactory or the OSGi HttpClientBuilderFactory for creating instances of HttpClient. This ensures you benefit from connection pooling, proper configuration, and optimizations tailored for AEM. Avoid manually creating HttpClient instances as it bypasses these optimizations and can lead to resource leakage and scalability issues.</p>
<h3 id="OSGiComponentsSlingServlets">1.1 OSGi Components &#038; Sling Servlets</h3>
<p><strong>OSGi Components</strong>: Custom services, event handlers, schedulers, and any other Java class defined as an OSGi component can use this code pattern below. OSGi components are the backbone for modular development in AEM, allowing for dynamic module management within the application.</p>
<p><strong>Sling Servlets</strong>: Sling Servlets are another type of OSGi component that can use HttpClientBuilderFactory for making HTTP requests; using the code pattern below. Sling Servlets handle HTTP requests and can be used for various purposes, including interfacing with external systems.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@<span style="color: #003399;">Reference</span><br />
<span style="color: #000000; font-weight: bold;">private</span> HttpClientBuilderFactory httpClientBuilderFactory<span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> executeHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>CloseableHttpClient httpClient <span style="color: #339933;">=</span> httpClientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Perform HTTP operations with httpClient</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium" />
<h3 id="SlingModelsWorkflows">1.2 Sling Models &#038; Workflows</h3>
<p><strong>Sling Models</strong>: While not typically using @Reference (as they are not OSGi components themselves), Sling Models can still access OSGi services like HttpClientBuilderFactory through the use of the @OSGiService annotation or by adapting from a Sling resource or request; using the code pattern below. This allows Sling Models to utilize services for fetching or sending data to external systems as part of their logic.</p>
<p><strong>Workflows</strong>: Custom workflow steps can interact with OSGi services, including HttpClientBuilderFactory, to perform operations like sending data to external systems or integrating with RESTful services as part of the workflow process. These custom steps are developed as OSGi components and can use the @Reference annotation to inject necessary services.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> Resource.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExampleSlingModel <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @OSGiService<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> HttpClientBuilderFactory httpClientBuilderFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> executeHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>CloseableHttpClient httpClient <span style="color: #339933;">=</span> httpClientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Perform HTTP operations with httpClient</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h2 id="EnsureProperResourceManagement">2. Ensure Proper Resource Management</h2>
<p>Always close HttpClient, HttpResponse, and any InputStream or Entity objects to release system resources and avoid connection leaks. Utilize try-with-resources or finally blocks to ensure resources are closed even if exceptions occur.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>CloseableHttpResponse response <span style="color: #339933;">=</span> httpClient.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>httpGet<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the response</span><br />
&nbsp; &nbsp; EntityUtils.<span style="color: #006633;">consume</span><span style="color: #009900;">&#40;</span>response.<span style="color: #006633;">getEntity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h2 id="ConfigureHttpClientSecurely">3. Configure HttpClient Securely</h2>
<p>Pay attention to security configurations when setting up your HttpClient. This includes setting up SSL context correctly, enabling hostname verification, and configuring timeouts to prevent attacks that target resource consumption. Avoid disabling SSL verification or other security features unless absolutely necessary and understood.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">RequestConfig requestConfig <span style="color: #339933;">=</span> RequestConfig.<span style="color: #006633;">custom</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #006633;">setConnectTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #006633;">setSocketTimeout</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>CloseableHttpClient httpClient <span style="color: #339933;">=</span> httpClientBuilderFactory.<span style="color: #006633;">newBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">setDefaultRequestConfig</span><span style="color: #009900;">&#40;</span>requestConfig<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Securely configured HttpClient</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exceptions</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h2 id="ReuseHttpClientInstancesWisely">4. Reuse HttpClient Instances Wisely</h2>
<p>For efficiency, reuse HttpClient instances across requests whenever possible. This approach benefits from the HTTP connection pooling managed by AEM, reducing the overhead of establishing connections for each request. However, ensure that any client-specific configurations do not unintentionally affect subsequent requests.</p>
<hr class="spacer-large" />
<h2 id="MonitorAndLogHttpClientActivity">5. Monitor and Log HttpClient Activity</h2>
<p>Implement logging and monitoring around your HTTP requests to diagnose issues, track performance, and understand dependencies on external services. Use AEM’s logging capabilities to log request details, response times, and errors. This can aid in troubleshooting and optimizing the use of HttpClient in your application.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>CloseableHttpResponse response <span style="color: #339933;">=</span> httpClient.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span>httpGet<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Log the status line</span><br />
&nbsp; &nbsp; LOG.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP response status: {}&quot;</span>, response.<span style="color: #006633;">getStatusLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; LOG.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;HTTP request execution failed&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>By adhering to these five rules, developers can ensure their AEM projects make the most out of HttpClient for external communications, balancing efficiency with robust security practices. Utilizing AEM&#8217;s HttpClientFactory not only streamlines the development process but also enhances the ease of <a href="https://sourcedcode.com/blog/aem/junit-5-mocking-the-httpclient-in-java" rel="noopener" target="_blank">unit testing</a>, making it an indispensable practice for AEM development.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem">Handling Http Requests, HttpClient, in AEM</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/handling-http-requests-httpclient-in-aem/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>Benefits of SlingSafeMethodsServlet with Code Examples</title>
		<link>https://sourcedcode.com/blog/aem/benefits-of-slingsafemethodsservlet-with-code-examples</link>
					<comments>https://sourcedcode.com/blog/aem/benefits-of-slingsafemethodsservlet-with-code-examples#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Sat, 02 Dec 2023 19:34:29 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6275</guid>

					<description><![CDATA[<p>A robust security strategy is pivotal to safeguarding applications. As an AEM developer, we must really understand the SlingSafeMethodsServlet, an invaluable feature within the Apache Sling framework. This article explores the core concepts, practical benefits, and the heightened security it brings to servlet development. The org.apache.sling.api.servlets.SlingSafeMethodsServlet is an extension of the trusted HttpServlet in Apache [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/benefits-of-slingsafemethodsservlet-with-code-examples">Benefits of SlingSafeMethodsServlet with Code Examples</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<section>
<p>A robust security strategy is pivotal to safeguarding applications. As an AEM developer, we must really understand the <strong>SlingSafeMethodsServlet</strong>, an invaluable feature within the Apache Sling framework. This article explores the core concepts, practical benefits, and the heightened security it brings to servlet development.</p>
<p>The <strong>org.apache.sling.api.servlets.SlingSafeMethodsServlet</strong> is an extension of the trusted HttpServlet in Apache Sling, introduces a focused approach to reinforcing security by limiting HTTP methods to read-only operations. This deep dive into its functionality unveils the foundation upon which secure servlets can be built.</p>
</section>
<div class="mention-block">
<strong>Quick Links</strong></p>
<ol>
<li><a href="#advanntages">Advantages of SlingSafeMethodsServlet</a></li>
<li><a href="#code-sample">SlingSafeMethodsServlet in Action: Example Use Case</a></li>
</ol>
</div>
<hr class="spacer-larger"/>
<section>
<h2 id="advanntages">Advantages of SlingSafeMethodsServlet</h2>
<ol>
<li>
                <strong>Read-Only Operations:</strong></p>
<p>At its core, SlingSafeMethodsServlet champions a read-only paradigm. This deliberate limitation ensures that servlets exclusively respond to safe HTTP methods, fortifying applications against unintentional data modifications.</p>
</li>
<li>
                <strong>Guard Against CSRF Attacks:</strong></p>
<p>By constraining actions to safe methods, SlingSafeMethodsServlet provides an inherent defense against Cross-Site Request Forgery (CSRF) attacks. This proactive measure significantly reduces the risk of unauthorized manipulations.</p>
</li>
<li>
                <strong>Built-In Support for Idempotent Methods:</strong></p>
<p>The servlet seamlessly integrates with idempotent methods like GET and HEAD, aligning with industry best practices. This default behavior enhances the predictability and reliability of request handling.</p>
</li>
<li>
                <strong>Resource-Based Permissions:</strong></p>
<p>Leveraging Sling&#8217;s resource resolution capabilities, SlingSafeMethodsServlet dynamically inherits permissions tied to resolved resources. This ensures a granular and secure access control model.</p>
</li>
</ol>
</section>
<hr class="spacer-larger"/>
<h2 id="code-sample">SlingSafeMethodsServlet in Action: Example Use Case</h2>
<p>In this practical example, the <span class="code-highlight-primary">ContentDisplayServlet:line17</span> showcases the seamless integration of SlingSafeMethodsServlet. The provided code exemplifies its application in retrieving and securely rendering content, encapsulating the essence of secure servlet development.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.ServletException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>javax.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">Servlet</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.resourceTypes=yourapp/components/content&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContentDisplayServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Fetch and render content</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Send JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/benefits-of-slingsafemethodsservlet-with-code-examples">Benefits of SlingSafeMethodsServlet with Code Examples</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/benefits-of-slingsafemethodsservlet-with-code-examples/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AEM Convert Excel to JSON Simple Servlet Example</title>
		<link>https://sourcedcode.com/blog/aem/aem-convert-excel-to-json-simple-servlet-example</link>
					<comments>https://sourcedcode.com/blog/aem/aem-convert-excel-to-json-simple-servlet-example#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Thu, 02 Nov 2023 01:28:53 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Short Post]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6192</guid>

					<description><![CDATA[<p>In this article, I present a straightforward yet powerful solution that demonstrates how to seamlessly convert Excel data into JSON format using Adobe Experience Manager (AEM). By implementing a custom servlet, I will illustrate the process of converting an Excel sheet named &#8216;example.xlsx&#8217; into structured JSON data; it can be downloaded below for you following [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-convert-excel-to-json-simple-servlet-example">AEM Convert Excel to JSON Simple Servlet Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this article, I present a straightforward yet powerful solution that demonstrates how to seamlessly convert Excel data into JSON format using Adobe Experience Manager (AEM). By implementing a custom servlet, I will illustrate the process of converting an Excel sheet named &#8216;example.xlsx&#8217; into structured JSON data; it can be downloaded below for you following this tutorial. This tutorial is validated on both AEM 6.5 with the latest Service Pack and AEM as a Cloud Service (AEMaaCS), ensuring its compatibility with diverse AEM environments.</p>
<p><strong>Resource File:</strong><br />
<a href="https://sourcedcode.com/blog/aem/aem-convert-excel-to-json-simple-servlet-example/attachment/example" rel="attachment wp-att-6196">example.xlsx</a></p>
<hr class="spacer-large"/>
<p><strong>Request URI:</strong><br />
The request URI for accessing the JSON output through the ExampleServlet that we&#8217;ve been discussing would typically look like this:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//localhost:4502/bin/exportexcelfortext.json</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<p><strong>JSON Output:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #0000ff;">&quot;people&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Brian&quot;</span>, <span style="color: #0000ff;">&quot;age&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;100&quot;</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Doris&quot;</span>, <span style="color: #0000ff;">&quot;age&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;20&quot;</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Stuart&quot;</span>, <span style="color: #0000ff;">&quot;age&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;103&quot;</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;David&quot;</span>, <span style="color: #0000ff;">&quot;age&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;5&quot;</span> <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<p><strong>Servlet Implementation ExcelExampleServlet.java:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlets</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.fasterxml.jackson.databind.ObjectMapper</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.poi.ss.usermodel.*</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.poi.xssf.usermodel.XSSFWorkbook</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.Resource</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.http.HttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.LinkedHashMap</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Objects</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.dam.api.Asset</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/exportexcelfortext&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.extensions=json&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ExcelExampleServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the Excel file path in DAM</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> excelFilePath <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/content/dam/example/example.xlsx&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the resource resolver</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResourceResolver resourceResolver <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Check if the Excel file exists</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Resource excelResource <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span>excelFilePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>excelResource <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_NOT_FOUND</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Adapt the resource to an Asset</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Asset asset <span style="color: #339933;">=</span> Objects.<span style="color: #006633;">requireNonNull</span><span style="color: #009900;">&#40;</span>excelResource<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Asset.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>asset <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get an InputStream from the Asset's original</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">InputStream</span> excelInputStream <span style="color: #339933;">=</span> Objects.<span style="color: #006633;">requireNonNull</span><span style="color: #009900;">&#40;</span>asset<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOriginal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the Excel content and convert it to JSON</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Workbook workbook <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XSSFWorkbook<span style="color: #009900;">&#40;</span>excelInputStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;&gt;</span> jsonData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Iterate through the Excel rows and columns</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sheet sheet <span style="color: #339933;">=</span> workbook.<span style="color: #006633;">getSheetAt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Assuming it's the first sheet</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Row headerRow <span style="color: #339933;">=</span> sheet.<span style="color: #006633;">getRow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Assuming the first row is the header row</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> totalColumns <span style="color: #339933;">=</span> headerRow.<span style="color: #006633;">getLastCellNum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> sheet.<span style="color: #006633;">getPhysicalNumberOfRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Row dataRow <span style="color: #339933;">=</span> sheet.<span style="color: #006633;">getRow</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> rowMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedHashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> totalColumns<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cell headerCell <span style="color: #339933;">=</span> headerRow.<span style="color: #006633;">getCell</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Cell dataCell <span style="color: #339933;">=</span> dataRow.<span style="color: #006633;">getCell</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>headerCell <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> dataCell <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Check the cell type to handle numeric and string values</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dataCell.<span style="color: #006633;">getCellType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> CellType.<span style="color: #006633;">STRING</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>headerCell.<span style="color: #006633;">getStringCellValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, dataCell.<span style="color: #006633;">getStringCellValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>dataCell.<span style="color: #006633;">getCellType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> CellType.<span style="color: #006633;">NUMERIC</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle numeric values as integers (remove decimals)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">int</span> age <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#41;</span> dataCell.<span style="color: #006633;">getNumericCellValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>headerCell.<span style="color: #006633;">getStringCellValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>age<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonData.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>rowMap<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Prepare the JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;&gt;</span> peopleList <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> personData <span style="color: #339933;">:</span> jsonData<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;</span> person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedHashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, personData.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;age&quot;</span>, personData.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Age&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; peopleList.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, List<span style="color: #339933;">&lt;</span>Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, String<span style="color: #339933;">&gt;&gt;&gt;</span> jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedHashMap<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;people&quot;</span>, peopleList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set response content type and write JSON content to the response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ObjectMapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">writeValueAsString</span><span style="color: #009900;">&#40;</span>jsonResponse<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>HttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<p><strong>Servlet Depdendencies:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>dependencies<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;!--</span> Apache POI <span style="color: #000000; font-weight: bold;">for</span> Excel processing <span style="color: #339933;">--&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">poi</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>poi<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.2.3<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">poi</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>poi<span style="color: #339933;">-</span>ooxml<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>5.2.3<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;!--</span> Jackson <span style="color: #000000; font-weight: bold;">for</span> JSON processing <span style="color: #339933;">--&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>com.<span style="color: #006633;">fasterxml</span>.<span style="color: #006633;">jackson</span>.<span style="color: #006633;">core</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>jackson<span style="color: #339933;">-</span>core<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>2.13.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>dependency<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>groupId<span style="color: #339933;">&gt;</span>com.<span style="color: #006633;">fasterxml</span>.<span style="color: #006633;">jackson</span>.<span style="color: #006633;">core</span><span style="color: #339933;">&lt;/</span>groupId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>artifactId<span style="color: #339933;">&gt;</span>jackson<span style="color: #339933;">-</span>databind<span style="color: #339933;">&lt;/</span>artifactId<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>version<span style="color: #339933;">&gt;</span>2.13.0<span style="color: #339933;">&lt;/</span>version<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>dependency<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>dependencies<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-convert-excel-to-json-simple-servlet-example">AEM Convert Excel to JSON Simple Servlet Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-convert-excel-to-json-simple-servlet-example/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>JCR SQL2 ISDESCENDANTNODE Multiple Paths Example</title>
		<link>https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example</link>
					<comments>https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 18 Oct 2023 02:01:56 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6203</guid>

					<description><![CDATA[<p>While working with the JCR SQL2 Queries, you may encounter scenarios where content is distributed across various branches of the repository. Querying these branches efficiently is key to implement features to capture requirements. In this article, we will focus o how to collect and query for distributed across various branches of the repository. Using the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example">JCR SQL2 ISDESCENDANTNODE Multiple Paths Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>While working with the JCR SQL2 Queries, you may encounter scenarios where content is distributed across various branches of the repository. Querying these branches efficiently is key to implement features to capture requirements. In this article, we will focus o how to collect and query for distributed across various branches of the repository. Using the ISDESCENDANTNODE feature of the JCR SQL Query proves it possible.</p>
<p><strong>Understanding ISDESCENDANTNODE</strong></p>
<p>ISDESCENDANTNODE is a condition in JCR SQL2 that checks if a node is a descendant of another, based on their paths. It is particularly beneficial for querying content hierarchies. Imagine you have content in different branches (content tree) of your AEM repository. You want to search across these branches simultaneously, without running separate queries. This is where ISDESCENDANTNODE shines.</p>
<p>By specifying multiple paths within the ISDESCENDANTNODE condition, you can search for content across these paths concurrently, streamlining your queries. This powerful feature simplifies and optimizes the content retrieval process in AEM.</p>
<hr class="spacer-large" />
<p><strong>Finding all cq:Pages under 1 path</strong><br />
with the jcr:content/jcr:title CONTAINS %terms%</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span><br />
WHERE ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/us/'</span><span style="color: #009900;">&#41;</span><br />
AND CONTAINS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>jcr<span style="color: #339933;">:</span>title<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'terms'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<p><strong>Finding all cq:Pages under 2 paths</strong><br />
with the jcr:content/jcr:title CONTAINS %terms%</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span><br />
WHERE <span style="color: #009900;">&#40;</span>ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/us/'</span><span style="color: #009900;">&#41;</span> <br />
OR ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/ca/'</span><span style="color: #009900;">&#41;</span> <br />
AND CONTAINS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>jcr<span style="color: #339933;">:</span>title<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'terms'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<p><strong>Finding all cq:Pages under 3 paths</strong><br />
with the jcr:content/jcr:title CONTAINS %terms%</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">SELECT <span style="color: #339933;">*</span> FROM <span style="color: #009900;">&#91;</span>cq<span style="color: #339933;">:</span>Page<span style="color: #009900;">&#93;</span><br />
WHERE <span style="color: #009900;">&#40;</span>ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/us/'</span><span style="color: #009900;">&#41;</span> <br />
OR ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/ca/'</span><span style="color: #009900;">&#41;</span> <br />
OR ISDESCENDANTNODE<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/content/sourcedcode/pl/'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
AND CONTAINS<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>jcr<span style="color: #339933;">:</span>content<span style="color: #339933;">/</span>jcr<span style="color: #339933;">:</span>title<span style="color: #009900;">&#93;</span>, <span style="color: #0000ff;">'terms'</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example">JCR SQL2 ISDESCENDANTNODE Multiple Paths Example</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/jcr-sql2-isdescendantnode-multiple-paths-example/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Retrieving the Current Page in an AEM Servlet via ResourceType</title>
		<link>https://sourcedcode.com/blog/aem/retrieving-the-current-page-in-an-aem-servlet-via-resourcetype</link>
					<comments>https://sourcedcode.com/blog/aem/retrieving-the-current-page-in-an-aem-servlet-via-resourcetype#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Wed, 06 Sep 2023 03:48:43 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quick Reference]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6104</guid>

					<description><![CDATA[<p>In the context of &#8220;Retrieving the Current Page in an AEM Servlet via ResourceType&#8221;, it is great to understand how this is done. This guide provides a simplified walkthrough on how you can adeptly achieve this. In the code example, we will associating the servlet with a resourceType. 123456789101112131415161718192021222324252627282930313233343536373839package com.example.aem.servlets; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/retrieving-the-current-page-in-an-aem-servlet-via-resourcetype">Retrieving the Current Page in an AEM Servlet via ResourceType</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the context of &#8220;Retrieving the Current Page in an AEM Servlet via ResourceType&#8221;, it is great to understand how this is done. This guide provides a simplified walkthrough on how you can adeptly achieve this. In the code example, we will associating the servlet with a resourceType.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.example.aem.servlets</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.injectorspecific.Self</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.wcm.api.Page</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.wcm.api.PageManager</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Optional</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
@SlingServletResourceTypes<span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; resourceTypes<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;example/components/content/mycomponent&quot;</span>,<br />
&nbsp; &nbsp; methods<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;GET&quot;</span>,<br />
&nbsp; &nbsp; extensions<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;html&quot;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ResourceTypeServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Self<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> SlingHttpServletRequest request<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Page currentPage <span style="color: #339933;">=</span> Optional.<span style="color: #006633;">ofNullable</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>PageManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>pm <span style="color: #339933;">-&gt;</span> pm.<span style="color: #006633;">getContainingPage</span><span style="color: #009900;">&#40;</span>request.<span style="color: #006633;">getResource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.<span style="color: #006633;">orElse</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>currentPage <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Gotcha! Current page is: &quot;</span> <span style="color: #339933;">+</span> currentPage.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Oops, couldn't find the current page.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger" />
<h2>Understanding the AEM Code Snippet</h2>
<h3>1. <span class="code-highlight-secondary">request.getResourceResolver().adaptTo(PageManager.class)</span>:</h3>
<p><span class="code-highlight-secondary">request.getResourceResolver()</span>: This fetches a <span class="code-highlight-secondary">ResourceResolver</span> from the servlet&#8217;s request. A <span class="code-highlight-secondary">ResourceResolver</span> in AEM assists in translating resource paths to actual resources.</p>
<p><span class="code-highlight-secondary">.adaptTo(PageManager.class)</span>: This method tries to adapt or convert the <span class="code-highlight-secondary">ResourceResolver</span> into another type, specifically a <span class="code-highlight-secondary">PageManager</span>. The <span class="code-highlight-secondary">PageManager</span> in AEM offers utilities for working with pages, such as fetching, creating, or deleting pages.</p>
<hr class="spacer-large" />
<h3>2. <span class="code-highlight-secondary">Optional.ofNullable(&#8230;)</span></h3>
<p>This wraps the value returned by the preceding code (which might be a <span class="code-highlight-secondary">PageManager</span> object or <span class="code-highlight-secondary">null</span> if the adaptation failed) within an <span class="code-highlight-secondary">Optional</span> object. <span class="code-highlight-secondary">Optional</span> is a container that may or may not contain a value.</p>
<hr class="spacer-large" />
<h3>3. <span class="code-highlight-secondary">.map(pm -> pm.getContainingPage(request.getResource()))</span></h3>
<p>If the <span class="code-highlight-secondary">Optional</span> contains a non-null <span class="code-highlight-secondary">PageManager</span> value, the <span class="code-highlight-secondary">.map</span> method applies the given function to it. Here, it uses the <span class="code-highlight-secondary">PageManager</span> (denoted as <span class="code-highlight-secondary">pm</span> in the lambda) to retrieve the containing page of the present resource.</p>
<p><span class="code-highlight-secondary">pm.getContainingPage(request.getResource())</span>: This extracts the AEM Page that encompasses the current resource.</p>
<hr class="spacer-large" />
<h3>4. <span class="code-highlight-secondary">.orElse(null)</span></h3>
<p>Lastly, if any prior steps don&#8217;t succeed (i.e., if the adaptation to <span class="code-highlight-secondary">PageManager</span> was unsuccessful or if no containing page for the resource exists), the <span class="code-highlight-secondary">orElse</span> method ensures <span class="code-highlight-secondary">currentPage</span> is set to <span class="code-highlight-secondary">null</span>.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/retrieving-the-current-page-in-an-aem-servlet-via-resourcetype">Retrieving the Current Page in an AEM Servlet via ResourceType</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/retrieving-the-current-page-in-an-aem-servlet-via-resourcetype/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AEM Sling Servlet: resourceTypes for POST &#038; GET Code Example Code</title>
		<link>https://sourcedcode.com/blog/aem/aem-sling-servlet-resource-type-for-post-get-code-example-code</link>
					<comments>https://sourcedcode.com/blog/aem/aem-sling-servlet-resource-type-for-post-get-code-example-code#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 28 Aug 2023 06:37:48 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=6020</guid>

					<description><![CDATA[<p>When working with Sling Servlets in AEM, the choice between using a resource type or a sling path as the basis for your servlet&#8217;s operation is crucial. This article delves into the benefits of utilizing resource types and provides practical examples of how resource types can streamline your development process for both POST and GET [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-sling-servlet-resource-type-for-post-get-code-example-code">AEM Sling Servlet: resourceTypes for POST &#038; GET Code Example Code</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>When working with Sling Servlets in AEM, the choice between using a resource type or a sling path as the basis for your servlet&#8217;s operation is crucial. This article delves into the benefits of utilizing resource types and provides practical examples of how resource types can streamline your development process for both POST and GET templates.</p>
<hr class="spacer-larger" />
<h2>Resource Type vs. Sling Path</h2>
<p>The decision between using a resource type and a sling path as the foundation of your servlet&#8217;s functionality can significantly impact your development approach. A resource type is a more flexible and future-proof choice due to its abstraction from specific URLs. Resource types abstract the underlying storage structure, enabling your servlets to work consistently even if the content hierarchy changes. On the other hand, relying on sling paths can lead to brittle implementations, as they are tied to specific URLs that might change over time.</p>
<hr class="spacer-larger" />
<h2>Advantages of Resource Types</h2>
<ol>
<li><strong>Abstraction: </strong>Resource types provide an abstraction layer, separating the servlet&#8217;s logic from the actual URL structure. This ensures that your servlet remains functional even when the content structure evolves.
</li>
<li><strong>Adaptability: </strong>With resource types, your servlet can adapt to different parts of the content hierarchy without requiring modifications. This adaptability becomes crucial in scenarios where content structures are reorganized or repurposed.</li>
<li><strong>Consistency: </strong>By using resource types, you promote consistency in your application. Different components or pages with the same resource type can share the same servlet logic, enhancing maintainability.
</li>
</ol>
<hr class="spacer-large" />
<h2>Caveats when using @SlingServletPaths</h2>
<ol>
<li>Path-bound servlets cannot be access-controlled using the default JCR repository ACLs.</i>
<li>Path-bound servlets can only be registered to a path and not a resource type (i.e. no suffix handling).</i>
<li>If a path-bound servlet is not active, e.g. if the bundle is missing or not started, a POST might result in unexpected results. usually creating a node at /bin/xyz which subsequently overlays the servlets path binding. The mapping is not transparent to a developer looking just at the repository.</i>
</ol>
<hr class="spacer-larger" />
<h3>POST Template using Resource Type</h3>
<p>Consider a scenario where you want to handle form submissions via a POST request for multiple types of components. By utilizing resource types, you can create a centralized servlet that handles these submissions consistently, regardless of the content&#8217;s location.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.resourceTypes=yourapp/components/form&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=POST&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FormSubmitServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doPost<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process form submission</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Send JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h3>GET Template using Resource Type</h3>
<p>Imagine you need to retrieve data from different types of resources, such as articles and videos, and display them using a single servlet. Utilizing resource types allows you to create a unified servlet to serve diverse content.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.resourceTypes=yourapp/components/content&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ContentDisplayServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> ServletException, <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Fetch and render content</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ...</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Send JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;success&quot;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger" />
<h2>Conclusion</h2>
<p>Resource types stand as a powerful tool in AEM&#8217;s arsenal, offering a versatile and robust approach to developing Sling Servlets for both POST and GET templates. By abstracting the underlying URL structure, resource types enable consistent servlet behavior across different content hierarchies. This approach enhances code maintainability, adaptability, and future-proofing. In contrast, sling paths can result in rigid implementations tied to specific URLs. By embracing resource types, developers can create more flexible, reliable, and adaptable solutions in Adobe Experience Manager.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-sling-servlet-resource-type-for-post-get-code-example-code">AEM Sling Servlet: resourceTypes for POST &#038; GET Code Example Code</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-sling-servlet-resource-type-for-post-get-code-example-code/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>AEM JCR SQL2 Full Text Search Case Insensitive</title>
		<link>https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive</link>
					<comments>https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 28 Aug 2023 06:06:51 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5999</guid>

					<description><![CDATA[<p>One of the key features of AEM is its support for searching and querying content stored in its Java Content Repository (JCR). In this article, we will explore how to perform a case-insensitive full-text search using the JCR_SQL2 query language in AEM and provide a practical example of achieving this through a servlet. Performing Case-Insensitive [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive">AEM JCR SQL2 Full Text Search Case Insensitive</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>One of the key features of AEM is its support for searching and querying content stored in its Java Content Repository (JCR). In this article, we will explore how to perform a case-insensitive full-text search using the JCR_SQL2 query language in AEM and provide a practical example of achieving this through a servlet.</p>
<hr class="spacer-large"/>
<h2>Performing Case-Insensitive Full Text Search</h2>
<p>A case-insensitive full-text search is a common requirement when you want to retrieve content regardless of the letter casing used in the search query. In AEM, the JCR_SQL2 query language is used to interact with the JCR repository. To perform a case-insensitive full-text search, you can utilize the<span class="code-highlight-secondary">LOWER()</span>function to convert both the search query and the content being searched to lowercase. This ensures that the search is not case-sensitive.</p>
<hr class="spacer-larger"/>
<h2>Example Query Code</h2>
<p>Consider a scenario where you want to search for assets that are descendants of a specific path and meet certain criteria, while also performing a case-insensitive search for specific keywords. Below is an example of a JCR_SQL2 query code block that achieves this:</p>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.RowIterator</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/mysearchservlet&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySearchServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Retrieve the search query parameter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryParameter <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;query&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate if the query parameter is not null or empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryParameter <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> queryParameter.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;Missing query parameter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Retrieve the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Session session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Define your query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM [cq:Page] AS page &quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/myproject')&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;AND (&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;LOWER(page.[jcr:content/jcr:title]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/cq:template]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/jcr:description]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;)&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;ORDER BY [jcr:score] DESC&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the QueryManager and create the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the results and build JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RowIterator rows <span style="color: #339933;">=</span> result.<span style="color: #006633;">getRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>rows.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> rows.<span style="color: #006633;">nextRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject pageData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:title&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/cq:template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;description&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:description&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>pageData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Respond with JSON search results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>resultsArray.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Close the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session.<span style="color: #006633;">logout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<hr class="spacer-large" />
<h2>Example Try Catch</h2>
<div class="mention-block">
When utilizing service users in Java, it&#8217;s essential to incorporate try-catch blocks for exception handling. Java introduced enhanced try-catch blocks, which allow you to catch multiple exceptions using a single catch block, in version 7. Before Java 7, try-catch blocks could only handle one exception type per catch block. With the improvements in Java 7, developers gained the ability to efficiently catch and manage various types of exceptions within a structured framework. These enhanced try-catch blocks serve as a powerful means of error management, preventing application crashes, facilitating user-friendly error messages, assisting in the debugging process, and bolstering overall code reliability.</p>
<p>Moreover, when working with service users, it&#8217;s important to remember that the JCR session should always be properly closed. Failing to close the session can lead to lingering threads and resource leaks. Incorporating try-with-resources or explicit session closure ensures that resources are released and system resources are utilized optimally. This practice contributes to maintaining the stability and efficiency of your application.
</p></div>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.resource.ResourceResolverFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Collections</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Map</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.methods=GET&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/mysearchservlet&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MySearchServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> ResourceResolverFactory resolverFactory<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setCharacterEncoding</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Retrieve the search query parameter</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryParameter <span style="color: #339933;">=</span> request.<span style="color: #006633;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;query&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Validate if the query parameter is not null or empty</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>queryParameter <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> queryParameter.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;Missing query parameter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&lt;</span><span style="color: #003399;">String</span>, Object<span style="color: #339933;">&gt;</span> authInfo <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">singletonMap</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResourceResolverFactory.<span style="color: #006633;">SUBSERVICE</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;sourcedCodeSystemUser&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span>ResourceResolver resourceResolver <span style="color: #339933;">=</span> resolverFactory.<span style="color: #006633;">getServiceResourceResolver</span><span style="color: #009900;">&#40;</span>authInfo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Define your query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT * FROM [cq:Page] AS page &quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/myproject')&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;AND (&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;LOWER(page.[jcr:content/jcr:title]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/cq:template]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp; &nbsp; &nbsp;OR LOWER(page.[jcr:content/jcr:description]) LIKE '%&quot;</span> <span style="color: #339933;">+</span> queryParameter <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;%'&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot; &nbsp; &nbsp;)&quot;</span> <span style="color: #339933;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;ORDER BY [jcr:score] DESC&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the QueryManager and create the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> resourceResolver.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>QueryManager.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Process the results and build JSON response</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RowIterator rows <span style="color: #339933;">=</span> result.<span style="color: #006633;">getRows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>rows.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> rows.<span style="color: #006633;">nextRow</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getNode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;page&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject pageData <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:title&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/cq:template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageData.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;description&quot;</span>, pageNode.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jcr:content/jcr:description&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>pageData<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Respond with JSON search results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>resultsArray.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject errorResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span>, <span style="color: #0000ff;">&quot;An error occurred while processing the request.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>errorResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>Curl Example:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl <span style="color: #0000ff;">&quot;http://localhost:4502/bin/mysearchservlet?query=my_search_keyword&quot;</span></div></td></tr></tbody></table></div>
<p><strong>Output:</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Page Title 1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;template&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;/conf/phonak/settings/wcm/templates/mytemplate&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;This is the description of Page 1&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Page Title 2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;template&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;/conf/phonak/settings/wcm/templates/anothertemplate&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Description for Page 2&quot;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#93;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large"/>
<p>In this example, the<span class="code-highlight-secondary">LOWER()</span>function is applied to both the properties being searched and the search query<span class="code-highlight-secondary">fulltext</span> This ensures that the search is case-insensitive, allowing you to retrieve relevant assets regardless of letter casing.</p>
<p>Practical Example: Implementing the Search in a Servlet</p>
<p>To practically implement the above query within a servlet in AEM, you would typically perform the following steps:</p>
<li>Create a Java servlet that extends<span class="code-highlight-secondary">SlingAllMethodsServlet</span>or implements<span class="code-highlight-secondary">SlingServlet</span></li>
<li>In the servlet&#8217;s<span class="code-highlight-secondary">doGet</span>or<span class="code-highlight-secondary">doPost</span>method, create an instance of the<span class="code-highlight-secondary">QueryManager</span>to execute the JCR_SQL2 query.</li>
<li>Use the query string from the example above, replacing placeholders like<span class="code-highlight-secondary">productCFsRootPath</span>and<span class="code-highlight-secondary">fulltext</span>with actual values.</li>
<li>Process the query result, extract the relevant data, and respond to the client with the search results.</li>
<hr class="spacer-large"/>
<h2>Conclusion</h2>
<p>Performing a case-insensitive full-text search using the JCR_SQL2 query language in Adobe Experience Manager allows you to provide a seamless search experience for users regardless of their choice of letter casing. By utilizing the<span class="code-highlight-secondary">LOWER()</span>function, you can effectively eliminate case sensitivity from your search queries and retrieve the most relevant content. Incorporating this technique into your AEM projects can enhance user experience and ensure that content discovery is efficient and user-friendly.</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive">AEM JCR SQL2 Full Text Search Case Insensitive</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-jcr-sql2-full-text-search-case-insensitive/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to access Design Dialogue Properties in Sling Model</title>
		<link>https://sourcedcode.com/blog/aem/how-to-access-design-dialogue-properties-in-sling-model</link>
					<comments>https://sourcedcode.com/blog/aem/how-to-access-design-dialogue-properties-in-sling-model#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 25 Aug 2023 21:37:11 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quick Reference]]></category>
		<category><![CDATA[Short Post]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=5962</guid>

					<description><![CDATA[<p>Design Dialogs are especially useful for creating components that need to be easily configurable through editable template and policies without directly editing the underlying code. They enable a clear separation between content and presentation, allowing content authors to make changes without needing technical expertise. Design Dialogs are defined using the AEM Touch UI Dialog, which [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-access-design-dialogue-properties-in-sling-model">How to access Design Dialogue Properties in Sling Model</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Design Dialogs are especially useful for creating components that need to be easily configurable through editable template and policies without directly editing the underlying code. They enable a clear separation between content and presentation, allowing content authors to make changes without needing technical expertise. Design Dialogs are defined using the AEM Touch UI Dialog, which is an integral part of the component development process.</p>
<p>In this article we will talk through how to access the design dialogue settings that is configured in editable template policies within Sling Models. This article will be a quick reference, which means we will provide you code examples for the solution.</p>
<hr class="spacer-larger"/>
<h2>1. Example _cq_design_dialog.xml configuration</h2>
<p>This is a snippet that resides in /apps/core/wcm/components/title/v1/title/_cq_design_dialog.xml</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #339933;">&lt;</span>advancedSettings<br />
&nbsp; &nbsp; jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; jcr<span style="color: #339933;">:</span>title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Advanced Settings&quot;</span><br />
&nbsp; &nbsp; sling<span style="color: #339933;">:</span>resourceType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;granite/ui/components/coral/foundation/container&quot;</span><br />
&nbsp; &nbsp; margin<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{Boolean}true&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;</span>items jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>renderLayoutAs<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sling<span style="color: #339933;">:</span>resourceType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;granite/ui/components/coral/foundation/form/select&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldDescription<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Forcing the component to render column layouts as&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fieldLabel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Render Layout As&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;./renderLayoutAs&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>items jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>oneColumn<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Default (One Column)&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{Boolean}true&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;oneColumn&quot;</span><span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>twoColumns<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Two Columns&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{Boolean}true&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;twoColumns&quot;</span><span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>threeColumns<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Three Columns&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;threeColumns&quot;</span><span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;</span>fourColumns<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jcr<span style="color: #339933;">:</span>primaryType<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;nt:unstructured&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Four Columns&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;fourColumns&quot;</span><span style="color: #339933;">/&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>items<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>renderLayoutAs<span style="color: #339933;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339933;">&lt;/</span>items<span style="color: #339933;">&gt;</span><br />
<span style="color: #339933;">&lt;/</span>advancedSettings<span style="color: #339933;">&gt;</span></div></td></tr></tbody></table></div>
<hr class="spacer-larger"/>
<h2>2. Accessing Design Dialog Properties in Sling Models:</h2>
<p>In AEM, Sling Models provide a way to map resource properties to Java objects, making it easier to work with the content of a page. To access Design Dialog properties within a Sling Model, take a look at the example code below.</p>
<p>Take notice, in line:17, we are using <a href="/blog/aem/aem-sling-model-injectors-annotations-cheat-sheet-reference-guide" rel="noopener" target="_blank">Sling Model Specific Annotations, @ScriptVariable</a>, to initiate the currentStyle object. In line:22, we are calling the get method() which is a public method, which here we will attempt to find configuration set via design dialog &#8220;renderLayoutAs&#8221;. If &#8220;renderLayoutAs&#8221; is not configured, then String renderLayoutAs == null. Finally in line:24, we will use a switch statement to execute code based on what is configured.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.internal.components</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.wcm.api.designer.Style</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.models.annotations.Model</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.PostConstruct</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.inject.Inject</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">commons</span>.<span style="color: #006633;">lang3</span>.<span style="color: #006633;">StringUtils</span>.<span style="color: #006633;">isNotEmpty</span><span style="color: #339933;">;</span><br />
<br />
@Model<span style="color: #009900;">&#40;</span>adaptables <span style="color: #339933;">=</span> SlingHttpServletRequest.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LayoutColumn <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> RENDER_LAYOUT_AS_PROP <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;renderLayoutAs&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @ScriptVariable<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Style</span> currentStyle<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @PostConstruct<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> renderLayoutAs <span style="color: #339933;">=</span> currentStyle.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>RENDER_LAYOUT_AS_PROP, <span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isNotEmpty<span style="color: #009900;">&#40;</span>renderLayoutAs<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>renderLayoutAs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;oneColumn&quot;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;twoColumns&quot;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;threeColumns&quot;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">case</span> <span style="color: #0000ff;">&quot;fourColumns&quot;</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Default case&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/how-to-access-design-dialogue-properties-in-sling-model">How to access Design Dialogue Properties in Sling Model</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/how-to-access-design-dialogue-properties-in-sling-model/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Send Out Emails with AEM Servlet</title>
		<link>https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet</link>
					<comments>https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet#respond</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Fri, 21 Jan 2022 03:31:31 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[OSGI]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<guid isPermaLink="false">https://sourcedcode.com/?p=3339</guid>

					<description><![CDATA[<p>In this article, we will be setting up the local AEM author developer&#8217;s environment to send out an email utilizing the Day CQ Mail Service. This article includes code snippets to a Sling Servlet that will be used to send out the email. We will be using a Google Gmail account as the SMTP Mail [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet">Send Out Emails with AEM Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this article, we will be setting up the local AEM author developer&#8217;s environment to send out an email utilizing the Day CQ Mail Service. This article includes code snippets to a Sling Servlet that will be used to send out the email.</p>
<div class="mention-block">
We will be using a Google Gmail account as the SMTP Mail Server in this article. To learn how to set up your Google Gmail account as the SMTP server, <a href="https://sourcedcode.com/blog/general-web/how-to-use-the-gmail-smtp-server-to-send-emails-for-free" rel="noopener" target="_blank">click here</a>.
</div>
<hr class="spacer-large" />
<h2>1. Configure the Day CQ Mail Service</h2>
<p>a. Visit <a href="http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService" rel="noopener" target="_blank">http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService</a></p>
<p><a class="foobox fbx-instance fbx-link" href="https://sourcedcode.com/wp-content/uploads/2022/01/step-1-send-email-aem.png"><img decoding="async" width="300" src="https://sourcedcode.com/wp-content/uploads/2022/01/step-1-send-email-aem.png" alt="This is a screenshot of the Apache Felix Console, Day CQ Mail Service Configuration"/></a></p>
<p>b. Include the configurations below:<br />
<strong>Google SMTP Configurations</strong><br />
SMTP server hostname: smtp.gmail.com<br />
SMTP server port: 465, 25 or 587 (test in that order)<br />
SMTP user: @@YOUR_EMAIL_ADDRESS@@<br />
SMTP from emai: @@YOUR_EMAIL_ADDRESS@@<br />
SMTP password: @@YOUR_PASSWORD@@<br />
SMTP TLS/SSL: true</p>
<p>To automate this configuration, you can run a Curl command like the example below. But please be aware, you must use your own Google Gmail account. </p>
<p>To update the Curl command below, use this tool here, <a class="code-highlight-primary" href="https://sourcedcode.com/blog/aem/tool-generate-curl-command-to-update-osgi-configurations?aemUrl=http://localhost:4502&#038;username=admin&#038;password=admin&#038;pid=com.day.cq.mailer.DefaultMailService&#038;osgiKeys=smtp.host,smtp.port,smtp.user,smtp.password,from.address,smtp.ssl&#038;osgiValues=smtp.gmail.com,587,contact@sourcedcode.com,p4ssw0rd,contact@sourcedcode.com,true#curltool" target="_blank" rel="noopener">Tool: Generate Curl Command to Update OSGI Configurations</a></p>
<pre style="white-space: pre-wrap; font-size: 12px; padding: 5px;">
curl -u admin:admin 'http://localhost:4502/system/console/configMgr/com.day.cq.mailer.DefaultMailService' --data-raw 'apply=true&action=ajaxConfigManager&%24location=&smtp.host=smtp.gmail.com&smtp.port=587&smtp.user=contact@sourcedcode.com&smtp.password=p4ssw0rd&from.address=contact@sourcedcode.com&smtp.ssl=true&propertylist=smtp.host%2Csmtp.port%2Csmtp.user%2Csmtp.password%2Cfrom.address%2Csmtp.ssl'
</pre >
<hr class="spacer-large" />
<h2>2. Create the SendEmailServletExample AEM Servlet</h2>
<p>Create a Java class named, SendEmailServletExample.java, copy and paste the code below, replace line:44 with your own email, and then use your Java Maven skills to build the servlet in AEM.</p>
<p>Once when the SendEmailServletExample.java is built into AEM, visit <a class="code-highlight-primary" href="http://localhost:4502/bin/sendemail" rel="noopener" target="_blank">http://localhost:4502/bin/sendemail</a>,</p>
<div class="code-1000">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourcedcode.core.servlet</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.mailer.MessageGateway</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.day.cq.mailer.MessageGatewayService</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.Email</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.EmailException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.mail.HtmlEmail</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingAllMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONObject</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Reference</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.Logger</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.LoggerFactory</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.Servlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">apache</span>.<span style="color: #006633;">sling</span>.<span style="color: #006633;">api</span>.<span style="color: #006633;">servlets</span>.<span style="color: #006633;">ServletResolverConstants</span>.<span style="color: #006633;">SLING_SERVLET_PATHS</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span><br />
&nbsp; service <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> Servlet.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #009900;">&#125;</span>,<br />
&nbsp; property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; SLING_SERVLET_PATHS <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=/bin/sendemail&quot;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SendEmailServletExample <span style="color: #000000; font-weight: bold;">extends</span> SlingAllMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Logger log <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>SendEmailServletExample.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; @<span style="color: #003399;">Reference</span><br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> MessageGatewayService messageGatewayService<span style="color: #339933;">;</span><br />
<br />
&nbsp; @Override<br />
&nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; JSONObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JSONObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">boolean</span> sent <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> recipients <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;contact@sourcedcode.com&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; sendEmail<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This is an test email&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;This is the email body&quot;</span>, recipients<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; sent <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>EmailException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, sent <span style="color: #339933;">?</span> <span style="color: #0000ff;">&quot;done&quot;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;something went wrong&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JSONException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> sendEmail<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> subjectLine, <span style="color: #003399;">String</span> msgBody, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> recipients<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> EmailException <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Email email <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HtmlEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient <span style="color: #339933;">:</span> recipients<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; email.<span style="color: #006633;">addTo</span><span style="color: #009900;">&#40;</span>recipient, recipient<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; email.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span>subjectLine<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; email.<span style="color: #006633;">setMsg</span><span style="color: #009900;">&#40;</span>msgBody<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; MessageGateway<span style="color: #339933;">&lt;</span>Email<span style="color: #339933;">&gt;</span> messageGateway <span style="color: #339933;">=</span> messageGatewayService.<span style="color: #006633;">getGateway</span><span style="color: #009900;">&#40;</span>HtmlEmail.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>messageGateway <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sending out email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; messageGateway.<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>Email<span style="color: #009900;">&#41;</span> email<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; log.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The message gateway could not be retrieved.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet">Send Out Emails with AEM Servlet</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/send-out-emails-with-aem-servlet/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to use AEM JCR SQL2 query strings to query for nodes in Java Content Repository</title>
		<link>https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet</link>
					<comments>https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet#comments</comments>
		
		<dc:creator><![CDATA[briankasingli]]></dc:creator>
		<pubDate>Mon, 02 Apr 2018 19:31:06 +0000</pubDate>
				<category><![CDATA[AEM]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Quick Reference]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sling Servlet]]></category>
		<category><![CDATA[Video Tutorial]]></category>
		<guid isPermaLink="false">http://sourcedcode.com/?p=82</guid>

					<description><![CDATA[<p>JCR-SQL2 (Java Content Repository &#8211; Structured Query Language 2) is domain specific language used to query JCR nodes held in the JCR repository. The syntax for JCR-SQL2 is fairly similar to SQL, so if you have some experience with SQL, this tutorial will be a walk in the park for you. The best way to [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet">How to use AEM JCR SQL2 query strings to query for nodes in Java Content Repository</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>JCR-SQL2 (Java Content Repository &#8211; Structured Query Language 2) is domain specific language used to query JCR nodes held in the JCR repository. The syntax for JCR-SQL2 is fairly similar to SQL, so if you have some experience with SQL, this tutorial will be a walk in the park for you.</p>
<p>The best way to learn the power of the JCR-SQL2 language is to try it out on CRX/DE Lite. Without future or do, below will be a lesson on JCR-SQL2. To locate CRX/DE Lite&#8217;s query tool, visit <a href="http://localhost:4502/crx/de/index.jsp" rel="noopener noreferrer" target="_blank">http://localhost:4502/crx/de/index.jsp</a>, then select on the tools drop down, and choose query.</p>
<div class="mention-block--gold">
<h3>2023 Added Section: SQL-JCR2 Snippets</h3>
<p>Examples can be found at the bottom of this tutorial, <a href="#jcr-sql2-code-snippets-cheat-sheet">JCR-SQL2 Code Snippets Cheat Sheet</a>, where you can find some examples for real life use cases. Head into your <a href="http://localhost:4502/crx/de/index.jsp" rel="noopener" target="_blank">crx-de > tools > query > type:JCR SQL2</a>, and try it our yourself.
</div>
<p><img fetchpriority="high" decoding="async" src="http://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-1024x662.png" alt="" width="1024" height="662" class="alignnone size-large wp-image-167" srcset="https://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-1024x662.png 1024w, https://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-300x194.png 300w, https://sourcedcode.com/wp-content/uploads/2018/04/tools-query-tool-768x496.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></p>
<hr />
<h2>JCR-SQL2 Syntax</h2>
<ol>
<li><a href="#select-statement">SELECT Statement</a></li>
<li><a href="#name-statement">NAME() Statement</a></li>
<li><a href="#isdescendantnode-statement">ISDESCENDANTNODE Statement</a></li>
<li><a href="#contains-operator">CONTAINS Statement</a></li>
<li><a href="#like-operator">LIKE Operator</a></li>
<li><a href="#is-not-null">IS NOT NULL Property</a></li>
<li><a href="#order-by-keyword">ORDER BY Keyword</a></li>
<li><a href="#cast-statement">CAST() Statement</a></li>
<li><a href="#jcr-sql2-code-snippets-cheat-sheet">JCR-SQL2 Code Snippets Cheat Sheet</a> (new 2023)</li>
<li><a href="#jcr-sql2-java-code-example-servlet">JCR SQL2 Java Code Example Servlet</a> (new 2023)</li>
</ol>
<p><iframe width="560" height="315" src="https://www.youtube.com/embed/0sMMzJs1dpc" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<hr class="spacer-large" />
<h4 id="select-statement">1. The JCR-SQL2 SELECT Statement</h4>
<ul>
<li>The SELECT statement is used to select all JCR nodes that matches the JCR node&#8217;s primary type.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- nt:base is a primary type that represents every single node in the JCR.</span><br />
<span style="color: #808080; font-style: italic;">-- returns all nodes in the JCR.</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [dam:Asset] nodes</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>dam:Asset<span style="color: #FF00FF;">&#93;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="8658919803"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h4 id="name-statement">2. The JCR-SQL2 NAME() Statement</h4>
<ul>
<li>Selecting nodes with a specific node name.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes WHERE the node's name is equal to &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> NAME<span style="color: #FF00FF;">&#40;</span>nodes<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [dam:Asset] nodes WHERE the node's name is equal to &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>dam:Asset<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> NAME<span style="color: #FF00FF;">&#40;</span>nodes<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="isdescendantnode-statement">3. The JCR-SQL2 ISDESCENDANTNODE Statement</h4>
<ul>
<li>Selecting nodes under a file path.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under &quot;/content/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">select</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes WHERE nodes exist under &quot;/content/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [dam:Asset] nodes WHERE nodes exist under &quot;/content/dam/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>dam:Asset<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>dam<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [nt:unstructured] nodes WHERE nodes exist under &quot;/content/we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="contains-statement">4. The JCR-SQL2 CONTAINS Statement</h4>
<ul>
<li>Selecting nodes where the properties contain a value.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE node's property jcr:title CONTAINS &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span>nodes.title<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:Page] nodes where node's property jcr:title CONTAINS &quot;we-retail&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span>nodes.title<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #2 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2263738126"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h4 id="like-operator">5. The JCR-SQL2 LIKE Operator</h4>
<ul>
<li>The LIKE operator is used to search for a specified pattern in node properties.</li>
<li>There are two wildcards used in conjunction with the LIKE operator: % and _</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title starts with any values and ends with &quot;w&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">%</span>w&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title starts with &quot;w&quot; and ends with any values</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;w<span style="color: #008080; font-weight: bold;">%</span>&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/contentl&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title matching &quot;w&quot; in any position</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">%</span>w<span style="color: #008080; font-weight: bold;">%</span>&quot;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot; </span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title start with any values and have &quot;w&quot; in the second position and ends with any values</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">LIKE</span> <span style="color: #008000;">&quot;<span style="color: #008080; font-weight: bold;">_</span>w<span style="color: #008080; font-weight: bold;">%</span>&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="is-not-null">6. The JCR-SQL2 IS NOT NULL Property</h4>
<ul>
<li>Used for validation of property&#8217;s value is not null.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under &quot;/content&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title IS NOT NULL</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under &quot;/content&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's property jcr:title IS NOT NULL</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099; font-weight: bold;">IS NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #3 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="3385248102"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<hr class="spacer-large" />
<h4 id="order-by-keyword">7. The JCR-SQL2 ORDER BY Keyword</h4>
<ul>
<li>The ORDER BY keyword is used to sort the result-set in ascending or descending order.</li>
<li>Returned values are sorted by default in ascending order.</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- ORDER BY jcr:created ascending</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #990099; font-weight: bold;">ORDER BY</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- ORDER BY node's property jcr:created descending</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:base<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">ORDER BY</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">DESC</span></div></td></tr></tbody></table></div>
<hr class="spacer-large" />
<h4 id="cast-statement">8. The JCR-SQL2 CAST() Statement</h4>
<ul>
<li>Convert an expression from one data type to another.</li>
<li>Cast Types:
<ul>
<li>STRING</li>
<li>BINARY</li>
<li>DATE</li>
<li>LONG</li>
<li>DOUBLE</li>
<li>DECIMAL</li>
<li>BOOLEAN</li>
<li>NAME</li>
<li>PATH</li>
<li>REFERENCE</li>
<li>WEAKREFERENCE</li>
<li>URI</li>
</ul>
</li>
<li><b>Returns in CRX/DE Lite</b>: A list of JCR nodes in the results table with their corresponding paths.</li>
<li><b>Returns in OSGI Bundle (Sling API)</b>: All the found nodes stored in the Iterator&lt;Resource&gt; object.</li>
<li><b>Returns in OSGI Bundle (JCR API)</b>: The QueryResult object where you can call getNodes() method to get the NodeIterator object.</li>
</ul>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's navRoot equals to &quot;true&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">as</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>navRoot<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;true&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">BOOLEAN</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's creation date is greater than &quot;April 1st, 2018&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&gt;</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;2018-04-01T00:00:00.000Z&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">-- returns all [cq:PageContent] nodes WHERE nodes exist under the &quot;/content/we-retail&quot;</span><br />
<span style="color: #808080; font-style: italic;">-- AND node's maxRating is less than &quot;5.0&quot;</span><br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">as</span> nodes <span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span><span style="color: #CC0099;">/</span>content<span style="color: #CC0099;">/</span>we<span style="color: #CC0099;">-</span>retail<span style="color: #FF00FF;">&#93;</span><span style="color: #FF00FF;">&#41;</span> <br />
<span style="color: #CC0099; font-weight: bold;">AND</span> nodes.<span style="color: #FF00FF;">&#91;</span>maxRating<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&lt;</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;5.0&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DECIMAL</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<div class="mention-block">
<h3>Final Notes: When is the perfect time to use JCR-SQL2?</h3>
<ol>
<li>Build a images and documents search component, if your AEM site that allows consumers to search for images or documents.</li>
<li>Build a blog post search component, If your AEM site has a blog section.</li>
<li>Build a secure resources query API, If you are allowing 3rd party applications to query and find nodes in your AEM site.</li>
</ol>
<p>There&#8217;s many other things to utilize JCR-SQL2 to query nodes, but those are just some quick examples.</p>
</div>
<hr class="spacer-larger" />
<h4 id="jcr-sql2-code-snippets-cheat-sheet">9. JCR-SQL2 Code Snippets Cheat Sheet</h4>
<p>Use these code snippets below to rapidly build your AEM JCR SQL2 search queries, I will try my best to give you some really great examples:</p>
<h5>a. Find all [nt:unstructured] nodes, under the path &#8220;/content/we-retail&#8221;, where the node name = &#8220;root&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> NAME<span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;root&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>b. Find all components ([nt:unstructured] nodes), under the path &#8220;/content/we-retail&#8221;, where component resourceType = &#8220;weretail/components/content/image&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>sling:resourceType<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;weretail/components/content/image&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>b. Find all components ([nt:unstructured] nodes), under the path &#8220;/content/we-retail&#8221;, where component resourceType = &#8220;weretail/components/content/image&#8221; AND the fileReference property contains with &#8220;/content/dam/we-retail/en/experiences&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>sling:resourceType<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;weretail/components/content/image&quot;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span>fileReference<span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/dam/we-retail/en/experiences&quot;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>c. Find all [nt:unstructured] nodes, under the path &#8220;/content/we-retail&#8221;,  where the property(&#8220;jcr:title&#8221;) contains &#8220;Featured products&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>nt:unstructured<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #00CC00;">CONTAINS</span><span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#91;</span>jcr:title<span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> <span style="color: #008000;">&quot;Featured products&quot;</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>d. Find all pages (cq:Page nodes), under the path &#8220;/content/we-retail&#8221;,  where the cq:lastModified date is before or equals to &#8220;January 1st, 2000&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> page.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> page<br />
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> jcrContentNode <span style="color: #990099; font-weight: bold;">ON</span> ISCHILDNODE<span style="color: #FF00FF;">&#40;</span>jcrContentNode<span style="color: #000033;">,</span> page<span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>page<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> jcrContentNode.<span style="color: #FF00FF;">&#91;</span>cq:lastModified<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&lt;=</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;2023-01-01T00:00:00.000+00:00&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>e. Find all pages (cq:Page nodes), under the path &#8220;/content/we-retail&#8221;,  where the jcr:created date is before to &#8220;July 1st, 2022&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> page.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> page<br />
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> jcrContentNode <span style="color: #990099; font-weight: bold;">ON</span> ISCHILDNODE<span style="color: #FF00FF;">&#40;</span>jcrContentNode<span style="color: #000033;">,</span> page<span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>page<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> jcrContentNode.<span style="color: #FF00FF;">&#91;</span>jcr:created<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">&lt;</span> CAST<span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">&quot;2022-07-01T00:00:00.000+00:00&quot;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #999900; font-weight: bold;">DATE</span><span style="color: #FF00FF;">&#41;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>f. Find all [cq:PageContent] nodes, under the path &#8220;/content/we-retail&#8221;,  where the cq:template equals to &#8220;/conf/we-retail/settings/wcm/templates/hero-page&#8221;</h5>
<p>This AEM JCR SQ2 Query is an example for how to find all nodes with a specific template, results returned as cq:PageContent node.</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:template<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;/conf/we-retail/settings/wcm/templates/hero-page&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>g. Find all pages (cq:Page nodes), under the path &#8220;/content/we-retail&#8221;,  where the cq:template equals to &#8220;/conf/we-retail/settings/wcm/templates/hero-page&#8221;</h5>
<p>This AEM JCR SQ2 Query is an example for how to find all nodes with a specific template, results returned as cq:Page node.</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #990099; font-weight: bold;">SELECT</span> page.<span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:Page<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> page<br />
<span style="color: #990099; font-weight: bold;">INNER</span> <span style="color: #990099; font-weight: bold;">JOIN</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> jcrContentNode <span style="color: #990099; font-weight: bold;">ON</span> ISCHILDNODE<span style="color: #FF00FF;">&#40;</span>jcrContentNode<span style="color: #000033;">,</span> page<span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>page<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> jcrContentNode.<span style="color: #FF00FF;">&#91;</span>cq:template<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;/conf/we-retail/settings/wcm/templates/hero-page&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>h. Find all [cq:PageContent] nodes, under the path &#8220;/content/we-retail&#8221;,  where the cq:tags[] contains &#8220;we-retail:gender/men&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">This AEM JCR SQ2 Query <span style="color: #CC0099; font-weight: bold;">is</span> an example for how <span style="color: #990099; font-weight: bold;">to</span> find <span style="color: #990099; font-weight: bold;">all</span> nodes <span style="color: #990099; font-weight: bold;">with</span> a single cq:tags<span style="color: #FF00FF;">&#91;</span><span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> results returned <span style="color: #990099; font-weight: bold;">as</span> cq:PageContent node.<br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:tags<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail:gender/men&quot;</span></div></td></tr></tbody></table></div>
<hr class="spacer-medium"/>
<h5>i. Find all [cq:PageContent] nodes, under the path &#8220;/content/we-retail&#8221;,  where the cq:tags[] contains &#8220;we-retail:gender/men&#8221; AND cq:tags[] contains &#8220;we-retail:activity/surfing&#8221;</h5>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">This AEM JCR SQ2 Query <span style="color: #CC0099; font-weight: bold;">is</span> an example for how <span style="color: #990099; font-weight: bold;">to</span> find <span style="color: #990099; font-weight: bold;">all</span> nodes <span style="color: #990099; font-weight: bold;">with</span> multiple cq:tags<span style="color: #FF00FF;">&#91;</span><span style="color: #FF00FF;">&#93;</span><span style="color: #000033;">,</span> results returned <span style="color: #990099; font-weight: bold;">as</span> cq:PageContent node.<br />
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #FF00FF;">&#91;</span>cq:PageContent<span style="color: #FF00FF;">&#93;</span> <span style="color: #990099; font-weight: bold;">AS</span> node<br />
<span style="color: #990099; font-weight: bold;">WHERE</span> ISDESCENDANTNODE<span style="color: #FF00FF;">&#40;</span>node<span style="color: #000033;">,</span> <span style="color: #008000;">&quot;/content/we-retail&quot;</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:tags<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail:gender/men&quot;</span><br />
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#91;</span>cq:tags<span style="color: #FF00FF;">&#93;</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">&quot;we-retail:activity/surfing&quot;</span></div></td></tr></tbody></table></div>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #4 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="2079905296"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<!-- Horizontal Preview Ad #5 --><br />
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9053803095882933"
     data-ad-slot="5183553115"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<style>
h5 {
    font-size: 16px;
    font-weight: 100;
}
</style>
<hr class="spacer-larger" />
<h4 id="jcr-sql2-java-code-example-servlet">9. JCR-SQL2 Java Code Example Servlet (2023)</h4>
<p>This is an example of how an AEM servlet would actually utilize the JCR-SQL2 Query with the examples above. It will output a JSON array of the page paths.</p>
<div class="code-1200">
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:400px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.PrintWriter</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.NodeIterator</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.RepositoryException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.Session</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.Query</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.jcr.query.QueryResult</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletRequest</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.SlingHttpServletResponse</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.sling.api.servlets.SlingSafeMethodsServlet</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.osgi.service.component.annotations.Component</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonArray</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.gson.JsonObject</span><span style="color: #339933;">;</span><br />
<br />
@<span style="color: #003399;">Component</span><span style="color: #009900;">&#40;</span>service <span style="color: #339933;">=</span> javax.<span style="color: #006633;">servlet</span>.<span style="color: #006633;">Servlet</span>.<span style="color: #000000; font-weight: bold;">class</span>, property <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;sling.servlet.paths=/bin/getpagedbypath&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GetPagedByPathServlet <span style="color: #000000; font-weight: bold;">extends</span> SlingSafeMethodsServlet <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> doGet<span style="color: #009900;">&#40;</span>SlingHttpServletRequest request, SlingHttpServletResponse response<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the JCR session</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getResourceResolver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">adaptTo</span><span style="color: #009900;">&#40;</span>Session.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Query statement</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> queryString <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT page.* FROM [cq:Page] AS page &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;INNER JOIN [cq:PageContent] AS jcrContentNode ON ISCHILDNODE(jcrContentNode, page) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;WHERE ISDESCENDANTNODE(page, '/content/we-retail') &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;AND jcrContentNode.[cq:lastModified] &lt;= CAST('2023-01-01T00:00:00.000+00:00' AS DATE)&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create the query object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryManager queryManager <span style="color: #339933;">=</span> session.<span style="color: #006633;">getWorkspace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getQueryManager</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Query query <span style="color: #339933;">=</span> queryManager.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>queryString, Query.<span style="color: #006633;">JCR_SQL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Execute the query</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QueryResult result <span style="color: #339933;">=</span> query.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Get the nodes from the query result</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeIterator nodeIterator <span style="color: #339933;">=</span> result.<span style="color: #006633;">getNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Collect the paths of the first ten pages in a list</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> pagePaths <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>nodeIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node pageNode <span style="color: #339933;">=</span> nodeIterator.<span style="color: #006633;">nextNode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> pagePath <span style="color: #339933;">=</span> pageNode.<span style="color: #006633;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pagePaths.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>pagePath<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Create a JSON object to hold the results</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonObject jsonResponse <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JsonArray resultsArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JsonArray<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the paths to the JSON array</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> path <span style="color: #339933;">:</span> pagePaths<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; resultsArray.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Add the JSON array to the response object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonResponse.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;results&quot;</span>, resultsArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Set the response content type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;application/json&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Write the JSON response to the output</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">PrintWriter</span> writer <span style="color: #339933;">=</span> response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">print</span><span style="color: #009900;">&#40;</span>jsonResponse.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>RepositoryException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Handle exception</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">setStatus</span><span style="color: #009900;">&#40;</span>SlingHttpServletResponse.<span style="color: #006633;">SC_INTERNAL_SERVER_ERROR</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.<span style="color: #006633;">getWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error executing query: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
</div>
<p><strong>JSON response</strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page1&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page2&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page3&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page4&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page5&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page6&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page7&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page8&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page9&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;/content/we-retail/page10&quot;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><strong>Curl call</strong></p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap;">curl -X GET http://localhost:4502/bin/getpagedbypath</div></td></tr></tbody></table></div>
<p>The post <a rel="nofollow" href="https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet">How to use AEM JCR SQL2 query strings to query for nodes in Java Content Repository</a> appeared first on <a rel="nofollow" href="https://sourcedcode.com">Sourced Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sourcedcode.com/blog/aem/aem-jcr-sql2-tutorial-and-examples-and-cheatsheet/feed</wfw:commentRss>
			<slash:comments>26</slash:comments>
		
		
			</item>
	</channel>
</rss>
