Thursday 2 August 2018

Basic Sling Model Exporter

/*
Open a page which is having component /apps/project/components/page/page
/content/project/en/jcr:content.model.json
Don't miss jcr:content in the path. Add jcr:content.model.json
*/
package com.org.project.models;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Required;
import org.apache.sling.models.annotations.Source;
import org.apache.sling.models.annotations.injectorspecific.Self;
import com.day.cq.wcm.api.Page;
@Model(adaptables = Resource.class, resourceType = "/apps/project/components/page/page")
@Exporter(name = "jackson", extensions = "json")
public class PageModelExporter {
@Self
private Resource resource;
@Inject @Named("jcr:title") @Required
private String title;
@PostConstruct
private void init() {
}
public String getTitle() {
return title;
}
}

Basic Sling Model Example

/*
<div data-sly-use.object="com.org.project.models.PageModel"/>
${object.title}
</div>
*/
package com.org.project.models;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Required;
import org.apache.sling.models.annotations.injectorspecific.Self;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
@Model(
adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class PageModel {
@Self
private Resource resource;
@Inject @Named("jcr:title") @Required
private String title;
@PostConstruct
private void init() {
}
public String getTitle() {
return title;
}
}

Sunday 10 June 2018

Sling Servlet POST request throwing 403 Forbidden Error

Namastey,

If you are working on your local machine and have created a sling servlet to make post request to local AEM Server , you may be getting 403 Error Code : Forbidden

This issue is due to CSRF Filter blocking POST Requests.

You can unblock this via OSGi Configuration. You shouldn't be doing it on production instances.


  1. Go to http://localhost:4502/system/console/configMgr
  2. Find Adobe Granite CSRF Filter 
  3. Remove POST from Filter Methods
  4. Save and Test the servlet again.









Now let us try to understand 

  • Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated.
  • Adobe also provides a new CSRF Protection Framework to protect against this type of attack.
  • The framework makes use of tokens to guarantee that the client request is legitimate. The tokens are generated when the form is sent to the client and validated when the form is sent back to the server.


Refer following for more details 
https://helpx.adobe.com/experience-manager/6-4/forms/using/admin-help/preventing-csrf-attacks.html


Dhanywaad

Thursday 17 May 2018

Remote Debugging in AEM 6.4

Namastey,

There are situations when you want to do debugging of OSGI java code while it is running on your AEM.

There are two parts to it : 

  1. AEM server should be enabled to accept remote debugging connection
  2. Eclipse IDE / Intellij should be configured to make remote debugging connection to AEM
Start AEM JAR with following command


java -Xmx2g -agentlib:jdwp=transport=dt_socket,address=5402,server=y,suspend=n -jar cq-author-p4502.jar 


-Xrunjdwp loads the JPDA reference implementation of JDWP
  • transport-dt_socket is the name of the transport to use in connecting to debugger
  • server=y  > means listen for a debugger application to attach
  • address=5402  > means listen for a connection at this address (no host = this port on add interfaces)
  • suspend=n  > means do not wait for a debugger to attach

Add Debug Configuration in Eclipse
  • click on debug configuration 
  • Select Remote Java Application
  • Add Configuration 
  • In the new configuration, select project and provide  aem host and aem port

Now setup some break points in java code ( except JSP)
Run your program and eclipse should ask you to enter debug mode.



Dhanywad

Wednesday 7 February 2018

HTL Basic Examples

Global Objects
  • page 
  • currentPage
  • properties
  • inheritedPageProperties
  • currentDesign
  • currentSession
  • wcmmode
Refer for list of global objects


Converting JSP Method into HTL

  • JSPcurrentPage.getContentResource()).getResourceType()
Remove() from JSP methods

  • HTL 1 :  ${currentPage.getContentResource.getResourceType}
OR Simply Remove get from method name
  • HTL2${currentPage.contentResource.resourceType}


HTL Examples

Get title of page using HTL

  • ${page.title}
Get any custom property from page using HTL
  • ${page.getProperties['root/responsivegrid/content/customPropertyName']
Handling Property Array using HTL
<div data-sly-list = "${currentPage.getProperties['component/reference']}"> 
${item}
</div>

( reference is property name , component is component node under jcr:content)


Get date using HTL
  • ${ 'dd-MMMM-yyyy hh:mm:ss' @
           format=currentPage.lastModified,

           timezone='IST',

           locale='en'}



Get logged in user using HTL

  •  ${currentSession.userID} 
Get design path using HTL
  • ${currentDesign.path}
Check wcmmode using HTL
  • ${wcmmode.edit}







Wednesday 29 November 2017

Setting up Dispatcher on Mac OS X

Namastey, 

Setting up dispatcher is one of the most common thing we do in AEM projects. Sometimes developers face issues in configuring it on Mac OS X. 

You can follow below steps and get basic dispatcher setup done. 

Step 1: Install Apache Server on your machine.Verify the version by running.

sudo httpd -version

Step 2: Download Dispatcher Module and sample files as per OS and apache version

Step 3: Extract the downloaded dispatcher module file and copy file "dispatcher-apache<x.y>-<rel-nr>.so" to directory /usr/libexec/apache2/
You can rename the copied file to mod_dispatcher.so 

Step 4: Open httpd.conf from /private/etc/apache2/

Step 5: Add following configurations after last load module. ( it should be around line 170 in default in httpd.conf).

LoadModule dispatcher_module libexec/apache2/mod_dispatcher.so

<IfModule disp_apache2.c>
  DispatcherConfig /etc/apache2/conf/dispatcher.any
  DispatcherLog    /etc/apache2/logs/dispatcher.log
  DispatcherLogLevel 3
  DispatcherDeclineRoot 0
  DispatcherUseProcessedURL 0
  DispatcherPassError 0
  DispatcherKeepAliveTimeout 60
</IfModule>

<Directory />
  <IfModule disp_apache2.c>
    SetHandler dispatcher-handler
    ModMimeUsePathInfo On
  </IfModule>

  Options FollowSymLinks
  AllowOverride None
</Directory>

Step 6: Copy dispatcher.any from downloaded dispatcher module and paste it to /etc/apache2/conf/dispatcher.any

Step 7: Open dispatcher.any and find docroot in the configurations and change the location to match document root of httpd.conf

/docroot "/Library/WebServer/Documents"

Step 8: Run following command to verify configurations. You should get Synatax OK in response .
sudo apachectl start
sudo apachectl configtest

Step 9: Restart apache 
sudo apachectl restart

Step 10: Start author instance on port 4502 and navigate to http://localhost:80 and it should redirect you to author instance.
You can find cached file at "/Library/WebServer/Documents"

For any issues you can tail apache and dispatcher log
/private/var/log/apache2/
/etc/apache2/logs/dispatcher.log

Note: This is basic configuration for author instance using out of the box files available.For more details refer adobe doc [0][1]


Dhanywaad



Basic Sling Model Exporter

/* Open a page which is having component /apps/project/components/page/page /content/project/en/jcr:content.model.json Don't miss...