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}







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...