Skip to content

Commit

Permalink
Out of scope for system provider #10478
Browse files Browse the repository at this point in the history
(cherry picked from commit 5dc120e)
  • Loading branch information
rymsha committed Apr 2, 2024
1 parent 5b6566e commit c80742c
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
public class AdminSiteHandler
extends BaseSiteHandler
{
private static final String BASE_URI_START = "/admin/site";
private static final String ADMIN_SITE_PREFIX = "/admin/site/";

private static final Pattern BASE_URI_PATTERN = Pattern.compile( "^" + BASE_URI_START + "/(edit|preview|admin|inline)" );
private static final Pattern BASE_URI_PATTERN = Pattern.compile( "^/admin/site/(edit|preview|admin|inline)" );

private volatile String previewContentSecurityPolicy;

Expand All @@ -45,7 +45,7 @@ public void activate( final AdminConfig config )
@Override
protected boolean canHandle( final WebRequest webRequest )
{
return webRequest.getRawPath().startsWith( BASE_URI_START );
return webRequest.getRawPath().startsWith( ADMIN_SITE_PREFIX );
}

@Override
Expand All @@ -54,7 +54,7 @@ protected PortalRequest createPortalRequest( final WebRequest webRequest, final
final Matcher matcher = BASE_URI_PATTERN.matcher( webRequest.getRawPath() );
if ( !matcher.find() )
{
throw WebException.notFound( "Mode needs to be specified" );
throw WebException.notFound( "Mode must be specified" );
}
final String baseUri = matcher.group( 0 );
final RenderMode mode = RenderMode.from( matcher.group( 1 ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public AdminToolHandler()
@Override
protected boolean canHandle( final WebRequest webRequest )
{
return webRequest.getRawPath().startsWith( AdminToolPortalHandler.ADMIN_TOOL_START );
return webRequest.getRawPath().equals( AdminToolPortalHandler.ADMIN_TOOL_BASE ) ||
webRequest.getRawPath().startsWith( AdminToolPortalHandler.ADMIN_TOOL_PREFIX );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
public class AdminToolPortalHandler
extends BasePortalHandler
{
public static final String ADMIN_TOOL_START = "/admin/tool";
public static final String ADMIN_TOOL_BASE = "/admin/tool";

public static final String ADMIN_TOOL_PREFIX = ADMIN_TOOL_START + "/";
public static final String ADMIN_TOOL_PREFIX = ADMIN_TOOL_BASE + "/";

public static final DescriptorKey DEFAULT_DESCRIPTOR_KEY = DescriptorKey.from( "com.enonic.xp.app.main:home" );

public static final Pattern PATTERN = Pattern.compile( "^([^/^_]+)/([^/^_]+)" );
private static final Pattern PATTERN = Pattern.compile( "^([^/]+)/([^/]+)" );

@Override
protected boolean canHandle( final WebRequest webRequest )
{
return webRequest.getRawPath().startsWith( ADMIN_TOOL_START );
return webRequest.getRawPath().equals( ADMIN_TOOL_BASE ) || webRequest.getRawPath().startsWith( ADMIN_TOOL_PREFIX );
}

@Override
Expand All @@ -43,12 +43,12 @@ protected PortalRequest createPortalRequest( final WebRequest webRequest, final
final DescriptorKey descriptorKey = getDescriptorKey( webRequest );
if ( descriptorKey == null )
{
portalRequest.setBaseUri( ADMIN_TOOL_START );
portalRequest.setBaseUri( ADMIN_TOOL_BASE );
portalRequest.setApplicationKey( DEFAULT_DESCRIPTOR_KEY.getApplicationKey() );
}
else
{
portalRequest.setBaseUri( ADMIN_TOOL_PREFIX + descriptorKey.getApplicationKey() + "/" + descriptorKey.getName() );
portalRequest.setBaseUri( ADMIN_TOOL_BASE + "/" + descriptorKey.getApplicationKey() + "/" + descriptorKey.getName() );
portalRequest.setApplicationKey( descriptorKey.getApplicationKey() );
}
portalRequest.setMode( RenderMode.ADMIN );
Expand All @@ -58,9 +58,15 @@ protected PortalRequest createPortalRequest( final WebRequest webRequest, final
public static DescriptorKey getDescriptorKey( final WebRequest webRequest )
{
final String path = webRequest.getRawPath();
if ( path.startsWith( ADMIN_TOOL_PREFIX ) )

if ( path.equals( ADMIN_TOOL_BASE ) )
{
return null;
}
else if ( path.startsWith( ADMIN_TOOL_PREFIX ) )
{
final String subPath = path.substring( ADMIN_TOOL_PREFIX.length() );
final int endpoint = path.indexOf( "/_/" );
final String subPath = path.substring( ADMIN_TOOL_PREFIX.length(), endpoint == -1 ? path.length() : endpoint + 1 );
final Matcher matcher = PATTERN.matcher( subPath );
if ( matcher.find() )
{
Expand All @@ -83,4 +89,4 @@ public void setExceptionRenderer( final ExceptionRenderer exceptionRenderer )
{
this.exceptionRenderer = exceptionRenderer;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class AdminToolHandlerTest

@BeforeEach
public final void setup()
throws Exception
{

this.adminToolDescriptorService = Mockito.mock( AdminToolDescriptorService.class );
Expand All @@ -70,7 +69,7 @@ public final void setup()
this.portalRequest.setMode( RenderMode.ADMIN );
final DescriptorKey defaultDescriptorKey = AdminToolPortalHandler.DEFAULT_DESCRIPTOR_KEY;
this.portalRequest.setBaseUri(
AdminToolPortalHandler.ADMIN_TOOL_PREFIX + defaultDescriptorKey.getApplicationKey() + "/" + defaultDescriptorKey.getName() );
AdminToolPortalHandler.ADMIN_TOOL_BASE + "/" + defaultDescriptorKey.getApplicationKey() + "/" + defaultDescriptorKey.getName() );
this.portalRequest.setApplicationKey( defaultDescriptorKey.getApplicationKey() );

this.webResponse = WebResponse.create().build();
Expand All @@ -94,7 +93,6 @@ public void testCannotHandle()

@Test
public void testWithoutPermissions()
throws Exception
{
this.portalRequest.setRawPath( "/admin/tool/webapp/tool/1" );
Mockito.when( this.rawRequest.isUserInRole( Mockito.anyString() ) ).thenReturn( false );
Expand All @@ -103,7 +101,6 @@ public void testWithoutPermissions()

@Test
public void testWithNoDescriptor()
throws Exception
{
Mockito.when( this.adminToolDescriptorService.getByKey( Mockito.any( DescriptorKey.class ) ) ).thenReturn( null );
this.portalRequest.setRawPath( "/admin/tool/webapp/tool/1" );
Expand All @@ -112,7 +109,6 @@ public void testWithNoDescriptor()

@Test
public void testWithNoAccessToApplication()
throws Exception
{
this.mockDescriptor( DescriptorKey.from( "app:tool" ), false );
this.portalRequest.setRawPath( "/admin/tool/webapp/tool/1" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,6 @@ public void setValidTicket( final Boolean validTicket )

public boolean isSiteBase()
{
return baseUri.startsWith( "/site" ) || baseUri.startsWith( "/admin/site" );
return baseUri.equals( "/site" ) || baseUri.startsWith( "/admin/site/" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ protected WebResponse doHandle( final WebRequest webRequest, final WebResponse w
{
return handleError( portalRequest, e );
}
finally
{
PortalRequestAccessor.remove();
}
}

protected abstract PortalRequest createPortalRequest( WebRequest webRequest, WebResponse webResponse );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import com.enonic.xp.web.WebException;
import com.enonic.xp.web.WebRequest;

import static com.google.common.base.Strings.isNullOrEmpty;

public abstract class BaseSiteHandler
extends BasePortalHandler
{
Expand All @@ -19,21 +17,46 @@ private static RepositoryId findRepository( final String baseSubPath )
final String result = baseSubPath.substring( 0, index > 0 ? index : baseSubPath.length() );
if ( result.isEmpty() )
{
throw WebException.notFound( "Repository needs to be specified" );
throw WebException.notFound( "Repository must be specified" );
}

try
{
return toRepositoryId( result );
}
catch ( IllegalArgumentException e )
{
throw WebException.notFound( String.format( "Repository name is invalid [%s]", result ) );
}
}

private static RepositoryId toRepositoryId( String result )
{
final RepositoryId repositoryId = RepositoryUtils.fromContentRepoName( result );
if ( repositoryId == null )
{
throw new IllegalArgumentException();
}
return RepositoryUtils.fromContentRepoName( result );
return repositoryId;
}

private static Branch findBranch( final String baseSubPath )
{
final String branchSubPath = findPathAfterRepository( baseSubPath );
final int index = branchSubPath.indexOf( '/' );
final String result = branchSubPath.substring( 0, index > 0 ? index : branchSubPath.length() );
if ( isNullOrEmpty( result ) )
if ( result.isEmpty() )
{
throw WebException.notFound( "Branch must be specified" );
}
try
{
return Branch.from( result );
}
catch ( IllegalArgumentException e )
{
throw WebException.notFound( "Branch needs to be specified" );
throw WebException.notFound( String.format( "Branch name is invalid [%s]", result ) );
}
return Branch.from( result );
}

private static ContentPath findContentPath( final String baseSubPath )
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.enonic.xp.app.ApplicationKey;
import com.enonic.xp.portal.PortalRequest;
import com.enonic.xp.portal.handler.BasePortalHandler;
import com.enonic.xp.web.WebException;
import com.enonic.xp.web.WebRequest;
import com.enonic.xp.web.WebResponse;
import com.enonic.xp.web.exception.ExceptionMapper;
Expand All @@ -19,24 +20,27 @@
public class AppPortalHandler
extends BasePortalHandler
{
public static final Pattern PATTERN = Pattern.compile( "(/webapp/([^/]+))(?:/.*)?" );
private static final String WEBAPP_PREFIX = "/webapp/";
private static final Pattern PATTERN = Pattern.compile( "^/webapp/([^/]+)" );

@Override
protected boolean canHandle( final WebRequest webRequest )
{
return PATTERN.matcher( webRequest.getRawPath() ).
matches();
return webRequest.getRawPath().startsWith( WEBAPP_PREFIX );
}

@Override
protected PortalRequest createPortalRequest( final WebRequest webRequest, final WebResponse webResponse )
{
final Matcher matcher = PATTERN.matcher( webRequest.getRawPath() );
matcher.matches();
if ( !matcher.find() )
{
throw WebException.notFound( "Application must be specified" );
}

final PortalRequest portalRequest = new PortalRequest( webRequest );
portalRequest.setBaseUri( matcher.group( 1 ) );
portalRequest.setApplicationKey( ApplicationKey.from( matcher.group( 2 ) ) );
portalRequest.setBaseUri( matcher.group( 0 ) );
portalRequest.setApplicationKey( ApplicationKey.from( matcher.group( 1 ) ) );
return portalRequest;
}

Expand All @@ -51,4 +55,4 @@ public void setExceptionRenderer( final ExceptionRenderer exceptionRenderer )
{
this.exceptionRenderer = exceptionRenderer;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public PortalResponse render( final WebRequest webRequest, final WebException ca
return defaultCustomError;
}

if ( "/site".equals( portalRequest.getBaseUri() ) && ContentConstants.BRANCH_MASTER.equals( portalRequest.getBranch() ) &&
if ( portalRequest.isSiteBase() && ContentConstants.BRANCH_MASTER.equals( portalRequest.getBranch() ) &&
HttpStatus.NOT_FOUND.equals( cause.getStatus() ) )
{
tip = "Tip: Did you remember to publish the site?";
Expand Down

0 comments on commit c80742c

Please sign in to comment.