Skip to content

Commit

Permalink
ApiDescriptor rename slash-api to mount #10755
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Nov 4, 2024
1 parent 23b49d1 commit d58a990
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public final class ApiDescriptor

private final String documentationUrl;

private final boolean slashApi;
private final boolean mount;

private ApiDescriptor( final Builder builder )
{
Preconditions.checkNotNull( builder.key, "key cannot be null" );

this.key = builder.key;
this.allowedPrincipals = builder.allowedPrincipals;
this.allowedPrincipals = Objects.requireNonNullElseGet( builder.allowedPrincipals, () -> PrincipalKeys.from( RoleKeys.EVERYONE ) );
this.displayName = builder.displayName;
this.description = builder.description;
this.documentationUrl = builder.documentationUrl;
this.slashApi = Objects.requireNonNullElse( builder.slashApi, true );
this.mount = Objects.requireNonNullElse( builder.mount, false );
}

public DescriptorKey getKey()
Expand Down Expand Up @@ -62,15 +62,14 @@ public String getDocumentationUrl()
return documentationUrl;
}

public Boolean isSlashApi()
public boolean isMount()
{
return slashApi;
return mount;
}

public boolean isAccessAllowed( final PrincipalKeys principalKeys )
{
return allowedPrincipals == null || principalKeys.contains( RoleKeys.ADMIN ) ||
allowedPrincipals.stream().anyMatch( principalKeys::contains );
return allowedPrincipals.stream().anyMatch( principalKeys::contains );
}

public static ResourceKey toResourceKey( final DescriptorKey key, final String extension )
Expand All @@ -95,7 +94,7 @@ public static final class Builder

private String documentationUrl;

private Boolean slashApi;
private Boolean mount;

private Builder()
{
Expand Down Expand Up @@ -131,9 +130,9 @@ public Builder documentationUrl( final String documentationUrl )
return this;
}

public Builder slashApi( final Boolean useInSlashApi )
public Builder mount( final Boolean mount )
{
this.slashApi = useInSlashApi;
this.mount = mount;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@
<xs:element minOccurs="0" name="display-name" type="xs:string"/>
<xs:element minOccurs="0" name="description" type="xs:string"/>
<xs:element minOccurs="0" name="documentation-url" type="xs:string"/>
<xs:element minOccurs="0" name="slash-api" type="xs:boolean"/>
<xs:element minOccurs="0" name="mount" type="xs:boolean"/>
<xs:element minOccurs="0" name="allow">
<xs:complexType>
<xs:sequence>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private ApiDescriptor createDynamicApiDescriptor( final Map<String, ?> propertie
}
if ( properties.get( "slashApi" ) != null )
{
builder.slashApi( Boolean.valueOf( properties.get( "slashApi" ).toString() ) );
builder.mount( Boolean.valueOf( properties.get( "slashApi" ).toString() ) );
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void doParse( final DomElement root )
final String useInSlashApi = root.getChildValueTrimmed( SLASH_API_TAG_NAME );
if ( useInSlashApi != null )
{
builder.slashApi( Boolean.valueOf( useInSlashApi ) );
builder.mount( Boolean.valueOf( useInSlashApi ) );
}

DomElement allowedPrincipals = root.getChild( ALLOW_TAG_NAME );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private boolean verifyRequestMounted( final ApiDescriptor apiDescriptor, final P

if ( portalRequest.getEndpointPath() == null )
{
return rawPath.startsWith( "/api/" ) && apiDescriptor.isSlashApi();
return rawPath.startsWith( "/api/" ) && apiDescriptor.isMount();
}
else if ( rawPath.startsWith( "/site/" ) || rawPath.startsWith( "/admin/site/" ) )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private Map<String, Object> map( final ApiDescriptor apiDescriptor )
result.put( "displayName", apiDescriptor.getDisplayName() );
result.put( "description", apiDescriptor.getDescription() );
result.put( "documentationUrl", apiDescriptor.getDocumentationUrl() );
result.put( "slashApi", apiDescriptor.isSlashApi() );
result.put( "mount", apiDescriptor.isMount() );

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void assertResult()
assertEquals( "My API", result.getDisplayName() );
assertEquals( "This is my API", result.getDescription() );
assertEquals( "https://apis.enonic.com", result.getDocumentationUrl() );
assertFalse( result.isSlashApi() );
assertFalse( result.isMount() );

final PrincipalKeys allowedPrincipals = result.getAllowedPrincipals();
assertNotNull( allowedPrincipals );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ void testHandleApi()
ApiDescriptor apiDescriptor = ApiDescriptor.create()
.key( DescriptorKey.from( ApplicationKey.from( "com.enonic.app.myapp" ), "api-key" ) )
.allowedPrincipals( null )
.mount( true )
.build();

when( apiDescriptorService.getByKey( any( DescriptorKey.class ) ) ).thenReturn( apiDescriptor );
Expand All @@ -226,7 +227,7 @@ void testHandleApiWhenApiDisabled()
ApiDescriptor apiDescriptor = ApiDescriptor.create()
.key( DescriptorKey.from( ApplicationKey.from( "com.enonic.app.myapp" ), "api-key" ) )
.allowedPrincipals( null )
.slashApi( false )
.mount( false )
.build();

when( apiDescriptorService.getByKey( any( DescriptorKey.class ) ) ).thenReturn( apiDescriptor );
Expand Down Expand Up @@ -273,6 +274,7 @@ void testHandleApiError()
ApiDescriptor apiDescriptor = ApiDescriptor.create()
.key( DescriptorKey.from( ApplicationKey.from( "com.enonic.app.myapp" ), "api-key" ) )
.allowedPrincipals( null )
.mount( true )
.build();

when( apiDescriptorService.getByKey( any( DescriptorKey.class ) ) ).thenReturn( apiDescriptor );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.enonic.xp.page.DescriptorKey;
import com.enonic.xp.portal.impl.api.ApiConfig;
import com.enonic.xp.portal.impl.api.DynamicUniversalApiHandlerRegistry;
import com.enonic.xp.security.RoleKeys;
import com.enonic.xp.server.RunMode;
import com.enonic.xp.web.HttpStatus;
import com.enonic.xp.web.WebRequest;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void testDoHandle()
universalApiHandlerRegistry.addApiHandler( request -> WebResponse.create().build(),
Map.of( "applicationKey", "admin", "apiKey", "widget", "displayName", "Display Name",
"description", "Brief description", "documentationUrl",
"https://docs.enonic.com", "slashApi", "true" ) );
"https://docs.enonic.com", "mount", "true" ) );

WebResponse webResponse =
this.handler.doHandle( mock( WebRequest.class ), mock( WebResponse.class ), mock( WebHandlerChain.class ) );
Expand All @@ -144,14 +145,14 @@ public void testDoHandle()
assertEquals( "Display Name", dynamicApiResource.get( "displayName" ) );
assertEquals( "Brief description", dynamicApiResource.get( "description" ) );
assertEquals( "https://docs.enonic.com", dynamicApiResource.get( "documentationUrl" ) );
assertTrue( (boolean) dynamicApiResource.get( "slashApi" ) );
assertFalse( (boolean) dynamicApiResource.get( "mount" ) );

final Map<String, Object> apiResource = resources.get( 1 );

assertEquals( "myapplication:myapi", apiResource.get( "descriptor" ) );
assertEquals( "myapplication", apiResource.get( "application" ) );
assertEquals( "myapi", apiResource.get( "name" ) );
assertEquals( List.of(), apiResource.get( "allowedPrincipals" ) );
assertEquals( List.of( RoleKeys.EVERYONE.toString() ), apiResource.get( "allowedPrincipals" ) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<display-name>My API</display-name>
<description>This is my API</description>
<documentation-url>https://apis.enonic.com</documentation-url>
<slash-api>false</slash-api>
<mount>false</mount>
<allow>
<principal>role:system.admin</principal>
</allow>
Expand Down

0 comments on commit d58a990

Please sign in to comment.