Skip to content

Commit

Permalink
lib-portal.getContent doesn't resolve page templates #10800
Browse files Browse the repository at this point in the history
- Added Page resolving for the mappers, thus request get content with injected page/regions etc.
  • Loading branch information
ashklianko committed Dec 13, 2024
1 parent 1856eb1 commit d199f10
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import org.osgi.service.component.annotations.Reference;

import com.enonic.xp.content.ContentService;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.controller.ControllerScriptFactory;
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.handler.EndpointHandler;
import com.enonic.xp.portal.impl.ContentResolver;
import com.enonic.xp.portal.impl.handler.render.PageResolver;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.ResourceService;
import com.enonic.xp.site.SiteService;
import com.enonic.xp.web.HttpMethod;
Expand All @@ -33,13 +37,18 @@ public AttachmentServiceMappingHandler( @Reference final ProjectService projectS
@Reference final ControllerScriptFactory controllerScriptFactory,
@Reference final FilterScriptFactory filterScriptFactory,
@Reference final RendererDelegate rendererDelegate, @Reference final SiteService siteService,
@Reference final ContentService contentService )
@Reference final ContentService contentService,
@Reference final PageTemplateService pageTemplateService,
@Reference final PageDescriptorService pageDescriptorService,
@Reference final LayoutDescriptorService layoutDescriptorService)
{
super( EnumSet.of( HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS ), "attachment" );

this.mappingHandlerHelper =
new MappingHandlerHelper( projectService, resourceService, controllerScriptFactory, filterScriptFactory, rendererDelegate,
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ) );
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ),
new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService )
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
import org.osgi.service.component.annotations.Reference;

import com.enonic.xp.content.ContentService;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.controller.ControllerScriptFactory;
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.handler.EndpointHandler;
import com.enonic.xp.portal.impl.ContentResolver;
import com.enonic.xp.portal.impl.handler.render.PageResolver;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.ResourceService;
import com.enonic.xp.site.SiteService;
import com.enonic.xp.web.HttpMethod;
Expand All @@ -30,13 +34,17 @@ public ComponentServiceMappingHandler( @Reference final ProjectService projectSe
@Reference final ControllerScriptFactory controllerScriptFactory,
@Reference final FilterScriptFactory filterScriptFactory,
@Reference final RendererDelegate rendererDelegate, @Reference final SiteService siteService,
@Reference final ContentService contentService )
@Reference final ContentService contentService,
@Reference final PageTemplateService pageTemplateService,
@Reference final PageDescriptorService pageDescriptorService,
@Reference final LayoutDescriptorService layoutDescriptorService )
{
super( HttpMethod.standard(), "component" );

this.mappingHandlerHelper =
new MappingHandlerHelper( projectService, resourceService, controllerScriptFactory, filterScriptFactory, rendererDelegate,
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ) );
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ),
new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService ));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import org.osgi.service.component.annotations.Reference;

import com.enonic.xp.content.ContentService;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.controller.ControllerScriptFactory;
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.handler.EndpointHandler;
import com.enonic.xp.portal.impl.ContentResolver;
import com.enonic.xp.portal.impl.handler.render.PageResolver;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.ResourceService;
import com.enonic.xp.site.SiteService;
import com.enonic.xp.web.HttpMethod;
Expand All @@ -32,13 +36,17 @@ public ImageServiceMappingHandler( @Reference final ProjectService projectServic
@Reference final ControllerScriptFactory controllerScriptFactory,
@Reference final FilterScriptFactory filterScriptFactory,
@Reference final RendererDelegate rendererDelegate, @Reference final SiteService siteService,
@Reference final ContentService contentService )
@Reference final ContentService contentService,
@Reference final PageTemplateService pageTemplateService,
@Reference final PageDescriptorService pageDescriptorService,
@Reference final LayoutDescriptorService layoutDescriptorService )
{
super( EnumSet.of( HttpMethod.GET, HttpMethod.HEAD, HttpMethod.OPTIONS ), "image" );

this.mappingHandlerHelper =
new MappingHandlerHelper( projectService, resourceService, controllerScriptFactory, filterScriptFactory, rendererDelegate,
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ) );
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ),
new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import org.osgi.service.component.annotations.Reference;

import com.enonic.xp.content.ContentService;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.controller.ControllerScriptFactory;
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.impl.ContentResolver;
import com.enonic.xp.portal.impl.handler.render.PageResolver;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.ResourceService;
import com.enonic.xp.site.SiteService;
import com.enonic.xp.web.WebRequest;
Expand All @@ -28,11 +32,15 @@ public MappingHandler( @Reference final SiteService siteService, @Reference fina
@Reference final ResourceService resourceService,
@Reference final ControllerScriptFactory controllerScriptFactory,
@Reference final FilterScriptFactory filterScriptFactory, @Reference final RendererDelegate rendererDelegate,
@Reference final ProjectService projectService )
@Reference final ProjectService projectService, @Reference final PageTemplateService pageTemplateService,
@Reference final PageDescriptorService pageDescriptorService,
@Reference final LayoutDescriptorService layoutDescriptorService
)
{
this.mappingHandlerHelper =
new MappingHandlerHelper( projectService, resourceService, controllerScriptFactory, filterScriptFactory, rendererDelegate,
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ) );
new ControllerMappingsResolver( siteService ), new ContentResolver( contentService ),
new PageResolver( pageTemplateService, pageDescriptorService, layoutDescriptorService ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.enonic.xp.portal.handler.WebHandlerHelper;
import com.enonic.xp.portal.impl.ContentResolver;
import com.enonic.xp.portal.impl.ContentResolverResult;
import com.enonic.xp.portal.impl.handler.render.PageResolver;
import com.enonic.xp.portal.impl.handler.render.PageResolverResult;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.Project;
import com.enonic.xp.project.ProjectName;
Expand Down Expand Up @@ -58,10 +60,12 @@ class MappingHandlerHelper

private final ContentResolver contentResolver;

private final PageResolver pageResolver;

MappingHandlerHelper( final ProjectService projectService, final ResourceService resourceService,
final ControllerScriptFactory controllerScriptFactory, final FilterScriptFactory filterScriptFactory,
final RendererDelegate rendererDelegate, final ControllerMappingsResolver controllerMappingsResolver,
final ContentResolver contentResolver )
final ContentResolver contentResolver, final PageResolver pageResolver )
{
this.projectService = projectService;
this.resourceService = resourceService;
Expand All @@ -70,6 +74,7 @@ class MappingHandlerHelper
this.rendererDelegate = rendererDelegate;
this.controllerMappingsResolver = controllerMappingsResolver;
this.contentResolver = contentResolver;
this.pageResolver = pageResolver;
}

public WebResponse handle( final WebRequest webRequest, final WebResponse webResponse, final WebHandlerChain webHandlerChain )
Expand Down Expand Up @@ -127,7 +132,19 @@ public WebResponse handle( final WebRequest webRequest, final WebResponse webRes
{
final ControllerMappingDescriptor mapping = resolve.get();

request.setContent( content );
if ( content != null )
{
final PageResolverResult resolvedPage = pageResolver.resolve( request.getMode(), content, site );
final Content effectiveContent = Content.create( content ).page( resolvedPage.getEffectivePage() ).build();
request.setContent( effectiveContent );
request.setApplicationKey( resolvedPage.getApplicationKey() );
request.setPageDescriptor( resolvedPage.getPageDescriptor() );
}
else
{
request.setContent( null );
}

request.setSite( site );
request.setContextPath(
request.getBaseUri() + "/" + RepositoryUtils.getContentRepoName( request.getRepositoryId() ) + "/" + request.getBranch() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.enonic.xp.content.ContentPath;
import com.enonic.xp.content.ContentService;
import com.enonic.xp.data.PropertyTree;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.controller.ControllerScript;
Expand All @@ -19,6 +21,7 @@
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.Resource;
import com.enonic.xp.resource.ResourceKey;
import com.enonic.xp.resource.ResourceService;
Expand Down Expand Up @@ -63,6 +66,12 @@ class AttachmentServiceMappingHandlerTest

private WebHandlerChain webHandlerChain;

private PageTemplateService pageTemplateService;

private PageDescriptorService pageDescriptorService;

private LayoutDescriptorService layoutDescriptorService;

@BeforeEach
final void setup()
{
Expand All @@ -73,6 +82,9 @@ final void setup()
this.rendererDelegate = mock( RendererDelegate.class );
this.siteService = mock( SiteService.class );
this.webHandlerChain = mock( WebHandlerChain.class );
this.pageTemplateService = mock( PageTemplateService.class );
this.pageDescriptorService = mock( PageDescriptorService.class );
this.layoutDescriptorService = mock( LayoutDescriptorService.class );

final ControllerScriptFactory controllerScriptFactory = mock( ControllerScriptFactory.class );
ControllerScript controllerScript = mock( ControllerScript.class );
Expand All @@ -86,7 +98,8 @@ final void setup()
when( filterScript.execute( Mockito.any(), Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse );

this.handler = new AttachmentServiceMappingHandler( projectService, resourceService, controllerScriptFactory, filterScriptFactory,
rendererDelegate, siteService, contentService );
rendererDelegate, siteService, contentService, pageTemplateService,
pageDescriptorService, layoutDescriptorService );

this.request.setMethod( HttpMethod.GET );
this.request.setBranch( ContentConstants.BRANCH_MASTER );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.enonic.xp.content.ContentPath;
import com.enonic.xp.content.ContentService;
import com.enonic.xp.data.PropertyTree;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.controller.ControllerScript;
Expand All @@ -19,6 +21,7 @@
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.Resource;
import com.enonic.xp.resource.ResourceKey;
import com.enonic.xp.resource.ResourceService;
Expand Down Expand Up @@ -62,6 +65,12 @@ class ComponentServiceMappingHandlerTest

private FilterScript filterScript;

private PageTemplateService pageTemplateService;

private PageDescriptorService pageDescriptorService;

private LayoutDescriptorService layoutDescriptorService;

@BeforeEach
final void setup()
{
Expand All @@ -72,6 +81,9 @@ final void setup()
RendererDelegate rendererDelegate = mock( RendererDelegate.class );
this.siteService = mock( SiteService.class );
this.webHandlerChain = mock( WebHandlerChain.class );
this.pageTemplateService = mock( PageTemplateService.class );
this.pageDescriptorService = mock( PageDescriptorService.class );
this.layoutDescriptorService = mock( LayoutDescriptorService.class );

final ControllerScriptFactory controllerScriptFactory = mock( ControllerScriptFactory.class );
ControllerScript controllerScript = mock( ControllerScript.class );
Expand All @@ -85,7 +97,8 @@ final void setup()
when( filterScript.execute( Mockito.any(), Mockito.any(), Mockito.any() ) ).thenReturn( portalResponse );

this.handler = new ComponentServiceMappingHandler( projectService, resourceService, controllerScriptFactory, filterScriptFactory,
rendererDelegate, siteService, contentService );
rendererDelegate, siteService, contentService, pageTemplateService,
pageDescriptorService, layoutDescriptorService );

this.request.setMethod( HttpMethod.GET );
this.request.setBranch( ContentConstants.BRANCH_MASTER );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.enonic.xp.content.ContentPath;
import com.enonic.xp.content.ContentService;
import com.enonic.xp.data.PropertyTree;
import com.enonic.xp.page.PageDescriptorService;
import com.enonic.xp.page.PageTemplateService;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.PortalResponse;
import com.enonic.xp.portal.controller.ControllerScript;
Expand All @@ -19,6 +21,7 @@
import com.enonic.xp.portal.filter.FilterScriptFactory;
import com.enonic.xp.portal.impl.rendering.RendererDelegate;
import com.enonic.xp.project.ProjectService;
import com.enonic.xp.region.LayoutDescriptorService;
import com.enonic.xp.resource.Resource;
import com.enonic.xp.resource.ResourceKey;
import com.enonic.xp.resource.ResourceService;
Expand Down Expand Up @@ -63,6 +66,12 @@ class ImageServiceMappingHandlerTest

private WebHandlerChain webHandlerChain;

private PageTemplateService pageTemplateService;

private PageDescriptorService pageDescriptorService;

private LayoutDescriptorService layoutDescriptorService;

@BeforeEach
final void setup()
{
Expand All @@ -73,6 +82,9 @@ final void setup()
this.rendererDelegate = mock( RendererDelegate.class );
this.siteService = mock( SiteService.class );
this.webHandlerChain = mock( WebHandlerChain.class );
this.pageTemplateService = mock( PageTemplateService.class );
this.pageDescriptorService = mock( PageDescriptorService.class );
this.layoutDescriptorService = mock( LayoutDescriptorService.class );

final ControllerScriptFactory controllerScriptFactory = mock( ControllerScriptFactory.class );
ControllerScript controllerScript = mock( ControllerScript.class );
Expand All @@ -87,7 +99,8 @@ final void setup()

this.handler =
new ImageServiceMappingHandler( projectService, resourceService, controllerScriptFactory, filterScriptFactory, rendererDelegate,
siteService, contentService );
siteService, contentService, pageTemplateService, pageDescriptorService,
layoutDescriptorService );

this.request.setMethod( HttpMethod.GET );
this.request.setBranch( ContentConstants.BRANCH_MASTER );
Expand Down
Loading

0 comments on commit d199f10

Please sign in to comment.