2022-06-10 17:06:28 +02:00
package org.gcube.common.core.publisher.is.legacy.application ;
import java.net.URI ;
import java.util.Arrays ;
import java.util.Calendar ;
import java.util.List ;
2024-03-12 17:28:51 +01:00
import jakarta.servlet.ServletRegistration ;
2022-06-10 17:06:28 +02:00
import org.gcube.common.resources.gcore.GCoreEndpoint ;
2022-06-24 15:55:05 +02:00
import org.gcube.smartgears.configuration.ProxyAddress ;
2022-06-10 17:06:28 +02:00
import org.gcube.smartgears.configuration.application.ApplicationConfiguration ;
import org.gcube.smartgears.configuration.container.ContainerConfiguration ;
import org.gcube.smartgears.context.application.ApplicationContext ;
2022-06-24 15:55:05 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2022-06-10 17:06:28 +02:00
public class ApplicationProfileBuilder {
private static List < String > servletExcludes = Arrays . asList ( " default " , " jsp " ) ;
2022-06-24 15:55:05 +02:00
private static final Logger log = LoggerFactory . getLogger ( ApplicationProfileBuilder . class ) ;
2022-06-10 17:06:28 +02:00
private ApplicationContext context ;
public ApplicationProfileBuilder ( ApplicationContext context ) {
this . context = context ;
}
public GCoreEndpoint create ( ) {
GCoreEndpoint endpoint = new GCoreEndpoint ( ) ;
2022-06-10 18:41:20 +02:00
endpoint . setId ( context . id ( ) ) ;
2022-06-10 17:06:28 +02:00
ApplicationConfiguration configuration = context . configuration ( ) ;
ContainerConfiguration container = context . container ( ) . configuration ( ) ;
endpoint . profile ( )
. description ( configuration . description ( ) )
. serviceName ( configuration . name ( ) )
2023-04-04 09:30:03 +02:00
. serviceClass ( configuration . group ( ) )
2022-06-10 17:06:28 +02:00
. version ( configuration . version ( ) )
2023-04-04 09:30:03 +02:00
. serviceId ( configuration . name ( ) + configuration . group ( ) + configuration . version ( ) )
2022-06-10 18:41:20 +02:00
. ghnId ( context . container ( ) . id ( ) ) ;
2022-06-10 17:06:28 +02:00
endpoint . profile ( ) . newDeploymentData ( )
. activationTime ( Calendar . getInstance ( ) )
. status ( ( context . lifecycle ( ) . state ( ) . remoteForm ( ) ) ) ;
2022-06-24 15:55:05 +02:00
log . debug ( " publishing aplication profile with id {} for {} with status {} " , context . id ( ) , configuration . name ( ) , context . lifecycle ( ) . state ( ) . remoteForm ( ) ) ;
2022-06-10 17:06:28 +02:00
endpoint . profile ( ) . endpoints ( ) . clear ( ) ;
String baseAddress ;
2023-04-04 09:30:03 +02:00
if ( configuration . proxable ( ) & & container . proxy ( ) ! = null ) {
ProxyAddress proxy = container . proxy ( ) ;
2022-06-24 15:55:05 +02:00
String protocol = proxy . getProtocol ( ) ;
String port = proxy . getPort ( ) ! = null ? " : " + proxy . getPort ( ) : " " ;
2022-06-10 17:06:28 +02:00
2022-06-24 15:55:05 +02:00
baseAddress = String . format ( " %s://%s%s%s " , protocol , proxy . getHostname ( ) , port , context . application ( ) . getContextPath ( ) ) ;
2022-06-10 17:06:28 +02:00
} else {
String protocol = container . protocol ( ) ;
int port = container . port ( ) ;
baseAddress = String . format ( " %s://%s:%d%s " , protocol , container . hostname ( ) , port , context . application ( ) . getContextPath ( ) ) ;
}
for ( ServletRegistration servlet : context . application ( ) . getServletRegistrations ( ) . values ( ) )
if ( ! servletExcludes . contains ( servlet . getName ( ) ) )
for ( String mapping : servlet . getMappings ( ) ) {
String address = baseAddress + ( mapping . endsWith ( " * " ) ? mapping . substring ( 0 , mapping . length ( ) - 2 ) : mapping ) ;
endpoint . profile ( ) . endpoints ( ) . add ( ) . nameAndAddress ( servlet . getName ( ) , URI . create ( address ) ) ;
}
return endpoint ;
}
}