adobe / aem-core-wcm-components

Standardized components to build websites with AEM.
https://docs.adobe.com/content/help/en/experience-manager-core-components/using/introduction.html
Apache License 2.0
744 stars 752 forks source link

Can not adapt most core components from request #2711

Closed marto closed 7 months ago

marto commented 7 months ago

Bug Report

Current Behavior Majority of WCM Core components can not be adapted from a SlingHttpServletRequest to a model in code. We can no longer use and extend WCM Core components using the proxy pattern as per the recommended way of using and extending core components. For example a Teaser or Button component can no longer be adapted. The example below shows how proxy components can't be adapted.

Expected behavior/code modelFactory.getModelFromRequest method should return a valid model and not null slingHttpServletRequest.adaptTo(Teaser.class) method should return a valid model not null

Environment

Possible Solution Not sure if this is an AEM issue or a Core Components issue.

Additional context / Screenshots

Expected Behaviour:

Impact

package com.adobe.aem.guides.wknd.core.servlets;

import com.adobe.cq.wcm.core.components.models.Teaser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Component(service = { Servlet.class })
@SlingServletResourceTypes(
        resourceTypes= { "wknd/components/teaser"},
        methods=HttpConstants.METHOD_GET,
        selectors="custom",
        extensions = "html"
)
public class AdaptComponentServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private ModelFactory modelFactory;

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException {
        Object model = null;
        String response = null;
        logger.error("AdaptComponentServlet is being executed");

        try {
            modelFactory.getModelFromWrappedRequest(req, req.getResource(), Teaser.class);
        } catch (Exception e) {
            logger.error("An error occurred while resolving the model from req.adaptTo.", e);
            response = "<html><body><h1>Error: An error occurred while resolving the model from modelFactory.getModelFromWrappedRequest.</h1><p>Model: " + e.getLocalizedMessage() + "</p></body></html>";
        }

        try {
            if (model == null) {
                model = modelFactory.getModelFromRequest(req);
            }
        } catch (Exception e) {
            logger.error("An error occurred while resolving the model from modelFactory.getModelFromRequest.", e);
            response = "<html><body><h1>Error: An error occurred while resolving the model from modelFactory.getModelFromRequest</h1><p>Model: " + e.getLocalizedMessage() + "</p></body></html>";
        }

        try {
            if (model == null) {
                model = req.adaptTo(Teaser.class);
            }
        } catch (Exception e) {
            logger.error("An error occurred while resolving the model from req.adaptTo.", e);
            response = "<html><body><h1>Error: An error occurred while resolving the model from req.adaptTo.</h1><p>Model: " + e.getLocalizedMessage() + "</p></body></html>";
        }

        if (model != null) {
            String modelAsString = objectMapper.writeValueAsString(model);
            response = "<html><body><h1>OK</h1><p>Model: " + modelAsString + "</p></body></html>";
        }

        // Write the response body to the servlet response writer
        resp.setContentType("text/html");
        resp.setStatus(model == null ? 500 : 200);
        resp.getWriter().write(response);
    }
}

Errors:

02.04.2024 14:47:34.315 *ERROR* [127.0.0.1 [1712029654311] GET /content/wknd/us/en/jcr:content/root/container/teaser.custom.html HTTP/1.1] com.adobe.aem.guides.wknd.core.servlets.AdaptComponentServlet An error occurred while resolving the model from modelFactory.getModelFromRequest.
org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.adobe.cq.wcm.core.components.internal.models.v2.TeaserImpl
    at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:754) [org.apache.sling.models.impl:1.4.16.B0002]
    at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:428) [org.apache.sling.models.impl:1.4.16.B0002]
    at org.apache.sling.models.impl.ModelAdapterFactory.getModelFromRequest(ModelAdapterFactory.java:1297) [org.apache.sling.models.impl:1.4.16.B0002]
    at com.adobe.aem.guides.wknd.core.servlets.AdaptComponentServlet.doGet(AdaptComponentServlet.java:45)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:266) [org.apache.sling.api:2.22.0.B002]
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:342) [org.apache.sling.api:2.22.0.B002]
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:374) [org.apache.sling.api:2.22.0.B002]
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:579) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:88) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilterWithErrorHandling(WCMDebugFilter.java:192) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:159) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:375) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:190) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.core.impl.page.PageLockFilter.doFilter(PageLockFilter.java:91) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.personalization.impl.TargetComponentFilter.doFilter(TargetComponentFilter.java:94) [com.day.cq.cq-personalization:5.12.48]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:283) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:82) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:84) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:109) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.cq.social.ugcbase.security.impl.SaferSlingPostServlet.doFilter(SaferSlingPostServlet.java:114) [com.adobe.cq.social.cq-social-ugcbase-impl:2.5.3]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.dam.core.impl.assetlinkshare.AdhocAssetShareAuthHandler.doFilter(AdhocAssetShareAuthHandler.java:440) [com.day.cq.dam.cq-dam-core:5.12.382]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:84) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:84) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.dam.core.impl.servlet.ActivityRecordHandler.doFilter(ActivityRecordHandler.java:141) [com.day.cq.dam.cq-dam-core:5.12.382]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.rest.impl.servlet.ApiResourceFilter.doFilter(ApiResourceFilter.java:70) [com.adobe.granite.rest.api:1.1.16.CQ650-B0007]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.requests.logging.impl.RequestLoggerImpl.doFilter(RequestLoggerImpl.java:134) [com.adobe.granite.requests.logging:1.0.20]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.rest.assets.impl.AssetContentDispositionFilter.doFilter(AssetContentDispositionFilter.java:96) [com.adobe.granite.rest.assets:1.0.58.CQ650-B0002]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl.doFilter(AuthoringUIModeServiceImpl.java:394) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:248) [com.day.cq.wcm.cq-wcm-mobile-core:5.11.16]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:84) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:110) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.cq.social.commons.cors.CORSAuthenticationFilter.doFilter(CORSAuthenticationFilter.java:91) [com.adobe.cq.social.cq-social-commons:1.10.87]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.foundation.forms.FormsHandlingServletHelper.handleFilter(FormsHandlingServletHelper.java:226) [com.day.cq.wcm.cq-wcm-foundation:5.12.68]
    at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:138) [com.day.cq.wcm.cq-wcm-foundation:5.12.68]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.optout.impl.OptOutFilter.doFilter(OptOutFilter.java:76) [com.adobe.granite.optout:1.0.0]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.foundation.forms.FormsHandlingServletHelper.handleFilter(FormsHandlingServletHelper.java:226) [com.day.cq.wcm.cq-wcm-foundation:5.12.68]
    at com.adobe.cq.wcm.core.components.internal.servlets.CoreFormHandlingServlet.doFilter(CoreFormHandlingServlet.java:123)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:90) [com.day.cq.wcm.cq-wcm-core:5.12.252]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.cq.history.impl.HistoryRequestFilter.doFilter(HistoryRequestFilter.java:122) [com.adobe.cq.cq-history:5.10.6]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:87) [org.apache.sling.rewriter:1.3.6]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.httpcache.impl.InnerCacheFilter.doFilter(InnerCacheFilter.java:81) [com.adobe.granite.httpcache.core:1.0.8]
    at com.adobe.granite.httpcache.impl.InnerCacheFilter.doFilter(InnerCacheFilter.java:60) [com.adobe.granite.httpcache.core:1.0.8]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:131) [org.apache.sling.i18n:2.6.2]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.csrf.impl.CSRFFilter.doFilter(CSRFFilter.java:217) [com.adobe.granite.csrf:1.0.20.CQ650-B0002]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.security.impl.ContentDispositionFilter.doFilter(ContentDispositionFilter.java:152) [org.apache.sling.security:1.1.16]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:84) [org.apache.sling.engine:2.7.10.B0006]
    at com.adobe.granite.resourceresolverhelper.impl.ResourceResolverHelperImpl.doFilter(ResourceResolverHelperImpl.java:83) [com.adobe.granite.resourceresolverhelper:1.0.4]
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:78) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.doProcessRequest(SlingRequestProcessorImpl.java:151) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:250) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:127) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:86) [org.apache.felix.http.jetty:4.2.20]
    at com.adobe.granite.cors.impl.CORSFilter.doFilter(CORSFilter.java:88) [com.adobe.granite.cors:1.0.10.CQ650-B0002]
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:149) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) [org.apache.felix.http.jetty:4.2.20]
    at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:308) [com.adobe.granite.license:1.2.10]
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:149) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:131) [org.apache.sling.i18n:2.6.2]
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:149) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.sling.featureflags.impl.FeatureManager.doFilter(FeatureManager.java:116) [org.apache.sling.featureflags:1.2.2]
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:149) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:73) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:149) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.sling.engine.impl.parameters.RequestParameterSupportConfigurer.doFilter(RequestParameterSupportConfigurer.java:67) [org.apache.sling.engine:2.7.10.B0006]
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:149) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.InvocationChain.doFilter(InvocationChain.java:81) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.Dispatcher$1.doFilter(Dispatcher.java:152) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager$2.doFilter(WhiteboardManager.java:1000) [org.apache.felix.http.jetty:4.2.20]
    at com.adobe.granite.cors.impl.CORSFilter.doFilter(CORSFilter.java:88) [com.adobe.granite.cors:1.0.10.CQ650-B0002]
    at org.apache.felix.http.base.internal.handler.PreprocessorHandler.handle(PreprocessorHandler.java:137) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager$2.doFilter(WhiteboardManager.java:1006) [org.apache.felix.http.jetty:4.2.20]
    at com.adobe.granite.auth.oauth.impl.OAuthCallbackFilter.doFilter(OAuthCallbackFilter.java:78) [com.adobe.granite.auth.oauth:1.10.8]
    at org.apache.felix.http.base.internal.handler.PreprocessorHandler.handle(PreprocessorHandler.java:137) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager$2.doFilter(WhiteboardManager.java:1006) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:326) [org.apache.sling.security:1.1.16]
    at org.apache.felix.http.base.internal.handler.PreprocessorHandler.handle(PreprocessorHandler.java:137) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager$2.doFilter(WhiteboardManager.java:1006) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.sslfilter.internal.SslFilter.doFilter(SslFilter.java:97) [org.apache.felix.http.sslfilter:1.2.6]
    at org.apache.felix.http.base.internal.handler.PreprocessorHandler.handle(PreprocessorHandler.java:137) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager$2.doFilter(WhiteboardManager.java:1006) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.whiteboard.WhiteboardManager.invokePreprocessors(WhiteboardManager.java:1010) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:94) [org.apache.felix.http.jetty:4.2.20]
    at org.apache.felix.http.base.internal.dispatch.DispatcherServlet.service(DispatcherServlet.java:49) [org.apache.felix.http.jetty:4.2.20]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) [org.apache.felix.http.servlet-api:1.2.0]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:799) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:554) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1624) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:233) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1440) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:188) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:505) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1594) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:186) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1355) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:191) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.Server.handle(Server.java:516) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:338) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:315) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:173) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:409) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:883) [org.apache.felix.http.jetty:4.2.20]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$Runner.run(QueuedThreadPool.java:1034) [org.apache.felix.http.jetty:4.2.20]
    at java.base/java.lang.Thread.run(Thread.java:829)
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject protected com.day.cq.wcm.api.Page com.adobe.cq.wcm.core.components.internal.models.v2.TeaserImpl.currentPage
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:738) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:615) [org.apache.sling.models.impl:1.4.16.B0002]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:733) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private com.day.cq.wcm.api.components.Component com.adobe.cq.wcm.core.components.internal.models.v1.TeaserImpl.component
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:738) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:615) [org.apache.sling.models.impl:1.4.16.B0002]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:733) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject protected com.day.cq.wcm.api.PageManager com.adobe.cq.wcm.core.components.internal.models.v1.TeaserImpl.pageManager
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:738) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:615) [org.apache.sling.models.impl:1.4.16.B0002]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:733) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject protected com.day.cq.wcm.api.designer.Style com.adobe.cq.wcm.core.components.internal.models.v1.TeaserImpl.currentStyle
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:738) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
    Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
        at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:615) [org.apache.sling.models.impl:1.4.16.B0002]
        at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:733) [org.apache.sling.models.impl:1.4.16.B0002]
        ... 144 common frames omitted
ky940819 commented 7 months ago

I haven't tested it, but this can happen because some of the injected variables come from the sling script bindings (which haven't been initialized before the request gets to your servlet).

Try adding this


 /**
     * Bundle context.
     */
    private BundleContext bundleContext;

    /**
     * Activate the service.
     *
     * @param bundleContext The bundle context.
     */
    @Activate
    protected void activate(final BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }

     @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException {
        SlingBindings bindings = new SlingBindings();
        bindings.setSling(new ScriptHelper(bundleContext, null, req, resp));
        req.setAttribute(SlingBindings.class.getName(), bindings);

        // ....
    }

You can see an example of this being done in the SearchResultServlet

marto commented 7 months ago

Thanks @ky940819, that was the issue.

It seems that allot of the models now rely on the SlingBindings to be setup which are always setup when you use those models inside a HTL template but have to be manually setup if accessed directly from a filter/servlet. For reference here is the working code.

package com.adobe.aem.guides.wknd.core.servlets;

import com.adobe.cq.wcm.core.components.models.Teaser;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.scripting.core.ScriptHelper;
import org.apache.sling.servlets.annotations.SlingServletResourceTypes;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Component(service = { Servlet.class })
@SlingServletResourceTypes(
        resourceTypes= { "wknd/components/teaser"},
        methods=HttpConstants.METHOD_GET,
        selectors="custom",
        extensions = "html"
)
public class SSRComponentServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUID = 1L;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    private ModelFactory modelFactory;

    private BundleContext bundleContext;

    @Activate
    protected void activate(final BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws ServletException, IOException {
        Object model = null;
        String response = null;
        logger.error("SSRComponentServlet is being executed");

        final SlingBindings binding = new SlingBindings();
        binding.setSling(new ScriptHelper(bundleContext, null, req, resp));
        binding.setResponse(resp);
        binding.setRequest(req);
        binding.setResource(req.getResource());
        req.setAttribute(SlingBindings.class.getName(), binding);
        logger.error("Added " + SlingBindings.class.getName());

        try {
            model = modelFactory.getModelFromWrappedRequest(req, req.getResource(), Teaser.class);
        } catch (Exception e) {
            logger.error("An error occurred while resolving the model from req.adaptTo.", e);
            response = "<html><body><h1>Error: An error occurred while resolving the model from modelFactory.getModelFromWrappedRequest.</h1><p>Model: " + e.getLocalizedMessage() + "</p></body></html>";
        }

        if (model != null) {
            String modelAsString = objectMapper.writeValueAsString(model);
            response = "<html><body><h1>OK</h1><p>Model: " + modelAsString + "</p></body></html>";
        }

        // Write the response body to the servlet response writer
        resp.setContentType("text/html");
        resp.setStatus(model == null ? 500 : 200);
        resp.getWriter().write(response);
    }
}

For testing this is usefull: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/junit-test-cases-for-sling-models-based-on-delegation-pattern/m-p/396541