-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
PluginServiceCEImpl.java
650 lines (557 loc) · 27.6 KB
/
PluginServiceCEImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
package com.appsmith.server.plugins.base;
import com.appsmith.external.models.Datasource;
import com.appsmith.external.models.PluginType;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.domains.Plugin;
import com.appsmith.server.domains.Workspace;
import com.appsmith.server.domains.WorkspacePlugin;
import com.appsmith.server.dtos.InstallPluginRedisDTO;
import com.appsmith.server.dtos.PluginWorkspaceDTO;
import com.appsmith.server.dtos.WorkspacePluginStatus;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.repositories.PluginRepository;
import com.appsmith.server.services.AnalyticsService;
import com.appsmith.server.services.BaseService;
import com.appsmith.server.services.WorkspaceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.validation.Validator;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.pf4j.PluginManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
public class PluginServiceCEImpl extends BaseService<PluginRepository, Plugin, String> implements PluginServiceCE {
public static final String UQI_DB_EDITOR_FORM = "UQIDbEditorForm";
protected final WorkspaceService workspaceService;
private final PluginManager pluginManager;
private final ReactiveRedisTemplate<String, String> reactiveTemplate;
private final ChannelTopic topic;
private final ObjectMapper objectMapper;
private final Map<String, Mono<Map>> formCache = new HashMap<>();
private final Map<String, Mono<Map<String, String>>> templateCache = new HashMap<>();
private final Map<String, Mono<Map>> labelCache = new HashMap<>();
private static final int CONNECTION_TIMEOUT = 10000;
private static final int READ_TIMEOUT = 10000;
private static final String UQI_QUERY_EDITOR_BASE_FOLDER = "editor";
private static final String UQI_QUERY_EDITOR_ROOT_FILE = "root.json";
private static final String KEY_EDITOR = "editor";
private static final String KEY_CONFIG_PROPERTY = "configProperty";
private static final String KEY_LABEL = "label";
private static final String KEY_INTERNAL_LABEL = "internalLabel";
private static final String KEY_CHILDREN = "children";
private static final String DEFAULT_LABEL = "Query";
public static final String KEY_CONTROL_TYPE = "controlType";
public static final String KEY_COMMENT = "_comment";
public static final String KEY_FILES = "files";
@Autowired
public PluginServiceCEImpl(
Validator validator,
PluginRepository repository,
AnalyticsService analyticsService,
WorkspaceService workspaceService,
PluginManager pluginManager,
ReactiveRedisTemplate<String, String> reactiveTemplate,
ChannelTopic topic,
ObjectMapper objectMapper) {
super(validator, repository, analyticsService);
this.workspaceService = workspaceService;
this.pluginManager = pluginManager;
this.reactiveTemplate = reactiveTemplate;
this.topic = topic;
this.objectMapper = objectMapper;
}
@Override
public Flux<Plugin> getInWorkspace(@NonNull String workspaceId) {
// TODO : Think about the various scenarios where this plugin api is called and then decide on permissions.
Mono<Workspace> workspaceMono = workspaceService.getById(workspaceId);
return workspaceMono
.flatMapMany(workspace -> {
if (workspace.getPlugins() == null) {
log.debug(
"Null installed plugins found for workspace: {}. Return empty plugins",
workspace.getName());
return Flux.empty();
}
Set<String> pluginIds = workspace.getPlugins().stream()
.map(WorkspacePlugin::getPluginId)
.filter(Objects::nonNull)
.collect(Collectors.toUnmodifiableSet());
return repository.findAllById(pluginIds);
})
.flatMap(plugin ->
getTemplates(plugin).doOnSuccess(plugin::setTemplates).thenReturn(plugin));
}
@Override
public Mono<Plugin> create(Plugin plugin) {
if (plugin.getId() != null) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, "id"));
}
return super.create(plugin);
}
@Override
public Flux<Plugin> getDefaultPlugins() {
return repository.findByDefaultInstall(true);
}
@Override
public Flux<Plugin> getDefaultPluginIcons() {
return repository.findDefaultPluginIcons();
}
@Override
public Mono<Workspace> installPlugin(PluginWorkspaceDTO pluginOrgDTO) {
if (pluginOrgDTO.getPluginId() == null) {
return Mono.error(new AppsmithException(AppsmithError.PLUGIN_ID_NOT_GIVEN));
}
if (pluginOrgDTO.getWorkspaceId() == null) {
return Mono.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, FieldName.WORKSPACE_ID));
}
return storeWorkspacePlugin(pluginOrgDTO, pluginOrgDTO.getStatus()).switchIfEmpty(Mono.empty());
}
@Override
public Flux<Workspace> installDefaultPlugins(List<Plugin> plugins) {
final List<WorkspacePlugin> newWorkspacePlugins = plugins.stream()
.filter(plugin -> Boolean.TRUE.equals(plugin.getDefaultInstall()))
.map(plugin -> {
return new WorkspacePlugin(plugin.getId(), WorkspacePluginStatus.ACTIVATED);
})
.collect(Collectors.toList());
if (newWorkspacePlugins.isEmpty()) {
return Flux.empty();
}
return workspaceService.getAll().flatMap(workspace -> {
// Only perform a DB op if plugins associated to this org have changed
if (workspace.getPlugins().containsAll(newWorkspacePlugins)) {
return Mono.just(workspace);
} else {
workspace.getPlugins().addAll(newWorkspacePlugins);
return workspaceService.save(workspace);
}
});
}
private Mono<Workspace> storeWorkspacePlugin(PluginWorkspaceDTO pluginDTO, WorkspacePluginStatus status) {
Mono<Workspace> pluginInWorkspaceMono =
workspaceService.findByIdAndPluginsPluginId(pluginDTO.getWorkspaceId(), pluginDTO.getPluginId());
// If plugin is already present for the workspace, just return the workspace, else install and return workspace
return pluginInWorkspaceMono.switchIfEmpty(Mono.defer(() -> {
log.debug("Plugin {} not already installed. Installing now", pluginDTO.getPluginId());
// If the plugin is not found in the workspace, its not installed already. Install now.
return repository
.findById(pluginDTO.getPluginId())
.map(plugin -> {
log.debug("Before publishing to the redis queue");
// Publish the event to the pub/sub queue
InstallPluginRedisDTO installPluginRedisDTO = new InstallPluginRedisDTO();
installPluginRedisDTO.setWorkspaceId(pluginDTO.getWorkspaceId());
installPluginRedisDTO.setPluginWorkspaceDTO(pluginDTO);
String jsonString;
try {
jsonString = objectMapper.writeValueAsString(installPluginRedisDTO);
} catch (JsonProcessingException e) {
log.error("", e);
return Mono.error(e);
}
return reactiveTemplate
.convertAndSend(topic.getTopic(), jsonString)
.subscribe();
})
// Now that the plugin jar has been successfully downloaded, go on and add the plugin to the
// workspace
.then(workspaceService.getById(pluginDTO.getWorkspaceId()))
.flatMap(workspace -> {
Set<WorkspacePlugin> workspacePluginList = workspace.getPlugins();
if (workspacePluginList == null) {
workspacePluginList = new HashSet<>();
}
WorkspacePlugin workspacePlugin = new WorkspacePlugin();
workspacePlugin.setPluginId(pluginDTO.getPluginId());
workspacePlugin.setStatus(status);
workspacePluginList.add(workspacePlugin);
workspace.setPlugins(workspacePluginList);
log.debug(
"Going to save the workspace with install plugin. This means that installation has been successful");
return workspaceService.save(workspace);
});
}));
}
public Mono<Plugin> findByName(String name) {
return repository.findByName(name);
}
public Mono<Plugin> findByPackageName(String packageName) {
return repository.findByPackageName(packageName);
}
@Override
public Mono<Plugin> findById(String id) {
return repository.findById(id);
}
@Override
public Mono<String> getPluginName(Mono<Datasource> datasourceMono) {
return datasourceMono.flatMap(datasource -> this.findById(datasource.getPluginId())
.map(plugin -> plugin.getPluginName() == null ? plugin.getPackageName() : plugin.getPluginName()));
}
@Override
public Plugin redisInstallPlugin(InstallPluginRedisDTO installPluginRedisDTO) {
Mono<Plugin> pluginMono = repository.findById(
installPluginRedisDTO.getPluginWorkspaceDTO().getPluginId());
return pluginMono
.flatMap(plugin -> downloadAndStartPlugin(installPluginRedisDTO.getWorkspaceId(), plugin))
.switchIfEmpty(Mono.defer(() -> {
log.debug(
"During redisInstallPlugin, no plugin with plugin id {} found. Returning without download and install",
installPluginRedisDTO.getPluginWorkspaceDTO().getPluginId());
return Mono.just(new Plugin());
}))
.block();
}
private Mono<Plugin> downloadAndStartPlugin(String workspaceId, Plugin plugin) {
if (plugin.getJarLocation() == null) {
// Plugin jar location not set. Must be local
/** TODO
* In future throw an error if jar location is not set
*/
log.debug("plugin jarLocation is null. Not downloading and starting. Returning now");
return Mono.just(plugin);
}
String baseUrl = "../dist/plugins/";
String pluginJar = plugin.getName() + "-" + workspaceId + ".jar";
log.debug("Going to download plugin jar with name : {}", baseUrl + pluginJar);
try {
FileUtils.copyURLToFile(
new URL(plugin.getJarLocation()), new File(baseUrl, pluginJar), CONNECTION_TIMEOUT, READ_TIMEOUT);
} catch (Exception e) {
log.error("", e);
return Mono.error(new AppsmithException(AppsmithError.PLUGIN_INSTALLATION_FAILED_DOWNLOAD_ERROR));
}
// Now that the plugin has been downloaded, load and restart the plugin
pluginManager.loadPlugin(Path.of(baseUrl + pluginJar));
// The following only starts plugins which have been loaded but hasn't been started yet.
pluginManager.startPlugins();
return Mono.just(plugin);
}
@Override
public Mono<Map> getFormConfig(String pluginId) {
if (!formCache.containsKey(pluginId)) {
final Mono<Map> formMono = loadPluginResource(pluginId, "form.json")
.doOnError(throwable ->
// Remove this pluginId from the cache so it is tried again next time.
formCache.remove(pluginId))
.onErrorMap(Exceptions::unwrap)
.cache();
final Mono<Map> editorMono = loadPluginResource(pluginId, "editor.json")
.doOnError(throwable ->
// Remove this pluginId from the cache so it is tried again next time.
formCache.remove(pluginId))
.onErrorReturn(new HashMap())
.cache();
final Mono<Map> settingMono = loadPluginResource(pluginId, "setting.json")
.doOnError(throwable ->
// Remove this pluginId from the cache so it is tried again next time.
formCache.remove(pluginId))
.onErrorReturn(new HashMap())
.cache();
final Mono<Map> dependencyMono = loadPluginResource(pluginId, "dependency.json")
.doOnError(throwable ->
// Remove this pluginId from the cache so it is tried again next time.
formCache.remove(pluginId))
.onErrorReturn(new HashMap())
.cache();
Mono<Map> resourceMono = Mono.zip(formMono, editorMono, settingMono, dependencyMono)
.map(tuple -> {
Map formMap = tuple.getT1();
Map editorMap = tuple.getT2();
Map settingMap = tuple.getT3();
Map dependencyMap = tuple.getT4();
formMap.putAll(editorMap);
formMap.putAll(settingMap);
formMap.putAll(dependencyMap);
return formMap;
});
formCache.put(pluginId, resourceMono);
}
return formCache.get(pluginId);
}
@Override
public Mono<Map> getEditorConfigLabelMap(String pluginId) {
if (labelCache.containsKey(pluginId)) {
return labelCache.get(pluginId);
}
Mono<Map> formConfig = getFormConfig(pluginId);
if (formConfig == null) {
return Mono.just(new HashMap());
}
Mono<Map> labelMapMono = formConfig.flatMap(formMap -> {
Map<String, String> labelMap = new LinkedHashMap(); // need to keep the key value pairs in order
List editorMap = (List) formMap.get(KEY_EDITOR);
if (editorMap == null) {
return Mono.just(new HashMap());
}
editorMap.stream()
.filter(item -> {
if (((Map) item).get(KEY_CHILDREN) == null) {
return false;
}
return true;
})
.map(item -> ((Map) item).get(KEY_CHILDREN))
.forEach(item -> ((List<Map>) item).stream().forEach(queryField -> {
/*
* - First check for "label" key.
* - If "label" key has empty value, then get the value against
* "internalLabel" key.
*/
String label = StringUtils.isEmpty(queryField.get(KEY_LABEL))
? (StringUtils.isEmpty(queryField.get(KEY_INTERNAL_LABEL))
? DEFAULT_LABEL
: (String) queryField.get(KEY_INTERNAL_LABEL))
: (String) queryField.get(KEY_LABEL);
String configProperty = (String) queryField.get(KEY_CONFIG_PROPERTY);
labelMap.put(configProperty, label);
}));
return Mono.just(labelMap);
});
labelCache.put(pluginId, labelMapMono);
return labelMapMono;
}
private Mono<Map<String, String>> getTemplates(Plugin plugin) {
final String pluginId = plugin.getId();
if (!templateCache.containsKey(pluginId)) {
final Mono<Map<String, String>> mono = Mono.fromSupplier(() -> loadTemplatesFromPlugin(plugin))
.onErrorResume(
throwable -> throwable.getCause() instanceof FileNotFoundException,
throwable -> Mono.just(Collections.emptyMap()))
.doOnError(throwable ->
// Remove this pluginId from the cache so it is tried again next time.
templateCache.remove(pluginId))
// It's okay if the templates folder is not present, we just return empty templates collection.
.onErrorMap(throwable -> {
log.error("Error loading templates for plugin {}.", plugin.getPackageName(), throwable);
return new AppsmithException(
AppsmithError.PLUGIN_LOAD_TEMPLATES_FAIL,
Exceptions.unwrap(throwable).getMessage());
})
.cache();
templateCache.put(pluginId, mono);
}
return templateCache.get(pluginId);
}
private Map<String, String> loadTemplatesFromPlugin(Plugin plugin) {
final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
pluginManager.getPlugin(plugin.getPackageName()).getPluginClassLoader());
final PluginTemplatesMeta pluginTemplatesMeta;
try {
pluginTemplatesMeta = objectMapper.readValue(
resolver.getResource("templates/meta.json").getInputStream(), PluginTemplatesMeta.class);
} catch (IOException e) {
log.error("Error loading templates metadata in plugin {}", plugin.getPackageName());
throw Exceptions.propagate(e);
}
if (pluginTemplatesMeta.getTemplates() == null) {
log.warn("Missing templates key in plugin templates meta.");
return Collections.emptyMap();
}
final Map<String, String> templates = new LinkedHashMap<>();
for (final PluginTemplate template : pluginTemplatesMeta.getTemplates()) {
final String filename = template.getFile();
if (filename == null) {
log.warn("Empty or missing file for a template in plugin {}.", plugin.getPackageName());
continue;
}
final Resource resource = resolver.getResource("templates/" + filename);
final String title = StringUtils.isEmpty(template.getTitle())
? filename.replaceFirst("\\.\\w+$", "")
: template.getTitle();
try {
templates.put(title, StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()));
} catch (IOException e) {
log.error("Error loading template {} for plugin {}", filename, plugin.getId());
throw Exceptions.propagate(e);
}
}
return templates;
}
InputStream getConfigInputStream(Plugin plugin, String fileName) throws IOException {
String resourcePath = UQI_QUERY_EDITOR_BASE_FOLDER + "/" + fileName;
return pluginManager
.getPlugin(plugin.getPackageName())
.getPluginClassLoader()
.getResourceAsStream(resourcePath);
}
/**
* This function reads from the folder editor/ starting with file root.json. root.json declares all the combination
* of commands that would be present as well as the files from which the action types should be loaded.
*
* @param plugin
* @return Map of the editor in the format expected by the client for displaying all the UI fields with conditionals
*/
@Override
public Map loadEditorPluginResourceUqi(Plugin plugin) {
String resourcePath = UQI_QUERY_EDITOR_BASE_FOLDER + "/" + UQI_QUERY_EDITOR_ROOT_FILE;
ObjectNode rootTree;
try (InputStream resourceAsStream = getConfigInputStream(plugin, UQI_QUERY_EDITOR_ROOT_FILE)) {
if (resourceAsStream == null) {
throw new AppsmithException(
AppsmithError.PLUGIN_LOAD_FORM_JSON_FAIL,
plugin.getPackageName(),
"form resource " + resourcePath + " not found");
}
// Read the root.json content.
rootTree = objectMapper.readValue(resourceAsStream, ObjectNode.class);
} catch (IOException e) {
log.error(
"Error loading resource JSON for plugin {} and resourcePath {}",
plugin.getPackageName(),
resourcePath,
e);
throw new AppsmithException(
AppsmithError.PLUGIN_LOAD_FORM_JSON_FAIL, plugin.getPackageName(), e.getMessage());
}
/*
* Generate the top level section which would hold all the command templates read from files
* It has the following format:
* {
* "controlType": "SECTION",
* "_comment": "This section holds all the templates",
* "children": []
* }
*/
ObjectNode uiSectionNode = objectMapper.createObjectNode();
uiSectionNode.put(KEY_CONTROL_TYPE, "SECTION");
uiSectionNode.put(KEY_COMMENT, "This section holds all the templates");
final ArrayNode templateChildrenNode = objectMapper.createArrayNode();
uiSectionNode.set(KEY_CHILDREN, templateChildrenNode);
// From root tree, fetch the key "files" which contains the declaration for all the template files
ArrayNode filesArray = (ArrayNode) rootTree.get(KEY_FILES);
// Adding a section to hold all the template configurations
ArrayNode editorArray = (ArrayNode) rootTree.get(KEY_EDITOR);
editorArray.add(uiSectionNode);
if (filesArray != null) {
for (JsonNode fileName : filesArray) {
String path = fileName.asText();
try {
final JsonNode templateConfig = loadPluginResourceGivenPluginAsJsonNode(plugin, path);
templateChildrenNode.add(templateConfig);
} catch (AppsmithException e) {
// Either the file doesn't exist or malformed JSON was found. Ignore the command template
log.error(
"Error loading resource JSON for plugin {} and resourcePath {} : ",
plugin.getPackageName(),
resourcePath,
e);
}
}
}
ObjectNode topLevel = objectMapper.createObjectNode();
topLevel.set("editor", editorArray);
return objectMapper.convertValue(topLevel, new TypeReference<Map<String, Object>>() {});
}
@Override
public Flux<Plugin> saveAll(Iterable<Plugin> plugins) {
return repository.saveAll(plugins);
}
@Override
public Flux<Plugin> findAllByIdsWithoutPermission(Set<String> ids, List<String> includeFields) {
return repository.findAllByIdsWithoutPermission(ids, includeFields);
}
@Override
public Flux<Plugin> getAllRemotePlugins() {
return repository.findByType(PluginType.REMOTE);
}
private Map loadPluginResourceGivenPluginAsMap(Plugin plugin, String resourcePath) {
try (InputStream resourceAsStream = pluginManager
.getPlugin(plugin.getPackageName())
.getPluginClassLoader()
.getResourceAsStream(resourcePath)) {
if (resourceAsStream == null) {
throw new AppsmithException(
AppsmithError.PLUGIN_LOAD_FORM_JSON_FAIL,
plugin.getPackageName(),
"form resource " + resourcePath + " not found");
}
Map resourceMap = objectMapper.readValue(resourceAsStream, Map.class);
return resourceMap;
} catch (IOException e) {
log.error(
"[{}] : Error loading resource JSON for resourcePath {}", plugin.getPackageName(), resourcePath, e);
throw new AppsmithException(
AppsmithError.PLUGIN_LOAD_FORM_JSON_FAIL, plugin.getPackageName(), e.getMessage());
}
}
private JsonNode loadPluginResourceGivenPluginAsJsonNode(Plugin plugin, String resourcePath) {
try (InputStream resourceAsStream = getConfigInputStream(plugin, resourcePath)) {
if (resourceAsStream == null) {
throw new AppsmithException(
AppsmithError.PLUGIN_LOAD_FORM_JSON_FAIL,
plugin.getPackageName(),
"form resource " + resourcePath + " not found");
}
return objectMapper.readTree(resourceAsStream);
} catch (IOException e) {
log.error(
"[{}] : Error loading resource JSON for resourcePath {}", plugin.getPackageName(), resourcePath, e);
throw new AppsmithException(
AppsmithError.PLUGIN_LOAD_FORM_JSON_FAIL, plugin.getPackageName(), e.getMessage());
}
}
@Override
public Mono<Map> loadPluginResource(String pluginId, String resourcePath) {
return findById(pluginId).map(plugin -> {
if ("editor.json".equals(resourcePath)) {
// UI config will be available if this plugin is sourced from the cloud
if (plugin.getActionUiConfig() != null) {
return plugin.getActionUiConfig();
}
// For UQI, use another format of loading the config
if (UQI_DB_EDITOR_FORM.equals(plugin.getUiComponent())) {
return loadEditorPluginResourceUqi(plugin);
}
}
if ("form.json".equals(resourcePath)) {
// UI config will be available if this plugin is sourced from the cloud
if (plugin.getDatasourceUiConfig() != null) {
return plugin.getDatasourceUiConfig();
}
}
return loadPluginResourceGivenPluginAsMap(plugin, resourcePath);
});
}
@Data
static class PluginTemplatesMeta {
List<PluginTemplate> templates;
}
@Data
static class PluginTemplate {
String file;
String title = null;
}
}