Skip to content

Commit

Permalink
Remove Unnamed API #10717 (#10720)
Browse files Browse the repository at this point in the history
Remove Unnamed API #10717
  • Loading branch information
anatol-sialitski authored Oct 14, 2024
1 parent 9c433d9 commit cdec22e
Show file tree
Hide file tree
Showing 32 changed files with 162 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class WidgetDescriptor

private final ImmutableMap<String, String> config;

private static final String URL_PREFIX = "_/admin/widget/";
private static final String URL_PREFIX = "_/admin:widget/";

private WidgetDescriptor( final Builder builder )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@Component(immediate = true, service = WidgetApiHandler.class)
public class WidgetApiHandler
{
private static final Pattern WIDGET_API_PATTERN = Pattern.compile( "^/(_|api)/admin/widget/(?<appKey>[^/]+)/(?<widgetKey>[^/]+)" );
private static final Pattern WIDGET_API_PATTERN = Pattern.compile( "^/(_|api)/admin:widget/(?<appKey>[^/]+)/(?<widgetKey>[^/]+)" );

private static final Pattern TOOL_PREFIX_PATTERN = Pattern.compile( "^/admin/(?<appKey>[^/]+)/(?<toolName>[^/]+)" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.google.common.collect.Multimap;

import com.enonic.xp.web.HttpMethod;
import com.enonic.xp.web.WebException;
import com.enonic.xp.web.WebRequest;
import com.enonic.xp.web.WebResponse;
Expand All @@ -19,7 +20,7 @@
public class WidgetDispatcherApiHandler
implements UniversalApiHandler
{
private static final Pattern WIDGET_API_PATTERN = Pattern.compile( "^/(_|api)/admin/widget/?$" );
private static final Pattern WIDGET_API_PATTERN = Pattern.compile( "^/(_|api)/admin:widget/?$" );

private final GetListAllowedWidgetsHandler listWidgetsHandler;

Expand All @@ -43,7 +44,7 @@ public WebResponse handle( final WebRequest webRequest )
{
final String path = Objects.requireNonNullElse( webRequest.getEndpointPath(), webRequest.getRawPath() );

if ( WIDGET_API_PATTERN.matcher( path ).matches() )
if ( WIDGET_API_PATTERN.matcher( path ).matches() && HttpMethod.GET.equals( webRequest.getMethod() ) )
{
final Multimap<String, String> params = webRequest.getParams();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,14 @@ private ApiMountDescriptor toApiMountDescriptor( final DomElement apiElement )
if ( !apiMount.contains( ":" ) )
{
builder.applicationKey( this.currentApplication );
if ( !apiMount.isBlank() )
{
builder.apiKey( apiMount );
}
builder.apiKey( apiMount );
}
else
{
final String[] parts = apiMount.split( ":", 2 );

builder.applicationKey( resolveApplicationKey( parts[APPLICATION_KEY_INDEX].trim() ) );
final String apiKey = parts[API_KEY_INDEX].trim();
if ( !apiKey.isBlank() )
{
builder.apiKey( apiKey );
}
builder.apiKey( parts[API_KEY_INDEX].trim() );
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ void testInvalidApplicationKey()
{
final WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/<app>/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin/widget/<app>/widgetName" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/<app>/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin:widget/<app>/widgetName" );

IllegalArgumentException ex = assertThrows( IllegalArgumentException.class, () -> this.handler.handle( webRequest ) );
assertEquals( "Invalid application key: <app>", ex.getMessage() );
Expand All @@ -85,8 +85,8 @@ void testNoWidgetDescriptor()

final WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin/widget/app/widgetName" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin:widget/app/widgetName" );

WebException ex = assertThrows( WebException.class, () -> this.handler.handle( webRequest ) );
assertEquals( "Widget [app:widgetName] not found", ex.getMessage() );
Expand All @@ -106,8 +106,8 @@ void testWidgetDescriptorAccessDenied()

final WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin/widget/app/widgetName" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin:widget/app/widgetName" );

WebException ex = assertThrows( WebException.class, () -> this.handler.handle( webRequest ) );
assertEquals( HttpStatus.UNAUTHORIZED, ex.getStatus() );
Expand All @@ -126,8 +126,8 @@ void testHandle()

final WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin/widget/app/widgetName" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/path/_/admin:widget/app/widgetName" );

final ControllerScript controllerScript = mock( ControllerScript.class );
when( controllerScript.execute( any( PortalRequest.class ) ) ).thenReturn( PortalResponse.create().build() );
Expand Down Expand Up @@ -159,8 +159,8 @@ void testVerifyMounts()

final WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/admin/myapp/toolName/_/admin/widget/app/widgetName" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/admin/myapp/toolName/_/admin:widget/app/widgetName" );

final ControllerScript controllerScript = mock( ControllerScript.class );
when( controllerScript.execute( any( PortalRequest.class ) ) ).thenReturn( PortalResponse.create().build() );
Expand Down Expand Up @@ -192,8 +192,8 @@ void testWidgetDoesNotMountedToAdminTool()

final WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/admin/myapp/toolName/_/admin/widget/app/widgetName" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/app/widgetName" );
when( webRequest.getRawPath() ).thenReturn( "/admin/myapp/toolName/_/admin:widget/app/widgetName" );

final ControllerScript controllerScript = mock( ControllerScript.class );
when( controllerScript.execute( any( PortalRequest.class ) ) ).thenReturn( PortalResponse.create().build() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import com.enonic.xp.web.HttpMethod;
import com.enonic.xp.web.HttpStatus;
import com.enonic.xp.web.WebException;
import com.enonic.xp.web.WebRequest;
Expand All @@ -27,7 +28,8 @@ void testHandle()
WidgetDispatcherApiHandler handler = new WidgetDispatcherApiHandler( listWidgetsHandler, getWidgetIconHandler, widgetApiHandler );

WebRequest webRequest = mock( WebRequest.class );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget" );
when( webRequest.getMethod() ).thenReturn( HttpMethod.GET );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget" );
when( webRequest.getParams() ).thenReturn( HashMultimap.create() );

WebException ex = assertThrows( WebException.class, () -> handler.handle( webRequest ) );
Expand All @@ -54,7 +56,7 @@ void testHandle()
assertEquals( response, handler.handle( webRequest ) );

// widget harmonized api
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin/widget/myapp/mywidget" );
when( webRequest.getEndpointPath() ).thenReturn( "/_/admin:widget/myapp/mywidget" );
when( webRequest.getParams() ).thenReturn( null );
when( widgetApiHandler.handle( webRequest ) ).thenReturn( response );
assertEquals( response, handler.handle( webRequest ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,15 @@ public void testParseWithApis()

assertResult( toolDescriptor );

assertEquals( 4, toolDescriptor.getApiMounts().getSize() );
assertEquals( 2, toolDescriptor.getApiMounts().getSize() );

final ApiMountDescriptor apiMountDescriptor1 = toolDescriptor.getApiMounts().get( 0 );
assertEquals( ApplicationKey.from( "com.enonic.app.myapp" ), apiMountDescriptor1.getApplicationKey() );
assertEquals( "api-key", apiMountDescriptor1.getApiKey() );

final ApiMountDescriptor apiMountDescriptor2 = toolDescriptor.getApiMounts().get( 1 );
assertEquals( ApplicationKey.from( "com.enonic.app.myapp" ), apiMountDescriptor2.getApplicationKey() );
assertEquals( "", apiMountDescriptor2.getApiKey() );

final ApiMountDescriptor apiMountDescriptor3 = toolDescriptor.getApiMounts().get( 2 );
final ApiMountDescriptor apiMountDescriptor3 = toolDescriptor.getApiMounts().get( 1 );
assertEquals( ApplicationKey.from( "myapplication" ), apiMountDescriptor3.getApplicationKey() );
assertEquals( "api-key", apiMountDescriptor3.getApiKey() );

final ApiMountDescriptor apiMountDescriptor4 = toolDescriptor.getApiMounts().get( 3 );
assertEquals( ApplicationKey.from( "myapplication" ), apiMountDescriptor4.getApplicationKey() );
assertEquals( "", apiMountDescriptor4.getApiKey() );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void assertResult()
assertEquals( "My widget", result.getDisplayName() );
assertEquals( "My widget description", result.getDescription() );
assertEquals( "myapplication:mywidget", result.getKeyString() );
assertEquals( "_/admin/widget/myapplication/mywidget", result.getUrl() );
assertEquals( "_/admin:widget/myapplication/mywidget", result.getUrl() );
assertEquals( "key.display-name", result.getDisplayNameI18nKey() );
assertEquals( "key.description", result.getDescriptionI18nKey() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
</allow>
<apis>
<api>com.enonic.app.myapp:api-key</api>
<api>com.enonic.app.myapp:</api>
<api>api-key</api>
<api/>
</apis>
</tool>
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ public boolean isAccessAllowed( final PrincipalKeys principalKeys )

public static ResourceKey toResourceKey( final DescriptorKey key, final String extension )
{
if ( "".equals( key.getName() ) )
{
return ResourceKey.from( key.getApplicationKey(), "apis/api." + extension );
}
else
{
return ResourceKey.from( key.getApplicationKey(), "apis/" + key.getName() + "/" + key.getName() + "." + extension );
}
return ResourceKey.from( key.getApplicationKey(), "apis/" + key.getName() + "/" + key.getName() + "." + extension );
}

public static Builder create()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package com.enonic.xp.api;

import java.util.Objects;
import com.google.common.base.Preconditions;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.app.ApplicationKey;

@PublicApi
public final class ApiMountDescriptor
{
private static final String DEFAULT_API_KEY = "";

private final ApplicationKey applicationKey;

private final String apiKey;

private ApiMountDescriptor( final Builder builder )
{
this.applicationKey = Objects.requireNonNull( builder.applicationKey );
this.apiKey = Objects.requireNonNullElse( builder.apiKey, DEFAULT_API_KEY );
this.applicationKey = builder.applicationKey;
this.apiKey = builder.apiKey;
}

public ApplicationKey getApplicationKey()
Expand Down Expand Up @@ -60,6 +58,8 @@ public Builder apiKey( final String apiKey )

public ApiMountDescriptor build()
{
Preconditions.checkArgument( applicationKey != null, "applicationKey must be set." );
Preconditions.checkArgument( apiKey != null && !apiKey.isBlank(), "apiKey must be set." );
return new ApiMountDescriptor( this );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,14 @@ private ApiMountDescriptor toApiMountDescriptor( final DomElement apiElement )
if ( !apiMount.contains( ":" ) )
{
builder.applicationKey( this.currentApplication );
if ( !apiMount.isBlank() )
{
builder.apiKey( apiMount );
}
builder.apiKey( apiMount );
}
else
{
final String[] parts = apiMount.split( ":", 2 );

builder.applicationKey( resolveApplicationKey( parts[APPLICATION_KEY_INDEX].trim() ) );
final String apiKey = parts[API_KEY_INDEX].trim();
if ( !apiKey.isBlank() )
{
builder.apiKey( apiKey );
}
builder.apiKey( parts[API_KEY_INDEX].trim() );
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,14 @@ private ApiMountDescriptor toApiMountDescriptor( final DomElement apiElement )
if ( !apiMount.contains( ":" ) )
{
builder.applicationKey( this.currentApplication );
if ( !apiMount.isBlank() )
{
builder.apiKey( apiMount );
}
builder.apiKey( apiMount );
}
else
{
final String[] parts = apiMount.split( ":", 2 );

builder.applicationKey( resolveApplicationKey( parts[APPLICATION_KEY_INDEX].trim() ) );
final String apiKey = parts[API_KEY_INDEX].trim();
if ( !apiKey.isBlank() )
{
builder.apiKey( apiKey );
}
builder.apiKey( parts[API_KEY_INDEX].trim() );
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void testSiteXmlWithApis()
assertEquals( 0, siteDescriptor.getXDataMappings().getSize() );
assertEquals( 0, siteDescriptor.getResponseProcessors().getSize() );
assertEquals( 0, siteDescriptor.getMappingDescriptors().getSize() );
assertEquals( 5, siteDescriptor.getApiDescriptors().getSize() );
assertEquals( 4, siteDescriptor.getApiDescriptors().getSize() );

ApiMountDescriptor apiDescriptor1 = siteDescriptor.getApiDescriptors().get( 0 );
assertEquals( "myapi1", apiDescriptor1.getApiKey() );
Expand All @@ -283,11 +283,8 @@ public void testSiteXmlWithApis()
assertEquals( "myapi3", apiDescriptor3.getApiKey() );

ApiMountDescriptor apiDescriptor4 = siteDescriptor.getApiDescriptors().get( 3 );
assertEquals( "", apiDescriptor4.getApiKey() );

ApiMountDescriptor apiDescriptor5 = siteDescriptor.getApiDescriptors().get( 4 );
assertEquals( ApplicationKey.from( "com.enonic.app.external" ), apiDescriptor5.getApplicationKey() );
assertEquals( "myapi", apiDescriptor5.getApiKey() );
assertEquals( ApplicationKey.from( "com.enonic.app.external" ), apiDescriptor4.getApplicationKey() );
assertEquals( "myapi", apiDescriptor4.getApiKey() );
}

private String loadTestFile( final String fileName, Charset charset )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,23 @@ public void testWebappXmlDeserialization()
final WebappDescriptor webappDescriptor = builder.build();

assertEquals( applicationKey, webappDescriptor.getApplicationKey() );
assertEquals( 5, webappDescriptor.getApiMounts().getSize() );
assertEquals( 4, webappDescriptor.getApiMounts().getSize() );

final ApiMountDescriptor apiMountDescriptor1 = webappDescriptor.getApiMounts().get( 0 );
assertEquals( ApplicationKey.from( "com.enonic.app.myapp" ), apiMountDescriptor1.getApplicationKey() );
assertEquals( "api-key", apiMountDescriptor1.getApiKey() );
assertEquals( "api-key1", apiMountDescriptor1.getApiKey() );

final ApiMountDescriptor apiMountDescriptor2 = webappDescriptor.getApiMounts().get( 1 );
assertEquals( ApplicationKey.from( "com.enonic.app.myapp" ), apiMountDescriptor2.getApplicationKey() );
assertEquals( "", apiMountDescriptor2.getApiKey() );
assertEquals( "api-key2", apiMountDescriptor2.getApiKey() );

final ApiMountDescriptor apiMountDescriptor3 = webappDescriptor.getApiMounts().get( 2 );
assertEquals( applicationKey, apiMountDescriptor3.getApplicationKey() );
assertEquals( "api-key", apiMountDescriptor3.getApiKey() );

final ApiMountDescriptor apiMountDescriptor4 = webappDescriptor.getApiMounts().get( 3 );
assertEquals( applicationKey, apiMountDescriptor4.getApplicationKey() );
assertEquals( "", apiMountDescriptor4.getApiKey() );

final ApiMountDescriptor apiMountDescriptor5 = webappDescriptor.getApiMounts().get( 4 );
assertEquals( ApplicationKey.from( "com.enonic.app.myapp2" ), apiMountDescriptor5.getApplicationKey() );
assertEquals( "api-key", apiMountDescriptor5.getApiKey() );
assertEquals( ApplicationKey.from( "com.enonic.app.myapp2" ), apiMountDescriptor4.getApplicationKey() );
assertEquals( "api-key", apiMountDescriptor4.getApiKey() );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<api>myapi1</api>
<api>myapi2</api>
<api>myapi3</api>
<api/>
<api>com.enonic.app.external:myapi</api>
</apis>
</site>
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<webapp xmlns="urn:enonic:xp:model:1.0">
<apis>
<api>com.enonic.app.myapp:api-key</api>
<api>com.enonic.app.myapp:</api>
<api>com.enonic.app.myapp:api-key1</api>
<api>com.enonic.app.myapp:api-key2</api>
<api>api-key</api>
<api/>
<api>com.enonic.app.myapp2: api-key</api>
</apis>
</webapp>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const url = portalLib.apiUrl({

const unnamedApiUrl = portalLib.apiUrl({
application: 'com.enonic.app.myapp',
api: 'myapi',
path: ['mypath', 'myotherpath']
});

Expand All @@ -25,5 +26,5 @@ const apiUrl = portalLib.apiUrl({
// END

assert.assertEquals('ApiUrlParams{type=server, params={a=[1], b=[2]}, api=myapi, application=com.enonic.app.myapp, path=mypath/subpath}', url);
assert.assertEquals('ApiUrlParams{type=server, params={}, application=com.enonic.app.myapp, pathSegments=[mypath, myotherpath]}', unnamedApiUrl);
assert.assertEquals('ApiUrlParams{type=server, params={}, api=myapi, application=com.enonic.app.myapp, pathSegments=[mypath, myotherpath]}', unnamedApiUrl);
assert.assertEquals('ApiUrlParams{type=server, params={}, api=myapi, application=com.enonic.app.myapp}', apiUrl);
Loading

0 comments on commit cdec22e

Please sign in to comment.