/* | |
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; | |
} | |
} |
Thursday, 2 August 2018
Basic Sling Model Exporter
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.
Now let us try to understand
Refer following for more details
https://helpx.adobe.com/experience-manager/6-4/forms/using/admin-help/preventing-csrf-attacks.html
Dhanywaad
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.
- Go to http://localhost:4502/system/console/configMgr
- Find Adobe Granite CSRF Filter
- Remove POST from Filter Methods
- 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 :
java -Xmx2g -agentlib:jdwp=transport=dt_socket,address=5402,server=y,suspend=n -jar cq-author-p4502.jar
Dhanywad
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 :
- AEM server should be enabled to accept remote debugging connection
- 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
HTL Examples
Get title of page using HTL
- page
- currentPage
- properties
- inheritedPageProperties
- currentDesign
- currentSession
- wcmmode
Refer for list of global objects
Converting JSP Method into HTL
Converting JSP Method into HTL
- JSP : currentPage.getContentResource()).getResourceType()
- HTL 1 : ${currentPage.getContentResource.getResourceType}
- 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
<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}
Subscribe to:
Posts (Atom)
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...
-
Namastey, Templates are basis of AEM page. Author can create a page using a template . These templates define basic structure of the ...
-
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...