Skip to content

Commit

Permalink
Migrate Admin Event API to Universal API #10762
Browse files Browse the repository at this point in the history
  • Loading branch information
anatol-sialitski committed Nov 13, 2024
1 parent fb86601 commit 7fd5d51
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 9 deletions.
2 changes: 1 addition & 1 deletion modules/admin/admin-event/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dependencies {
implementation project( ':web:web-api' )
api project( ':portal:portal-api' )
implementation project( ':core:core-internal' )

testImplementation( testFixtures( project(":web:web-jetty") ) )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.enonic.xp.admin.event.impl;

import java.util.List;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import com.enonic.xp.portal.universalapi.UniversalApiHandler;
import com.enonic.xp.web.HttpStatus;
import com.enonic.xp.web.WebRequest;
import com.enonic.xp.web.WebResponse;
import com.enonic.xp.web.websocket.EndpointFactory;
import com.enonic.xp.web.websocket.WebSocketConfig;
import com.enonic.xp.web.websocket.WebSocketService;

@Component(immediate = true, service = UniversalApiHandler.class, property = {"applicationKey=admin", "apiKey=event",
"displayName=Event API", "allowedPrincipals=role:system.admin.login", "mount=true"})
public class EventApiHandler
implements UniversalApiHandler
{
private final WebSocketService webSocketService;

private final EndpointFactory endpointFactory;

@Activate
public EventApiHandler( @Reference final WebSocketService webSocketService,
@Reference/*(service = EventEndpointFactory.class)*/ final EndpointFactory endpointFactory )
{
this.webSocketService = webSocketService;
this.endpointFactory = endpointFactory;
}

@Override
public WebResponse handle( final WebRequest request )
{
final WebResponse.Builder<?> responseBuilder = WebResponse.create();

if ( !webSocketService.isUpgradeRequest( request.getRawRequest(), null ) )
{
responseBuilder.status( HttpStatus.FORBIDDEN );
return responseBuilder.build();
}

final WebSocketConfig webSocketConfig = new WebSocketConfig();
webSocketConfig.setSubProtocols( List.of( "text" ) );

responseBuilder.webSocket( webSocketConfig );

return responseBuilder.build();
}

@Override
public EndpointFactory getEndpointFactory()
{
return endpointFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.Locale;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -39,6 +41,41 @@ public String getBaseUri()
return ServletRequestUrlHelper.createUri( ServletRequestHolder.getRequest(), ADMIN_URI_PREFIX );
}

private String subPath( final String requestURI, final String prefix )
{
final int endpoint = requestURI.indexOf( "/_/" );
final int endIndex = endpoint == -1 ? requestURI.length() : endpoint + 1;
return requestURI.substring( prefix.length(), endIndex );
}

public String getBaseUriNew()
{
final String requestURI = ServletRequestHolder.getRequest().getRequestURI();

String path = null;
if ( requestURI.equals( "/admin" ) )
{
path = "/admin/com.enonic.xp.app.main/home";
}
else
{
final Pattern TOOL_CXT_PATTERN = Pattern.compile( "^([^/]+)/([^/]+)" );
final String subPath = subPath( requestURI, "/admin/" );
final Matcher matcher = TOOL_CXT_PATTERN.matcher( subPath );
if ( matcher.find() )
{
path = "/admin/" + matcher.group( 0 );
}
}

if ( path == null )
{
throw new IllegalArgumentException( String.format( "Invalid tool context: %s", requestURI ) );
}

return ServletRequestUrlHelper.createUri( ServletRequestHolder.getRequest(), path );
}

public String getAssetsUri()
{
return ServletRequestUrlHelper.createUri( ServletRequestHolder.getRequest(), ADMIN_ASSETS_URI_PREFIX + this.version );
Expand All @@ -54,12 +91,14 @@ public String generateAdminToolUri( String application, String adminTool )
return this.adminToolDescriptorService.get().generateAdminToolUri( application, adminTool );
}

public String getHomeAppName() {
public String getHomeAppName()
{
return ADMIN_APP_NAME;
}

public String getLauncherToolUrl() {
return generateAdminToolUri(ADMIN_APP_NAME, "launcher");
public String getLauncherToolUrl()
{
return generateAdminToolUri( ADMIN_APP_NAME, "launcher" );
}

public String getLocale()
Expand Down
11 changes: 11 additions & 0 deletions modules/lib/lib-admin/src/main/resources/lib/xp/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ interface AdminLibHelper {

getBaseUri(): string;

getBaseUriNew(): string;

getHomeToolUri(): string;

getInstallation(): string;
Expand All @@ -58,6 +60,15 @@ export function getBaseUri(): string {
return helper.getBaseUri();
}

/**
* Returns the base uri.
*
* @returns {string} Admin base uri.
*/
export function getBaseUriNew(): string {
return helper.getBaseUriNew();
}

/**
* Returns the admin assets uri.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.enonic.xp.web.WebRequest;
import com.enonic.xp.web.WebResponse;
import com.enonic.xp.web.websocket.EndpointFactory;
import com.enonic.xp.web.websocket.WebSocketEvent;

public interface UniversalApiHandler
Expand All @@ -11,4 +12,9 @@ public interface UniversalApiHandler
default void onSocketEvent( WebSocketEvent event )
{
}

default EndpointFactory getEndpointFactory()
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.enonic.xp.portal.universalapi.UniversalApiHandler;
import com.enonic.xp.web.WebRequest;
import com.enonic.xp.web.WebResponse;
import com.enonic.xp.web.websocket.EndpointFactory;
import com.enonic.xp.web.websocket.WebSocketEvent;

public final class DynamicUniversalApiHandler
Expand Down Expand Up @@ -31,6 +32,12 @@ public void onSocketEvent( final WebSocketEvent event )
apiHandler.onSocketEvent( event );
}

@Override
public EndpointFactory getEndpointFactory()
{
return apiHandler.getEndpointFactory();
}

public ApiDescriptor getApiDescriptor()
{
return apiDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.function.Supplier;

import com.enonic.xp.portal.universalapi.UniversalApiHandler;
import com.enonic.xp.web.websocket.EndpointFactory;
import com.enonic.xp.web.websocket.WebSocketConfig;
import com.enonic.xp.web.websocket.WebSocketEndpoint;
import com.enonic.xp.web.websocket.WebSocketEvent;
Expand Down Expand Up @@ -31,4 +32,9 @@ public void onEvent( final WebSocketEvent event )
{
this.apiHandlerSupplier.get().onSocketEvent( event );
}

public EndpointFactory getEndpointFactory()
{
return this.apiHandlerSupplier.get().getEndpointFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.enonic.xp.web.websocket.EndpointFactory;
import com.enonic.xp.web.websocket.WebSocketContext;
import com.enonic.xp.web.websocket.WebSocketEndpoint;
import com.enonic.xp.web.websocket.WebSocketService;
Expand All @@ -24,10 +25,17 @@ final class WebSocketContextImpl
public boolean apply( final WebSocketEndpoint endpoint )
throws IOException
{
final EndpointFactoryImpl factory = new EndpointFactoryImpl();
factory.registry = this.registry;
factory.endpoint = endpoint;

return this.webSocketService.acceptWebSocket( this.request, this.response, factory );
if ( endpoint instanceof WebSocketApiEndpointImpl )
{
EndpointFactory factory = ( (WebSocketApiEndpointImpl) endpoint ).getEndpointFactory();
return this.webSocketService.acceptWebSocket( this.request, this.response, factory );
}
else
{
final EndpointFactoryImpl factory = new EndpointFactoryImpl();
factory.registry = this.registry;
factory.endpoint = endpoint;
return this.webSocketService.acceptWebSocket( this.request, this.response, factory );
}
}
}

0 comments on commit 7fd5d51

Please sign in to comment.