Highlighted

Issue with Creating Content Fragment Programatically

niteshsoni21

21-06-2019

Hi Team,

There is a requirement where we need to create the AEM fragment programatically in AEM6.4.2 version.

Based on the document (Customizing and Extending Content Fragments ) , we are using the below code to create Content Fragment

Referred Code :

Resource templateOrModelRsc = resourceResolver.getResource("...");

FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class);

ContentFragment newFragment = tpl.createFragment(parentRsc, "A fragment name", "A fragment description.");

The code is as below:

String parent = "/content/dam/cwb-poc";

Resource templateOrModelRsc = resolver.getResource("/conf/Test-Configuration/settings/dam/cfm/models/content-fragment-model");

FragmentTemplate tpl = templateOrModelRsc.adaptTo(FragmentTemplate.class);

try {

ContentFragment newFragment = tpl.createFragment(resolver.getResource(parent), "cwb-content-fragment-test",

"CWB POC Content Fragment Test.");

session.save();

}

The configuration and content fragment model is already present in crx. The code is throwing Null Pointer Exception.

Error:

Internal Server Error

Cannot serve request to /services/createContentFragment in com.capgroup.dam.core.CreateContentFragment

Internal Server Error

Cannot serve request to /services/createContentFragment in com.capgroup.dam.core.CreateContentFragment

Exception:

java.lang.NullPointerException at com.capgroup.dam.core.CreateContentFragment.doGet(CreateContentFragment.java:59) at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:266) at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:137) at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:342) at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:374) at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:77) at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:156) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:375) at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:190) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.core.impl.PageLockFilter.doFilter(PageLockFilter.java:91) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:96) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:282) at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:77) at com.day.cq.dam.core.impl.servlet.ActivityRecordHandler.doFilter(ActivityRecordHandler.java:154) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:73) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:73) at com.day.cq.dam.core.impl.assetlinkshare.AdhocAssetShareAuthHandler.doFilter(AdhocAssetShareAuthHandler.java:437) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:73) at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:109) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.cq.social.ugcbase.security.impl.SaferSlingPostServlet.doFilter(SaferSlingPostServlet.java:114) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:73) at com.adobe.granite.rest.impl.servlet.ApiResourceFilter.doFilter(ApiResourceFilter.java:70) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.granite.requests.logging.impl.RequestLoggerImpl.doFilter(RequestLoggerImpl.java:121) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.granite.rest.assets.impl.AssetContentDispositionFilter.doFilter(AssetContentDispositionFilter.java:96) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.granite.csrf.impl.CSRFFilter.doFilter(CSRFFilter.java:217) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl.doFilter(AuthoringUIModeServiceImpl.java:372) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:248) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.capgroup.dam.filters.ManageReferencesFilter.doFilter(ManageReferencesFilter.java:243) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:107) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.cq.social.commons.cors.CORSAuthenticationFilter.doFilter(CORSAuthenticationFilter.java:91) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.foundation.forms.FormsHandlingServletHelper.handleFilter(FormsHandlingServletHelper.java:226) at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:138) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.granite.optout.impl.OptOutFilter.doFilter(OptOutFilter.java:76) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.foundation.forms.FormsHandlingServletHelper.handleFilter(FormsHandlingServletHelper.java:226) at com.adobe.cq.wcm.core.components.internal.servlets.CoreFormHandlingServlet.doFilter(CoreFormHandlingServlet.java:126) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:90) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.cq.history.impl.HistoryRequestFilter.doFilter(HistoryRequestFilter.java:122) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.cq.mcm.campaign.servlets.CampaignCopyTracker.doFilter(CampaignCopyTracker.java:100) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:87) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at com.adobe.granite.httpcache.impl.InnerCacheFilter.doFilter(InnerCacheFilter.java:81) at com.adobe.granite.httpcache.impl.InnerCacheFilter.doFilter(InnerCacheFilter.java:60) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:131) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.security.impl.ContentDispositionFilter.doFilter(ContentDispositionFilter.java:152) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:73) at com.adobe.granite.resourceresolverhelper.impl.ResourceResolverHelperImpl.doFilter(ResourceResolverHelperImpl.java:83) at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:68) at org.apache.sling.engine.impl.SlingRequestProcessorImpl.doProcessRequest(SlingRequestProcessorImpl.java:151) at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:234) at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:120) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:86) at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:308) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:328) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.felix.http.sslfilter.internal.SslFilter.doFilter(SslFilter.java:96) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:131) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.sling.featureflags.impl.FeatureManager.doFilter(FeatureManager.java:116) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:72) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.sling.engine.impl.parameters.RequestParameterSupportConfigurer.doFilter(RequestParameterSupportConfigurer.java:63) at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:135) at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) at org.apache.felix.http.base.internal.dispatch.Dispatcher$1.doFilter(Dispatcher.java:146) at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager.invokePreprocessors(WhiteboardManager.java:1000) at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:91) at org.apache.felix.http.base.internal.dispatch.DispatcherServlet.service(DispatcherServlet.java:49) at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1317) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1219) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:219) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) at org.eclipse.jetty.server.Server.handle(Server.java:531) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102) at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:118) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:126) at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:366) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:762) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:680) at java.lang.Thread.run(Unknown Source) 

Request Progress:

      0 TIMER_START{Request Processing} 2 COMMENT timer_end format is {<elapsed microseconds>,<timer name>} <optional message> 8 LOG Method=GET, PathInfo=null 10 TIMER_START{handleSecurity} 2082 TIMER_END{2070,handleSecurity} authenticator org.apache.sling.auth.core.impl.SlingAuthenticator@507ea43f returns true 2431 TIMER_START{ResourceResolution} 2564 TIMER_END{131,ResourceResolution} URI=/services/createContentFragment resolves to Resource=ServletResource, servlet=com.capgroup.dam.core.CreateContentFragment, path=/services/createContentFragment 2573 LOG Resource Path Info: SlingRequestPathInfo: path='/services/createContentFragment', selectorString='null', extension='null', suffix='null' 2574 TIMER_START{ServletResolution} 2578 TIMER_START{resolveServlet(/services/createContentFragment)} 2591 TIMER_END{11,resolveServlet(/services/createContentFragment)} Using servlet com.capgroup.dam.core.CreateContentFragment 2596 TIMER_END{21,ServletResolution} URI=/services/createContentFragment handled by Servlet=com.capgroup.dam.core.CreateContentFragment 2603 LOG Applying Requestfilters 2606 LOG Calling filter: com.adobe.granite.resourceresolverhelper.impl.ResourceResolverHelperImpl 2615 LOG Calling filter: org.apache.sling.security.impl.ContentDispositionFilter 2619 LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter 2633 LOG Calling filter: com.adobe.granite.httpcache.impl.InnerCacheFilter 2645 LOG Calling filter: org.apache.sling.rewriter.impl.RewriterFilter 2649 LOG Calling filter: com.adobe.cq.mcm.campaign.servlets.CampaignCopyTracker 2653 LOG Calling filter: com.adobe.cq.history.impl.HistoryRequestFilter 2813 LOG Calling filter: com.day.cq.wcm.core.impl.WCMRequestFilter 2828 LOG Calling filter: com.adobe.cq.wcm.core.components.internal.servlets.CoreFormHandlingServlet 2833 LOG Calling filter: com.adobe.granite.optout.impl.OptOutFilter 2843 LOG Calling filter: com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet 2846 LOG Calling filter: com.adobe.cq.social.commons.cors.CORSAuthenticationFilter 2850 LOG Calling filter: org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter 2852 LOG Calling filter: com.capgroup.dam.filters.ManageReferencesFilter 2863 LOG Calling filter: com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter 2866 LOG Calling filter: com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl 2981 LOG Calling filter: com.adobe.granite.csrf.impl.CSRFFilter 2986 LOG Calling filter: com.adobe.granite.rest.assets.impl.AssetContentDispositionFilter 2992 LOG Calling filter: com.adobe.granite.requests.logging.impl.RequestLoggerImpl 2997 LOG Calling filter: com.adobe.granite.rest.impl.servlet.ApiResourceFilter 3026 LOG Calling filter: com.adobe.cq.social.ugcbase.security.impl.SaferSlingPostServlet 3029 LOG Calling filter: com.day.cq.wcm.core.impl.warp.TimeWarpFilter 3034 LOG Calling filter: com.day.cq.dam.core.impl.assetlinkshare.AdhocAssetShareAuthHandler 3039 LOG Calling filter: com.day.cq.dam.core.impl.servlet.ActivityRecordHandler 3052 LOG Applying Componentfilters 3054 LOG Calling filter: com.day.cq.personalization.impl.TargetComponentFilter 3059 LOG Calling filter: com.day.cq.wcm.core.impl.PageLockFilter 3062 LOG Calling filter: com.day.cq.wcm.core.impl.WCMComponentFilter 3160 LOG Calling filter: com.day.cq.wcm.core.impl.WCMDebugFilter 3176 TIMER_START{com.capgroup.dam.core.CreateContentFragment#0} 5804 TIMER_END{2627,com.capgroup.dam.core.CreateContentFragment#0} 6013 LOG Filter timing: filter=com.day.cq.wcm.core.impl.WCMDebugFilter, inner=3, total=3, outer=0 6017 LOG Filter timing: filter=com.day.cq.wcm.core.impl.WCMComponentFilter, inner=3, total=3, outer=0 6020 LOG Filter timing: filter=com.day.cq.wcm.core.impl.PageLockFilter, inner=3, total=3, outer=0 6562 LOG Filter timing: filter=com.day.cq.dam.core.impl.servlet.ActivityRecordHandler, inner=3, total=3, outer=0 6566 LOG Filter timing: filter=com.adobe.cq.dtm.reactor.impl.servlets.ClientLibraryFilter, inner=3, total=3, outer=0 6569 LOG Filter timing: filter=com.adobe.cq.dtm.impl.servlets.DTMLibraryCompatibilityFilter, inner=3, total=3, outer=0 6572 LOG Filter timing: filter=com.day.cq.dam.core.impl.assetlinkshare.AdhocAssetShareAuthHandler, inner=3, total=3, outer=0 6576 LOG Filter timing: filter=com.adobe.cq.dam.webdav.impl.io.DamWebdavRequestFilter, inner=3, total=3, outer=0 6579 LOG Filter timing: filter=com.day.cq.wcm.core.impl.warp.TimeWarpFilter, inner=3, total=3, outer=0 6581 LOG Filter timing: filter=com.adobe.cq.social.ugcbase.security.impl.SaferSlingPostServlet, inner=3, total=3, outer=0 6585 LOG Filter timing: filter=com.day.cq.dam.core.impl.servlet.DamContentDispositionFilter, inner=3, total=3, outer=0 6588 LOG Filter timing: filter=com.adobe.granite.rest.impl.servlet.ApiResourceFilter, inner=3, total=3, outer=0 6590 LOG Filter timing: filter=com.adobe.granite.requests.logging.impl.RequestLoggerImpl, inner=3, total=3, outer=0 6593 LOG Filter timing: filter=com.adobe.granite.rest.assets.impl.AssetContentDispositionFilter, inner=3, total=3, outer=0 6596 LOG Filter timing: filter=com.adobe.granite.csrf.impl.CSRFFilter, inner=3, total=3, outer=0 6598 LOG Filter timing: filter=com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl, inner=3, total=3, outer=0 6608 LOG Filter timing: filter=com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter, inner=3, total=3, outer=0 6611 LOG Filter timing: filter=com.capgroup.dam.filters.ManageReferencesFilter, inner=3, total=3, outer=0 6614 LOG Filter timing: filter=org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter, inner=3, total=3, outer=0 6616 LOG Filter timing: filter=com.adobe.cq.social.commons.cors.CORSAuthenticationFilter, inner=3, total=3, outer=0 6619 LOG Filter timing: filter=com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet, inner=3, total=3, outer=0 6621 LOG Filter timing: filter=com.adobe.granite.optout.impl.OptOutFilter, inner=3, total=3, outer=0 6625 LOG Filter timing: filter=com.adobe.cq.wcm.core.components.internal.servlets.CoreFormHandlingServlet, inner=3, total=3, outer=0 6627 LOG Filter timing: filter=com.day.cq.wcm.core.impl.WCMRequestFilter, inner=3, total=3, outer=0 6630 LOG Filter timing: filter=com.adobe.cq.history.impl.HistoryRequestFilter, inner=3, total=3, outer=0 6632 LOG Filter timing: filter=com.adobe.cq.mcm.campaign.servlets.CampaignCopyTracker, inner=3, total=3, outer=0 6636 LOG Filter timing: filter=org.apache.sling.rewriter.impl.RewriterFilter, inner=3, total=3, outer=0 6638 LOG Filter timing: filter=com.adobe.granite.httpcache.impl.InnerCacheFilter, inner=3, total=4, outer=1 6641 LOG Filter timing: filter=org.apache.sling.i18n.impl.I18NFilter, inner=4, total=4, outer=0 6644 LOG Filter timing: filter=org.apache.sling.security.impl.ContentDispositionFilter, inner=4, total=5, outer=1 6648 LOG Filter timing: filter=org.apache.sling.distribution.servlet.DistributionAgentCreationFilter, inner=5, total=5, outer=0 7568 LOG Applying Error filters 7572 LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter 7574 LOG Calling filter: org.apache.sling.rewriter.impl.RewriterFilter 7632 TIMER_START{handleError:throwable=java.lang.NullPointerException} 9398 TIMER_END{1764,handleError:throwable=java.lang.NullPointerException} Using handler /libs/sling/servlet/errorhandler/default.jsp 20595 LOG Found processor for post processing ProcessorConfiguration: {contentTypes=[text/html], order=-1, active=true, valid=true, processErrorResponse=true, pipeline=(generator=Config(type=htmlparser, config={}), transformers=(Config(type=linkchecker, config={}), Config(type=mobile, config=JcrPropertyMap [node=Node[NodeDelegate{tree=/libs/cq/config/rewriter/default/transformer-mobile: { jcr:primaryType = nt:unstructured, component-optional = true}}], values={jcr:primaryType=nt:unstructured, component-optional=true}]), Config(type=mobiledebug, config=JcrPropertyMap [node=Node[NodeDelegate{tree=/libs/cq/config/rewriter/default/transformer-mobiledebug: { jcr:primaryType = nt:unstructured, component-optional = true}}], values={jcr:primaryType=nt:unstructured, component-optional=true}]), Config(type=contentsync, config=JcrPropertyMap [node=Node[NodeDelegate{tree=/libs/cq/config/rewriter/default/transformer-contentsync: { jcr:primaryType = nt:unstructured, component-optional = true}}], values={jcr:primaryType=nt:unstructured, component-optional=true}]), serializer=Config(type=htmlwriter, config={}))} 24646 TIMER_END{24643,Request Processing} Dumping SlingRequestProgressTracker Entries

Replies

Highlighted
Highlighted

niteshsoni21

22-06-2019

NPE is coming on :

ContentFragment newFragment = tpl.createFragment(resolver.getResource(parent), "cwb-content-fragment-test",

"CWB POC Content Fragment Test.");

Highlighted

Jörg_Hoh

Employee

23-06-2019

That means that "tpl" is null.

Which is perfectly possible if the adaptTo(FragmentTemplate.class) fails, returning NULL is a documented behavior of adaptTo(). This might be caused by the fact, that the resource is no ContentFragment.

Highlighted

niteshsoni21

23-06-2019

Yes that's correct, the "tpl" is null and the questions which needs clarity are as below:

1)  Resource templateOrModelRsc = resolver.getResource("/conf/Test-Configuration/settings/dam/cfm/models/content-fragment-model");

Here the templateOrModelRsc  is resource for content fragment model and not the content fragment. Is this expecting the content fragment? The node properties for this resource is :

1778182_pastedImage_0.png

2) If it is expecting the content fragment in point 1, then the question is how to create the content fragment programatically? Because as per our requirement we have a use-case where we would be getting the content in the Json format from the service and using the content from Json, we would have to create the content fragment. For this we were following the approach mentioned here :" Customizing and Extending Content Fragments

3) Based on the details mentioned in the document our assumptions were :

a) Resource templateOrModelRsc  : This is the resource of the content fragment model that is present under the path : /conf/ (configuration - name) /settings/dam/cfm/models/

b) tpl.createFragment(parentRsc, "A fragment name", "A fragment description."); here the parent resource us the path of the folder where we need to create the content fragment, along with the name and description of the fragment that we need to give.

So our assumptions are not correct or does it need some correction? Would appreciate your help in this.

Highlighted

Jörg_Hoh

Employee

23-06-2019

You can set the class "com.adobe.cq.dam.cfm.impl" to debug, which might print messages in case an adaption failed. Also you should make sure that your content fragment model has a "model" subnode (like  /conf/we-retail/settings/dam/cfm/models/we-retail-experience/ has).

Jörg

Highlighted

niteshsoni21

26-06-2019

After setting the class "com.adobe.cq.dam.cfm.impl" to debug, there is no message getting printed. (I hope that is what you meant as mentioned in below screenshot)1779723_pastedImage_1.png

Also I have validate that our content fragment model has a "model" subnode available.

1779724_pastedImage_3.png

Highlighted

Jörg_Hoh

Employee

26-06-2019

Can you compare your own content fragment to  /content/dam/we-retail/en/experiences/48-hours-of-wilderness/48-hours-of-wilderness? Do you have the property "contentfragment = true" set? Also from your screenshot I wonder about the the primaryType = cq:PageContent... From a nodetype point of view conntentfragments are assets. Also the cq:dialog node below the model node shouldn't be there.

So it looks to me that there is quite a bit wrong.

Highlighted

niteshsoni21

26-06-2019

Let me clear the ambiguity based on your queries:

Query 1: Can you compare your own content fragment to  /content/dam/we-retail/en/experiences/48-hours-of-wilderness/48-hours-of-wilderness? Do you have the property "contentfragment = true" set?

Answer : As mentioned above the requirement is to create the content fragment programatically and the content fragment does not exist at present. In the above details also I have mentioned that we are trying to create this content fragment programatically.

Query 2 : Also from your screenshot I wonder about the the primaryType = cq:PageContent... From a nodetype point of view conntentfragments are assets. Also the cq:dialog node below the model node shouldn't be there.

Answer : The screenshot that I share above was the screenshot of content fragment model nodes that are present at the location  (/conf/{project configuration}/settings/dam/cfm/models/{content-fragment-model}/jcr:content/model) as you asked in

your previous comment : Also you should make sure that your content fragment model has a "model" subnode (like  /conf/we-retail/settings/dam/cfm/models/we-retail-experience/ has).

The content fragment model nodes and properties matches to that of we-retail as below:

Our Content Fragment Model Nodes:

1780267_pastedImage_7.png

We Retail Content Fragment Model Nodes:

1780370_pastedImage_9.png

Highlighted

jitendrap369693

08-09-2019

Hi Nitesh,

Please try below code snippet :

try{

          String name = request.getParameter("name");

          String title = request.getParameter("title");

          ResourceResolver resourceResolver = request.getResourceResolver();

          Resource parent = resourceResolver.getResource("/content/dam/we-retail");

          Resource template = resourceResolver.getResource("/libs/settings/dam/cfm/templates/simple/jcr:content");

          FragmentTemplate fragmentTemplate = template.adaptTo(FragmentTemplate.class);

          ContentFragment cf = CreateFragment(

                    parent,

                    template,

                    name,

                    title

            );

          resourceResolver.commit();

} catch(Exception e)

      {

          e.printStackTrace();

}

protected ContentFragment CreateFragment(Resource parent, Resource template, String name, String title) throws ContentFragmentException {

        Resource fragmentResource = parent.getChild(name);

        if (fragmentResource == null) {

            try {

                FragmentTemplate fragmentTemplate = template.adaptTo(FragmentTemplate.class);

                return (ContentFragment) MethodUtils.invokeMethod(fragmentTemplate, "createFragment", parent, name, title);

            } catch (Exception ex) {

    ex.printStackTrace();

                            return null;

            }

        } else {

            return fragmentResource.adaptTo(ContentFragment.class);

        }

    }