Skip to content

Commit

Permalink
Pattern mapping does not work on project #10085
Browse files Browse the repository at this point in the history
(cherry picked from commit 267f963)
  • Loading branch information
rymsha committed Mar 20, 2023
1 parent 9e2daee commit e3b83bd
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.enonic.xp.portal.impl;

import java.util.Optional;
import java.util.concurrent.Callable;

import com.enonic.xp.content.Content;
Expand All @@ -14,6 +13,7 @@
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.RenderMode;
import com.enonic.xp.security.RoleKeys;
import com.enonic.xp.security.acl.Permission;
import com.enonic.xp.security.auth.AuthenticationInfo;
import com.enonic.xp.site.Site;

Expand Down Expand Up @@ -42,84 +42,93 @@ public ContentResolverResult resolve( final PortalRequest request )

private ContentResolverResult resolveInNonEditMode( final ContentPath contentPath )
{
Content content = getContentByPath( contentPath );

final boolean contentExists = content != null || contentExistsByPath( contentPath );
final Content content = callAsContentAdmin( () -> getContentByPath( contentPath ) );

final Site site = callAsContentAdmin( () -> this.contentService.findNearestSiteByPath( contentPath ) );

return new ContentResolverResult( content, contentExists, site, siteRelativePath( site, contentPath ), contentPath.toString() );
final String siteRelativePath = siteRelativePath( site, contentPath );
return new ContentResolverResult( visibleContent( content ), content != null, site, siteRelativePath, contentPath.toString() );
}

private ContentResolverResult resolveInEditMode( final ContentPath contentPath )
{
final String contentPathString = contentPath.toString();

ContentId contentId;
final ContentId contentId = tryConvertToContentId( contentPathString );

try
{
contentId = ContentId.from( contentPathString.substring( 1 ) );
}
catch ( Exception e )
{
contentId = null;
}

final Content content =
Optional.ofNullable( contentId ).map( this::getContentById ).orElseGet( () -> getContentByPath( contentPath ) );
final Content contentById = contentId != null ? callAsContentAdmin( () -> getContentById( contentId ) ) : null;

final boolean contentExists = content != null || contentExistsById( contentId ) || contentExistsByPath( contentPath );
final Content content = contentById != null ? contentById : callAsContentAdmin( () -> this.getContentByPath( contentPath ) );

final Site site = content != null ? callAsContentAdmin( () -> this.contentService.getNearestSite( content.getId() ) ) : null;
final Site site = getNearestSite( content );

return new ContentResolverResult( content, contentExists, site,
siteRelativePath( site, content == null ? null : content.getPath() ), contentPathString );
final String siteRelativePath = siteRelativePath( site, content != null ? content.getPath() : contentPath );
return new ContentResolverResult( visibleContent( content ), content != null, site, siteRelativePath, contentPathString );
}
private Content getContentById( final ContentId contentId )

private Site getNearestSite( final Content content )
{
try
if ( content != null )
{
final Content content = this.contentService.getById( contentId );
if ( ContentPath.ROOT.equals( content.getPath() ) )
{
return null;
}
else
{
return content;
return content.isSite()
? (Site) content
: callAsContentAdmin( () -> this.contentService.getNearestSite( content.getId() ) );
}
}
catch ( final Exception e )
else
{
return null;
}
}

private Content getContentByPath( final ContentPath contentPath )
private static ContentId tryConvertToContentId( final String contentPathString )
{
if ( contentPath.equals( ContentPath.ROOT ) )
try
{
return ContentId.from( contentPathString.substring( 1 ) );
}
catch ( Exception e )
{
return null;
}
}

private Content visibleContent( final Content content )
{
return content == null || ContentPath.ROOT.equals( content.getPath() ) ||
!content.getPermissions().isAllowedFor( ContextAccessor.current().getAuthInfo().getPrincipals(), Permission.READ )
? null
: content;
}

private Content getContentById( final ContentId contentId )
{
try
{
return this.contentService.getByPath( contentPath );
return this.contentService.getById( contentId );
}
catch ( final ContentNotFoundException e )
{
return null;
}
}

private boolean contentExistsById( final ContentId contentId )
{
return this.contentService.contentExists( contentId );
}

private boolean contentExistsByPath( final ContentPath contentPath )
private Content getContentByPath( final ContentPath contentPath )
{
return !ContentPath.ROOT.equals( contentPath ) && this.contentService.contentExists( contentPath );
try
{
return this.contentService.getByPath( contentPath );
}
catch ( final ContentNotFoundException e )
{
return null;
}
}

private static <T> T callAsContentAdmin( final Callable<T> callable )
Expand All @@ -133,14 +142,17 @@ private static <T> T callAsContentAdmin( final Callable<T> callable )

private static String siteRelativePath( final Site site, final ContentPath contentPath )
{
if ( site == null || contentPath == null)
if ( site == null )
{
return null;
return contentPath.toString();
}
if ( site.getPath().equals( contentPath ) )
else if ( site.getPath().equals( contentPath ) )
{
return "/";
}
return contentPath.toString().substring( site.getPath().toString().length() );
else
{
return contentPath.toString().substring( site.getPath().toString().length() );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ private static String urlEscape( final String value )
return UrlEscapers.urlFormParameterEscaper().escape( value );
}

private boolean matchesUrlPattern( final ControllerMappingDescriptor descriptor, final String relativePath, final String relativeUrl )
private boolean matchesUrlPattern( final ControllerMappingDescriptor descriptor, final String relativePath,
final Multimap<String, String> params )
{
final boolean patternWithQueryParameters = descriptor.getPattern().toString().contains( "\\?" );
final boolean patternMatches = descriptor.getPattern().matcher( patternWithQueryParameters ? relativeUrl : relativePath ).matches();
final boolean patternHasQueryParameters = descriptor.getPattern().toString().contains( "\\?" );
final boolean patternMatches = descriptor.getPattern()
.matcher( patternHasQueryParameters ? relativePath + normalizedQueryParams( params ) : relativePath )
.matches();
return descriptor.invertPattern() != patternMatches;
}

Expand Down Expand Up @@ -105,10 +108,7 @@ private boolean filterDescriptor( final ControllerMappingDescriptor controllerMa
{
if ( matchesContent( controllerMappingDescriptor, content ) )
{
final String contentPath = siteRelativePath != null ? siteRelativePath : content.getPath().toString();
final String contentUrl = contentPath + normalizedQueryParams( params );

return matchesUrlPattern( controllerMappingDescriptor, contentPath, contentUrl );
return matchesUrlPattern( controllerMappingDescriptor, siteRelativePath, params );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,25 @@ public WebResponse handle( final WebRequest webRequest, final WebResponse webRes

final Content content = resolvedContent.getContent();

final SiteConfigs.Builder siteConfigs = SiteConfigs.create();
final SiteConfigs siteConfigs;

if ( site != null )
{
site.getSiteConfigs().forEach( siteConfigs::add );
siteConfigs = site.getSiteConfigs();
}
else if ( content != null )
else
{
Optional.ofNullable( ProjectName.from( ( request.getRepositoryId() ) ) )
.map( projectName -> callAsAdmin( () -> projectService.get( projectName ) ) )
.map( Project::getSiteConfigs )
.ifPresent( configs -> configs.forEach( siteConfigs::add ) );
final Project project = callAsAdmin( () -> projectService.get( ProjectName.from( request.getRepositoryId() ) ) );
siteConfigs = Optional.ofNullable( project ).map( Project::getSiteConfigs ).orElse( SiteConfigs.empty() );
}
else

if ( siteConfigs.isEmpty() )
{
return webHandlerChain.handle( webRequest, webResponse );
}

final Optional<ControllerMappingDescriptor> resolve =
controllerMappingsResolver.resolve( resolvedContent.getSiteRelativePath(), request.getParams(), content, siteConfigs.build(),
controllerMappingsResolver.resolve( resolvedContent.getSiteRelativePath(), request.getParams(), content, siteConfigs,
getServiceType( request ) );

if ( resolve.isPresent() )
Expand Down
Loading

0 comments on commit e3b83bd

Please sign in to comment.