21
21
import org .lowcoder .plugin .api .data .EndpointRequest ;
22
22
import org .lowcoder .plugin .api .data .EndpointResponse ;
23
23
import org .lowcoder .sdk .exception .BaseException ;
24
+ import org .springframework .context .ApplicationContext ;
25
+ import org .springframework .context .support .GenericApplicationContext ;
24
26
import org .springframework .core .ResolvableType ;
25
27
import org .springframework .http .ResponseCookie ;
26
28
import org .springframework .stereotype .Component ;
27
29
import org .springframework .web .reactive .function .server .RequestPredicate ;
28
30
import org .springframework .web .reactive .function .server .RouterFunction ;
29
31
import org .springframework .web .reactive .function .server .ServerResponse ;
30
32
import org .springframework .web .reactive .function .server .ServerResponse .BodyBuilder ;
33
+ import org .springframework .web .reactive .function .server .support .RouterFunctionMapping ;
31
34
35
+ import lombok .RequiredArgsConstructor ;
32
36
import lombok .extern .slf4j .Slf4j ;
33
37
import reactor .core .publisher .Mono ;
34
38
35
39
@ Slf4j
40
+ @ RequiredArgsConstructor
36
41
@ Component
37
42
public class PluginEndpointHandlerImpl implements PluginEndpointHandler
38
43
{
39
44
private static final String PLUGINS_BASE_URL = "/plugins/" ;
40
45
private List <RouterFunction <ServerResponse >> routes = new ArrayList <>();
41
-
46
+
47
+ private final ApplicationContext applicationContext ;
48
+ private final RouterFunctionMapping routerFunctionMapping ;
49
+
42
50
@ Override
43
51
public void registerEndpoints (String pluginUrlPrefix , List <PluginEndpoint > endpoints )
44
52
{
@@ -57,7 +65,9 @@ public void registerEndpoints(String pluginUrlPrefix, List<PluginEndpoint> endpo
57
65
registerEndpointHandler (urlPrefix , endpoint , handler );
58
66
}
59
67
}
60
- }
68
+ }
69
+
70
+ ((ReloadableRouterFunctionMapping )routerFunctionMapping ).reloadFunctionMappings ();
61
71
}
62
72
}
63
73
@@ -74,8 +84,10 @@ private void registerEndpointHandler(String urlPrefix, PluginEndpoint endpoint,
74
84
if (checkHandlerMethod (handler ))
75
85
{
76
86
77
- EndpointExtension endpointMeta = handler .getAnnotation (EndpointExtension .class );
78
- routes .add (route (createRequestPredicate (urlPrefix , endpointMeta ), req -> {
87
+ EndpointExtension endpointMeta = handler .getAnnotation (EndpointExtension .class );
88
+ String endpointName = endpoint .getClass ().getSimpleName () + "_" + handler .getName ();
89
+
90
+ RouterFunction <ServerResponse > routerFunction = route (createRequestPredicate (urlPrefix , endpointMeta ), req -> {
79
91
Mono <ServerResponse > result = null ;
80
92
try
81
93
{
@@ -87,8 +99,10 @@ private void registerEndpointHandler(String urlPrefix, PluginEndpoint endpoint,
87
99
throw new BaseException ("Error running handler for [ " + endpointMeta .method () + ": " + endpointMeta .uri () + "] !" );
88
100
}
89
101
return result ;
90
- })
91
- );
102
+ });
103
+ routes .add (routerFunction );
104
+ registerRouterFunctionMapping (endpointName , routerFunction );
105
+
92
106
log .info ("Registered endpoint: {} -> {}: {}" , endpoint .getClass ().getSimpleName (), endpointMeta .method (), urlPrefix + endpointMeta .uri ());
93
107
}
94
108
else
@@ -98,6 +112,18 @@ private void registerEndpointHandler(String urlPrefix, PluginEndpoint endpoint,
98
112
}
99
113
}
100
114
115
+ private void registerRouterFunctionMapping (String endpointName , RouterFunction <ServerResponse > routerFunction )
116
+ {
117
+ String beanName = "pluginEndpoint_" + endpointName + "_" + System .currentTimeMillis ();
118
+
119
+ ((GenericApplicationContext )applicationContext ).registerBean (beanName , RouterFunction .class , () -> {
120
+ return routerFunction ;
121
+ });
122
+
123
+ log .debug ("Registering RouterFunction bean definition: {}" , beanName );
124
+ }
125
+
126
+
101
127
private Mono <ServerResponse > createServerResponse (EndpointResponse pluginResponse )
102
128
{
103
129
/** Create response with given status **/
0 commit comments