Skip to content

Commit

Permalink
Out of scope for system provider #10478
Browse files Browse the repository at this point in the history
  • Loading branch information
rymsha committed Apr 2, 2024
1 parent 27d5cb3 commit 8c83169
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 );

Check warning on line 46 in modules/admin/admin-impl/src/main/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/admin/admin-impl/src/main/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandler.java#L46

Added line #L46 was not covered by tests
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() );

Check warning on line 51 in modules/admin/admin-impl/src/main/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/admin/admin-impl/src/main/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandler.java#L51

Added line #L51 was not covered by tests
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;

Check warning on line 64 in modules/admin/admin-impl/src/main/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/admin/admin-impl/src/main/java/com/enonic/xp/admin/impl/portal/AdminToolPortalHandler.java#L64

Added line #L64 was not covered by tests
}
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" );

Check warning on line 20 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L20

Added line #L20 was not covered by tests
}

try
{
return toRepositoryId( result );
}
catch ( IllegalArgumentException e )

Check warning on line 27 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L27

Added line #L27 was not covered by tests
{
throw WebException.notFound( String.format( "Repository name is invalid [%s]", result ) );

Check warning on line 29 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L29

Added line #L29 was not covered by tests
}
}

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

Check warning on line 38 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L38

Added line #L38 was not covered by tests
}
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" );

Check warning on line 50 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L50

Added line #L50 was not covered by tests
}
try
{
return Branch.from( result );
}
catch ( IllegalArgumentException e )

Check warning on line 56 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L56

Added line #L56 was not covered by tests
{
throw WebException.notFound( "Branch needs to be specified" );
throw WebException.notFound( String.format( "Branch name is invalid [%s]", result ) );

Check warning on line 58 in modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java

View check run for this annotation

Codecov / codecov/patch

modules/portal/portal-api/src/main/java/com/enonic/xp/portal/handler/BaseSiteHandler.java#L58

Added line #L58 was not covered by tests
}
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 8c83169

Please sign in to comment.