55import org .apache .commons .collections4 .CollectionUtils ;
66import org .apache .commons .collections4 .MapUtils ;
77import org .apache .commons .lang3 .StringUtils ;
8+ import org .lowcoder .domain .encryption .EncryptionService ;
89import org .lowcoder .domain .plugin .client .dto .DatasourcePluginDefinition ;
910import org .lowcoder .domain .plugin .client .dto .GetPluginDynamicConfigRequestDTO ;
1011import org .lowcoder .infra .js .NodeServerClient ;
1112import org .lowcoder .infra .js .NodeServerHelper ;
13+ import org .lowcoder .sdk .config .CommonConfig ;
1214import org .lowcoder .sdk .config .CommonConfigHelper ;
1315import org .lowcoder .sdk .exception .ServerException ;
1416import org .lowcoder .sdk .models .DatasourceTestResult ;
3032
3133import static org .lowcoder .sdk .constants .GlobalContext .REQUEST ;
3234
35+ import com .fasterxml .jackson .databind .ObjectMapper ;
36+
3337@ Slf4j
3438@ RequiredArgsConstructor
3539@ Component
@@ -45,13 +49,17 @@ public class DatasourcePluginClient implements NodeServerClient {
4549 .build ();
4650
4751 private final CommonConfigHelper commonConfigHelper ;
52+ private final CommonConfig commonConfig ;
4853 private final NodeServerHelper nodeServerHelper ;
54+ private final EncryptionService encryptionService ;
4955
5056 private static final String PLUGINS_PATH = "plugins" ;
5157 private static final String RUN_PLUGIN_QUERY = "runPluginQuery" ;
5258 private static final String VALIDATE_PLUGIN_DATA_SOURCE_CONFIG = "validatePluginDataSourceConfig" ;
5359 private static final String GET_PLUGIN_DYNAMIC_CONFIG = "getPluginDynamicConfig" ;
5460
61+ private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
62+
5563 public Mono <List <Object >> getPluginDynamicConfigSafely (List <GetPluginDynamicConfigRequestDTO > getPluginDynamicConfigRequestDTOS ) {
5664 return getPluginDynamicConfig (getPluginDynamicConfigRequestDTOS )
5765 .onErrorResume (throwable -> {
@@ -119,21 +127,47 @@ public Flux<DatasourcePluginDefinition> getDatasourcePluginDefinitions() {
119127 @ SuppressWarnings ("unchecked" )
120128 public Mono <QueryExecutionResult > executeQuery (String pluginName , Object queryDsl , List <Map <String , Object >> context , Object datasourceConfig ) {
121129 return getAcceptLanguage ()
122- .flatMap (language -> WEB_CLIENT
123- .post ()
124- .uri (nodeServerHelper .createUri (RUN_PLUGIN_QUERY ))
125- .header (HttpHeaders .ACCEPT_LANGUAGE , language )
126- .bodyValue (Map .of ("pluginName" , pluginName , "dsl" , queryDsl , "context" , context , "dataSourceConfig" , datasourceConfig ))
127- .exchangeToMono (response -> {
128- if (response .statusCode ().is2xxSuccessful ()) {
129- return response .bodyToMono (Map .class )
130- .map (map -> map .get ("result" ))
131- .map (QueryExecutionResult ::success );
132- }
133- return response .bodyToMono (Map .class )
134- .map (map -> MapUtils .getString (map , "message" ))
135- .map (QueryExecutionResult ::errorWithMessage );
136- }));
130+ .flatMap (language -> {
131+ try {
132+ Map <String , Object > body = Map .of (
133+ "pluginName" , pluginName ,
134+ "dsl" , queryDsl ,
135+ "context" , context ,
136+ "dataSourceConfig" , datasourceConfig
137+ );
138+ String json = OBJECT_MAPPER .writeValueAsString (body );
139+
140+ boolean encryptionEnabled = !(commonConfig .getJsExecutor ().getPassword ().isEmpty () || commonConfig .getJsExecutor ().getSalt ().isEmpty ());
141+ String payload ;
142+ WebClient .RequestBodySpec requestSpec = WEB_CLIENT
143+ .post ()
144+ .uri (nodeServerHelper .createUri (RUN_PLUGIN_QUERY ))
145+ .header (HttpHeaders .ACCEPT_LANGUAGE , language );
146+
147+ if (encryptionEnabled ) {
148+ payload = encryptionService .encryptStringForNodeServer (json );
149+ requestSpec = requestSpec .header ("X-Encrypted" , "true" );
150+ } else {
151+ payload = json ;
152+ }
153+
154+ return requestSpec
155+ .bodyValue (payload )
156+ .exchangeToMono (response -> {
157+ if (response .statusCode ().is2xxSuccessful ()) {
158+ return response .bodyToMono (Map .class )
159+ .map (map -> map .get ("result" ))
160+ .map (QueryExecutionResult ::success );
161+ }
162+ return response .bodyToMono (Map .class )
163+ .map (map -> MapUtils .getString (map , "message" ))
164+ .map (QueryExecutionResult ::errorWithMessage );
165+ });
166+ } catch (Exception e ) {
167+ log .error ("Encryption error" , e );
168+ return Mono .error (new ServerException ("Encryption error" ));
169+ }
170+ });
137171 }
138172
139173 @ SuppressWarnings ("unchecked" )
0 commit comments